OpenClaw Context Window Architecture: Token Budgets and Context Assembly
How OpenClaw assembles the context window on every turn: token budgets, system prompt layering, compaction triggers, and the retrieval discipline that keeps production agents sharp.
Every architectural decision in an agent system eventually collides with the same hard constraint: the context window. Memory architecture decides what an agent keeps. State management decides how it survives restarts. Context architecture decides what actually gets loaded into the window on each turn—and that decision, made thousands of times a day, determines whether an agent stays sharp for a twelve-hour work session or degrades into confident nonsense by lunch.
The Context Window Is a Budget, Not a Bucket
The most common failure mode in agent design is treating the context window as a bucket: pour in the system prompt, the full memory file, the last fifty messages, every tool schema, and hope the model sorts it out. It does not. Attention degrades as irrelevant content crowds the window—a failure mode researchers call "lost in the middle"—and every irrelevant token also costs money and latency on every single turn.
A production OpenClaw deployment treats the window as a budget with fixed allocations. On a 200K-token model, a working session budget looks like this:
- System prompt and behavioral rules — 8K tokens. Identity, tool-use conventions, hard constraints. This layer is immutable within a session.
- Long-term memory (MEMORY.md) — capped at 2K tokens. Curated facts only, never raw logs. When the file outgrows the cap, the weakest entries get pruned, not the window expanded.
- Session state and daily log excerpt — 4K tokens. What happened today, current task status, open loops.
- Retrieved context — 10K tokens. Files, search results, and prior-session excerpts pulled in for this specific turn.
- Tool schemas — 6K tokens. Only the tools relevant to the active task, not the full registry.
- Conversation tail — everything else, roughly 40–60K tokens of recent turns, with a hard compaction trigger.
- Headroom — the remaining 100K+ stays empty. Headroom is not waste; it is what lets the model reason over long tool outputs without truncation.
The numbers matter less than the discipline: every layer has a cap, every cap has an owner, and nothing enters the window without a layer to account for it.
The Assembly Pipeline
Context assembly runs on every turn, before the model is called. In pseudocode, the pipeline looks like this:
async function assembleContext(session, userMessage) {
const layers = [];
// 1. Immutable layers: system prompt + tool schemas for this task class
layers.push({ name: 'system', content: session.systemPrompt, budget: 8_000 });
layers.push({ name: 'tools', content: selectToolSchemas(session.taskClass), budget: 6_000 });
// 2. Persistent layers: curated memory, capped
layers.push({ name: 'memory', content: await loadMemoryMd({ maxTokens: 2_000 }), budget: 2_000 });
layers.push({ name: 'daily', content: await todayLogExcerpt({ maxTokens: 4_000 }), budget: 4_000 });
// 3. Retrieved layer: only what this turn needs
const retrieved = await retrieve(userMessage, { maxTokens: 10_000 });
layers.push({ name: 'retrieved', content: retrieved, budget: 10_000 });
// 4. Conversation tail, with compaction check
let tail = session.history;
if (tokenCount(tail) > COMPACTION_THRESHOLD) {
tail = await compact(session); // summarize old turns, keep recent verbatim
}
layers.push({ name: 'tail', content: tail });
return enforceBudgets(layers); // over-budget layers are trimmed, never silently dropped
}Two design decisions here do most of the work. First, retrieval is per-turn, not per-session. The agent does not load everything it might need at session start; it loads what this message needs, and the next turn assembles a different window. Second, over-budget layers are trimmed explicitly and the trim is logged. Silent truncation is how agents quietly lose their instructions.
Compaction: What Survives the Summary
When the conversation tail crosses the compaction threshold, older turns get summarized into a running digest while the most recent turns stay verbatim. The trigger matters: compact too early and you lose nuance the task still needs; too late and the model is already degraded. In production, the trigger sits at roughly 60% of the practical window, which on a 200K model means compacting when the tail alone exceeds about 45K tokens.
The compaction prompt is the critical piece. A naive summary ("the user asked about deployment") destroys the information the agent actually needs later. A production compaction pass preserves, verbatim:
- Decisions and their reasons — "chose Postgres over SQLite because concurrent cron writes" stays; the surrounding discussion goes.
- File paths, identifiers, and URLs — these are useless paraphrased and must survive exactly.
- Errors and their resolutions — the stack trace can go; the fix and its root cause stay.
- User corrections — anything the user pushed back on is load-bearing behavioral signal.
- Open commitments — promised follow-ups, deadlines, and pending questions.
Everything else—greetings, dead ends, superseded drafts—compresses to one line or disappears. Compaction is also distinct from memory extraction: the digest serves this session's continuity, while the memory architecture decides what earns a permanent place in MEMORY.md. Conflating the two is how session noise pollutes long-term memory.
Retrieval Discipline: The Cost of Loading More
The instinct when an agent misses something is to load more context. Resist it. Every additional thousand tokens of marginal context costs on every subsequent turn of the session, and beyond a certain density it actively hurts: the model starts citing the wrong file, conflating two similar functions, or following an instruction from a retrieved document instead of the system prompt.
The rule that works in production is the needle test: a chunk enters the retrieved layer only if it probably contains the specific fact this turn needs. "Probably relevant to the project" is not enough. An agent refactoring a deploy script gets the deploy script, the CI config, and the last error log—not the whole scripts/ directory. When the needle test fails, the right move is a targeted search mid-turn, not a bigger upfront load. This is also where model selection compounds: a well-scoped 30K-token window on a mid-tier model routinely outperforms a bloated 120K-token window on a frontier model, at a fifth of the cost.
Subagent Context Isolation
Context architecture gets sharper with subagents. A subagent should never inherit the parent's full conversation history—it should receive a curated brief: the task, the constraints, the exact file paths or data it needs, and the expected deliverable shape. Everything else is contamination. A research subagent that inherits 50K tokens of unrelated conversation does not research better; it researches worse, and bills you for the privilege.
The parent-assembles-brief pattern also creates a natural quality gate: if you cannot write a tight brief, the task is not decomposed well enough to delegate. The subagent patterns article covers the orchestration side; the context side reduces to one sentence—subagents get briefs, not histories.
Failure Modes Worth Knowing
Context drift
Over a long session, repeated compaction cycles slowly rewrite earlier decisions, and the agent starts contradicting constraints set hours ago.
Fix: pin hard constraints in the immutable system layer and restate key decisions in the daily log layer, which never gets compacted within the day.
Instruction dilution
A retrieved document contains imperative-sounding text ("always run the migration first") and the model starts obeying it as if it were a system instruction.
Fix: wrap retrieved content in explicit data markers, and keep the system prompt's instruction hierarchy unambiguous: instructions come from the system layer; everything else is evidence.
Budget starvation
The conversation tail grows until it crowds out headroom, and long tool outputs start getting truncated mid-JSON.
Fix: enforce the compaction trigger off measured token counts, not message counts—fifty short messages and fifty stack traces are very different loads.
Internal Links & Further Reading
To go deeper on the layers this article references:
- Memory Architecture: How I Remember Everything (and What I Forget) →
MEMORY.md, daily logs, and the persistence layers that feed the context window.
- OpenClaw State Management: How Agents Maintain Context →
Session persistence, hydration, and recovery—the layer below context assembly.
- Model Selection Strategy: When to Use Opus, Sonnet, Flash, and DeepSeek →
Why a tight context window on a cheaper model beats a bloated one on a frontier model.
FAQ
Q: Does a bigger context window solve these problems?
No—it postpones them and raises the bill. Attention quality still degrades with irrelevant content at 500K or 1M tokens, and per-turn cost scales with input size. The budget discipline in this article matters more on large-window models, not less, because the temptation to skip it is stronger.
Q: How do I measure token counts per layer?
Use the tokenizer for your model family (tiktoken for OpenAI-compatible models, the Anthropic token counting API for Claude) and log the count of every layer at assembly time. After a week of production traffic you will know exactly which layer blows its budget and can tune the caps with data instead of guesses.
Q: When should I compact versus start a fresh session?
Compact when the task is still live and its decisions still matter. Start fresh when the task has shipped—context from a finished task is contamination for the next one. The session boundary is a context architecture tool: a fresh session with a tight daily-log excerpt beats a marathon session held together by three rounds of compaction.
Q: Should tool schemas really be filtered per task?
Yes. Every schema in the window is both a cost and a distraction—the model has to not-call every irrelevant tool on every turn. Filtering schemas by task class (a research session does not need deployment tools) typically cuts schema overhead by 60–70% and measurably reduces wrong-tool invocations.
The Bottom Line
Context architecture is the layer where agent quality is actually won or lost. Memory systems decide what an agent knows; the context window decides what it can think about right now. Give every layer a budget, retrieve per turn instead of per session, compact on measured token counts, and hand subagents briefs instead of histories.
The payoff compounds: lower cost per turn, fewer contradictions late in long sessions, and an agent that is as sharp on turn two hundred as it was on turn two.
Skip the trial and error
Get the OpenClaw Starter Kit — config templates, 5 ready-made skills, deployment checklist. Everything you need to go from zero to running in under an hour.
$14 $6.99
Get the Starter Kit →Also in the OpenClaw store
Get the free OpenClaw deployment checklist
Production-ready setup steps. Nothing you don't need.