ctxoptimizer
A local, deterministic cost optimizer for AI coding agents. It sits between your agent and the LLM provider, measures real token usage and spend, explains why prompt caching missed, and applies cache-safe payload reductions.
- Local only — binds to loopback; no telemetry leaves the machine.
- Deterministic — no LLM calls inside the proxy; every decision is a hash, regex, counter or threshold.
- Honest numbers — provider-reported usage is ground truth; everything else
is labelled
est.orsim.. - Fail-open — invalid transforms retry with the original bytes; the proxy never blocks traffic.
Install
uv tool install ctxoptimizer
# or: pipx install ctxoptimizer
ctxoptimizer run
If PyPI does not have the release yet, install from GitHub:
uv tool install "git+https://github.com/ahmed-dev25/ctxoptimizer.git"
# or: pipx install "git+https://github.com/ahmed-dev25/ctxoptimizer.git"
ctxoptimizer run
From a checkout:
uv sync --extra dev
uv run ctxoptimizer run
The startup banner prints the dashboard URL with a per-run admin token:
dashboard: http://127.0.0.1:8787/_ctxoptimizer/?token=...
Open that URL once; the token is stored as an HttpOnly; SameSite=Strict
cookie. The dashboard is also protected by a Host allowlist and CSRF checks.
Quickstart
ctxoptimizer env --shell powershell # bash | zsh | fish | powershell
ctxoptimizer status # health + counters
ctxoptimizer stats # recorded usage and cost
Point a client at the printed base URL. See
docs/client-setup.md for Claude Code, OpenCode,
DeepSeek, OpenRouter, Gemini, Ollama, Codex and the other supported clients.
Default mode is shadow: everything is measured and simulated, while the
client's bytes are forwarded unchanged. Switch to active from the dashboard
(Settings), with ctxoptimizer run --mode active, or with
x-ctxoptimizer-mode: active per request.
What it does
- Measures provider usage and cost for Anthropic, OpenAI Chat/Responses, native Gemini, native Ollama and named OpenAI-compatible upstreams (DeepSeek, OpenRouter, Gemini/Ollama compatibility, and custom gateways).
- Detects drift (tools/system/history/TTL/config/model) and waste W1–W8: redundant re-reads, identical outputs, unbatched fan-out, runaway sessions, recurring/param drift, edit churn and unused tool schemas.
- Applies deterministic transforms: ANSI/progress cleanup, line dedupe,
whitespace trim, signal-aware truncation, duplicate back-references,
byte-true
cache_serve, opt-in stale-result elision, Anthropiccache_controlhints and OpenAIprompt_cache_key. - Auto-quarantines a transform per session when fallback/drift guard counters trip; the dashboard shows the history and lets you re-enable it.
- Provides
ctxoptimizer_expandover MCP so an agent can retrieve any omitted original from the local blob store. - Serves a local dashboard: Overview, Live (SSE), Sessions, Requests + diff viewer, Waste Opportunities with status controls, Transforms with hot toggles/stats, Pricing editor, Settings and Doctor.
Privacy
Payload capture defaults to changed_blocks; set it to off to store nothing.
Captured text stays in the per-user data directory, is redacted, and is bounded
by retention. ctxoptimizer purge --all wipes telemetry, blobs and findings. See
docs/privacy.md.
Service
ctxoptimizer service install # launchd / systemd --user / Windows scheduled task
ctxoptimizer service install --dry-run
Contributing
See CONTRIBUTING.md for development setup and gates. Report
vulnerabilities privately via SECURITY.md; do not open a public
issue for a security problem.
License
ctxoptimizer is free and open-source software licensed under the MIT License.
See LICENSE and NOTICE.
Updating
Use the package manager that installed ctxoptimizer:
uv tool upgrade ctxoptimizer
# or
pipx upgrade ctxoptimizer
There is no built-in self-update command. PyPI releases are published with
Trusted Publishing and PEP 740 attestations. Source installs follow the GitHub
repository; downloaded wheels must be replaced manually. See
docs/updating.md.
Documentation
SPEC.md— authoritative specificationdocs/client-setup.md— per-client recipesdocs/how-to-use.md— complete install/run/update guidedocs/provider-notes.md— dated provider behaviordocs/architecture.md— component overviewdocs/privacy.md— data handling and purgedocs/faq.md— cache, savings, troubleshootingdocs/test-report.md— gates and test coverage summarydocs/updating.md— package-manager upgrades and release artifactsdocs/pypi-release.md— PyPI Trusted Publishing setupdocs/release-checklist.md— release checklistdocs/progress.md— build status and handoff notesCHANGELOG.mdCONTRIBUTING.mdSECURITY.mdTHIRD_PARTY_LICENSES.mddocs/decisions/— architecture decision recordsAGENTS.md— coding and git workflow rules
Development
uv run ruff check .
uv run ruff format --check .
uv run mypy --strict src
uv run pytest -q
uv build --wheel
Supported Python: 3.11+. The package is built with uv_build; the test suite
uses a real fake upstream and recorded/documented fixtures. Live provider tests
are opt-in (pytest -m live) and require keys.
Metadata
Release files for ctxoptimizer 0.4.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 | |
|---|---|---|---|---|
| ctxoptimizer-0.4.1.tar.gz | 98.1 kB | With provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| ctxoptimizer-0.4.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 233.4 kB
Release files / ctxoptimizer-0.4.1.tar.gz
| Download URL | ctxoptimizer-0.4.1.tar.gz |
|---|---|
| Size | 98.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
86ed75735f2331b44215e29dbe0f67a02073d7845b47f4145b120d9715650076
|
|
BLAKE2b-256 checksum How to use checksums |
515e92683df63e806b0c90c2b6c145675069aaaec2f7b1f0f950bcabff2fa919
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
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 Oct 25, 2026.
Transparency logRelease files / ctxoptimizer-0.4.1-py3-none-any.whl
| Download URL | ctxoptimizer-0.4.1-py3-none-any.whl |
|---|---|
| Size | 135.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
f7f1d44cc5e40db990dfe50555401af0c595d866d7936d02edf7f702acf5203b
|
|
BLAKE2b-256 checksum How to use checksums |
7fda5eba891ffadb3a47791b8aa3198c3b09dabe9feea3dd50fae17086ac8e93
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
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 Oct 25, 2026.
Transparency logSecurity for ctxoptimizer 0.4.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.
Full provenance
All 2 files in this release have been uploaded with provenance metadata.
Attestation for ctxoptimizer-0.4.1.tar.gz
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 Oct 25, 2026.
Transparency logA 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.