|
| 1 | +# context-profiler |
| 2 | + |
| 3 | +[](https://pypi.org/project/context-profiler/) |
| 4 | +[](https://pypi.org/project/context-profiler/) |
| 5 | +[](https://opensource.org/licenses/MIT) |
| 6 | + |
| 7 | +Framework-agnostic profiler for LLM agent context windows. Parses raw API request JSON and visualizes **where your tokens go** — no SDK instrumentation needed. |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | +## Why |
| 12 | + |
| 13 | +LLM agent frameworks accumulate tool definitions, system prompts, and conversation history in the context window. You can't optimize what you can't see. |
| 14 | + |
| 15 | +context-profiler gives you: |
| 16 | + |
| 17 | +- **Token distribution** — breakdown by role, content type, and tool name |
| 18 | +- **Icicle visualization** — [speedscope](https://www.speedscope.app/)-style interactive view, zoom into any node |
| 19 | +- **Context growth timeline** — stacked area chart across a session, see the inflection point |
| 20 | +- **Diff visualization** — what's new vs. what's history vs. what got pruned, per request |
| 21 | + |
| 22 | + |
| 23 | + |
| 24 | +## Install |
| 25 | + |
| 26 | +```bash |
| 27 | +pip install context-profiler |
| 28 | +``` |
| 29 | + |
| 30 | +Or install from source: |
| 31 | + |
| 32 | +```bash |
| 33 | +git clone https://github.com/Turdot/context-profiler.git |
| 34 | +cd context-profiler |
| 35 | +pip install -e . |
| 36 | +``` |
| 37 | + |
| 38 | +## Quick Start |
| 39 | + |
| 40 | +```bash |
| 41 | +# Analyze a single API request (snapshot mode) |
| 42 | +context-profiler analyze request.json |
| 43 | + |
| 44 | +# Analyze context growth over multiple requests (session mode) |
| 45 | +context-profiler analyze session.jsonl |
| 46 | +context-profiler analyze requests_dir/ |
| 47 | + |
| 48 | +# Generate an interactive HTML report |
| 49 | +context-profiler analyze session.jsonl --html report.html |
| 50 | + |
| 51 | +# Export JSON report |
| 52 | +context-profiler analyze request.json -o report.json |
| 53 | + |
| 54 | +# Specify format explicitly |
| 55 | +context-profiler analyze request.json --format openai |
| 56 | +context-profiler analyze request.json --format anthropic |
| 57 | + |
| 58 | +# Analyze Langfuse traces |
| 59 | +context-profiler analyze trace.json --format langfuse |
| 60 | + |
| 61 | +# Multi-trace session (multiple Langfuse exports) |
| 62 | +context-profiler analyze trace1.json trace2.json trace3.json --html report.html |
| 63 | +``` |
| 64 | + |
| 65 | +## Supported Formats |
| 66 | + |
| 67 | +Auto-detected from JSON structure: |
| 68 | + |
| 69 | +| Format | Input | Mode | |
| 70 | +|--------|-------|------| |
| 71 | +| **OpenAI** | `{messages, tools}` | snapshot | |
| 72 | +| **Anthropic** | `{messages, tools}` with content blocks | snapshot | |
| 73 | +| **Langfuse trace** | `{observations: [{type: "GENERATION", ...}]}` | session | |
| 74 | +| **JSONL** | One request per line | session | |
| 75 | +| **Directory** | Folder of `.json` files | session | |
| 76 | + |
| 77 | +## HTML Report Features |
| 78 | + |
| 79 | +The HTML report is a self-contained file with no external dependencies: |
| 80 | + |
| 81 | +- **Icicle view** — hierarchical token breakdown, click to zoom, breadcrumb navigation |
| 82 | +- **Tools view** — per-tool token table with stacked bars, sortable columns |
| 83 | +- **Timeline** — stacked area chart (system / tool defs / messages), click to select request |
| 84 | +- **Color modes** — Semantic (by role) or Diff (unchanged / added / removed) |
| 85 | +- **Role filters** — toggle visibility by role (system, user, assistant, tool) |
| 86 | +- **Detail panel** — content preview and JSON tree for any selected node |
| 87 | + |
| 88 | +## CLI Output |
| 89 | + |
| 90 | +``` |
| 91 | +⚠ Warnings |
| 92 | + • Tool definitions consume 15.2K tokens (35.4% of total) |
| 93 | +
|
| 94 | +Token Distribution |
| 95 | + Category Tokens % of Total |
| 96 | + Total Input 42.9K 100% |
| 97 | + System Prompt 1.2K 2.8% |
| 98 | + Tool Definitions 15.2K 35.4% |
| 99 | + Messages (assistant) 8.4K 19.6% |
| 100 | + Messages (tool) 14.1K 32.9% |
| 101 | + Messages (user) 4.0K 9.3% |
| 102 | +
|
| 103 | +Top Tools by Token Usage |
| 104 | + Tool Tokens Calls |
| 105 | + playwright_with_chunk_browser_snap 12.4K 8 |
| 106 | + filesystem-read_file 3.2K 5 |
| 107 | + local-search_in_turn 1.1K 3 |
| 108 | +``` |
| 109 | + |
| 110 | +## Examples |
| 111 | + |
| 112 | +The `examples/` directory contains a complete demo using [Toolathlon](https://huggingface.co/datasets/hkust-nlp/Toolathlon-Trajectories) trajectories: |
| 113 | + |
| 114 | +```bash |
| 115 | +cd examples/ |
| 116 | + |
| 117 | +# Convert Toolathlon data to context-profiler input |
| 118 | +python convert_toolathlon.py toolathlon_raw.json --mode snapshot -o snapshot.json |
| 119 | +python convert_toolathlon.py toolathlon_raw.json --mode session -o session.jsonl |
| 120 | + |
| 121 | +# Analyze |
| 122 | +context-profiler analyze snapshot.json |
| 123 | +context-profiler analyze session.jsonl --html report.html |
| 124 | +``` |
| 125 | + |
| 126 | +See [`examples/README.md`](examples/README.md) for supported formats and conversion patterns. |
| 127 | + |
| 128 | +## Acknowledgements |
| 129 | + |
| 130 | +This project is inspired by and learned from: |
| 131 | + |
| 132 | +- [context-lens](https://github.com/larsderidder/context-lens) — local proxy for capturing and visualizing LLM API calls |
| 133 | +- [ContextFlame](https://github.com/jcgs2503/contextflame) — flamegraph-based token profiling for Claude Code |
| 134 | +- [speedscope](https://www.speedscope.app/) — the icicle / flamegraph UI design is inspired by speedscope's interactive visualization |
| 135 | + |
| 136 | +## License |
| 137 | + |
| 138 | +[MIT](LICENSE) |
0 commit comments