# 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#