Skip to main content

How to Read the Token Usage of a Claude API Response in Python

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

The Claude API bills by tokens, and every response tells you exactly how many it used. Reading those numbers is how you know what a call cost, whether caching worked, and how much Claude spent thinking. This guide prints each field of the usage object and turns them into money, with real output from anthropic 2.14.0.

Syntax​

Syntax
usage = message.usage

usage.input_tokens
usage.output_tokens
usage.output_tokens_details.thinking_tokens
usage.cache_creation_input_tokens
usage.cache_read_input_tokens
FieldWhat it counts
input_tokensInput tokens that weren't written to or read from the cache
output_tokensEverything Claude produced, thinking included. This is what's billed as output
output_tokens_details.thinking_tokensThe part of output_tokens spent thinking
cache_creation_input_tokensInput tokens written to the cache by this call
cache_read_input_tokensInput tokens read from the cache by this call

Anthropic's API reference: "Total input tokens in a request is the summation of input_tokens, cache_creation_input_tokens, and cache_read_input_tokens." About thinking, "output_tokens remains the inclusive, authoritative total used for billing."

What It Costs​

The Claude API is paid. Claude Opus 5.5 costs $4 per million input tokens and $20 per million output tokens (Anthropic's pricing). The call below cost $0.0023.

Prerequisites​

  • A Claude API key, in a .env file as ANTHROPIC_API_KEY=....
  • The Anthropic Python SDK in a uv project. This guide uses anthropic 2.14.0, added with uv add anthropic, on Python 3.15.2.

Step 1: Write the Script​

usage.py
import anthropic

client = anthropic.Anthropic()

message = client.messages.create(
model="claude-opus-5-5",
max_tokens=1024,
messages=[{"role": "user", "content": "Give me one tip for packing a backpack."}],
)

usage = message.usage
print("input tokens:", usage.input_tokens)
print("output tokens:", usage.output_tokens)
print(" thinking tokens among them:", usage.output_tokens_details.thinking_tokens)
print("cache write tokens:", usage.cache_creation_input_tokens)
print("cache read tokens:", usage.cache_read_input_tokens)

The usage.py script in a macOS-style code window: one question to claude-opus-5-5, then five prints of the usage fields: input, output, thinking, cache write and cache read tokens.

Step 2: Run It​

Terminal
uv run --env-file .env usage.py
Output
input tokens: 22
output tokens: 112
thinking tokens among them: 11
cache write tokens: 0
cache read tokens: 0

The output in a terminal: input tokens 22, output tokens 112 with 11 thinking tokens among them, cache write tokens 0 and cache read tokens 0.

Step 3: Work Out the Cost​

At Claude Opus 5.5's prices:

PartTokensPrice per millionCost
Input22$4$0.000088
Output, thinking included112$20$0.00224
Total$0.002328

Output dominates: 112 output tokens cost 25 times more than 22 input tokens. The 11 thinking tokens are already inside the 112. Both cache fields are 0 because this call didn't use caching; with caching, add cache writes at $5 per million and reads at $0.20 per million.

Common Mistakes​

MistakeWhat happensDo this instead
Adding thinking_tokens to output_tokensThinking is counted twiceoutput_tokens already includes it
Treating input_tokens as the whole inputCached tokens are left outAdd the two cache fields for the total input

Troubleshooting​

SymptomLikely causeFix
output_tokens is larger than the visible answerClaude thought before answeringCheck output_tokens_details.thinking_tokens

FAQ​

Does Claude Opus 5.5 always think?​

Thinking is always on and can't be turned off on this model, but how much it thinks depends on the question and the effort setting. On a simple question it may think little or not at all, and thinking_tokens is then small or 0.

Can I see totals across many calls?​

Add the fields up yourself, or use a tracing tool that records each call's usage, such as MLflow.

Conclusion​

message.usage showed 22 input tokens and 112 output tokens, 11 of them thinking, with no cache use, which at $4 and $20 per million tokens came to $0.0023.

Additional Resources​

  • Create a Message — the usage object and every field in it.
  • Pricing — prices per model, including cache writes and reads.