Agent memory optimized for what's ahead: plan-aware, forward-looking context compression for LLM agents.
Project description
memahead
Agent memory optimized for what's ahead.
memahead compresses an LLM agent's context at each step of a multi-step workflow using forward-looking, plan-aware retention. Unlike tools that compress greedily based on what already happened, memahead scores every chunk of context against the remaining steps in the plan and drops what future steps won't need — dramatically fewer tokens per agent call, without losing the information that matters downstream.
memahead sits on top of Headroom
(pip install headroom-ai), which performs the underlying compression
mechanics. memahead adds the plan-aware retention scoring layer.
Install
pip install memahead
This pulls in headroom-ai, sentence-transformers, and numpy. For more
accurate token accounting, also install the optional extra:
pip install "memahead[tokenizers]" # adds tiktoken
Quick example
from memahead import Plan, Step, PlanAwareCompressor
plan = Plan([
Step("research", "Search and gather raw facts about the topic"),
Step("synthesize", "Identify key themes across the research"),
Step("draft", "Write a structured first draft"),
Step("revise", "Produce the final polished output"),
])
compressor = PlanAwareCompressor(quality=0.85)
compressed = compressor.compress(
history=prior_messages, # your prior chat messages
tools=all_tool_schemas, # the full tool catalog
plan=plan,
current_step="synthesize", # the step about to run
)
response = llm.call(
messages=compressed.messages,
tools=compressed.tools,
)
print(compressed.report)
# TokenReport(before=12400, after=3100, saved=9300, compression_ratio=0.75)
How it works
For the step about to run, memahead:
- Splits the conversation history into chunks (one per message).
- Scores each chunk against the remaining plan steps with a
RetentionScorer— embedding both withall-MiniLM-L6-v2and taking the cosine similarity. A chunk is valuable if a future step is likely to need it. - Drops chunks that future steps won't need (system messages and the current turn's input are always retained).
- Filters the tool catalog down to schemas relevant to the current step —
deterministically, with no extra LLM call (
tool_filter). - Hands the survivors to Headroom for the actual mechanical compression.
- Returns a
CompressedContextwith leanmessages, filteredtools, and aTokenReportof exactly what was saved.
The quality dial (0.0–1.0) trades tokens for retention: higher keeps more
context, lower compresses more aggressively. Pass an explicit
retention_threshold for fully reproducible runs.
Public API
| Symbol | Purpose |
|---|---|
Step, Plan, PlanGraph |
Model linear or branching workflows; plan.remaining_from(step) is the forward horizon. |
RetentionScorer |
Forward-looking chunk scoring (the core novelty). |
ToolFilter, filter_tools |
Deterministic, LLM-free tool-schema filtering. |
PlanAwareCompressor |
The full pipeline. |
CompressedContext, TokenReport |
Results and savings accounting. |
The embedding backend is swappable: pass any callable
list[str] -> np.ndarray (or an object with .encode) as embedder= to
RetentionScorer, ToolFilter, or PlanAwareCompressor. This makes memahead
fully testable offline.
Development
pip install -e ".[dev]"
pytest
Academic foundations
memahead productizes findings from two papers that did not ship a usable library. It is an independent implementation and is not affiliated with or endorsed by the original authors.
- PAACE — Yuksel et al., Plan-Aware Agent Context Engineering, arXiv:2512.16970 (Dec 2025).
- ACON — Kang et al. (Microsoft), Agent Context Optimization, arXiv:2510.00615 (2025).
See NOTICE for full attribution.
License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file memahead-0.1.0.tar.gz.
File metadata
- Download URL: memahead-0.1.0.tar.gz
- Upload date:
- Size: 27.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
36b622918e642e479b1d25c896092db98adffeca701ce7c8302c0099f9d554ff
|
|
| MD5 |
c15a692f29f0a9a2f7ea825e36a9c12b
|
|
| BLAKE2b-256 |
0a4002ec34323f24493f31a82d540d74260d51ed95d39d4b4f8a26872cd94e5e
|
File details
Details for the file memahead-0.1.0-py3-none-any.whl.
File metadata
- Download URL: memahead-0.1.0-py3-none-any.whl
- Upload date:
- Size: 25.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e1c1b95026c196e5fd5662ca44b3a6068aa939a7fc2b35f5cd7b040a481aa87a
|
|
| MD5 |
50e134cd4833bfd4e5f8e847775f29cf
|
|
| BLAKE2b-256 |
b6b59bdddcc4bccc4eb3a0ef3bbbc4d40c6edf53bc192c99b3fbfecb37f47d7a
|