Framework-agnostic profiler for LLM agent context windows
Project description
context-profiler
Framework-agnostic profiler for LLM agent context windows. Parses raw API request JSON and visualizes where your tokens go — no SDK instrumentation needed.
Why
LLM agent frameworks accumulate tool definitions, system prompts, and conversation history in the context window. You can't optimize what you can't see.
context-profiler gives you:
- Token distribution — breakdown by role, content type, and tool name
- Icicle visualization — speedscope-style interactive view, zoom into any node
- Context growth timeline — stacked area chart across a session, see the inflection point
- Diff visualization — what's new vs. what's history vs. what got pruned, per request
Install
pip install context-profiler
Or install from source:
git clone https://github.com/Turdot/context-profiler.git
cd context-profiler
pip install -e .
Quick Start
# Analyze a single API request (snapshot mode)
context-profiler analyze request.json
# Analyze context growth over multiple requests (session mode)
context-profiler analyze session.jsonl
context-profiler analyze requests_dir/
# Generate an interactive HTML report
context-profiler analyze session.jsonl --html report.html
# Export JSON report
context-profiler analyze request.json -o report.json
# Specify format explicitly
context-profiler analyze request.json --format openai
context-profiler analyze request.json --format anthropic
# Analyze Langfuse traces
context-profiler analyze trace.json --format langfuse
# Multi-trace session (multiple Langfuse exports)
context-profiler analyze trace1.json trace2.json trace3.json --html report.html
Supported Formats
Auto-detected from JSON structure:
| Format | Input | Mode |
|---|---|---|
| OpenAI | {messages, tools} |
snapshot |
| Anthropic | {messages, tools} with content blocks |
snapshot |
| Langfuse trace | {observations: [{type: "GENERATION", ...}]} |
session |
| JSONL | One request per line | session |
| Directory | Folder of .json files |
session |
HTML Report Features
The HTML report is a self-contained file with no external dependencies:
- Icicle view — hierarchical token breakdown, click to zoom, breadcrumb navigation
- Tools view — per-tool token table with stacked bars, sortable columns
- Timeline — stacked area chart (system / tool defs / messages), click to select request
- Color modes — Semantic (by role) or Diff (unchanged / added / removed)
- Role filters — toggle visibility by role (system, user, assistant, tool)
- Detail panel — content preview and JSON tree for any selected node
CLI Output
⚠ Warnings
• Tool definitions consume 15.2K tokens (35.4% of total)
Token Distribution
Category Tokens % of Total
Total Input 42.9K 100%
System Prompt 1.2K 2.8%
Tool Definitions 15.2K 35.4%
Messages (assistant) 8.4K 19.6%
Messages (tool) 14.1K 32.9%
Messages (user) 4.0K 9.3%
Top Tools by Token Usage
Tool Tokens Calls
playwright_with_chunk_browser_snap 12.4K 8
filesystem-read_file 3.2K 5
local-search_in_turn 1.1K 3
Examples
The examples/ directory contains a complete demo using Toolathlon trajectories:
cd examples/
# Convert Toolathlon data to context-profiler input
python convert_toolathlon.py toolathlon_raw.json --mode snapshot -o snapshot.json
python convert_toolathlon.py toolathlon_raw.json --mode session -o session.jsonl
# Analyze
context-profiler analyze snapshot.json
context-profiler analyze session.jsonl --html report.html
See examples/README.md for supported formats and conversion patterns.
Acknowledgements
This project is inspired by and learned from:
- context-lens — local proxy for capturing and visualizing LLM API calls
- ContextFlame — flamegraph-based token profiling for Claude Code
- speedscope — the icicle / flamegraph UI design is inspired by speedscope's interactive visualization
License
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_profiler-0.1.0.tar.gz.
File metadata
- Download URL: context_profiler-0.1.0.tar.gz
- Upload date:
- Size: 24.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3879d556d8887b6e51873231d747189c436397dd6da2f8af3170db101b3555d6
|
|
| MD5 |
b16e6078fd0da596e104eda038ddc4bd
|
|
| BLAKE2b-256 |
cb6856f8abc4e7b78d3b7067e3bbd601a3ecb18c840807800e6f5726d57b0585
|
Provenance
The following attestation bundles were made for context_profiler-0.1.0.tar.gz:
Publisher:
publish.yml on Turdot/context-profiler
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
context_profiler-0.1.0.tar.gz -
Subject digest:
3879d556d8887b6e51873231d747189c436397dd6da2f8af3170db101b3555d6 - Sigstore transparency entry: 1129630624
- Sigstore integration time:
-
Permalink:
Turdot/context-profiler@330715191823e1661d33dd6d43d525e4e9778c3f -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/Turdot
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@330715191823e1661d33dd6d43d525e4e9778c3f -
Trigger Event:
release
-
Statement type:
File details
Details for the file context_profiler-0.1.0-py3-none-any.whl.
File metadata
- Download URL: context_profiler-0.1.0-py3-none-any.whl
- Upload date:
- Size: 30.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
751ca228261a43944d1fc5209ca4abc27e6781e04dde5e67c571753ab1a478fe
|
|
| MD5 |
f65774fcd7cca3ee0c685b6c61c9b911
|
|
| BLAKE2b-256 |
68890a0b67285ac79e0d0fc7522f11d20f5f8dd0732d1b3deca23a31f28b366a
|
Provenance
The following attestation bundles were made for context_profiler-0.1.0-py3-none-any.whl:
Publisher:
publish.yml on Turdot/context-profiler
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
context_profiler-0.1.0-py3-none-any.whl -
Subject digest:
751ca228261a43944d1fc5209ca4abc27e6781e04dde5e67c571753ab1a478fe - Sigstore transparency entry: 1129630698
- Sigstore integration time:
-
Permalink:
Turdot/context-profiler@330715191823e1661d33dd6d43d525e4e9778c3f -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/Turdot
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@330715191823e1661d33dd6d43d525e4e9778c3f -
Trigger Event:
release
-
Statement type: