Context window forensics for Claude Code: track token usage, find dead weight, and optimize sessions via hooks, a web dashboard, and an MCP server
Project description
Context Analyzer
Context window forensics for AI coding agents.
See exactly where your tokens go — per call, per turn, per session · dashboard · MCP server · Claude Code + Codex.
Quick Start · Proof · What it does · Architecture · Issues
AI agents / LLMs: read /llms.txt
Your Claude Code calls cost 4.3x more at large context than at small context — $0.23/call at 63K tokens vs $0.98/call at 721K. Context Analyzer shows you exactly where that money goes, per call, per turn, per session.
Measured across real sessions:
- Cost per API call scales 4.3x from small to large context (63K peak = $0.23/call vs 721K peak = $0.98/call)
- 30% of context becomes stale dead weight within 5 turns — you keep paying for it anyway
- Tool I/O consumes 60%+ of used context — most of your window isn't conversation
- Sessions past 50% of the 1M window drive disproportionate cost — the last half of a long session is the expensive half
- Cache hit rate is 96–98% when the system prompt prefix stays stable — and every prefix change costs you a cold re-read
Quick Start
uv tool install context-tracker # or: pip install context-tracker
context-tracker up
context-tracker up installs the Claude Code hooks (backed up, reversible), ingests your existing transcripts, and opens the dashboard. You'll see data from your past sessions immediately — the hooks only add live capture going forward. Undo everything with context-tracker down.
Then open:
http://localhost:8080/— single-session dashboardhttp://localhost:8080/sessions— cross-session analytics
Proof
Cost scales with context — see it across sessions
The /sessions page plots cost/call against peak context for every session. This is where the 4.3x scaling shows up: the same kind of API call costs $0.23 at 63K peak context and $0.98 at 721K.
Watch a session get expensive in real time
The single-session dashboard shows context growth call by call. The steeper the curve, the faster each subsequent call gets more expensive — you can pinpoint the exact turn where a session tipped into the costly zone.
Know when you've crossed your budget
Toggle budget thresholds (200K / 500K / 700K / 1M) to see where usage exceeds your target. The dashed red line is your budget, the orange dotted line is where Claude Code auto-compacts, and the red shading is the zone where sessions drive disproportionate cost.
See what you're actually paying for
The composition panel breaks down every call into Tool I/O vs Conversation vs System prefix, using API-reported token counts as ground truth. This is where the 60%+ Tool I/O finding comes from — most of your bill is tool output, not dialogue.
Find the dead weight
The message inspector shows the full content of every turn — user prompts, tool calls, tool results, assistant responses. This is how you find the 30% of context that went stale 5 turns ago but is still riding along in every call.
Reproduce the headroom ceiling audit on your own sessions
Don't take our number — run the retrospective compression audit on your own transcripts. One command, fully offline, zero API spend; it builds a scratch corpus from ~/.claude/projects/ (never touching your live analyzer DB) and reports the maximum headroom's compressors could have saved on your workload:
pip install --no-deps headroom-ai==0.32.1 && pip install tiktoken
context-tracker audit-headroom
The headline (ceiling % and $) prints to your terminal and the full report lands in ./headroom-ceiling-report.md — numbers only, no prompt content.
What it does
- Hooks into Claude Code via
~/.claude/settings.jsonto capture tool calls, compaction events, session lifecycle, and subagent activity - Parses transcripts for exact API token usage (input, output, cache_read, cache_creation) — API-reported numbers, not estimates
- Ingests Codex CLI sessions from
~/.codex/sessions/rollout logs — Codex sessions appear in the same dashboard and analytics alongside Claude Code (API-level token forensics; compaction/subagent depth remains Claude Code-only) - SQLite persistence stores session data for fast queries and cross-session analysis
- Dashboard at
/for single-session visualization: context growth, cache churn, composition, message inspector - Cross-session analytics at
/sessionsfor comparing patterns across sessions: cost/call vs context size, insights, trends
Dashboard Features
Single-session view (/)
- Context Growth chart with budget line, danger band, and autocompact threshold
- Budget toggle buttons (200K / 500K / 700K / 1M)
- Cache-Read Churn chart showing per-call re-read cost
- Session dropdown to switch between sessions without restarting
- Message inspector with collapsible content blocks
- Token breakdown donut chart (Tool I/O vs Conversation vs System) — updates per-turn
- Composition breakdown with API-reported token counts
- Top growth turns, scrubber with playback
Cross-session analytics (/sessions)
- Summary cards: total sessions, API calls, cache read, cost, avg $/call
- Cost/Call vs Peak Context scatter plot
- Session comparison bar chart
- Auto-generated insights (cost patterns, expensive sessions, efficiency metrics)
- Sortable session table with $/call color coding
- Click any session to drill into its single-session view
Architecture
Claude Code Hooks (shell commands)
├── PostToolUse, PostToolUseFailure
├── PreCompact, PostCompact
├── SessionStart, SessionEnd
├── UserPromptSubmit
├── SubagentStart, SubagentStop
└── InstructionsLoaded
│
▼
~/.claude/context-trace/<session_id>.jsonl (hook events)
~/.claude/projects/<project>/<session_id>.jsonl (transcripts)
│
▼
SQLite (auto-ingest on first access)
├── sessions — summary stats, cost, model
├── api_calls — per-call token breakdown
├── blocks — content blocks with enter/exit turns
├── turns — conversation turn mapping
├── hook_events — tool calls, failures, lifecycle
├── subagents — agent summaries
├── subagent_api_calls — per-call churn for each subagent
└── tool_result_offloads — offloaded tool outputs
│
▼
FastAPI Dashboard + MCP Server
├── / — single-session dashboard
├── /sessions — cross-session analytics
├── /api/sessions — session list with stats
├── /api/sessions/trends — cross-session aggregation
└── /api/session/{id}/data — full session data (blocks + churn)
Upgrading
After upgrading context-tracker, re-run context-tracker up to refresh the
Claude Code hooks in ~/.claude/settings.json. The installer is idempotent,
backs up your settings first, and leaves any other hooks you have configured
untouched. See CHANGELOG.md for details.
Development
Contributor setup runs from a clone instead of the installed package:
git clone https://github.com/manavgup/context-analyzer.git
cd context-analyzer
make install-dev # create venv and install with uv
make hook-install # install Claude Code hooks (optional — dashboard works without hooks)
make dev # start dashboard on localhost:8080
Prerequisites: Python 3.11+, uv, make, git.
make help # see all available targets
make dev # start dashboard dev server with reload
make lint # run linter
make format # format code
make typecheck # run mypy
make coverage # run tests with coverage
make verify # run full verification suite (lint + format + typecheck + test)
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file context_tracker-1.0.0.tar.gz.
File metadata
- Download URL: context_tracker-1.0.0.tar.gz
- Upload date:
- Size: 264.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f5b5ada499eea63590d259210d5af58a93d50953d3b9056a813d083650bc233a
|
|
| MD5 |
b639b7b1b3790cba882e7c66477fd1c7
|
|
| BLAKE2b-256 |
015fa1aead88e675e47c088fcedf11feef890585b558b8ba966923d2f8f02615
|
Provenance
The following attestation bundles were made for context_tracker-1.0.0.tar.gz:
Publisher:
publish.yml on manavgup/context-analyzer
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
context_tracker-1.0.0.tar.gz -
Subject digest:
f5b5ada499eea63590d259210d5af58a93d50953d3b9056a813d083650bc233a - Sigstore transparency entry: 2213991170
- Sigstore integration time:
-
Permalink:
manavgup/context-analyzer@2c9e44682483531c8ba14df5f0d5d7703e4341a7 -
Branch / Tag:
refs/tags/v1.0.0 - Owner: https://github.com/manavgup
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@2c9e44682483531c8ba14df5f0d5d7703e4341a7 -
Trigger Event:
push
-
Statement type:
File details
Details for the file context_tracker-1.0.0-py3-none-any.whl.
File metadata
- Download URL: context_tracker-1.0.0-py3-none-any.whl
- Upload date:
- Size: 199.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d84e65d10f3d4d4e8f9d43e48e358e952101bab26e7e227e96cb9f2229a522bd
|
|
| MD5 |
a930749cb82eb5a85e60855526902476
|
|
| BLAKE2b-256 |
32a2afd52435daf2979f534181e21614da1a9244b4822652f0f7eeaead259d35
|
Provenance
The following attestation bundles were made for context_tracker-1.0.0-py3-none-any.whl:
Publisher:
publish.yml on manavgup/context-analyzer
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
context_tracker-1.0.0-py3-none-any.whl -
Subject digest:
d84e65d10f3d4d4e8f9d43e48e358e952101bab26e7e227e96cb9f2229a522bd - Sigstore transparency entry: 2213991202
- Sigstore integration time:
-
Permalink:
manavgup/context-analyzer@2c9e44682483531c8ba14df5f0d5d7703e4341a7 -
Branch / Tag:
refs/tags/v1.0.0 - Owner: https://github.com/manavgup
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@2c9e44682483531c8ba14df5f0d5d7703e4341a7 -
Trigger Event:
push
-
Statement type: