-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Closed
Labels
bugSomething isn't workingSomething isn't working
Description
Initial Checks
- I confirm that I'm using the latest version of Pydantic AI
- I confirm that I searched for my issue in https://github.com/pydantic/pydantic-ai/issues before opening this issue
Description
Issue: RunUsage reports 0 tokens while actual token counts appear in details field.
When creating an agent using Anthropic's models and executing it with a query, the RunUsage object in the result incorrectly reports 0 tokens for all token metrics. However, the actual token counts are present and accessible in the details field.
Message history contains the expected values is ModelResponse object.
Expected behavior: Token usage should be properly populated in the RunUsage object's top-level fields.
Example Code
from pydantic_ai import Agent
def main():
# Create a simple agent with Anthropic model
agent = Agent("anthropic:claude-3-7-sonnet-20250219")
# agent = Agent('openai:gpt-5')
query = "What is 2+2?"
result = agent.run_sync(query)
usage = result.usage()
print(f"Query: {query}")
print(f"Response: {result.output}")
print(f" Requests: {usage.requests}")
print(f" Input tokens: {usage.input_tokens}")
print(f" Output tokens: {usage.output_tokens}")
print(f" Details: {usage.details}")
for message in result.all_messages():
print(message)
if __name__ == "__main__":
main()
=== Output ===
Query: What is 2+2?
Response: 2+2 = 4
Requests: 1
Input tokens: 0
Output tokens: 0
Details: {'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0, 'input_tokens': 14, 'output_tokens': 11}
ModelRequest(parts=[UserPromptPart(content='What is 2+2?', timestamp=datetime.datetime(2025, 10, 22, 14, 12, 56, 814250, tzinfo=datetime.timezone.utc))])
ModelResponse(parts=[TextPart(content='2+2 = 4')], usage=RequestUsage(details={'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0, 'input_tokens': 14, 'output_tokens': 11}), model_name='claude-3-7-sonnet-20250219', timestamp=datetime.datetime(2025, 10, 22, 14, 12, 58, 10504, tzinfo=datetime.timezone.utc), provider_name='anthropic', provider_details={'finish_reason': 'end_turn'}, provider_response_id='msg_01BGULjbBK76JZCqno7uqAXb', finish_reason='stop')Python, Pydantic AI & LLM client version
Python 3.10.16
anthropic==0.71.0
pydantic-ai==1.2.0
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't working