headroom/examples
chopratejas 9c9bb30ded Add persistent memory system with zero-latency inline extraction
Features:
- with_fast_memory(): Zero-latency inline extraction (Letta-style)
  - Memory extracted as part of LLM response, no extra API calls
  - Semantic retrieval with local embeddings (sub-50ms)
- with_memory(): Background extraction for non-blocking memory
- SQLite + FTS5 storage with vector similarity search
- Multi-user isolation by user_id

Memory enables temporal compression - extract key facts instead of
carrying full conversation history (4000 tokens → 50 tokens).

Includes:
- Comprehensive test suite (71 new tests)
- Documentation (docs/memory.md)
- Benchmark examples comparing approaches
- E2E test with LLM-as-judge evaluation
2026-01-14 21:32:09 -08:00
..
langchain_demo Fix all ruff lint and format errors for CI 2026-01-10 15:33:44 -08:00
mcp_demo Fix all ruff lint and format errors for CI 2026-01-10 15:33:44 -08:00
anthropic_example.py Fix all ruff lint and format errors for CI 2026-01-10 15:33:44 -08:00
basic_usage.py Publish headroom-ai v0.2.0 to PyPI with DevEx fixes 2026-01-10 14:51:08 -08:00
ccr_demo.py v0.2.2: Add CCR Response Handler, Context Tracker, and restructure docs 2026-01-14 13:03:41 -08:00
fast_memory_e2e.py Add persistent memory system with zero-latency inline extraction 2026-01-14 21:32:09 -08:00
inline_memory_demo.py Add persistent memory system with zero-latency inline extraction 2026-01-14 21:32:09 -08:00
langchain_before_after.py Fix all ruff lint and format errors for CI 2026-01-10 15:33:44 -08:00
memory_e2e_test.py Add persistent memory system with zero-latency inline extraction 2026-01-14 21:32:09 -08:00
memory_latency_benchmark.py Add persistent memory system with zero-latency inline extraction 2026-01-14 21:32:09 -08:00
README.md Prepare for OSS release v0.2.0 2026-01-07 11:36:44 -08:00
real_world_eval.py Fix all ruff lint and format errors for CI 2026-01-10 15:33:44 -08:00
real_world_openai_eval.py Fix all ruff lint and format errors for CI 2026-01-10 15:33:44 -08:00
smart_vs_naive_eval.py Fix all ruff lint and format errors for CI 2026-01-10 15:33:44 -08:00
streaming_example.py Initial commit: Headroom SDK - LLM context optimization toolkit 2026-01-06 23:16:58 -08:00

Headroom Examples

This directory contains examples demonstrating Headroom's capabilities.

Quick Start Examples

basic_usage.py

Basic integration with OpenAI client:

export OPENAI_API_KEY='your-key'
python examples/basic_usage.py

anthropic_example.py

Integration with Anthropic Claude:

export ANTHROPIC_API_KEY='your-key'
python examples/anthropic_example.py

streaming_example.py

Streaming responses with optimization:

export OPENAI_API_KEY='your-key'
python examples/streaming_example.py

Evaluation Examples

smart_vs_naive_eval.py

Compare SmartCrusher against naive truncation:

export OPENAI_API_KEY='your-key'
python examples/smart_vs_naive_eval.py

real_world_eval.py

Comprehensive evaluation with Anthropic models:

export ANTHROPIC_API_KEY='your-key'
python examples/real_world_eval.py

real_world_openai_eval.py

Comprehensive evaluation with OpenAI models:

export OPENAI_API_KEY='your-key'
python examples/real_world_openai_eval.py

Demo Directories

langchain_demo/

Full LangChain agent integration demo:

# No API key needed for compression demo
PYTHONPATH=. python -m examples.langchain_demo.show_compression

# Full comparison (requires API key)
export OPENAI_API_KEY='your-key'
PYTHONPATH=. python -m examples.langchain_demo.run_comparison

See langchain_demo/README.md for details.

mcp_demo/

MCP (Model Context Protocol) integration demo:

export OPENAI_API_KEY='your-key'
PYTHONPATH=. python -m examples.mcp_demo.run_agent_eval

Running Examples

All examples can be run from the repository root:

# Install dependencies
pip install -e ".[dev]"

# Run any example
python examples/<example_name>.py

Expected Results

Example Token Savings Notes
basic_usage 50-70% Simple tool output compression
langchain_demo 70-85% Real agent with multiple tools
mcp_demo 60-80% MCP tool outputs
real_world_eval 50-90% Varies by scenario

Troubleshooting

ModuleNotFoundError: No module named 'headroom'

Run from the repository root with PYTHONPATH:

PYTHONPATH=. python examples/basic_usage.py

Or install in development mode:

pip install -e .

API Key Errors

Ensure your API keys are set:

export OPENAI_API_KEY='sk-...'
export ANTHROPIC_API_KEY='sk-ant-...'