# Practical ways to use the Claude Agent SDK
๐ฐ Track agent costs per step and per model to optimize your budget.
Cost and Usage Tracking uses `total_cost_usd` and `modelUsage` to monitor token consumption and costs in real time.
๐ Title: Cost and Usage Tracking
๐ URL:
๐งฉ Overview
` gives cumulative estimated cost per call. `modelUsage` breaks down tokens by model. Prompt caching optimization can significantly reduce costs.
๐ How to use it
Read `total_cost_usd` from `ResultMessage`. Deduplicate using `AssistantMessage` IDs for accurate per-step accounting.
๐ Practical usage
- Monitor `total_cost_usd` per `query()` call and trigger alerts when thresholds are exceeded.
- Use `modelUsage` to break down costs between sub-agent Haiku and main Opus usage, visualizing where costs concentrate.
- Set `ENABLE_PROMPT_CACHING_1H=1` to extend TTL from 5 minutes to 1 hour, preventing cache misses across many short sessions.
- Accumulate `total_cost_usd` across multiple `query()` calls for session-level cost management.
๐ก Use cases
๐ Per-step cost visualization dashboard
๐ Automatic budget threshold alerts
โก Prompt cache optimization for cost reduction
โ ๏ธ Watch out
`total_cost_usd` is an approximate estimate. Use the Usage and Cost API / Console for authoritative billing. The SDK doesn't provide session totals โ accumulate on the app side.
#
ClaudeAgentSDK# #
AI#