Quick start | Motivation | What's new | Install guide | Coding agent setup | FAQ | Supported agents
ContextSpy is a context window profiler for large language models and common agentic AI coding tools. It is used to intercept requests to an LLM API, analyze and visualize prompt composition, and track context changes between multiple requests in the same session. Modern AI coding agents (GitHub Copilot, Claude Code, opencode, etc.) pack a lot into each LLM request: system prompts, tool definitions and results, file contents, conversation history. It's often unclear why a session is slow, expensive, or hitting the context limit. ContextSpy makes the invisible visible - you see a live breakdown of every token category for every request, across sessions, over time.
Example dashboard view
Think of your favorite CPU or memory profiler, just applied to the contents of the context of an AI agent. While you can optimize performance just by reviewing code, having a profiler to capture and visualise snapshot data helps a lot. Same with LLM context optimisation.
Quick start
Quick setup for macOS (Apple Silicon) — see install guide for Linux, Windows, and PyPI options:
# install latest binary release with Homebrew
brew tap RimantasZ/contextspy
brew install contextspy
# Install CA certificate into system trust store (one-time, cloud mode only)
sudo contextspy install-cert
# Start the proxy (keep this terminal open)
contextspy start
# In a new terminal: launch your coding agent through the proxy
# contextspy run sets required environment variables, so LLM requests are routed through the proxy
contextspy run opencode <path to your project>
# contextspy run code <path to your project>
# contextspy run claude # run in the drectory of your project
# contextspy run codex # run in the directory of your project (Codex CLI, not the desktop app)
Open http://127.0.0.1:5173 in your browser for the ContextSpy dashboard.
If something doesn't work, see the troubleshooting section in the install guide.
Alternatively, refer to configure your agent on how to route LLM traffic through the proxy at http://127.0.0.1:8888
Context profiling? Why should I care?
Token costs are rising. With AI agents embracing more and more complex workflows and use cases, token consumption and subsequent cloud API bills are growing larger and larger. This is also applicable for AI coding agents and tools, where providers are gradually switching from subsidized subscription mode and are either reducing token limits or switching to token usage based billing (e.g. GitHub Copilot).
Input tokens are major part. When discussing AI model pricing, most people bring up token generation cost - that's where the numbers look most dramatic ($25 per million tokens for Opus 4.8 output vs $0.40 for gpt-5-nano). But in agentic workloads, input tokens outnumber output by 20-50x, or even more. So most of your API bill is influenced by input context, not the output the model generates.
AI coding agents = lots of input. The expensive part is the quick accumulation of context - with every turn it fills up with additional tokens - system prompt, skills, tool definitions, tool results, file contents, conversation history. You start with 5000 - 10000 tokens in a fresh session, but by turn 25 it might be 30 to 50 thousand, spend some more time and it might be hitting the context window limit and compacting. Every API call to the model sends the full context as part of the prompt - and here is where the token consumption and costs skyrocket quickly.
Why large context is bad
We all have been told that the more information we will give to the model, the more capable it will be. And there are models with 1M token (or even bigger) context windows.
There are three ways you pay for extra (and sometimes unnecessary) information in your context:
- API Costs - even with near perfect cache hits, input token costs outweigh output, often by order of magnitude or more.
- Compute and latency - larger contexts take considerably longer to process - especially in locally hosted models
- Context rot - with larger contexts, LLMs start to lose precision rapidly, with 100k being the limit where rapid degradation starts. So you are paying for more expensive model, but getting performance of cheaper one - or even worse.
ContextSpy makes these costs visible so you can act on them. The responsive light/dark dashboard includes session and provider summaries, model and latency breakdowns, tool-use treemaps, and a request workbench with compact, relative-size, and raw payload views. Filters, linked-block navigation, metadata inspection, and in-content search help trace exactly where the context went. Session detail also reconstructs conversations: unrelated roots, exact or inferred continuations, forks, parallel timing, and the blocks/tokens that persisted, were promoted from a parent response, added, removed, or replaced. Forked conversations can share earlier requests.
How does it work
ContextSpy starts an HTTPS proxy (or reverse proxy for locally hosted models) that captures supported LLM invocation endpoints, analyzes them, and stores the results in a local SQLite database. A local web server serves the dashboard used to explore the captured data.
Some screenshots
Request view
Context breakdown
Session view
Is it safe to use? Does it send my data to the cloud?
No, it does not send any data to the cloud. All data is stored locally on your machine.
But users must be aware, that it will be running proxy, and capturing all traffic from agent to LLM provider - and storing it locally to be displayed and analysed in the UI. The proxy and dashboard server are bound to localhost, and not exposed to external access, but still could be accessed locally.
The intended use case is to run ContextSpy as a profiler tool on dedicated profiling and optimisation sessions, rather than keeping it permanently as a monitoring tool.
The contents of requests (observed payloads, canonical request/response JSON, stream event logs,
and block contents) become eligible for purging after 7 days by default — configurable via
[retention] in ~/.contextspy/config.toml. Aggregated token counts
and classifications are retained indefinitely. Purging only runs at server startup, not on a
background timer, so a contextspy process left running for many days in a row won't purge again
until it's restarted.
The contents of database can be cleared manually by running contextspy reset-db.
In practice, it is recommended to do it from time to time.
Upgrades and migration
The new version can be installed with homebrew:
## optional - sometimes brew "forgets" custom tap, add it again if just update fails
brew tap RimantasZ/contextspy
## update homebrew and upgrade contextspy
brew update
brew upgrade contextspy
At this stage, the database schema is subject to change. Structural changes (new tables/columns)
are applied automatically on next startup. If a data migration is needed (e.g. backfilling new
derived data for existing requests), contextspy start will print a warning — run
contextspy db-upgrade to backfill, or contextspy reset-db to start fresh. Before an upgrade
makes any database changes, it copies the SQLite file to a backup beside it named
<db_name>_backup_v<version_from>_to_v<version_to>_<YYYY-MM-DD-HHMM>.back (UTC).
If that name already exists, -1, -2, and so on is added before .back.
Tech stack
| Layer | Technology |
|---|---|
| Backend | Python 3.11+, FastAPI + uvicorn, WebSocket for live push |
| Frontend | React + Vite, TanStack Query, Recharts, Tailwind CSS |
| CLI | Typer |
| Proxy | mitmproxy — TLS-terminating forward proxy (cloud) and reverse proxy (local) |
| Storage | SQLite via SQLAlchemy — all data local in ~/.contextspy/ |
| Tokenizer | tiktoken (o200k_base) for token estimation |
| Packaging | uv, Homebrew tap, .deb, standalone binary |
Features
- Two proxy modes — forward proxy for supported cloud APIs (OpenAI, Anthropic, Azure OpenAI, Copilot, opencode's gateway, and Codex's ChatGPT backend); reverse proxy for local LLM servers (Ollama, llama.cpp, vLLM)
- Context breakdown — input tokens split into 8 categories: system prompt, tool definitions, tool results, file contents, conversation history, current user message, assistant prefill, uncategorised
- Live dashboard — responsive light/dark charts, tool-use treemaps, and a request workbench with compact, relative-size, and raw payload views plus block search and inspection
- Session tracking — name time windows, keep stable per-session request numbers, and compare usage across runs
- Conversations — visualize independent and forked request paths without assuming session request order is conversation order; inspect exact/inferred evidence and parent-relative context changes
- SQLite storage — all data stored locally in
~/.contextspy/; no data leaves your machine - Agent detection — Copilot, Claude Code/SDK, Codex CLI, opencode, Cursor, OpenAI SDK, and generic clients
Documentation links
- Installation — PyPI, Homebrew, .deb, binary, CA certificate setup
- Cloud API mode — intercept OpenAI, Anthropic, Copilot, etc.
- Local LLM mode — intercept Ollama, llama-server, vLLM
- Usage examples — practical recipes and common workflows
- Request tracking and conversations — sessions, lineage evidence, conversation grouping, and UI icons
- REST and WebSocket handling — invocation boundaries, canonical JSON, reconstruction, storage, and token accounting
- CLI reference — all commands and options
- Development — architecture, data storage, contributing
License
Metadata
Release files for contextspy 0.5.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| contextspy-0.5.3.tar.gz | 958.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| contextspy-0.5.3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 1.9 MB
Release files / contextspy-0.5.3.tar.gz
| Download URL | contextspy-0.5.3.tar.gz |
|---|---|
| Size | 958.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
115cf7f8fa8cb88faf448b449e73b409cbd34c4b0810e03334467f17f3793f6c
|
|
BLAKE2b-256 checksum How to use checksums |
5f80a200a0c4f35c2ad7815fea8ca2723f20a5e6a2d69bebd09c1f8a4b05fdea
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 30, 2026.
Transparency logRelease files / contextspy-0.5.3-py3-none-any.whl
| Download URL | contextspy-0.5.3-py3-none-any.whl |
|---|---|
| Size | 933.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ecf3d77cd7b7c59e58324aa08fa55bc055eeedff081a3f92670ddafa695808e9
|
|
BLAKE2b-256 checksum How to use checksums |
fffb00c8683f1a926c9a6c60cab7aafc7d760f049e7b0a9d039ba09b27cdf4d2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 30, 2026.
Transparency log