Guides · Updated October 18, 2026
How to track AI coding agent token costs per session
Claude Code, Codex CLI and Cursor all record token usage on your own machine. Once you know where those records live and how to read them, you can see which session, model or agent is actually eating your budget or your usage limit, without a proxy and without handing anyone an API key.
Where each agent keeps its usage data
| Agent | Location | What is in it |
|---|---|---|
| Claude Code | ~/.claude/projects/<project>/<session>.jsonl (or $CLAUDE_CONFIG_DIR/projects) | One JSON line per event. Assistant messages carry message.usage: input_tokens, output_tokens, cache_read_input_tokens, cache_creation_input_tokens, plus the model. |
| Codex CLI | ~/.codex/sessions/YYYY/MM/DD/rollout-*.jsonl (or $CODEX_HOME) | token_count events with last_token_usage (this turn) and total_token_usage (running total). The model is in the preceding turn_context / session_meta. |
| Cursor | state.vscdb (SQLite) in Cursor’s User/globalStorage folder | Chat “bubbles” in the cursorDiskKV table (bubbleId:* keys). Some carry a tokenCount; many do not. The cursor-agent CLI keeps its chats elsewhere (~/.cursor/chats/). Exact per-turn counts are only handed to hooks: the stop hook gets input_tokens (which includes cache reads and writes), output_tokens, cache_read_tokens and cache_write_tokens. |
Three mistakes that make totals wrong
- Double counting Claude Code messages. A streamed response can be written to the transcript more than once. De-duplicate by message id + request id before summing.
- Summing Codex’s running total.
total_token_usageis cumulative, so adding it up per event inflates the number wildly. Sumlast_token_usageper turn instead, and skip repeated events where the total did not change. Also note that Codex’sinput_tokensalready includescached_input_tokens; split them before pricing, because cached input is much cheaper. - Pricing cache tokens like fresh input. Cache reads are typically billed at a fraction of the input price, and Claude’s cache writes at a premium over it. On long sessions most input tokens are cache reads, so getting this wrong can swing the estimate by several times.
From tokens to dollars
For each request: tokens in each category × that model’s price per million tokens, summed. Group the results by session id and you have cost per session. Two caveats:
- This is the API list-price equivalent. On a subscription you are not billed per token, but the number is still a useful, consistent measure of how hard a session is pushing your limits.
- When Cursor stores no token count for a response, the best you can do is estimate from text length (about 4 characters per token), and you should label that row as an estimate.
Doing it with one command
LoopGuard’s open-source CLI reads all three sources, de-duplicates and prices them, and keeps an incremental index so repeat reports are fast:
npm i -g loopguard-cli # puts `loopguard` on your PATH (one-off: npx loopguard-cli <command>) loopguard report --by session # or --by day | agent | model loopguard report --since 2026-10-01 --agent codex loopguard report --by model --json # machine-readable, for scripts and spreadsheets
Example output (from LoopGuard’s test fixtures, one small session per agent):
$ loopguard report --by session SESSION AGENT INPUT OUTPUT CACHE R CACHE W COST ------------------------------------------ ------ ----- ------ ------- ------- ------ claude:sess-claude-1 claude 3.0k 1.5k 10.0k 2.0k $0.13 cursor:comp-1 cursor 4.0k 1.8k 0 0 $0.04* codex:0199a1b2-c3d4-7e5f-8a9b-0c1d2e3f4a5b codex 14.0k 3.0k 18.0k 0 $0.02 TOTAL $0.19 * includes estimated tokens (Cursor bubbles without token counts) Costs are estimates at API list prices; subscription plans are billed differently.
Default prices are a built-in list-price table. If you pay different rates or use a model it does not know, override it in ~/.loopguard/pricing.json:
{ "my-model": { "input": 2, "output": 8, "cacheRead": 0.2, "cacheWrite": 0 } }Reading the numbers
- One session dominates the total. Very common: a session left open for days keeps growing its context and re-sending it. Start fresh sessions more often, and consider a per-session cap.
- High CACHE R relative to INPUT means prompt caching is working. High fresh INPUT on every turn usually means the cache keeps expiring (long pauses between turns) or the context is being rebuilt.
- Large CACHE W on Claude means a lot of new context is being cached: big files pasted in, many tools/skills loaded, or frequent context changes.
- Compare --by model. Routine work on the most expensive model is the easiest saving there is.
From tracking to limits
A report tells you what happened. If you also want something to stop the next expensive session, the same CLI installs hooks that deny tool calls once a session or daily dollar cap is reached, and catches repeated identical tool calls. See How to stop runaway Claude Code, Codex and Cursor agent loops, or the per-tool guides: Claude Code spending limit, limiting Cursor agent costs and capping Codex CLI spend.
Other good tools: ccusage is a popular reporting CLI if you only need reports, and Claude Code’s built-in /cost command shows the current session. For history across several machines, LoopGuard Pro’s dashboard receives aggregate numbers only (tokens and cost per agent, session, model and day; never prompts or code) when you run loopguard sync; loopguard sync --dry-run prints the exact payload first.
Related
- How to stop runaway Claude Code, Codex and Cursor agent loops
- How to set a spending limit for Claude Code
- How to limit Cursor agent usage costs
- How to cap OpenAI Codex CLI spend
- Claude Code usage limit reached: why it happens and how to monitor it
- How to set a hard budget for AI coding agents in CI
- Claude Code stuck in a loop: how to break it and stop it repeating the same command
- Codex usage limit reached: 5-hour and weekly limits explained
- Cursor agent stuck in a loop: how to stop it, roll back, and keep it from repeating
- Why Claude Code uses so many tokens, and how to use fewer
- How to set a budget limit for Aider
- GitHub Copilot agent mode stuck in a loop: how to stop it and keep it from repeating
- Cline stuck in a loop: how to stop it, roll back, and cap what it spends
- Gemini CLI usage limits: daily quotas, the June 2026 change, and keeping costs in check
- Codex stuck in a loop: how to stop it, undo it, and keep it from repeating