Cost Management and Analytics for Claude Code
Claude Code scales usage across teams—hundreds of engineers running thousands of sessions daily. Without visibility into costs, bills surprise you. With visibility, you can allocate budgets, compare models, and optimize spending.
This guide covers cost tracking: token accounting, per-team and per-developer breakdown, spend limits and alerts, model cost differences, and budget forecasting. You'll learn how extended thinking and prompt caching affect costs, and how to build dashboards that empower teams to optimize. For the broader enterprise rollout this cost data feeds into, see Claude Code enterprise setup; for the compliance controls that pair with spend limits, see enterprise security and compliance.
Token Usage Tracking: Prompt and Completion
Every Claude request consumes tokens: prompt tokens (input) and completion tokens (output). Costs differ by model and token type. Tracking token usage is the foundation of cost management.
The pipeline: runner captures tokens (via SDK metrics), sends to analytics collector, data warehouse aggregates by hour/day, and dashboards query warehouse. Token counts come from API responses; models report prompt_tokens and completion_tokens in usage fields.
Pricing varies: Claude Sonnet costs $3 per 1M prompt tokens, $15 per 1M completion tokens. Opus costs 3x more. Tracking per-model spending lets you make cost/quality tradeoffs.
Quick reference
- Prompt tokens: input context + user message; varies per request.
- Completion tokens: model output; can be 1–4K for typical responses.
- Cache tokens (with prompt caching): cached input tokens cost 10% of normal; cache hits save 90% on prompt cost.
- Price per model: Sonnet $3/$15; Opus $15/$75; Haiku $0.80/$4 (prices as of Aug 2026).
- Track per: session, user, team, model, feature_used (e.g., extended_thinking=true).
Remember this
Capture prompt + completion tokens per request, aggregate by day/team/model, and query to understand usage patterns and model choice tradeoffs.
Per-Project and Per-Developer Breakdown
Teams run multiple projects: "codebase-analysis" consumes high tokens; "comment-generation" low. Developers within teams have different usage patterns. Granular breakdown shows which projects and developers drive costs.
Tag events with project_id and developer_id at collection time. Aggregate to show cost per project (product teams see which projects are expensive), and per developer (for chargeback or kudos).
For chargeback: assign each developer a monthly budget ($100–200). Charge projects to cost centers. This incentivizes optimization.
Quick reference
- Project tag: add to token_usage event; e.g., project_id='analysis-engine'.
- Developer tag: user_id already present; optionally add name for dashboards.
- Cost calc: cost_usd = (prompt_tokens prompt_rate + completion_tokens completion_rate) / 1e6.
- Chargeback: allocate cost back to developer/project; bill monthly.
- Threshold alerts: if developer exceeds $50 in a day, alert them.
Remember this
Tag events with project and developer; aggregate costs per project/developer; set per-developer budgets and alert on overspend.
Spend Limits: Soft and Hard Caps
Two types of limits: soft caps (warn but allow) and hard caps (reject). A team with $1000/month budget: at $800 (80%), send warning; at $1000 (100%), reject new sessions.
Soft caps encourage optimization without disruption. Hard caps prevent runaway costs. For aggressive teams, you can adjust daily: if a team is tracking to overspend by month-end, lower their daily allowance for remaining days.
Implement at the gateway: before sending a request to Claude, check if the user's team (or individual) is over budget. If yes, return 429 Too Many Requests with a message explaining the limit.
Quick reference
- Soft cap: at 80% of monthly budget, send warning email to team lead.
- Hard cap: at 100%, reject new sessions; return 429; tell user to wait until next billing cycle.
- Daily smoothing: if monthly budget is $1000 and we're on day 20 of 30, daily allowance is $1000 * (remaining_days / total_days) = $333.
- Override: allow managers to override hard cap (e.g., for critical project) with approval log.
- Grace period: last 3 days of month, allow up to 10% over budget (catch-up for business needs).
Remember this
Soft caps warn; hard caps reject; daily smoothing allocates budget fairly across remaining days; override with approval.
Model Cost Differences: Sonnet vs Opus
Cost-quality tradeoff: Sonnet is 3x cheaper but slower. Opus is 3x more expensive but smarter. Haiku is 10x cheaper but limited. Choosing the right model per task saves 70% of costs.
For most tasks (summarization, routing, simple code review), Sonnet is fine and saves $. For complex reasoning (design decisions, advanced analysis), Opus is worth the premium.
Track cost per model to show which teams overspend on Opus when Sonnet would do. Give teams the choice, but highlight the impact.
Quick reference
- Haiku: $0.80 / $4 (input/output) — good for simple routing, classification.
- Sonnet: $3 / $15 — balanced cost/quality; default for most work.
- Opus: $15 / $75 — expensive; use for complex reasoning, customer-facing.
- Cost ratio: Opus costs ~8x more than Haiku for same task.
- Latency: Haiku ~200ms, Sonnet ~500ms, Opus ~800ms (approximate).
Remember this
Sonnet ($3/$15) is best for most work; use Opus ($15/$75) only for complex reasoning; Haiku ($0.8/$4) for simple routing.
Extended Thinking Cost Impact
Extended thinking (letting the model reason internally before responding) improves accuracy but increases token consumption and cost. A task might consume 2x tokens with extended thinking.
Track separately: tag events with extended_thinking=true. Calculate cost impact: "This team spent $50K; with extended thinking, they'd spend $100K." Show the tradeoff: better answers, higher cost.
Extended thinking is worth it for complex reasoning (system design, algorithm decisions) and customer-facing responses, not routine work.
Quick reference
- Extended thinking: model reasons internally; final answer is shorter but more accurate.
- Token cost: reasoning tokens count same as output tokens; ~2–5x more tokens for typical tasks.
- Quality gain: accuracy improves ~5–15% on hard tasks (software design, math).
- Cost/quality: calculate ROI—if accuracy improves 10% but cost increases 200%, not worth it.
- Tag: extended_thinking_enabled, reasoning_tokens in events.
Remember this
Extended thinking costs 2–5x more tokens but improves accuracy; tag separately and track ROI by task type.
Prompt Caching Savings
Prompt caching stores large context blocks (e.g., entire codebase, documentation) at the API layer. Subsequent requests reuse the cached context for 10% of normal prompt token cost. This is huge: if you analyze the same codebase 10 times, you pay prompt cost only once.
Track cache creation tokens (first request) and cache read tokens (subsequent). Monitor cache hit rate: high hit rate means big savings; low hit rate means you're not caching effectively.
For teams repeatedly analyzing the same context (e.g., QA testing against the same codebase), caching can reduce costs by 80%.
Quick reference
- Cache write: first request to a context; counted as full prompt tokens.
- Cache read: subsequent requests to same context; prompt tokens cost 1/10th normal rate.
- Cache TTL: 5 minutes for API cache; expired caches are re-created (cost reset).
- Hit rate: (cache_read_tokens / (cache_read_tokens + normal_prompt_tokens)) * 100.
- Ideal for: analyzing same codebase/docs repeatedly, e.g., QA, security review.
Remember this
Prompt caching reduces subsequent requests to 10% cost; ideal for teams repeatedly analyzing same context (80% savings for QA teams).
Analytics Dashboard Walkthrough
A dashboard should answer: "What are we spending? On what? Who? Is it growing? Which models? Is caching working?" Key charts:
1. Total spend (daily) — trend over time. 2. Spend by team — which teams drive costs. 3. Spend by model — are we using the right models? 4. Spend by feature (extended thinking, caching) — ROI on expensive features. 5. Per-developer spending — chargeback view. 6. Cache hit rate — is caching effective? 7. Budget vs actual — are we on track?
Quick reference
- Daily spend chart: x=date, y=spend_usd; trend line shows growth rate.
- Team breakdown: stacked bar chart; team colors; hover for details.
- Model distribution: pie chart; Sonnet %, Opus %, Haiku %; cost per model.
- Feature ROI: extended_thinking spend vs total; cache_savings vs cache_cost.
- Developer leaderboard: top spenders; filter by team or project.
- Budget tracking: gauge chart; % of monthly budget consumed; days remaining.
Remember this
A cost dashboard shows spend trends by team, model, and feature; enables data-driven budget decisions and optimization.
Budget Forecasting Patterns
Given 30 days of history, predict month-end spend. Simple method: daily average * days in month. Better: fit a trend line (spend growing 5%/day?) and extrapolate.
For teams with seasonal patterns (e.g., spike during launch prep), use historical month patterns or allow manual forecasting adjustments.
Alert if forecast > budget: "Your team is on track to spend $1200 this month; budget is $1000. Adjust model choices or extend budget."
Quick reference
- Simple forecast: avg_daily_spend * days_in_month.
- Trend-based: fit line to daily spend; extrapolate to month-end.
- Seasonal adjustment: compare to same month last year; adjust for growth rate.
- Confidence: low (day 5), high (day 25).
- Alert threshold: if forecast > budget * 0.9, warn team.
Remember this
Forecast month-end spend using daily average or trend; alert teams if forecast exceeds budget; adjust confidence based on days elapsed.
Key takeaway
Cost analytics are the backbone of enterprise Claude Code operations. Track token usage per request, aggregate by team/developer/model, enforce budgets, and forecast spend.
Start simple: collect token events, aggregate daily, show total spend. Add per-team breakdown. Layer in model recommendations and caching ROI.
The key insight: visibility enables optimization. Teams that see their costs and model choices optimize naturally. Teams without visibility tend to over-spend.
Next: combine cost analytics with enterprise security to ensure you're not only tracking spend, but also compliance and data residency.
Polo Khan
Lead Author & Systems ArchitectSoftware engineer and distributed systems architect specializing in backend scalability, cloud-native infrastructure, databases, and AI engineering workflows. Author and maintainer of Core Concept Learning.
Related Articles
Explore this topic