Headroom

How Compression Works

Understand Headroom's compression pipeline, automatic content routing, and how different content types are compressed.

Headroom automatically detects what kind of content you're sending and routes it to the right compressor. You don't need to configure anything -- just call compress() and the pipeline handles the rest.

The Pipeline

Every request flows through a short pipeline:

┌──────────────┐     ┌────────────────┐
│ CacheAligner │────>│ ContentRouter  │
│ (off by      │     │                │
│  default)    │     │ Detect type &  │
│ report drift │     │ route to best  │
│ for cache    │     │ compressor     │
└──────────────┘     └────────────────┘
  1. CacheAligner (detector-only, off by default) reports dynamic-prefix drift (dates, session context) so callers can keep the static prefix cacheable. It never rewrites your messages.
  2. ContentRouter inspects each content block and routes it to one compressor -- SmartCrusher for JSON arrays, CodeAwareCompressor for source code, LogCompressor for build output, and so on. This is where essentially all compression happens.

Content Type Detection

The router auto-detects content type by analyzing structure and patterns. No manual hints required.

Content TypeDetection SignalCompressorTypical Savings
JSON arraysValid JSON with array elementsSmartCrusher70-90%
Source codeSyntax patterns, indentation, keywordsCodeAwareCompressor40-70% (opt-in; disabled by default)
Search resultsfile:line:content formatSearchCompressor80-95%
Build/test logsTimestamps, log levels, pytest/npm markersLogCompressor85-95%
DiffsUnified diff formatDiffCompressor60-80%
HTMLTag structureHTMLExtractor50-70%
Tabular (CSV/TSV/markdown tables)Delimited rows / table syntaxTabularCompressor60-90%
Structured config (YAML/TOML/INI)Config syntaxConfigCompressor40-70%
Plain textText with no stronger signalTextCrusher30-60%
Anything elseML fallbackKompressvaries

Quick Start

import {  } from "headroom-ai";

const  = [
  { : "system" as , : "You are a helpful assistant." },
  { : "user" as , : "Summarize this data" },
  { : "tool" as , : '{"results": [...]}', : "call_1" },
];

const  = await ();
.(`Tokens saved: ${.tokensSaved}`);
.(`Compression ratio: ${.compressionRatio}`);
from headroom import compress

result = compress(content)
print(result.compressed)
print(f"Saved {result.savings_percentage:.0f}% tokens")

Configuring the Compressor

import {  } from "headroom-ai";

const  = await (messages, {
  : "gpt-4o",
  : 50000,
});

.(`Before: ${.tokensBefore} tokens`);
.(`After: ${.tokensAfter} tokens`);
.(`Transforms: ${.transformsApplied.join(", ")}`);
# Advanced / Internal API -- prefer `from headroom import compress` for typical use
from headroom.compression import UniversalCompressor, UniversalCompressorConfig

config = UniversalCompressorConfig(
    compression_ratio_target=0.5,  # Keep 50% of content
    use_entropy_preservation=True,  # Preserve UUIDs, hashes
    use_magika=True,                # ML-based content detection
    ccr_enabled=True,               # Store originals for retrieval
)

compressor = UniversalCompressor(config=config)
result = compressor.compress(content)

print(f"Type: {result.content_type}")
print(f"Handler: {result.handler_used}")
print(f"Saved: {result.savings_percentage:.0f}%")

Structure Preservation

Headroom doesn't blindly truncate. It identifies what matters in each content type and preserves it:

Content TypeWhat's PreservedWhat's Compressed
JSONKeys, brackets, booleans, nulls, short values, UUIDsLong string values, whitespace
CodeImports, function signatures, class definitions, typesFunction bodies, comments
LogsTimestamps, log levels, error messages, stack tracesRepeated patterns, verbose details
TextHigh-entropy tokens (IDs, hashes), headersLow-information content

Real Compression Ratios

Content TypeCompressionSpeedWhat's Preserved
JSON (large arrays)70-90%~1msAll keys, structure
Source code (Python)50-70%~10msSignatures, imports
Search results80-95%~2msRelevant matches
Build logs85-95%~3msErrors, stack traces
Plain text60-80%~5msHigh-entropy tokens

Batch Compression

For multiple contents, batch compression is more efficient:

# Advanced / Internal API -- prefer `from headroom import compress` for typical use
from headroom.compression import UniversalCompressor

compressor = UniversalCompressor()

contents = [
    '{"users": [...]}',
    'def hello(): pass',
    'Plain text content',
]

results = compressor.compress_batch(contents)

for result in results:
    print(f"{result.content_type}: {result.savings_percentage:.0f}% saved")

What Happens Under the Hood

When you call compress(), here is the full sequence:

  1. Content detection -- Magika (ML-based) or pattern matching identifies the content type
  2. Structure extraction -- A handler extracts a structure mask marking what to preserve
  3. Compression -- Non-structural content is compressed (SmartCrusher, Kompress ML, or text utilities like TextCrusher)
  4. CCR storage -- If enabled, the original is stored for retrieval when the LLM needs full context

Zero-config by default

The pipeline works out of the box with no configuration. All detection, routing, and compression happens automatically. Configuration is available when you need fine-grained control.

On this page