langchain - 💡(How to fix) Fix feat(anthropic): expose response headers in response_metadata [1 participants]

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…
GitHub stats
langchain-ai/langchain#36983Fetched 2026-04-25 06:03:17
View on GitHub
Comments
0
Participants
1
Timeline
5
Reactions
0
Author
Participants
Timeline (top)
labeled ×3issue_type_added ×1renamed ×1

Fix Action

Fix / Workaround

  • Ties us to httpx internals (fragile across anthropic-sdk-python versions)
  • Doesn't survive if the SDK switches transports
  • Every app re-implementing the workaround reinvents the same wheel

langchain-aws solved the equivalent problem officially in #473 (ChatBedrockConverse, released in 0.2.23). Would be good to have parity on the Anthropic side.

RAW_BUFFERClick to expand / collapse

Submission checklist

  • This is a feature request, not a bug report or usage question.
  • I added a clear and descriptive title that summarizes the feature request.
  • I used the GitHub search to find a similar feature request and didn't find it.
  • I checked the LangChain documentation and API reference to see if this feature already exists.
  • This is not related to the langchain-community package.

Package (Required)

  • langchain
  • langchain-openai
  • langchain-anthropic
  • langchain-classic
  • langchain-core
  • langchain-model-profiles
  • langchain-tests
  • langchain-text-splitters
  • langchain-chroma
  • langchain-deepseek
  • langchain-exa
  • langchain-fireworks
  • langchain-groq
  • langchain-huggingface
  • langchain-mistralai
  • langchain-nomic
  • langchain-ollama
  • langchain-openrouter
  • langchain-perplexity
  • langchain-qdrant
  • langchain-xai
  • Other / not sure / general

Feature Description

Expose the HTTP response headers returned by Anthropic's API (request-id, anthropic-ratelimit-*, etc.) on AIMessage.response_metadata, behind an opt-in flag — mirroring the include_response_headers parameter already available on ChatOpenAI.

Apply to both streaming and non-streaming paths (invoke, ainvoke, stream, astream).

Use Case

We log every LLM call in our production backend and want to store Anthropic's request-id (req_011...) alongside each call so we can jump from a log line straight to the Anthropic console when debugging. Same story for the rate-limit headers — we want to surface them to ops dashboards.

Right now that header never reaches user code from ChatAnthropic, so we can't correlate application logs with the Anthropic console without resorting to hacks (see Alternatives).

Proposed Solution

Add an include_response_headers: bool = False option to ChatAnthropic, aligned with how it works in ChatOpenAI. When enabled, the response headers should be exposed via response_metadata["headers"].

Implementation would follow the Anthropic SDK's .with_raw_response() path and be applied consistently across _generate, _agenerate, _stream, and _astream.

There was prior work in PR #31579 that implemented this end-to-end (including tests), but it was closed due to inactivity. I can take it over—rebase onto master, address any outstanding review comments, and get it merged.

A few small adjustments I'd make vs. the original PR:

  • Streaming: attach headers to the first chunk only (matches ChatOpenAI's is_first_chunk pattern) rather than every chunk.
  • response_metadata: add a "headers" key instead of replacing the existing dict, so model_provider and other keys remain untouched when the flag is on.
  • Tests: assert on request-id specifically — the original check passed as long as content-type was present, which it always is.

Alternatives Considered

We currently capture the header via an httpx AsyncClient event_hook that stashes the request-id in a ContextVar per request. It works but:

  • Ties us to httpx internals (fragile across anthropic-sdk-python versions)
  • Doesn't survive if the SDK switches transports
  • Every app re-implementing the workaround reinvents the same wheel

langchain-aws solved the equivalent problem officially in #473 (ChatBedrockConverse, released in 0.2.23). Would be good to have parity on the Anthropic side.

Additional Context

  • Previous attempt: #31579
  • Reference for similar feature in another provider: langchain-ai/langchain-aws#473

Would appreciate being assigned if there's no objection to resuming this. I'll base the PR on current master rather than reviving the stale branch.

extent analysis

TL;DR

Add an include_response_headers option to ChatAnthropic to expose Anthropic's API response headers, such as request-id and rate-limit headers, in the response_metadata.

Guidance

  • Review the prior work in PR #31579 and consider rebasing it onto the current master branch to implement the include_response_headers option.
  • Update the implementation to attach headers to the first chunk only in streaming paths and add a "headers" key to response_metadata instead of replacing the existing dict.
  • Ensure tests are updated to assert on the request-id specifically and verify the correctness of the implementation.
  • Consider the adjustments proposed in the feature request, such as making the include_response_headers option opt-in and applying it consistently across _generate, _agenerate, _stream, and _astream methods.

Example

No code example is provided as the implementation details are already discussed in the prior PR #31579 and the feature request.

Notes

The feature request is well-documented, and prior work has been done in PR #31579. However, it's essential to review and update the implementation to ensure it aligns with the current master branch and the proposed adjustments.

Recommendation

Apply the workaround by adding the include_response_headers option to ChatAnthropic, as it provides a more robust and maintainable solution compared to the current alternatives. This approach also aligns with the existing implementation in ChatOpenAI and langchain-aws.

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…

Still need to ship something?

×6

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

Back to top recommendations

TRENDING