## Description
This PR fixes a broken example and two stale docstrings in the
integrations.
- `examples/langchain_demo/run_comparison.py` could not run its Headroom
arm at all. It failed in three places, in this order:
1. `HeadroomConfig(smart_crusher_threshold=...,
smart_crusher_max_items=..., cache_alignment=..., rolling_window=...)`:
none of these keyword arguments exist, so it raised `TypeError` before
anything ran. (Rolling-window was removed from the pipeline; see
`docs/content/docs/architecture.mdx`.)
2. `HeadroomChatModel(..., headroom_config=config)`: the constructor
parameter is `config`, so this raised `TypeError: ... got multiple
values for keyword argument 'headroom_config'`.
3. `headroom_model.get_total_tokens_saved()`: `HeadroomChatModel` has no
such method; it exposes the `total_tokens_saved` property, so this would
have raised `AttributeError` at the end of each scenario.
- The `headroom/integrations/mcp/server.py` module docstring showed a
`HeadroomMCPMiddleware` class that does not exist.
- The `headroom/integrations/__init__.py` docstring showed the same Agno
import line twice.
Refs #4136
## Type of Change
- [x] Bug fix (non-breaking change that fixes an issue)
- [ ] New feature (non-breaking change that adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)
- [ ] Documentation update
- [ ] Performance improvement
- [ ] Code refactoring (no functional changes)
## Changes Made
- `examples/langchain_demo/run_comparison.py`:
- the config becomes `HeadroomConfig()`, with a comment saying why. The
old keyword arguments do not exist, and their nearest real equivalent
(`smart_crusher=SmartCrusherConfig(...)`) would have no effect:
`TransformPipeline` builds its `ContentRouter` from the router's own
defaults and never reads `HeadroomConfig.smart_crusher`. That wiring gap
is a separate product change; it is recorded in the docs ledger because
`api-reference.mdx` shows such a setting;
- `headroom_config=config` becomes `config=config`;
- `headroom_model.get_total_tokens_saved()` becomes
`headroom_model.total_tokens_saved`.
- the printed narrative no longer claims a 20-item cap or a CacheAligner
gain (CacheAligner is off by default, and the 15-item setting applies
only to lossy row selection); the simulation figure is labelled a rough
5x estimate.
- `headroom/integrations/mcp/server.py`: the "Middleware" docstring
example now uses the real `HeadroomMCPClientWrapper(base_client,
config=config)` and its `call_tool`.
- `headroom/integrations/__init__.py`: the first Agno example line now
shows the package-level import `from headroom.integrations import
HeadroomAgnoModel`, which is re-exported there. The "or explicitly" line
keeps the subpackage import.
## Testing
- [ ] Unit tests pass (`pytest`)
- [x] Linting passes (`ruff check .`)
- [ ] Type checking passes (`mypy headroom`)
- [ ] New tests added for new functionality
- [x] Manual testing performed
### Test Output
```text
$ python -c "from headroom.integrations.langchain import HeadroomChatModel as H; print(type(H.__dict__.get('total_tokens_saved')).__name__, hasattr(H, 'get_total_tokens_saved'))"
property False
$ python -c "from headroom.integrations import HeadroomAgnoModel; print('ok')"
ok
$ python -m py_compile examples/langchain_demo/run_comparison.py && echo compiled
compiled
# run_agent_headroom(SCENARIOS[0]) with ChatOpenAI replaced by a fake LangChain chat model (no API key)
# that first asks for a search_logs tool call, then answers "done"
origin/main: TypeError: HeadroomConfig.__init__() got an unexpected keyword argument 'smart_crusher_threshold'
this branch: model calls: 2 | 2nd call message types: ['SystemMessage', 'HumanMessage', 'AIMessage', 'ToolMessage']
OK scenario='User Account Investigation' tool_calls=1 tool_output_tokens=25643 final='done' messages=5
$ pytest tests/test_integrations/mcp tests/test_integrations/langchain/test_chat_model.py
70 passed, 9 skipped
$ uvx ruff@0.16.8 check / format --check <3 touched files> -> All checks passed! / already formatted
```
## Real Behavior Proof
- Environment: macOS, Python 3.12, repo venv.
- Exact command / steps: the commands above. The example run imports
`examples.langchain_demo.run_comparison`, swaps `ChatOpenAI` for a
`FakeMessagesListChatModel` that first returns a `search_logs` tool call
and then answers "done", and calls `run_agent_headroom` on the first
scenario. So construction, a tool-result turn through
`HeadroomChatModel`, and the metrics read all run.
- Observed result: on origin/main the Headroom arm raises `TypeError` at
config construction; on this branch it returns an `AgentRun`. The
docstring imports resolve.
- Smoke harness (real proxy started from this checkout against a local
fake upstream; temp HOME and workspace, offline):
```text
branch: cleanup/cli-integrations-integrations-unused-imports @ 0ff4cbf3
health: 200
POST /v1/messages -> HTTP200 smoke-ok
POST /v1/chat/completions -> HTTP200 smoke-ok
stats: requests=2 failed=0 tokens_saved=13574 savings_percent=99.82
proxy.log WARNING/ERROR lines (<tmp>/ws/logs/proxy-53143.log):
2 headroom.proxy - WARNING - event=proxy_offline_mode air-gap active — every connection Headroom initiates is refused (telemetry, update check, license reporter, model/tokenizer/binary/dataset downloads, remote Kompress, OTLP/Langfuse export, Copilot auth, subscription polling, OpenAI embedders, Headroom Cloud compression). Still allowed on purpose: forwarding your requests to the upstream you configured, operator-configured local endpoints, loopback health probes.
4 headroom.transforms.kompress_compressor - WARNING - Kompress: artifact prefetch found no usable file for chopratejas/kompress-v2-base
4 headroom.transforms.kompress_compressor - WARNING - Kompress: artifact prefetch refused for chopratejas/kompress-v2-base (HEADROOM_OFFLINE is set): HEADROOM_OFFLINE is set: refusing outbound network access for HuggingFace download of chopratejas/kompress-v2-base/onnx/kompress-int8-wo.onnx to huggingface.co. Unset HEADROOM_OFFLINE, or turn off the feature that needs this connection.
(smoke dir: <tmp>)
```
- Direct runtime command on this PR's own path (temp HOME,
HEADROOM_WORKSPACE_DIR and CODEX_HOME):
```text
$ python examples/langchain_demo check: run_agent_headroom(SCENARIOS[0]) with a fake chat model (tool call, then "done")
model calls: 2 | 2nd call message types: ['SystemMessage', 'HumanMessage', 'AIMessage', 'ToolMessage']
OK scenario='User Account Investigation' tool_calls=1 tool_output_tokens=25627 final='done' messages=5
```
- Not tested: running the full demo against a real model, which needs
live API keys.
## Runtime Rollout Safety
- Rollout-managed feature(s): none.
- Minimum rollout channel: n/a, example script and docstrings only.
- Stable/default behavior changed: no library behaviour changes.
- Kill switch / disable path: n/a.
- Unsafe override required: no.
- Qualification impact: none.
- Rollback path: revert this commit.
## Review Readiness
- [x] I have performed a self-review
- [x] This PR is ready for human review
## Checklist
- [x] My code follows the project's style guidelines
- [x] I have performed a self-review of my code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [x] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my
feature works
- [x] New and existing unit tests pass locally with my changes
- [x] I did **not** edit `CHANGELOG.md` — it is generated by
release-please from my Conventional Commit PR title (a CI guard enforces
this)
## Screenshots (if applicable)
N/A
## Additional Notes
Part of maintainer-requested cleanup #4136 (dead code, duplication,
debuggability), split into small PRs for review.
- The audit also proposed removing three unused try-block imports:
- `ModelResponse` in `agno/model.py`;
- `AIMessageChunk` in `langchain/streaming.py`;
- `BeforeToolCallEvent` in `strands/hooks.py`.
They are left in place. `from headroom.integrations.<x> import <name>`
works for each of them today, with or without the framework installed,
and nothing importable from `headroom/integrations/` may be removed.
- The branch name still says "unused-imports" for that reason.
- No regression test: the example is not part of the test suite; the
fake-model run above is the check.
- Docs: `wiki/langchain.md:587` documents the same nonexistent
`headroom_config=` keyword. That is recorded in the docs ledger; this PR
does not edit docs.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
<!-- devin-review-badge-begin -->
---
<a href="https://app.devin.ai/review/headroomlabs-ai/headroom/pull/4134"
target="_blank"><picture><source media="(prefers-color-scheme: dark)"
srcset="https://static.devin.ai/assets/gh-devin-review-dark.svg?v=4"><img
src="https://static.devin.ai/assets/gh-devin-review-light.svg?v=4"
alt="Devin Review"></picture></a>
<!-- devin-review-badge-end -->
---------
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
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
tabular_compression_demo.py
Tabular + spreadsheet compression on generated sample data (no API key needed).
Shows where CSV/markdown tables and .xlsx workbooks compress and where compact,
all-unique data correctly passes through:
python examples/tabular_compression_demo.py # run all scenarios
python examples/tabular_compression_demo.py --write DIR # also save the sample files
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
vertex_gemini_benchmark/
Gemini 3.8 Flash on Google Cloud Vertex AI (Gemini Enterprise Agent Platform) benchmark:
# Ensure Google Cloud ADC is active
gcloud auth application-default login
export GCP_PROJECT_ID="your-project-id"
# Run comparative benchmark (Direct vs Headroom Proxied)
python examples/vertex_gemini_benchmark/benchmark.py --model gemini-3.8-flash
See vertex_gemini_benchmark/README.md for full benchmark scenarios and methodology.
strands_bedrock_demo.py
AWS Strands Agents + Bedrock integration demo. Showcases two Headroom integration patterns:
- HeadroomHookProvider - Compresses tool outputs in real-time
- HeadroomStrandsModel - Optimizes entire conversation context
# Configure AWS credentials
export AWS_ACCESS_KEY_ID='your-access-key'
export AWS_SECRET_ACCESS_KEY='your-secret-key'
export AWS_DEFAULT_REGION='us-west-2' # Optional, defaults to us-west-2
# Or use AWS profile
export AWS_PROFILE='your-profile-name'
# Run the full demo (both integration patterns)
python examples/strands_bedrock_demo.py
# Run only the hook provider demo
python examples/strands_bedrock_demo.py --hook
# Run only the model wrapper demo
python examples/strands_bedrock_demo.py --model
# Specify a different AWS region
python examples/strands_bedrock_demo.py --region us-east-1
The demo uses Claude 3 Haiku via Bedrock for cost efficiency. It creates agents with 4 tools that return verbose JSON output (search results, logs, database records, metrics) and displays compression statistics with visual comparisons.
Requirements:
- AWS account with Bedrock enabled
- Claude 3 Haiku model access in your region
pip install strands-agents headroom-ai[strands]
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 |
| strands_bedrock_demo | 60-85% | Strands + Bedrock with verbose tools |
| 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-...'
AWS Credentials Errors (for Strands demo)
Ensure AWS credentials are configured:
# Option 1: Environment variables
export AWS_ACCESS_KEY_ID='your-access-key'
export AWS_SECRET_ACCESS_KEY='your-secret-key'
# Option 2: AWS profile
export AWS_PROFILE='your-profile-name'
# Option 3: AWS credentials file (~/.aws/credentials)
Also ensure Bedrock and the Claude 3 Haiku model are enabled in your AWS account.