Context Management
Automatic live-zone-only context management that compresses the newest content blocks while preserving the provider cache hot zone.
Context management is now handled automatically inside the pipeline (live-zone-only compression). Headroom never drops messages from the conversation history and does not do position-based or score-based context management.
How It Works
Headroom compresses only the newest content blocks — the latest user message and the latest tool result / tool output. Compression is type-aware and reversible via CCR, so the LLM can retrieve the original content on demand.
The cache hot zone — the system prompt, tool definitions, and older turns — is never mutated. Leaving the prefix untouched preserves provider prompt caching, so cache hit rates stay stable across turns.
Conversation with a large latest tool result
-> Identify the live zone (newest user message + latest tool output)
-> Compress the live zone type-aware, cache original in CCR (hash=def456)
-> Insert marker: "compressed, retrieve: def456"
-> Older turns, tools, and system prompt are forwarded byte-for-byteProtection rules
Headroom enforces several protections to ensure model output quality:
Output buffer reservation
A configurable number of tokens is reserved for the model's response. The context budget is calculated as:
context_budget = model_context_limit - output_buffer_tokensThis prevents the input from consuming the entire context window and leaving no room for the model to respond.
System message protection
System messages are never dropped. They contain critical instructions, persona definitions, and tool descriptions that the model needs throughout the conversation.
Turn protection
The last N messages are always preserved, ensuring the model has immediate conversational context. The one-function compress() API exposes this as CompressConfig.protect_recent, which defaults to 4 messages (roughly the last 2 user/assistant turns) -- see headroom/compress.py:110-112.
Configuration
Context management is now automatic. Use per-request overrides to control behavior:
import { } from "headroom-ai";
const = await (messages, {
: "gpt-4o",
: 32000,
});
.(`Compressed: ${.tokensBefore} -> ${.tokensAfter}`);from headroom import HeadroomClient, OpenAIProvider
from openai import OpenAI
client = HeadroomClient(
original_client=OpenAI(),
provider=OpenAIProvider(),
default_mode="optimize",
)
# Per-request overrides
response = client.chat.completions.create(
model="gpt-4o",
messages=messages,
headroom_output_buffer_tokens=8000, # More room for long responses
)HeadroomClient.chat.completions.create() also accepts a headroom_keep_turns
parameter, but as of this package version it is accepted and threaded through
the wrapper methods without being forwarded into the transform pipeline (see
headroom/client.py -- TransformPipeline.apply()'s accepted kwargs, listed
in its docstring, don't include a turns/keep-turns option). Passing it does
not raise an error, but it currently has no effect; use compress()'s
protect_recent (see Turn protection above) if you need
this control today.
Note: The
IntelligentContextConfig,ScoringWeights, andRollingWindowConfigclasses are no longer part of Headroom. Context management is now handled automatically inside the pipeline (live-zone-only compression).