How to Read the Token Usage of a Claude API Response in Python
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
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
| Field | What it counts |
|---|---|
input_tokens | Input tokens that weren't written to or read from the cache |
output_tokens | Everything Claude produced, thinking included. This is what's billed as output |
output_tokens_details.thinking_tokens | The part of output_tokens spent thinking |
cache_creation_input_tokens | Input tokens written to the cache by this call |
cache_read_input_tokens | Input 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
.envfile asANTHROPIC_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
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)

Step 2: Run It
uv run --env-file .env usage.py
input tokens: 22
output tokens: 112
thinking tokens among them: 11
cache write tokens: 0
cache read tokens: 0

Step 3: Work Out the Cost
At Claude Opus 5.5's prices:
| Part | Tokens | Price per million | Cost |
|---|---|---|---|
| Input | 22 | $4 | $0.000088 |
| Output, thinking included | 112 | $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
| Mistake | What happens | Do this instead |
|---|---|---|
Adding thinking_tokens to output_tokens | Thinking is counted twice | output_tokens already includes it |
Treating input_tokens as the whole input | Cached tokens are left out | Add the two cache fields for the total input |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
output_tokens is larger than the visible answer | Claude thought before answering | Check 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
usageobject and every field in it. - Pricing — prices per model, including cache writes and reads.
