ctxlens
A flamegraph for your agent's context window. ctxlens parses AI agent session transcripts and shows you exactly what is eating your context per turn, flags the waste, and tells you what to cut.
Why
Agents get slow, expensive, and dumb when their context window fills with junk: the same file read six times, a 12k-token tool result that mattered for one turn, tool schemas re-sent on every step. Token dashboards tell you the bill. ctxlens tells you where the bytes went and what to delete.
It works offline with a deterministic heuristic tokenizer (no network, no heavy
deps), and upgrades to exact counts automatically when tiktoken is installed.
Quickstart
pip install ctxlens-cli # core
pip install "ctxlens[tiktoken]" # optional exact token counts
ctxlens analyze session.jsonl
ctxlens report session.jsonl --html -o report.html
ctxlens diff before.jsonl after.jsonl
Point it at a real Claude Code session, or pipe a transcript in:
ctxlens analyze ~/.claude/projects/<slug>/<session>.jsonl
cat session.json | ctxlens analyze - --json
Example output
╭─ ctxlens ────────────────────────────────────╮
│ Source session.jsonl │
│ Format claude-code-jsonl Tokenizer heuristic │
│ Tokens 12,481 Turns 14 High-water 12,481 │
│ Waste 4,932 tokens (39.5%) │
╰───────────────────────────────────────────────╯
Context composition by segment
Segment Tokens % Msgs Share
tool result 6,204 49.7 22 ██████████████·······
assistant 2,110 16.9 14 ██████···············
system 1,540 12.3 1 ████·················
...
Context growth
cumulative ▁▁▂▂▃▄▄▅▆▆▇▇██ peak 12,481
per-turn ▂█▃▂▅▂▁▂▇▂▁▃▂▁ max 3,204
Recommendations
[HIGH] Repeated content wastes tokens (~2,410 tok)
'Read:file_path=config.py' appears 6 times (turns [2, 5, 7, 9, 11, 13]) ...
Supported formats
ctxlens auto-detects the format; override with --format.
| Format | --format |
Source |
|---|---|---|
| Claude Code session | claude-code-jsonl |
~/.claude/projects/*/*.jsonl |
| OpenAI/Codex session | codex-session |
Codex rollout JSON (items[]) |
| Generic OpenAI chat | openai-chat |
chat-messages array or {messages, tools} |
Every message is attributed to a segment: system, tool_definitions,
user, assistant, thinking, tool_call, tool_result.
Recommendations explained
The engine is rule-based and specific, not generic advice:
- Tool results dominate — flagged when tool outputs are >=30% of context.
- Repeated content — the same file/tool result (by reference or by exact body) appearing more than once; every copy after the first is wasted.
- Stale tool outputs — an older result superseded by a newer one for the same target still occupies context.
- Oversized tool definitions — tool schemas above budget, paid every turn.
- System prompt weight and single biggest consumer callouts.
Each recommendation carries a severity and an estimated token saving.
Waste report
waste_ratio = total_waste / total_tokens, where total waste sums duplicate
tokens, tool-result bloat (tokens above --tool-result-cap), stale tool
outputs, and tool-definition overage (above --tool-def-budget).
CI usage
Fail a build when a captured agent session wastes too much context:
ctxlens analyze session.jsonl --fail-over-ratio 0.30
Exit codes: 0 OK, 2 threshold exceeded, 1 error. Emit machine-readable
output with --json, or diff a baseline against a candidate in CI with
ctxlens diff baseline.jsonl candidate.jsonl --json.
Tokenizers
heuristic(default fallback): deterministic, dependency-free, great for relative profiling and CI.tiktoken: exact BPE counts when installed.--tokenizer autoprefers it.
Contributing
git clone https://github.com/AgentPostmortem/Ctxlens
cd ctxlens
python -m venv .venv && source .venv/bin/activate
pip install -e ".[dev]"
ruff check .
pytest
Issues and PRs welcome. Adding a parser? Implement Parser.sniff and
Parser.parse, register it in ctxlens/parsers/__init__.py, and drop a
fixture in tests/fixtures/.
License
MIT © royalpinto007
Metadata
Release files for ctxlens-cli 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| ctxlens_cli-0.1.1.tar.gz | 32.6 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| ctxlens_cli-0.1.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 67.7 kB
Release files / ctxlens_cli-0.1.1.tar.gz
| Download URL | ctxlens_cli-0.1.1.tar.gz |
|---|---|
| Size | 32.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
87940fa5ba299fa84d0182b1c389957fb464a9a39cbebab2f172a1f0ea2c3279
|
|
BLAKE2b-256 checksum How to use checksums |
885ea60c932f73f14b8b143dd980f5005eb1fc0c2498fd99dbb29713708a39e4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.3
|
Release files / ctxlens_cli-0.1.1-py3-none-any.whl
| Download URL | ctxlens_cli-0.1.1-py3-none-any.whl |
|---|---|
| Size | 35.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
9d3a5da94d70db8238452a316236a2fa1c18100523b01008781159aa26627310
|
|
BLAKE2b-256 checksum How to use checksums |
8b7fb05b5a4c5a3c5c528a8ce600b7d3883110c69f0a9fcaa91cdab703d9e277
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.3
|
Security for ctxlens-cli 0.1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.