Skip to content

fix: Add cache token support to ChatAnthropicVertex streaming responses #1010

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

imranarshad
Copy link

  • Add reusable _create_usage_metadata() function matching langchain_anthropic
  • Fix streaming message_start events to extract cache tokens
  • Fix streaming message_delta events to follow official pattern
  • Unify non-streaming to use same logic for consistency
  • Use proper LangChain UsageMetadata structure with input_token_details
  • Remove custom CacheUsageMetadata class in favor of standard types

Fixes missing cache token information in streaming responses to match official langchain_anthropic behavior and enable proper cost tracking.

PR Description

Relevant issues

Type

🆕 New Feature
🐛 Bug Fix
🧹 Refactoring
📖 Documentation
🚄 Infrastructure
✅ Test

Changes(optional)

Testing(optional)

Note(optional)

@lkuligin
Copy link
Collaborator

lkuligin commented Jul 9, 2025

@imranarshad could you fix the linting, please?

@imranarshad imranarshad force-pushed the fix/cache-tokens-streaming-anthropic-vertex branch 2 times, most recently from 445e033 to 145069c Compare July 9, 2025 05:35
- Add reusable _create_usage_metadata() function matching langchain_anthropic
- Fix streaming message_start events to extract cache tokens
- Fix streaming message_delta events to follow official pattern
- Unify non-streaming to use same logic for consistency
- Use proper LangChain UsageMetadata structure with input_token_details
- Remove custom CacheUsageMetadata class in favor of standard types

Fixes missing cache token information in streaming responses to match
official langchain_anthropic behavior and enable proper cost tracking.
- Fix import sorting and remove unused type: ignore comments
- Remove invalid stream_runnable parameter from AgentExecutor
- Update test assertions to expect correct LangChain UsageMetadata format
- All tests now pass with proper cache token handling
@imranarshad imranarshad force-pushed the fix/cache-tokens-streaming-anthropic-vertex branch from 145069c to 190587a Compare July 9, 2025 06:08
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.

3 participants