Skip to content

anthropic: add include_response_headers parameter to ChatAnthropic #31579

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

ketanbhatt
Copy link

Description:
This PR adds support for the include_response_headers parameter to the
ChatAnthropic class, providing feature parity with the existing ChatOpenAI
implementation. When set to True, HTTP response headers from the Anthropic
API are included in the response_metadata of AI messages.

We (Intercom) felt this gap when trying to instrument the quota we have with Anthropic for different models.

Changes:

  • Added include_response_headers: bool = False parameter to ChatAnthropic class
  • Implemented _create_with_raw_response() and _acreate_with_raw_response() methods using Anthropic SDK's with_raw_response feature
  • Updated all four core methods (_generate, _agenerate, _stream,
    _astream) to conditionally extract and include headers
  • Modified _format_output() method to handle headers in response metadata
  • Added comprehensive unit tests and integration tests
  • Maintains backward compatibility by only setting response_metadata when
    headers are present

Issue: N/A (feature request for parity with OpenAI integration)

Dependencies: None - uses existing anthropic SDK functionality

Twitter handle: @_ketanbhatt

ketanbhatt and others added 2 commits June 11, 2025 17:56
This change adds support for include_response_headers parameter to ChatAnthropic,
providing feature parity with ChatOpenAI. When set to True, HTTP response headers
are included in the response_metadata of AI messages.

Changes:
- Add include_response_headers boolean parameter (default: False)
- Implement _create_with_raw_response and _acreate_with_raw_response methods
- Update _generate, _agenerate, _stream, and _astream to support headers
- Modify _format_output to include headers in response_metadata
- Add comprehensive unit and integration tests

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
This change adds support for include_response_headers parameter to ChatAnthropic,
providing feature parity with ChatOpenAI. When set to True, HTTP response headers
are included in the response_metadata of AI messages.

- Add include_response_headers parameter to ChatAnthropic class
- Implement _create_with_raw_response and _acreate_with_raw_response methods
- Update _generate, _agenerate, _stream, and _astream methods to handle headers
- Modify _format_output to conditionally include headers in response_metadata
- Add comprehensive unit and integration tests
- Maintain backward compatibility by only setting headers when present

🤖 Generated with Claude Code

Co-Authored-By: Claude <[email protected]>
Copy link

vercel bot commented Jun 12, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
langchain ⬜️ Ignored (Inspect) Visit Preview Jun 12, 2025 9:51am

Copy link

codspeed-hq bot commented Jun 12, 2025

CodSpeed Walltime Performance Report

Merging #31579 will not alter performance

Comparing ketanbhatt:feature/anthropic-include-response-headers (1dbb372) with master (446a9d5)

⚠️ Unknown Walltime execution environment detected

Using the Walltime instrument on standard Hosted Runners will lead to inconsistent data.

For the most accurate results, we recommend using CodSpeed Macro Runners: bare-metal machines fine-tuned for performance measurement consistency.

Summary

✅ 13 untouched benchmarks

Copy link

codspeed-hq bot commented Jun 12, 2025

CodSpeed Instrumentation Performance Report

Merging #31579 will not alter performance

Comparing ketanbhatt:feature/anthropic-include-response-headers (1dbb372) with master (446a9d5)

Summary

✅ 13 untouched benchmarks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant