Anthropic SDK with the Langfuse AI Gateway
Beta. The Langfuse AI Gateway is in public beta and can still change.
Use Anthropic's official client SDKs for Python and TypeScript with the gateway by changing the base URL and the API key. Requests need an ANTHROPIC provider credential in your organization.
For tracing the Anthropic SDK without the gateway, see the Anthropic (Python) and Anthropic (JS/TS) integrations.
Before you start, create a gateway key and export LANGFUSE_GATEWAY_HOST and LANGFUSE_GATEWAY_KEY as in Send your first request. Replace <model-id> with an ID from the models endpoint.
Configure the client
Set the base URL to the gateway's Anthropic route, /anthropic, without /v1: the SDK appends /v1/messages itself. Pass the gateway key as the API key. The SDK sends it as x-api-key.
pip install anthropicimport os
from anthropic import Anthropic
client = Anthropic(
base_url=os.environ["LANGFUSE_GATEWAY_HOST"] + "/anthropic",
api_key=os.environ["LANGFUSE_GATEWAY_KEY"],
)npm install @anthropic-ai/sdkimport Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic({
baseURL: `${process.env.LANGFUSE_GATEWAY_HOST}/anthropic`,
apiKey: process.env.LANGFUSE_GATEWAY_KEY,
});Messages API
message = client.messages.create(
model="<model-id>",
max_tokens=1024,
messages=[{"role": "user", "content": "Say hello"}],
)
print(message.content[0].text)To stream the response:
with client.messages.stream(
model="<model-id>",
max_tokens=1024,
messages=[{"role": "user", "content": "Say hello"}],
) as stream:
for text in stream.text_stream:
print(text, end="", flush=True)const message = await client.messages.create({
model: "<model-id>",
max_tokens: 1024,
messages: [{ role: "user", content: "Say hello" }],
});
console.log(message.content);To stream the response:
const stream = client.messages.stream({
model: "<model-id>",
max_tokens: 1024,
messages: [{ role: "user", content: "Say hello" }],
});
stream.on("text", (text) => process.stdout.write(text));
await stream.finalMessage();Add request headers
Set request headers on the client for every call, or per call for values such as a session ID.
client = Anthropic(
base_url=os.environ["LANGFUSE_GATEWAY_HOST"] + "/anthropic",
api_key=os.environ["LANGFUSE_GATEWAY_KEY"],
default_headers={"langfuse-tags": "checkout,production"},
)
message = client.messages.create(
model="<model-id>",
max_tokens=1024,
messages=[{"role": "user", "content": "Say hello"}],
extra_headers={"langfuse-session-id": "<session-id>"},
)const client = new Anthropic({
baseURL: `${process.env.LANGFUSE_GATEWAY_HOST}/anthropic`,
apiKey: process.env.LANGFUSE_GATEWAY_KEY,
defaultHeaders: { "langfuse-tags": "checkout,production" },
});
const message = await client.messages.create(
{
model: "<model-id>",
max_tokens: 1024,
messages: [{ role: "user", content: "Say hello" }],
},
{ headers: { "langfuse-session-id": "<session-id>" } },
);The gateway already records every call. Don't also trace the same client with the Langfuse Anthropic integration, or each call is recorded twice.
Claude Agent SDK
The Claude Agent SDK runs Claude Code under the hood, so it is configured with the same environment variables as Claude Code, not with a client base URL. Set ANTHROPIC_BASE_URL and ANTHROPIC_AUTH_TOKEN in the environment of the agent process, as described in the Claude Code guide.
Last updated on