claude-code - 💡(How to fix) Fix [FEATURE] Support per-request trace context propagation for LLM API calls

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…

Root Cause

ANTHROPIC_CUSTOM_HEADERS can add static headers, but a fixed traceparent is not semantically correct because many unrelated requests would share the same parent span.

Code Example

ANTHROPIC_BASE_URL=http://localhost:8317

---

Claude Code span
  -> outbound LLM API request
    -> llm-proxy server span
      -> proxy/provider/model spans
RAW_BUFFERClick to expand / collapse

Request

Claude Code can export its own OpenTelemetry telemetry via OTLP, and it can also call an Anthropic-compatible proxy through ANTHROPIC_BASE_URL.

However, when Claude Code sends LLM API requests to the configured proxy, there does not appear to be a supported way to inject the current active trace context into each outbound model request.

Please support one of the following:

  1. Automatically inject W3C trace context into outbound LLM API calls when Claude Code OTEL is enabled:

    • traceparent
    • tracestate
    • optionally baggage
  2. Or expose a supported hook/helper for outbound model requests, for example:

    • PreLLMRequest
    • ability to modify outbound request headers
    • access to the current active OTEL span/context
  3. Or support a dynamic custom header helper, similar in spirit to apiKeyHelper, but for arbitrary headers:

    • invoked per request
    • can return headers like traceparent / baggage

Use case

We run Claude Code against a local LLM proxy:

ANTHROPIC_BASE_URL=http://localhost:8317

Both Claude Code and the proxy export telemetry to the same OTLP collector.

Desired trace tree:

Claude Code span
  -> outbound LLM API request
    -> llm-proxy server span
      -> proxy/provider/model spans

Today this cannot be represented as a single distributed trace unless Claude Code sends a valid per-request traceparent header to the proxy.

Why static headers are insufficient

ANTHROPIC_CUSTOM_HEADERS can add static headers, but a fixed traceparent is not semantically correct because many unrelated requests would share the same parent span.

Hooks and MCP also do not appear to expose a PreLLMRequest / PreModelRequest event that can modify outbound LLM request headers.

Expected behavior

When OTEL is enabled, Claude Code should either:

  • propagate the active trace context on each LLM API HTTP request automatically, or
  • provide a supported extension point to add dynamic per-request headers.

Acceptance criteria

  • Each Claude Code LLM API request can include a fresh/current traceparent.
  • Downstream Anthropic-compatible proxies can extract the context and attach their spans to the Claude Code trace.
  • This works with ANTHROPIC_BASE_URL.
  • Static ANTHROPIC_CUSTOM_HEADERS remains supported, but is not the only option for trace propagation.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

FAQ

Expected behavior

When OTEL is enabled, Claude Code should either:

  • propagate the active trace context on each LLM API HTTP request automatically, or
  • provide a supported extension point to add dynamic per-request headers.

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING