How to read Claude Code's session logs (and the 4 traps that inflate your token count)
Claude Code keeps a full log of every session on your disk. If you want to know how many tokens you've used, which model they went to, or what a session cost at API prices, everything you need is in there.
The format has a few traps that make naive totals wrong, sometimes several times too high. This is what I learned writing Agent Wrapped, a browser tool that reads these logs. It checked out against 2,845 files and 3.4 GB of my own history.
Where the logs are
~/.claude/projects/<encoded-project-path>/<session-id>.jsonl
~/.claude/projects/<encoded-project-path>/<session-id>/subagents/agent-<id>.jsonl
One JSONL file per session, one JSON object per line. Subagents get their own files in a subagents folder next to the session that launched them. On Windows the root is %USERPROFILE%\.claude.
By default Claude Code deletes logs older than about 30 days. Raise cleanupPeriodDays in ~/.claude/settings.json if you want a longer history.
The lines that matter
Most lines have a type. The two you need are assistant (a model response) and user (your prompts and tool results). There are many bookkeeping types too (custom-title, file-history-snapshot, queue-operation, attachment and others); skip them.
An assistant line looks roughly like this (trimmed):
{
"type" : "assistant",
"timestamp" : "2026-11-24T14:02:11.512Z",
"cwd" : "/home/me/project",
"entrypoint" : "cli",
"message" : {
"id" : "msg_01...",
"model" : "claude-opus-5-5",
"content" : [
{
"type" : "tool_use",
"id" : "toolu_01...",
"name" : "Read",
"input" : { "file_path" : "..." }
}
],
"usage" : {
"input_tokens" : 3,
"output_tokens" : 412,
"cache_read_input_tokens" : 182340,
"cache_creation_input_tokens" : 2210,
"cache_creation" : {
"ephemeral_5m_input_tokens" : 2210,
"ephemeral_1h_input_tokens" : 0
}
}
}
}
Trap 1: one response is several lines
Claude Code writes each content block of a response (a thinking block, a text block, each tool call) as its own line, and every one of those lines carries the same usage object. A response that reads a file and runs a command appears three or four times. If you sum usage over every line, you count that response three or four times.
Fix: count usage once per message.id.
Trap 2: resumed sessions copy history
Resume or continue a session and the earlier turns are copied into the new file. Sum per file and the same responses are counted again.
Fix: keep the set of message.ids you've already counted across all files, not just per file.
Trap 3: there are four kinds of input token
usage splits input into:
input_tokens: uncached input (usually tiny)cache_creation_input_tokens: written to the prompt cachecache_read_input_tokens: read back from the cache- inside
cache_creation: how much of the write was the 5-minute or the 1-hour cache
They are priced very differently. At list prices a cache read is a small fraction of the input price, a 5-minute write is 1.25× input and a 1-hour write is 2× input. In my logs, cache reads were 95% of all tokens but only about a quarter of the cost. Price them separately, per model, or the dollar figure is meaningless.
Older lines have only cache_creation_input_tokens with no breakdown; treat those as 5-minute writes.
Trap 4: synthetic and error lines
Lines with "model": "<synthetic>" aren't model calls. Usage-limit messages appear as assistant lines with apiError: "usage_limit_reached" and zero usage. Skip both for totals.
The limit lines are useful in their own right, though: they're the exact moments you hit the wall, with rateLimitType (five_hour, seven_day) and resetsAt.
Things you can work out once the counting is right
- Context size per request =
input_tokens+ cache_read + cache_creation. Track it per session to see when a conversation got too big. - Cache rebuilds: a request whose cache read is far below the previous request's context, while the context hasn't shrunk, means the cache was lost (after an idle gap, a model switch, or a change to the tool list) and everything was written again.
- Tool-result weight: the
tool_resultblocks inuserlines show what each tool returned. A big result is paid once on arrival and again on every later turn. - Who used what:
cwdgives the project andentrypointthe surface (cli,sdk-clifor headlessclaude -p,claude-desktop, and so on). Subagent files map back to their parent session through the folder they're in.
A minimal correct total
const seen = new Set();
let tokens = 0;
for (const line of allLinesFromAllFiles) {
const d = JSON.parse(line);
if (d.type !== 'assistant') continue;
const m = d.message;
if (!m || !m.usage || m.model === '<synthetic>' || seen.has(m.id)) continue;
seen.add(m.id);
const u = m.usage;
tokens += (u.input_tokens || 0) + (u.output_tokens || 0) + (u.cache_read_input_tokens || 0) + (u.cache_creation_input_tokens || 0);
}
Stream the files rather than loading them whole: long sessions reach tens of megabytes, and a heavy user has gigabytes.
If you'd rather not write it Agent Wrapped does all of the above in your browser: pick your .claude folder and it shows tokens, models, tools, rhythm, a share card, and where usage went to cache rebuilds, oversized context and large tool results. It's one HTML file with a Content-Security-Policy that blocks all network requests, so nothing leaves your machine. The source is on GitHub (MIT). There's also a $9 Pro file with per-session detail and export.
If you've found other quirks in the format, I'd like to hear about them in the comments. Independent tool, not made or endorsed by Anthropic.
Comments
No comments yet. Start the discussion.