tokencut
Analyzes pull requests for token impact and generates Markdown summaries for GitHub PR review comments, helping prevent context bloat from lockfiles and fixtures.
Supports Gemini CLI with context compression and optimizes Gemini prompt caching by detecting cache-invalidating dynamic content.
Reduces token consumption for OpenAI-powered coding sessions such as ChatGPT macOS by compacting terminal output and structured payloads while preserving failures.
Click on "Deploy Server".
Wait a few minutes for the server to deploy. Once ready, it will show a "Started" state.
In the chat, type
@followed by the MCP server name and your instructions, e.g., "@tokencutCompact this terminal output and keep the full error traceback"
That's it! The server will respond to your query, and you can continue using it as needed.
Here is a step-by-step guide with screenshots.
The Context Accumulation Problem
AI coding agents (Claude Code, Cursor, Codex, Gemini CLI) run inside multi-turn conversation sessions. Every executed command, test runner output, and inspected file is appended to the conversation history and resent on every subsequent turn:
Quadratic Token Accumulation: A 2,000-line test run does not consume tokens once—it is transmitted to the model on every subsequent prompt in the session.
Low Signal-to-Noise Ratio: Routine test passes, compiler progress bars, and ANSI escape sequences dominate terminal dumps. The actual failure or error trace is often under 30 lines.
Premature Rate Limits: API quotas and usage limits (such as the Claude Code 5-hour window) are exhausted by repetitive output rather than code generation and reasoning.
Context Window Degradation: Splicing thousands of irrelevant terminal lines into the context window triggers needle-in-a-haystack degradation, increasing the likelihood that the model forgets earlier instructions.
Related MCP server: project-graph-mcp
Comparison
How tokencut compares to standard agent execution, static context packagers (such as Repomix), and generic context buffers:
Capability | Raw Agent / CLI | Repomix | Headroom | tokencut |
Real-time terminal compaction | None | Static only | Truncation only | Adaptive (head + tail + full error trace) |
Traceback & error preservation | No | No | No | Deterministic extraction (tracebacks never lost) |
Reversible CCR architecture | No | No | SQLite cache | SQLite store + Ref IDs ( |
Repository token profiling | No | File list | No | Hierarchical breakdown ( |
AST code skeletonization | No | Tree-sitter | No | Python AST, TS, JS, Go, Rust ( |
Lockfile diff folding (-97%) | No | No | No | Automated lockfile folding ( |
Structured JSON payload compaction | Raw dump | No | Truncate | Schema-preserving array folding ( |
Secret & credential scrubbing | Leaks in history | Basic | No | Automatic regex redactor (OpenAI, Anthropic, GH) |
System diagnostics & auto-wiring | Manual | No | No | 1-click auto-config ( |
Native MCP server | No | No | Wrapper needed | 7 native stdio tools ( |
Token & cost telemetry | No | No | No | Session & lifetime tracking in USD |
Empirical Benchmarks
Measured on representative real-world developer workloads across Anthropic Claude, OpenAI, and Google Gemini tokenizers:
Workload / Scenario | Raw Tokens | With tokencut | Reduction | Signal Quality |
Pytest test suite (120 tests, 1 failure) | 2,108 | 441 | -79.1% | Full traceback, assertion, and frame context retained |
Vite / Webpack build (250 modules) | 4,272 | 1,012 | -76.3% | Errors, warnings, and asset summary retained |
Source file inspection (AST skeleton) | 2,508 | 642 | -74.4% | Class/method signatures and docstrings retained |
Git diff (feature code + lockfile) | 4,165 | 101 | -97.6% | Code changes preserved; lockfile diffs collapsed |
Git log history (50 commits) | 3,120 | 780 | -75.0% | Single-line short hashes and subject lines |
REST / GraphQL API JSON (50 items, 13KB) | 2,914 | 240 | -91.8% | Complete schema and sample items retained |
Architecture
tokencut is designed around six core mechanisms:
1. Reversible Compress-Cache-Retrieve (CCR)
Context compaction should never cause irreversible data loss. When tokencut truncates repetitive output, it persists the full uncompressed stream to a local SQLite database (~/.tokencut/cache.db) and injects a deterministic reference identifier:
[... 340 lines of routine output omitted by tokencut (-84.1%). Ref: tc_8f2a1b ...]If an agent or developer needs the omitted output, it can be fetched instantly:
tokencut retrieve tc_8f2a1b --lines 120-160Or programmatically through the native tokencut_retrieve tool via MCP.
2. Repository Token Profiling (tokencut tree)
Identifies high-consumption files and directories before context is loaded into an agent session:
uvx tokencut tree .tokencut/ · 170,499 tok (100.0%)
├── src/ · 15,282 tok (9.0%)
├── tests/ · 2,628 tok (1.5%)
└── uv.lock · 148,640 tok (87.2%) [Top Consumer]3. AST Code Skeletonization (tokencut cat --skeleton)
During multi-file codebase navigation, feeding complete implementation bodies into the prompt exhausts context rapidly. tokencut cat parses Python files via the standard library ast module and other languages (TypeScript, JavaScript, Go, Rust) via structural regex to extract classes, method signatures, type annotations, and docstrings:
# View outline of a module
uvx tokencut cat src/auth.py --skeleton
# Extract a specific class or method
uvx tokencut cat src/auth.py --symbol AuthService.verify_token
# Extract specific line slice with file context
uvx tokencut cat src/auth.py --lines 45-804. Git Diff Slimming (tokencut diff)
Package lockfiles (uv.lock, package-lock.json, pnpm-lock.yaml) often generate thousands of lines of machine-generated diffs that crowd out actual application changes. tokencut diff collapses lockfile modifications into summary counts while preserving application code diffs in full fidelity.
5. Credential & Secret Scrubbing
Intercepts API keys (OpenAI sk-*, Anthropic sk-ant-*, Google AIza*, GitHub ghp_*), JWTs, and database URLs containing passwords before they enter agent context or terminal logs.
6. Prompt Cache Optimization (tokencut lint)
Provider prompt caching (Anthropic, Gemini) offers up to 90% cost savings for invariant prompt prefixes. tokencut lint analyzes system instruction files (CLAUDE.md, .cursorrules, system prompts) to identify dynamic timestamps, non-deterministic paths, and volatile headers that invalidate prompt caches.
7. Structured JSON & API Payload Compaction (tokencut json)
When coding agents fetch API responses via curl or inspect JSON data files, hundreds of repetitive array items quickly burn tens of thousands of tokens. tokencut json folds large lists while retaining the first few items and schema annotations, truncates oversized strings (such as base64 images or hashes), and caches the raw JSON in SQLite with a reference ID.
8. System Diagnostics & Auto-Configuration (tokencut doctor)
Inspects your local environment across Python runtime, SQLite cache health, Claude Code CLI, Cursor MCP configurations, and shell aliases. Running tokencut doctor --fix or tokencut install --all automatically writes the required configurations with zero manual editing.
9. Pull Request Token Impact Analyzer (tokencut pr)
Evaluates the net token delta introduced by code changes against main or a target base ref. Categorizes token impact into code, documentation, and lockfiles, and emits a clean Markdown summary for GitHub PR review comments. When run with --max-delta <N>, it acts as an automated CI gatekeeper preventing accidental lockfile or fixture context bloat.
Quickstart
Run directly without installation via uvx:
# Execute a test suite through tokencut
uvx tokencut run -- pytest -v tests/
# Analyze repository token distribution
uvx tokencut tree .
# Inspect code structure without function bodies
uvx tokencut cat src/server.py --skeleton
# Inspect git diff with folded lockfiles
uvx tokencut diff
# Compact large JSON file or API response
uvx tokencut json api_response.json
# Check environment health & auto-configure Cursor / shell
uvx tokencut doctor --fix
# Audit instructions for prompt cache-busting
uvx tokencut lint CLAUDE.md
# Run the terminal demonstration
uvx tokencut demoOr install globally:
uv pip install tokencut
# or
pip install tokencutIntegrations & Supported Platforms
tokencut operates across desktop applications, AI-enabled IDEs, coding agents, and terminal command-line pipelines.
1-Click Auto-Configuration
Configure all desktop and editor integrations automatically:
uvx tokencut install --allOr verify setup status across all targets with:
uvx tokencut doctorClaude Desktop (macOS)
Auto-configure ~/Library/Application Support/Claude/claude_desktop_config.json:
uvx tokencut install --claude-desktopOr manually add:
{
"mcpServers": {
"tokencut": {
"command": "uvx",
"args": ["tokencut", "mcp"]
}
}
}ChatGPT Desktop (macOS)
Compatible with ChatGPT Desktop via Developer Mode local MCP tools or CLI piping:
# Start MCP server for ChatGPT Developer Mode
uvx tokencut mcpCursor & Windsurf
Auto-configure ~/.cursor/mcp.json:
uvx tokencut install --cursorOr manually add to mcp.json:
{
"mcpServers": {
"tokencut": {
"command": "uvx",
"args": ["tokencut", "mcp"]
}
}
}Claude Code CLI
Register tokencut as a native MCP server in one command:
claude mcp add tokencut uvx tokencut mcpThis exposes seven tools directly to Claude:
tokencut_exec: Runs bash commands with output compaction, CCR caching, and optional--budget.tokencut_read: Reads files with support for AST skeletons, symbol extraction, and line ranges.tokencut_retrieve: Retrieves omitted slices from cached terminal runs by reference ID.tokencut_diff: Generates slim git diffs with lockfile folding.tokencut_tree: Profiles directory-level token consumption directly inside conversation.tokencut_json: Compresses large JSON payloads and API responses with schema retention.tokencut_stats: Reports session and lifetime token savings.
Terminal CLI & POSIX Pipelines (Gemini CLI, Codex, bash, zsh)
tokencut integrates into standard terminal workflows:
# Add 'cc' shortcut to ~/.zshrc or ~/.bashrc
uvx tokencut install --alias
# Run commands with automatic token compaction
cc pytest -v tests/
cc npm test
# Pipe stdout/stderr through tokencut
cargo test 2>&1 | tokencut pipe
curl https://api.github.com/repos/00200200/tokencut/commits | tokencut jsonGitHub Actions CI Gatekeeper
Use the official composite action to audit PR token delta or wrap test steps:
- name: Check PR Token Impact
uses: 00200200/tokencut@main
with:
pr-check: 'true'
max-token-delta: '25000'Pre-Commit Hook
Add to your .pre-commit-config.yaml to prevent prompt cache-busting before committing:
repos:
- repo: https://github.com/00200200/tokencut
rev: main
hooks:
- id: tokencut-lint
- id: tokencut-prCLI Reference
Command | Description |
| Runs command with real-time log compaction, CCR caching, and telemetry. |
| Hierarchical directory token consumption profiler. |
| AST structural skeleton (classes, signatures, docstrings). |
| Extracts a specific class, method, or function by name. |
| Extracts a specific line range with file context. |
| Retrieves uncompressed output from the CCR cache. |
| Compacts large JSON payloads, folding arrays and caching raw data. |
| POSIX stdin filter for shell integration. |
| Slims git diffs by folding lockfiles and condensing whitespace. |
| Diagnoses environment health and auto-configures Cursor / shell. |
| Automatically configures Cursor MCP and shell aliases. |
| Analyzes PR token delta and formats Markdown summaries for CI. |
| Manages the local SQLite Compress-Cache-Retrieve store. |
| Lints agent instruction files for prompt cache-busting elements. |
| Starts the stdio JSON-RPC Model Context Protocol server. |
| Displays lifetime token savings in table, JSON, or Markdown. |
| Interactive visual demo benchmarking token savings on realistic failures. |
Development
# Clone the repository
git clone https://github.com/00200200/tokencut.git
cd tokencut
# Install dependencies in a virtual environment
uv sync
# Run the test suite (37 tests)
uv run pytest -v
# Run the linter
uv run ruff check .
# Run the benchmark suite
uv run python scripts/benchmark_suite.pyLicense
Released under the MIT License.
This server cannot be deployed
Maintenance
Related MCP Connectors
Provide your AI coding tools with token-efficient access to up-to-date technical documentation for…
Shared memory for coding agents. Stop re-explaining your codebase every session.
Shared distillation cache for AI agents — every fetch ~73-89% fewer tokens via a shared cache.
Shared debugging memory for AI coding agents
Related MCP Servers
- AlicenseAqualityDmaintenanceProvides intelligent code context and analysis through semantic compression, AST parsing, and multi-language support. Offers 60-80% token reduction while enabling AI assistants to understand codebases through local analysis, OpenAI-enhanced insights, and GitHub repository integration.616 npm3MIT
- AlicenseBqualityCmaintenanceMaximizes AI agent context window by enabling compact code reading and editing, reducing tokens by 40% for deeper codebase understanding.1944 npm3MIT
- AlicenseNot gradedqualityDmaintenanceToken compression for AI contexts, reducing token consumption by compressing conversation exchanges before they enter the LLM context window.MIT
- AlicenseNot gradedqualityDmaintenanceDeterministic context compression for MCP agents, reducing token usage via 11 tools for prompts, history, shell output, file deltas, and code navigation without ML or GPU.7MIT