SDK Cost Tracking
The Claude Code SDK provides detailed token usage information for each interaction with Claude. This guide explains how to properly track costs and understand usage reporting, especially when dealing with parallel tool uses and multi-step conversations. For complete API documentation, see the TypeScript SDK reference.Understanding Token Usage
When Claude processes requests, it reports token usage at the message level. This usage data is essential for tracking costs and billing users appropriately.Key Concepts
- Steps: A step is a single request/response pair between your application and Claude
- Messages: Individual messages within a step (text, tool uses, tool results)
- Usage: Token consumption data attached to assistant messages
Usage Reporting Structure
Single vs Parallel Tool Use
When Claude executes tools, the usage reporting differs based on whether tools are executed sequentially or in parallel:Message Flow Example
Here’s how messages and usage are reported in a typical multi-step conversation:Important Usage Rules
1. Same ID = Same Usage
All messages with the sameid
field report identical usage. When Claude sends multiple messages in the same turn (e.g., text + tool uses), they share the same message ID and usage data.
2. Charge Once Per Step
You should only charge users once per step, not for each individual message. When you see multiple assistant messages with the same ID, use the usage from any one of them.3. Result Message Contains Cumulative Usage
The finalresult
message contains the total cumulative usage from all steps in the conversation:
Implementation: Cost Tracking System
Here’s a complete example of implementing a cost tracking system:Handling Edge Cases
Output Token Discrepancies
In rare cases, you might observe differentoutput_tokens
values for messages with the same ID. When this occurs:
- Use the highest value - The final message in a group typically contains the accurate total
- Verify against total cost - The
total_cost_usd
in the result message is authoritative - Report inconsistencies - File issues at the Claude Code GitHub repository
Cache Token Tracking
When using prompt caching, track these token types separately:Best Practices
- Use Message IDs for Deduplication: Always track processed message IDs to avoid double-charging
- Monitor the Result Message: The final result contains authoritative cumulative usage
- Implement Logging: Log all usage data for auditing and debugging
- Handle Failures Gracefully: Track partial usage even if a conversation fails
- Consider Streaming: For streaming responses, accumulate usage as messages arrive
Usage Fields Reference
Each usage object contains:input_tokens
: Base input tokens processedoutput_tokens
: Tokens generated in the responsecache_creation_input_tokens
: Tokens used to create cache entriescache_read_input_tokens
: Tokens read from cacheservice_tier
: The service tier used (e.g., “standard”)total_cost_usd
: Total cost in USD (only in result message)
Example: Building a Billing Dashboard
Here’s how to aggregate usage data for a billing dashboard:Related Documentation
- TypeScript SDK Reference - Complete API documentation
- SDK Overview - Getting started with the SDK
- SDK Permissions - Managing tool permissions