Skip to content

Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.

Fix mypy errors in html_extraction.py

chopratejasbbbf0c5a7eab
diff --git a/README.md b/README.md
index 55cc2fdf6..cf3df9802 100644
--- a/README.md
+++ b/README.md
@@ -91,6 +91,42 @@ Run it yourself: `python examples/needle_in_haystack_test.py`
---
+## Accuracy Benchmarks
+
+> **Headroom's guarantee: compress without losing accuracy.**
+
+We validate against established open-source benchmarks. Full methodology and reproducible tests: [Benchmarks Documentation](https://chopratejas.github.io/headroom/benchmarks/)
+
+| Benchmark | Metric | Result | Status |
+|-----------|--------|--------|--------|
+| [Scrapinghub Article Extraction](https://huggingface.co/datasets/allenai/scrapinghub-article-extraction-benchmark) | F1 Score | **0.919** (baseline: 0.958) | :white_check_mark: |
+| [Scrapinghub Article Extraction](https://huggingface.co/datasets/allenai/scrapinghub-article-extraction-benchmark) | Recall | **98.2%** | :white_check_mark: |
+| [Scrapinghub Article Extraction](https://huggingface.co/datasets/allenai/scrapinghub-article-extraction-benchmark) | Compression | **94.9%** | :white_check_mark: |
+| SmartCrusher (JSON) | Accuracy | **100%** (4/4 correct) | :white_check_mark: |
+| SmartCrusher (JSON) | Compression | **87.6%** | :white_check_mark: |
+| Multi-Tool Agent | Accuracy | **100%** (all findings) | :white_check_mark: |
+| Multi-Tool Agent | Compression | **76.3%** | :white_check_mark: |
+
+**Why recall matters most**: For LLM applications, capturing all relevant information is critical. 98.2% recall means nearly all content is preserved — LLMs can answer questions accurately from compressed context.
+
+<details>
+<summary><b>Run benchmarks yourself</b></summary>
+
+```bash
+# Install with benchmark dependencies
+pip install "headroom-ai[evals,html]" datasets
+
+# Run HTML extraction benchmark (no API key needed)
+pytest tests/test_evals/test_html_oss_benchmarks.py::TestExtractionBenchmark -v -s
+
+# Run QA accuracy tests (requires OPENAI_API_KEY)
+pytest tests/test_evals/test_html_oss_benchmarks.py::TestQAAccuracyPreservation -v -s
+```
+
+</details>
+
+---
+
## Multi-Tool Agent Test: Real Function Calling
**The setup:** An Agno agent with 4 tools (GitHub Issues, ArXiv Papers, Code Search, Database Logs) investigating a memory leak. Total tool output: 62,323 chars (~15,580 tokens).
diff --git a/docs/benchmarks.md b/docs/benchmarks.md
new file mode 100644
index 000000000..73da0ea27
--- /dev/null
+++ b/docs/benchmarks.md
@@ -0,0 +1,205 @@
+# Accuracy Benchmarks
+
+Headroom's core promise: **compress context without losing accuracy**. This page shows our latest benchmark results against established open-source datasets.
+
+!!! success "Key Result"
+ **98.2% recall** on article extraction with **94.9% compression** — we preserve nearly all information while dramatically reducing tokens.
+
+---
+
+## Summary
+
+| Benchmark | Metric | Headroom | Baseline | Status |
+|-----------|--------|----------|----------|--------|
+| [Scrapinghub Article Extraction](#html-extraction) | F1 Score | **0.919** | 0.958 | :white_check_mark: |
+| [Scrapinghub Article Extraction](#html-extraction) | Recall | **98.2%** | — | :white_check_mark: |
+| [Scrapinghub Article Extraction](#html-extraction) | Compression | **94.9%** | — | :white_check_mark: |
+| [SmartCrusher (JSON)](#json-compression) | Accuracy | **100%** | — | :white_check_mark: |
+| [SmartCrusher (JSON)](#json-compression) | Compression | **87.6%** | — | :white_check_mark: |
+
+---
+
+## HTML Extraction
+
+**Dataset**: [Scrapinghub Article Extraction Benchmark](https://huggingface.co/datasets/allenai/scrapinghub-article-extraction-benchmark)
+**Samples**: 181 HTML pages with ground truth article bodies
+**Baseline**: trafilatura (0.958 F1)
+
+HTMLExtractor removes scripts, styles, navigation, ads, and boilerplate while preserving article content.
+
+### Results
+
+| Metric | Value | Description |
+|--------|-------|-------------|
+| **F1 Score** | 0.919 | Token-level overlap with ground truth |
+| **Precision** | 0.879 | Proportion of extracted content that's relevant |
+| **Recall** | 0.982 | Proportion of ground truth content captured |
+| **Compression** | 94.9% | Average size reduction |
+
+### Why Recall Matters Most
+
+For LLM applications, **recall is critical** — we must capture all relevant information. A 98.2% recall means:
+
+- Nearly all article content is preserved
+- LLMs can answer questions accurately from extracted content
+- The slight precision drop (some extra content) doesn't hurt LLM accuracy
+
+### Run It Yourself
+
+```bash
+# Install dependencies
+pip install "headroom-ai[html]" datasets
+
+# Run the benchmark
+pytest tests/test_evals/test_html_oss_benchmarks.py::TestExtractionBenchmark -v -s
+```
+
+---
+
+## JSON Compression (SmartCrusher)
+
+**Test**: 100 production log entries with critical error at position 67
+**Task**: Find the error, error code, resolution, and affected count
+
+### Results
+
+| Metric | Baseline | Headroom |
+|--------|----------|----------|
+| Input tokens | 10,144 | 1,260 |
+| Correct answers | 4/4 | **4/4** |
+| Compression | — | **87.6%** |
+
+SmartCrusher preserves:
+
+- First N items (schema examples)
+- Last N items (recency)
+- All anomalies (errors, warnings, outliers)
+- Statistical distribution
+
+### Run It Yourself
+
+```bash
+python examples/needle_in_haystack_test.py
+```
+
+---
+
+## QA Accuracy Preservation
+
+We verify that LLMs can answer questions equally well from compressed content.
+
+**Method**:
+1. Take original HTML content
+2. Extract with HTMLExtractor
+3. Ask LLM same question on both
+4. Compare answers against ground truth
+
+**Datasets**: SQuAD v2, HotpotQA
+
+### Results
+
+| Metric | Original HTML | Extracted | Delta |
+|--------|---------------|-----------|-------|
+| F1 Score | 0.85 | 0.87 | +0.02 |
+| Exact Match | 60% | 62% | +2% |
+
+!!! note "Extraction Can Improve Accuracy"
+ Removing HTML noise sometimes *helps* LLMs focus on relevant content.
+
+### Run It Yourself
+
+```bash
+# Requires OPENAI_API_KEY
+pytest tests/test_evals/test_html_oss_benchmarks.py::TestQAAccuracyPreservation -v -s
+```
+
+---
+
+## Multi-Tool Agent Test
+
+**Setup**: Agno agent with 4 tools investigating a memory leak
+**Total tool output**: 62,323 chars (~15,580 tokens)
+
+### Results
+
+| Metric | Baseline | Headroom |
+|--------|----------|----------|
+| Tokens sent | 15,662 | 6,100 |
+| Tool calls | 4 | 4 |
+| Correct findings | All | **All** |
+| Compression | — | **76.3%** |
+
+Both found: Issue #42, `cleanup_worker()` fix, OutOfMemoryError logs, relevant papers.
+
+### Run It Yourself
+
+```bash
+python examples/multi_tool_agent_test.py
+```
+
+---
+
+## Methodology
+
+### Token-Level F1
+
+We use the standard NLP metric for text overlap:
+
+```
+Precision = |predicted ∩ ground_truth| / |predicted|
+Recall = |predicted ∩ ground_truth| / |ground_truth|
+F1 = 2 * (Precision * Recall) / (Precision + Recall)
+```
+
+### QA Accuracy
+
+For question-answering, we measure:
+
+- **Exact Match**: Normalized answer strings match exactly
+- **F1 Score**: Token overlap between predicted and ground truth answers
+
+### Compression Ratio
+
+```
+Compression = 1 - (compressed_size / original_size)
+```
+
+A 94.9% compression means the output is 5.1% of the original size.
+
+---
+
+## Reproducing Results
+
+All benchmarks are reproducible:
+
+```bash
+# Clone the repo
+git clone https://github.com/chopratejas/headroom.git
+cd headroom
+
+# Install with eval dependencies
+pip install -e ".[evals,html]"
+
+# Run all benchmarks
+pytest tests/test_evals/ -v -s
+
+# Run specific benchmark
+pytest tests/test_evals/test_html_oss_benchmarks.py -v -s
+```
+
+### CI Integration
+
+Benchmarks run on every PR. See [.github/workflows/ci.yml](https://github.com/chopratejas/headroom/blob/main/.github/workflows/ci.yml).
+
+---
+
+## Adding New Benchmarks
+
+We welcome contributions! See [CONTRIBUTING.md](https://github.com/chopratejas/headroom/blob/main/CONTRIBUTING.md) for guidelines.
+
+Benchmarks should:
+
+1. Use established open-source datasets
+2. Include reproducible evaluation code
+3. Test accuracy preservation, not just compression
+4. Run in CI without API keys (or skip gracefully)
diff --git a/headroom/evals/html_extraction.py b/headroom/evals/html_extraction.py
index f6c5bb8cf..5a4cfaf8d 100644
--- a/headroom/evals/html_extraction.py
+++ b/headroom/evals/html_extraction.py
@@ -286,7 +286,7 @@ class HTMLExtractionEvaluator:
try:
import anthropic
- client = anthropic.Anthropic()
+ anthropic_client = anthropic.Anthropic()
def judge(question: str, ground_truth: str, prediction: str) -> tuple[float, str]:
prompt = HTML_JUDGE_PROMPT.format(
@@ -294,12 +294,16 @@ class HTMLExtractionEvaluator:
ground_truth=ground_truth,
prediction=prediction,
)
- response = client.messages.create(
+ anthropic_response = anthropic_client.messages.create(
model=self.judge_model,
max_tokens=200,
messages=[{"role": "user", "content": prompt}],
)
- text = response.content[0].text if response.content else ""
+ text = (
+ getattr(anthropic_response.content[0], "text", "")
+ if anthropic_response.content
+ else ""
+ )
return self._parse_judge_response(text)
return judge
@@ -364,36 +368,40 @@ Answer concisely and factually based only on the content provided."""
if self.provider == "openai":
from openai import OpenAI
- client = OpenAI()
- response = client.chat.completions.create(
+ openai_client = OpenAI()
+ openai_response = openai_client.chat.completions.create(
model=self.answer_model,
messages=[{"role": "user", "content": prompt}],
temperature=0.0,
max_tokens=500,
)
- return response.choices[0].message.content or ""
+ return openai_response.choices[0].message.content or ""
elif self.provider == "anthropic":
import anthropic
- client = anthropic.Anthropic()
- response = client.messages.create(
+ anthropic_client = anthropic.Anthropic()
+ anthropic_response = anthropic_client.messages.create(
model=self.answer_model,
max_tokens=500,
messages=[{"role": "user", "content": prompt}],
)
- return response.content[0].text if response.content else ""
+ return (
+ getattr(anthropic_response.content[0], "text", "")
+ if anthropic_response.content
+ else ""
+ )
else:
import litellm
- response = litellm.completion(
+ litellm_response = litellm.completion(
model=self.answer_model,
messages=[{"role": "user", "content": prompt}],
temperature=0.0,
max_tokens=500,
)
- return response.choices[0].message.content or ""
+ return litellm_response.choices[0].message.content or ""
def evaluate_case(self, case: HTMLEvalCase) -> HTMLEvalResult:
"""Evaluate a single HTML extraction case.
diff --git a/mkdocs.yml b/mkdocs.yml
index 6968addd9..9d286cf1f 100644
--- a/mkdocs.yml
+++ b/mkdocs.yml
@@ -60,6 +60,7 @@ nav:
- Getting Started:
- Quickstart: quickstart.md
- Installation: getting-started.md
+ - Benchmarks: benchmarks.md
- User Guide:
- Proxy Server: proxy.md
- Compression: compression.md