claude-agent-sdk-python icon indicating copy to clipboard operation
claude-agent-sdk-python copied to clipboard

Python SDK ResultMessage's message.usage['input_tokens'] isn't correct

Open kanlee opened this issue 6 months ago • 2 comments

I am trying to analyze token in and out for Claude Code Python SDK. For my task, after completion, I looked at the ResultMessage, usage['input_tokens'] is 69 usage['output_tokens'] is 18467

The input_tokens here is really off and output_tokens seem to be accumulated correctly.

I looked at cache_creation_input_tokens and cache_read_input_tokens stored in usage dictionary. They don't seem to represent cumulative input_tokens.

I used this prompt for my coding task

https://github.com/RooCodeInc/Roo-Code-Evals/blob/main/prompts/python.md

and had cwd pointed to this

https://github.com/RooCodeInc/Roo-Code-Evals/tree/main/python/pov

kanlee avatar Aug 05 '25 06:08 kanlee

cache_creation_input_tokens and cache_read_input_tokens are important to add, and they will also contain Claude Code's system prompt. You should expect ~10k cache creation and ~10k cache read tokens just from the system prompt alone.

dicksontsai avatar Aug 08 '25 06:08 dicksontsai

I've noticed this myself. Using Bedrock and the claude code preset system prompt. The input tokens reports just 18 tokens and does not reflect anywhere close to the size of context being sent to the LLM.

awmitch avatar Oct 10 '25 17:10 awmitch