Langfuse launches AI Gateway Learn more
IntegrationsAnthropic SDK (gateway)

Anthropic SDK with the Langfuse AI Gateway

Beta. The Langfuse AI Gateway is in public beta and can still change.

What is Anthropic? Anthropic is an AI safety company that develops Claude, a family of large language models designed to be helpful, harmless, and honest. Claude models excel at complex reasoning, analysis, and creative tasks.

What is Langfuse? Langfuse is an open-source AI engineering platform. It helps teams trace agentic applications, debug issues, evaluate quality, and monitor costs in production. The Langfuse AI Gateway is a central entry point for your AI traffic: it relays model calls to your providers, traces every call in Langfuse, and tracks its cost, without the Langfuse SDK. Use Langfuse Cloud (hosted by Langfuse, free tier, no infrastructure to run) or self-host it.

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 connection 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 anthropic
import 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/sdk
import 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.


Was this page helpful?

Last updated on