Context Management
Context is the scarcest resource in every agent system. Four disciplines — compaction, selective retrieval, budget planning, memory safety — that keep long runs coherent.
▶ Watch this reelWhat you'll learn
- Context compaction
- Selective retrieval
- Context budget planning
- Memory safety
Remember this
- Compaction preserves decisions, constraints, artifact pointers and open threads — triggered early, keeping recent turns verbatim
- Retrieve memory just-in-time with task anchors and thresholds; 'no memory' is a valid answer; conflicts resolved by provenance
- Budget every token: instrument layers, cap tool outputs, reserve reply headroom; never persist secrets or unscoped PII
Compaction
- Replace old turns with running summary: decisions · constraints · artifact paths · open threads.
- Trigger early (not at 99%), keep recent verbatim, verify ('state three constraints').
Selective retrieval
- Task-anchored, just-in-time, top-k with thresholds; abstain when thin.
- Detect conflicts by provenance; measure recall like RAG.
Budget planning
- Layers: system+tools (fixed) · memory cap · recent verbatim · reply reserve.
- Tool output quotas: summarize-or-store with pointers.
- Instrument per layer; plan for the worst turn.
Memory safety
- No secrets/PII persisted (secret-scan at write; consent basis).
- 'Unverified' ≠ fact. Per-user namespacing; retrieval-time access checks.
Code: The context budget, enforced in one wrapper
BUDGET = {
"system": None, # measured, fixed
"memory": 2_000, # tokens, retrieved top-k cap
"recent": 8_000, # verbatim turns
"reserve": 4_000, # reply + next tool-call headroom
}
async def build_context(state, task, user):
ctx = [system_prompt(), tool_schemas()] # fixed
mems = memory.search(embed(task), k=4, # selective
where={"user": user.id}, # access-checked
min_score=0.82) # abstain below
ctx += format_memories(mems)[:BUDGET["memory"]]
ctx += recent_turns(state, token_cap=BUDGET["recent"])
if usage(ctx) > WINDOW - BUDGET["reserve"]:
ctx = compact(ctx, keep_recent=3) # ch.1 discipline
return ctx
# Tool outputs are capped INSIDE each tool (AG-02): summarize-or-store,
# return pointer + summary. The wrapper never sees raw monsters.