Mirror of headroomlabs-ai/headroom (AI context compression proxy)
Find a file
chopratejas 905c229251 Add AST-based code compression and custom model configuration
CodeAwareCompressor:
- Tree-sitter based AST parsing for Python, JS, TS, Go, Rust, Java, C, C++
- Preserves imports, signatures, type annotations, error handlers
- Guarantees syntactically valid output
- Uses tree-sitter-language-pack for broad language support

ContentRouter:
- Intelligent compression orchestrator
- Auto-routes content to optimal compressor based on type detection
- Source hint support for high-confidence routing

Custom Model Configuration:
- HEADROOM_MODEL_LIMITS env var and ~/.headroom/models.json support
- Pattern-based inference for unknown models (opus/sonnet/haiku tiers)
- Support for Claude 4.5, Claude 4, o3, o3-mini
- Graceful fallback - never crashes on unknown models
2026-01-14 13:46:55 -08:00
.github Prepare for OSS release v0.2.0 2026-01-07 11:36:44 -08:00
benchmarks Fix all ruff lint and format errors for CI 2026-01-10 15:33:44 -08:00
docs Add AST-based code compression and custom model configuration 2026-01-14 13:46:55 -08:00
examples v0.2.2: Add CCR Response Handler, Context Tracker, and restructure docs 2026-01-14 13:03:41 -08:00
headroom Add AST-based code compression and custom model configuration 2026-01-14 13:46:55 -08:00
tests Add AST-based code compression and custom model configuration 2026-01-14 13:46:55 -08:00
.gitignore Prepare for OSS release v0.2.0 2026-01-07 11:36:44 -08:00
CHANGELOG.md Add AST-based code compression and custom model configuration 2026-01-14 13:46:55 -08:00
CODE_OF_CONDUCT.md Prepare for OSS release v0.2.0 2026-01-07 11:36:44 -08:00
CONTRIBUTING.md Update CONTRIBUTING.md 2026-01-10 15:05:16 -08:00
LICENSE Prepare for OSS release v0.2.0 2026-01-07 11:36:44 -08:00
NOTICE Prepare for OSS release v0.2.0 2026-01-07 11:36:44 -08:00
pyproject.toml Add AST-based code compression and custom model configuration 2026-01-14 13:46:55 -08:00
README.md Add AST-based code compression and custom model configuration 2026-01-14 13:46:55 -08:00
SECURITY.md Prepare for OSS release v0.2.0 2026-01-07 11:36:44 -08:00
uv.lock Fix all ruff lint and format errors for CI 2026-01-10 15:33:44 -08:00

Headroom

The Context Optimization Layer for LLM Applications

Cut your LLM costs by 50-90% without losing accuracy

CI PyPI Python License


What It Does

Headroom is a smart compression proxy for LLM applications:

  • Compresses tool outputs — 1000 search results → 15 items (keeps errors, anomalies, relevant items)
  • Enables provider caching — Stabilizes prefixes so cache hits actually happen
  • Manages context windows — Prevents token limit failures without breaking tool calls
  • Reversible compression — LLM can retrieve original data if needed (CCR architecture)

Zero code changes required — point your existing tools at the proxy.


30-Second Quickstart

# Install
pip install "headroom-ai[proxy]"

# Start proxy
headroom proxy --port 8787

# Verify
curl http://localhost:8787/health

Use with your tools:

# Claude Code
ANTHROPIC_BASE_URL=http://localhost:8787 claude

# Cursor / Continue / any OpenAI client
OPENAI_BASE_URL=http://localhost:8787/v1 cursor

# Python scripts
export OPENAI_BASE_URL=http://localhost:8787/v1
python your_script.py

That's it. You're saving tokens.


Verify It's Working

curl http://localhost:8787/stats
{
  "tokens": {"saved": 12500, "savings_percent": 25.0},
  "cost": {"total_savings_usd": 0.04}
}

Installation

pip install "headroom-ai[proxy]"     # Proxy server (recommended)
pip install headroom-ai              # SDK only
pip install "headroom-ai[code]"      # AST-based code compression
pip install "headroom-ai[llmlingua]" # ML-based compression
pip install "headroom-ai[all]"       # Everything

Requirements: Python 3.10+


Features

Feature Description Docs
SmartCrusher Compresses JSON tool outputs statistically Transforms
CacheAligner Stabilizes prefixes for provider caching Transforms
RollingWindow Manages context limits without breaking tools Transforms
CCR Reversible compression with automatic retrieval CCR Guide
Text Utilities Opt-in compression for search/logs Text Compression
LLMLingua-2 ML-based 20x compression (opt-in) LLMLingua
Code-Aware AST-based code compression (tree-sitter) Transforms
ContentRouter Auto-routes content to optimal compressor Transforms

Providers

Provider Token Counting Cache Optimization
OpenAI tiktoken (exact) Automatic prefix caching
Anthropic Official API cache_control blocks
Google Official API Context caching
Cohere Official API -
Mistral Official tokenizer -

New models auto-supported — Unknown models get sensible defaults based on naming patterns (e.g., claude-opus-* gets Opus pricing). Custom limits via ~/.headroom/models.json or HEADROOM_MODEL_LIMITS env var.


Performance

Scenario Before After Savings
Search results (1000 items) 45,000 tokens 4,500 tokens 90%
Log analysis (500 entries) 22,000 tokens 3,300 tokens 85%
Long conversation (50 turns) 80,000 tokens 32,000 tokens 60%

Overhead: ~1-5ms per request.


Safety

  • Never removes human content — User/assistant messages are never compressed
  • Never breaks tool ordering — Tool calls and responses stay paired
  • Parse failures are no-ops — Malformed content passes through unchanged
  • Compression is reversible — LLM can retrieve original data via CCR

Documentation

Guide Description
SDK Guide Wrap your client for fine-grained control
Proxy Guide Production deployment
Configuration All configuration options
CCR Guide Reversible compression architecture
Metrics Monitoring and observability
Troubleshooting Common issues
Architecture How it works internally

Examples

See examples/ for runnable code:

  • basic_usage.py — Simple SDK usage
  • proxy_integration.py — Using with different clients
  • ccr_demo.py — CCR architecture demonstration

Contributing

git clone https://github.com/chopratejas/headroom.git
cd headroom
pip install -e ".[dev]"
pytest

See CONTRIBUTING.md for details.


License

Apache License 2.0 — see LICENSE.


Built for the AI developer community