Claude Desktop and Claude Code with the Langfuse AI Gateway
Beta. The Langfuse AI Gateway is in public beta and can still change.
Use the Langfuse AI Gateway with the Claude Desktop app or the Claude Code CLI. Both require an ANTHROPIC provider credential.
This page routes Claude's model calls through the AI Gateway. For tracing Claude Code with hooks instead, see the Claude Code integration.
Claude Desktop
Create a gateway API key
Create a gateway API key under Organization settings → AI Gateway → Gateway keys. See API keys.
Connect to the gateway
Open Developer → Configure Third-Party Inference. If the Developer menu is unavailable, open Help → Troubleshooting and enable Developer Mode. Then, under Connection:
- Set Inference provider to Gateway.
- Set Gateway base URL to
https://gateway.cloud.langfuse.com/anthropic. Do not add/v1; Claude Desktop appends it. - Set Credential kind to Static API key, enter your Langfuse gateway key under Gateway API key, and select Bearer as the Gateway auth scheme.
Set capabilities
Under Capabilities:
- Under General restrictions, we recommend adding
*to Allowed egress hosts. Otherwise, Claude can't reach websites or services outside the gateway, such as GitHub, and it won't ask you for permission. - Optional: gateway mode uses a separate profile. To keep your existing claude.ai chats and sessions, turn on Claude.ai data import. After restarting, use Settings → Import & export. See Import and export.
Save and restart
Select Apply Changes, then select Save & Restart. Start a new session so that the egress settings apply. For more information about these settings, see the Claude Desktop third-party inference guide.
Use HTTPS git remotes or gh to push. SSH push fails in the macOS sandbox,
see anthropics/claude-code#98752.
MCP servers connected through Claude Desktop are associated with your claude.ai subscription. They are unavailable while Claude Desktop uses third-party gateway credentials. Switch back to your Claude account to use those subscription-backed connectors.
Verify
Test the connection in Developer → Configure Third-Party Inference, or send a message in a new session. If Claude replies, it runs through the gateway.
Claude Code CLI
Requires Claude Code 2.1 or later. Verified with Claude Code 2.1.282.
Create a gateway API key
Create a gateway API key under Organization settings → AI Gateway → Gateway keys. See API keys.
Log out of Claude
Remove existing subscription credentials:
claude /logoutAn existing Claude login can send both Authorization and x-api-key credentials and cause 401 Conflicting gateway credentials in the Authorization and x-api-key headers.
Point Claude Code at the gateway
Set the gateway variables in your shell:
export ANTHROPIC_BASE_URL="https://gateway.cloud.langfuse.com/anthropic" # no /v1: Claude Code appends /v1/messages
export ANTHROPIC_AUTH_TOKEN="<your-gateway-key>" # sent as Authorization: Bearer
unset ANTHROPIC_API_KEY # a different value here causes a 401
export CLAUDE_CODE_ENABLE_GATEWAY_MODEL_DISCOVERY=1 # optional: /model lists gateway models
export CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC=1 # optional
export ANTHROPIC_CUSTOM_HEADERS=$'langfuse-tags: claude-code,<team>\nlangfuse-environment: local\nlangfuse-metadata: repo:<repo>' # optional
claudeTo keep the setup across sessions, add the export lines to ~/.zshrc. If ~/.zshrc also sets ANTHROPIC_API_KEY, remove that line.
Alternatively, use ~/.claude/settings.json:
{
"env": {
"ANTHROPIC_BASE_URL": "https://gateway.cloud.langfuse.com/anthropic",
"ANTHROPIC_AUTH_TOKEN": "<your-gateway-key>",
"CLAUDE_CODE_ENABLE_GATEWAY_MODEL_DISCOVERY": "1",
"ANTHROPIC_CUSTOM_HEADERS": "langfuse-tags: claude-code\nlangfuse-environment: local"
}
}ANTHROPIC_CUSTOM_HEADERStakes newline-separatedName: valuepairs. See request headers.- Pick models with
--modelor/model, using IDs from/models. Bareclaude-…IDs and<credential>:<model>IDs both work, see model IDs. - MCP servers configured locally through the CLI remain available because Claude Code stores them in
.claude.json.
Verify
Run a prompt:
claude -p "Reply with: hello gateway"If Claude Code replies, it runs through the gateway.
Turn grouping (beta)
By default, every model call is its own trace. Claude Code can send a W3C traceparent header whose trace ID is its claude_code.interaction span, one per user prompt. The gateway adopts that trace ID, so every model call of a prompt lands in one trace, including subagent calls and retries.
This uses Claude Code's beta OpenTelemetry tracing. All four settings are required:
export CLAUDE_CODE_ENABLE_TELEMETRY=1
export CLAUDE_CODE_ENHANCED_TELEMETRY_BETA=1
export CLAUDE_CODE_PROPAGATE_TRACEPARENT=1
export OTEL_TRACES_EXPORTER=otlp
export OTEL_EXPORTER_OTLP_ENDPOINT="<your-otel-collector>"- Claude Code only sends
traceparentwhen tracing is actually active. WithOTEL_TRACES_EXPORTERunset or set tonone, nothing is sent and each call stays its own trace. OTEL_TRACES_EXPORTER=consolealso works, but it prints span dumps to stdout, which makes the interactive UI unusable.- Do not point this OTLP export at Langfuse. Claude Code's
claude_code.llm_requestspans would be recorded as a second generation next to the gateway's generation for every call. Use your own collector. - Because Claude Code's own spans are not in Langfuse, the gateway generations of a prompt appear as top-level observations of the shared trace.
- A resumed session starts a new trace per prompt, in the same session.
Troubleshooting and FAQ
Why does a failed prompt show two ERROR generations?
When a model call fails, Claude Code retries once without streaming, so a failed prompt shows two ERROR generations.
Why aren't subagent calls linked to their parent?
Subagent calls carry agent.id but no link to their parent, unless you enable turn grouping.
Why is the thinking text empty?
Claude Code often omits thinking text (thinking.display: "omitted"), so time to first token is close to latency. Thinking tokens are counted inside output tokens.
What is the HEAD /anthropic/api/hello 404 at startup?
It's Claude Code's connectivity probe. It is harmless and not recorded.
Why is the input missing on long sessions?
Long sessions can exceed the 5 MiB input capture limit. See known limitations.
Last updated on