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

AgentLocationWhat 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.
Cursorstate.vscdb (SQLite) in Cursor’s User/globalStorage folderChat “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

  1. 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.
  2. Summing Codex’s running total. total_token_usage is cumulative, so adding it up per event inflates the number wildly. Sum last_token_usage per turn instead, and skip repeated events where the total did not change. Also note that Codex’s input_tokens already includes cached_input_tokens; split them before pricing, because cached input is much cheaper.
  3. 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:

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

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

LoopGuard on GitHub · Pricing