-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Fix token usage for anthropic streaming #3111
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
Conversation
Docs Preview
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR fixes token usage calculation for Anthropic streaming responses by ensuring input tokens are properly tracked throughout the streaming process. The fix addresses a bug where input tokens were missing entirely from streaming usage data.
- Replaced manual usage calculation with
genai-prices
library'sextract_usage
function - Updated streaming usage logic to properly accumulate input tokens across message events
- Added comprehensive test coverage for streaming usage scenarios
Reviewed Changes
Copilot reviewed 3 out of 4 changed files in this pull request and generated 1 comment.
File | Description |
---|---|
pydantic_ai_slim/pydantic_ai/models/anthropic.py | Updated _map_usage function to use genai-prices library and fixed streaming token accumulation |
pydantic_ai_slim/pyproject.toml | Bumped genai-prices dependency to version 0.0.30 |
tests/models/test_anthropic.py | Added test for streaming usage and updated existing test expectations to include input tokens |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
Wanted to test using genai-prices to extract usage, discovered in the process that anthropic streaming usage was missing input tokens entirely!