AI Agent Cost Monitoring: Track Claude Code Token Costs
What Claude Code agents typically cost per day, what drives token spend, and how to monitor costs across your entire agent fleet. Updated July 2026.
A single Claude Code agent running Claude Sonnet 4.5 costs roughly $5–$25 per day for a focused coding task; heavy sessions with large context and many tool calls can reach $40–$60. Running five to ten parallel agents on Sonnet 4.5 can easily hit $50–$200/day. The Anthropic Console shows aggregate spend but provides no per-session or per-task breakdown — you need a tool like amux to attribute costs to specific agents and tasks before a surprise invoice arrives.
Typical Token Costs by Model
Anthropic prices Claude API access per million tokens (MTok) of input and output separately. As of mid-2026, the primary models used with Claude Code are:
| Model | Input (per MTok) | Output (per MTok) | Typical daily cost (1 agent) | Best for |
|---|---|---|---|---|
| Claude Sonnet 4.5 | $3.00 | $15.00 | $5–$25 | Production code generation, complex refactors |
| Claude Opus 4 | $15.00 | $75.00 | $25–$120 | Architecture decisions, hard bugs requiring deep reasoning |
| Claude Haiku 3.5 | $0.80 | $4.00 | $1–$6 | Exploratory tasks, linting, documentation, low-stakes automation |
Output tokens cost 5× more than input tokens on Sonnet 4.5. An agent that generates long code responses or detailed reasoning chains will skew toward the output rate. In practice, the input-to-output ratio for Claude Code sessions is typically 8:1 to 15:1 — most tokens are context, not generation — which keeps average costs closer to the input rate. But the ratio shifts on tasks requiring long file rewrites or verbose explanations.
A rough daily cost estimate:
# Back-of-envelope for one Sonnet 4.5 agent, moderate task
Input: ~2M tokens/day × $3.00/MTok = $6.00
Output: ~200k tokens/day × $15.00/MTok = $3.00
Total: ~$9.00/day
# 10 parallel agents, same rate
Total: ~$90.00/day
These estimates assume normal task pacing. A runaway agent (stuck in a retry loop, or processing large files repeatedly) can spike 5–10x above baseline. Without per-session tracking, these spikes are invisible until the monthly bill.
What Drives Agent Costs
Token costs in AI agent sessions are dominated by a few specific patterns. Understanding them is the first step to controlling spend.
1. Context Size (the Biggest Driver)
Every API call sends the entire conversation history as input tokens — your initial prompt, every tool call and its output, every file read, and every prior response. As a session runs longer, the input token count of each subsequent call grows linearly. An agent that runs for 6 hours accumulates far more input tokens per call than one that runs for 30 minutes.
Common context-bloating patterns:
- Reading whole files instead of targeted sections — a 1,000-line file read in full adds ~15,000 input tokens per subsequent call that file stays in context.
- Verbose bash output — long test runner output, large JSON API responses, or recursive directory listings left in context accumulate fast.
- Repeated reads of the same file — if the agent re-reads a file on each loop iteration, those tokens multiply.
2. Tool Calls
Each tool call (bash execution, file read, file write, web fetch) adds input tokens for the tool result on top of the existing context. A session that makes 200 tool calls is not just paying for 200 results — it is paying for each result plus the growing history every step of the way. Tool-call-heavy sessions grow super-linearly in token cost.
3. Number of Parallel Sessions
Each parallel agent is an independent API consumer. Ten agents running on Sonnet 4.5 cost ten times as much as one, all else equal. The risk compounds because idle sessions that were never stopped still consume tokens if they receive any stimulus — even a watchdog ping can trigger a response.
4. Model Choice per Task
Running every task on Opus 4 when Haiku 3.5 would suffice is a common over-spend pattern. Opus 4 costs roughly 19× more per input token than Haiku 3.5. For tasks that do not require deep reasoning — formatting, documentation, simple refactors, search-and-replace — Haiku 3.5 produces equivalent output at a fraction of the cost.
5. Overnight Cost Spikes
Overnight runs that encounter errors often enter retry loops the developer cannot see. A session trying to fix a failing test can make hundreds of API calls per hour without making progress — each call incurring full context cost. Without real-time cost monitoring, these loops run unchecked until morning.
How to Track Costs with amux
amux is an open-source control plane for AI agent teams that ships a built-in token/cost ledger — no third-party billing tool required. The ledger was introduced in 418a6eb (2026-07-20) with the Cost tab UI added in e592079 the same day.
The Token/Cost Ledger
Every token event from every agent session is recorded server-side with attribution to the originating session and the active task window. This means you can answer questions like:
- Which session spent the most tokens last night?
- Which task within a session was the most expensive?
- When did a cost spike start, and which tool call triggered it?
The ledger is queryable via the /api/observability endpoint:
# Per-session cost breakdown
curl -sk $AMUX_URL/api/observability | python3 -c "
import json, sys
data = json.load(sys.stdin)
for s in data.get('sessions', []):
print(s['name'], '|', s.get('total_cost_usd', 0), 'USD', '|', s.get('total_tokens', 0), 'tokens')
"
# Per-task breakdown within a session
curl -sk "$AMUX_URL/api/observability?session=my-session" | python3 -c "
import json, sys
data = json.load(sys.stdin)
for t in data.get('tasks', []):
print(t['task'], '|', t.get('cost_usd', 0), 'USD')
"
The Cost Tab in the Dashboard
The amux dashboard has a global Cost tab that shows a fleet-level overview: total spend today, spend by session, and a time-series chart of token consumption. Each session row is expandable to show task-level attribution.
The per-session peek panel also has a Cost sub-tab, so you can inspect the spend for a specific agent without leaving the session view. This is the fastest way to identify which session is responsible for a cost spike.
Real-Time Spend Visibility
The dashboard updates via SSE (server-sent events), so the Cost tab reflects spend in near-real time as sessions run. You do not need to wait for a billing cycle or poll an external API — the cost counter updates as each token event lands. For overnight monitoring, this means you can open the dashboard on your phone and see current spend at a glance.
# Observability endpoint response shape (simplified)
{
"fleet_total_usd": 12.47,
"sessions": [
{
"name": "auth-refactor",
"model": "claude-sonnet-4-5",
"input_tokens": 3820000,
"output_tokens": 284000,
"total_cost_usd": 8.21,
"tasks": [
{"task": "Refactor auth middleware", "cost_usd": 5.14},
{"task": "Write tests for auth flow", "cost_usd": 3.07}
]
},
{
"name": "docs-update",
"model": "claude-haiku-3-5",
"input_tokens": 9100000,
"output_tokens": 610000,
"total_cost_usd": 4.26
}
]
}
Best Practices to Reduce Costs
1. Context Compaction
Claude Code automatically compacts (summarizes) the conversation when the context window fills — this resets input tokens per call back to a low baseline and prevents runaway context growth. You cannot disable compaction, but you can reduce how often it fires by keeping sessions scoped to smaller tasks. See Claude Code Context Compaction for a full breakdown.
2. Targeted File Reads
Instruct agents to read specific functions or sections rather than entire files. Using grep to locate relevant lines before reading reduces the token footprint of each file read by 80–95% for large files. Add this to your CLAUDE.md:
# In CLAUDE.md — reduces context bloat
Read only the specific functions you need, not entire files.
Use grep to locate the relevant section first, then read
that section only. Never cat files longer than 200 lines
without narrowing to the relevant range.
3. Token Budgets per Session
Claude Code supports a --max-tokens flag and a token budget setting. Setting a per-session budget caps worst-case spend. When the budget is approached, you can receive an alert (via amux's notification system) rather than discovering the overage after the fact.
4. Model Selection by Task Type
Route tasks to the cheapest capable model:
- Haiku 3.5 — documentation, search, formatting, simple refactors, file moves
- Sonnet 4.5 — feature development, bug fixes, code review, test generation
- Opus 4 — architecture, hard debugging requiring deep chain-of-thought, security analysis
5. Session Hibernation
Stop idle sessions rather than leaving them parked. An agent with an open context window that receives any input — even a watchdog health-check — will generate a response and incur tokens. amux's hibernation feature stops idle sessions automatically and restores them on demand, eliminating parasitic spend from sessions that finished their task hours ago but were never explicitly stopped.
6. Structured Output over Verbose Prose
Agents asked to “explain your reasoning” at each step generate significantly more output tokens than agents instructed to “act, then commit, then move to the next task.” Verbose reasoning output is helpful when debugging agent behavior, but for production overnight runs it adds cost without adding value. Keep the output format terse in your task prompts.
Manual Tracking vs. amux Cost Monitoring
| Capability | Manual / Anthropic Console | amux Cost Monitoring |
|---|---|---|
| Aggregate daily spend | Yes — via console.anthropic.com | Yes — dashboard Cost tab |
| Per-session attribution | No — all sessions aggregate to one API key | Yes — per-session cost breakdown |
| Per-task attribution | No | Yes — task-window cost tracking |
| Real-time visibility | No — Anthropic Console updates with delay | Yes — SSE-driven live updates |
| Spike detection | Manual — check console periodically | Automatic — visible in fleet overview |
| Historical task cost audit | No per-task history | Yes — /api/observability ledger |
| Mobile access | Console is mobile-accessible but read-only | Dashboard PWA + iOS app |
| Programmatic access | Usage API (aggregate only) | /api/observability (per-session, per-task) |
FAQ
How much do Claude Code agents cost per day?
A single Claude Code session running Claude Sonnet 4.5 typically costs $5–$25 per day for a focused coding task. A heavy session with many file reads, tool calls, and long context windows can reach $40–$60 per day. Running five to ten parallel agents on Sonnet 4.5 can easily cost $50–$200 per day. Switching to Claude Haiku 3.5 for lower-priority tasks reduces cost by roughly 75%. Costs depend on model choice, task complexity, context size, and number of parallel sessions.
How do I track Claude Code API costs?
The Anthropic Console (console.anthropic.com) shows aggregate API usage by day, but does not break costs down by agent session or task. For per-session and per-task attribution, use amux — its token/cost ledger records every token event with task-window attribution, and the built-in Cost tab in the dashboard shows spend per session in real time. The /api/observability endpoint returns machine-readable breakdowns you can feed into your own monitoring.
Why do AI agent costs spike overnight?
Three common causes: (1) Context accumulation — as a session runs longer, each API call includes more input tokens from the growing conversation history. A 12-hour overnight run can have 10× the average input tokens of a fresh session. (2) Parallel session sprawl — agents started for a task but never stopped keep consuming tokens even when idle. (3) Tool-call loops — an agent stuck in a retry loop (failing tests, network errors) can spin up thousands of API calls per hour. amux’s fleet view and cost monitoring surface these spikes in real time.
How does amux track token costs across sessions?
amux ships a server-side token/cost ledger (introduced in commit 418a6eb, 2026-07-20) that records every token event emitted by any agent session. Each event is attributed to the session and the active task window, so you can see not just total spend but which task in which session consumed the most tokens. The dashboard’s Cost tab shows this per-session breakdown. The /api/observability endpoint returns the same data as JSON for programmatic access.
What is the cheapest way to run multiple AI coding agents?
The most effective cost levers are: (1) Use Claude Haiku 3.5 ($0.80/$4 per MTok) for exploratory or low-stakes tasks and reserve Sonnet 4.5 ($3/$15 per MTok) for production code generation. (2) Enable context compaction — Claude Code does this automatically, but keeping tasks scoped reduces how often you approach the limit. (3) Set token budgets on each session and get alerts before they are exceeded. (4) Use session hibernation to stop idle agents rather than leaving them running. amux provides all of these controls from a single dashboard.