Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Conversation

alexmojaki
Copy link
Contributor

@alexmojaki alexmojaki commented Oct 8, 2025

Wanted to test using genai-prices to extract usage, discovered in the process that anthropic streaming usage was missing input tokens entirely!

@alexmojaki alexmojaki changed the title Use genai-prices to extract usage for anthropic Fix token usage for anthropic streaming Oct 8, 2025
Copy link

github-actions bot commented Oct 8, 2025

Docs Preview

commit: c2c8a40
Preview URL: https://6adcebc8-pydantic-ai-previews.pydantic.workers.dev

@alexmojaki alexmojaki requested a review from Copilot October 8, 2025 08:34
Copy link
Contributor

@Copilot Copilot AI left a 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's extract_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.

pydantic_ai_slim/pydantic_ai/models/anthropic.py Outdated Show resolved Hide resolved
pydantic_ai_slim/pydantic_ai/models/anthropic.py Outdated Show resolved Hide resolved
@alexmojaki alexmojaki requested a review from DouweM October 8, 2025 09:49
@alexmojaki alexmojaki enabled auto-merge (squash) October 8, 2025 09:54
@alexmojaki alexmojaki merged commit 3eaa11e into main Oct 8, 2025
29 checks passed
@alexmojaki alexmojaki deleted the alex/extract-usage branch October 8, 2025 10:03
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.

2 participants

Morty Proxy This is a proxified and sanitized view of the page, visit original site.