Context Management in AI Agent Frameworks: How Pi, OpenClaw, Claude Code & Letta Converge
This analysis compares context management across four AI agent frameworks—Pi, OpenClaw, Claude Code, and Letta—revealing converging strategies for file reading limits, pagination, conversation pruning, sub-agent isolation, and active context management that gives models the right working set at the right time.
Every agent framework hits the same constraint: the context window is too small to hold everything the model might need to remember. As conversations grow, file reads expand, sub-agent calls multiply, and tool outputs accumulate, the framework must decide what stays in the working set, what gets compressed, and what gets retrieved later.
Managing the Context Window
Pi, OpenClaw, Claude Code, and Letta make different choices but converge on a similar underlying pattern. Context is no longer a passive transcript buffer; it becomes something the system actively manages. The core design question is how much of that management happens inside the framework versus how much is left for the model to handle.
The Bet: Trusting the Model to Manage Its Own Context
Every context-management decision embeds an assumption about model behavior. The key question: should the framework actively constrain context usage, or rely on the model to manage its own budget?
Large Files
File reading makes this concrete. When a model needs to read a file too large for the context window, something must decide what to keep. All four frameworks support offset and limit parameters for pagination.
Pi (pi-mono)
Pi enforces a hard cap on file reads: 2,000 lines or 50 KB, whichever comes first, even if the model doesn't request a slice. Output is truncated from the head with an explicit continuation hint:
[Showing lines 1-2000 of 50000. Use offset=2001 to continue.]The tool description reinforces this:
output is truncated to 2000 lines or 50KB. Use offset/limit for large files.Pi's approach is framework-first : protect the context first, then teach the model to paginate.
OpenClaw
OpenClaw inherits Pi's read tool and its 2K-line / 50KB truncation. It adds an extra budget for bootstrap files —one-time context files loaded at session start: 12,000 characters per file, 60,000 characters total. When a bootstrap file exceeds its budget, it uses a 75% head / 25% tail split. Tool results have a separate budget: 16,000 characters or 30% of the context window, whichever is smaller. If the tail looks “important” (errors, JSON closing braces, summary keywords), it switches to head+tail mode; otherwise it keeps only the head. OpenClaw's strategy is defense in depth : Pi's truncation as layer one, bootstrap caps as layer two, tool-result budgets as layer three.
Claude Code
Claude Code applies two gates to file reads. First, a 256 KB byte limit checked via stat before opening—files larger than this are rejected with an error telling the model to use offset/limit or grep. Second, a post-read 25,000-token budget catches files that are under the byte limit but token-dense. Both limits are remotely tunable via GrowthBook feature flags without a new release. Even files under the limit default to the first 2,000 lines, with any line over 2,000 characters truncated. The model must explicitly request more via offset and limit. The tool description is a multi-paragraph prompt explaining pagination, size limits, image/PDF/notebook support, and encouraging parallel reads. A deduplication system returns a stub instead of full content if the model re-reads the same file range without modification. Claude Code's approach is framework-first with remote tunability : pre-read byte gate, post-read token gate, line/length defaults, actionable errors, rich tool prompts, read deduplication, and feature flags for server-side adjustment.
Letta
Letta Code reads from the local filesystem with a pattern similar to Claude Code but fully open. Source comments note: Limits based on Claude Code's proven production values. The Read tool first calls stat, rejecting anything over 10 MB. It returns a contiguous window of up to 2,000 lines from an optional offset, each line capped at 2,000 characters. On truncation, a continuation hint tells the model to use offset and limit. When truncation occurs, the full content is written to an overflow file on disk and the path appended to the output. Tool outputs have per-tool character caps: 30,000 for bash and sub-agent results, 10,000 for grep. Mid-truncation (keep head and tail, drop middle) is the default for tool outputs and is configurable via environment variables.
What sets Letta apart is MemFS : a git-backed in-memory filesystem where the agent's persistent memory lives as markdown files. Files under system/ are pinned to the system prompt and always in context. Files outside system/ are visible as a tree of names and descriptions but are not loaded until the agent reads them. The agent manages its own progressive disclosure by moving files in and out of system/, restructuring hierarchies, and updating file descriptions. Memory edits are auto-committed and synced to a git remote. Letta's read path converges with the others (pre-read stat, line caps, offset/limit, continuation hints, overflow to disk) while adding a persistent memory layer none of the other three have. It is also the only fully Apache 2.0 open-source framework in this set, making every design decision visible and auditable.
The Real Engineering: Conversation Pruning
As conversations grow, every framework must decide what to keep and what to discard. This is where design differences matter most, because the compression strategy determines whether long-running agents stay coherent or gradually degrade.
Pi (pi-mono)
Pi uses compression : LLM-driven summarization triggered by a token threshold.
Trigger: estimated context tokens exceed contextWindow - reserveTokens (default reserve: 16,384 tokens).
Keep: walk backward from the end of the conversation, retaining roughly the most recent 20,000 tokens of messages ( keepRecentTokens).
Summarize: everything earlier is sent to an LLM for summarization.
Placement: the summary becomes a synthetic user message prepended before the retained tail.
Tool-call safety: never cuts at an orphaned tool result; moves along boundaries to keep tool-call/tool-result pairs intact.
OpenClaw
OpenClaw runs two distinct context-management mechanisms on top of Pi's compression.
Trigger: history exceeds 50% of the context window ( maxHistoryShare, default 0.5).
Keep: history is split into token-equal chunks; the oldest chunk is dropped, the rest retained, with tool-call/result pairs repaired.
Summarize: dropped content goes through a phased multi-turn LLM summarization with a merge step.
Placement: same as Pi—synthetic message prepended before the retained tail.
Tool-call safety: repairToolUseResultPairing fixes orphaned tool results after dropping chunks; splitMessagesByTokenShare avoids splitting inside a tool-call/result pair.
Pre-compression flush: a silent agent turn lets the agent persist state to memory files before history disappears.
Second layer: non-destructive in-memory trimming of tool results (soft-trim then hard-clear) with a 5-minute TTL cache, reclaiming context for the current request while protecting the durable conversation.
Claude Code
Claude Code manages context through pre-query optimization and LLM-driven compression.
Trigger: estimated tokens exceed the effective context window minus a 13,000-token buffer (for a 200K model, compression fires around 167K tokens).
Summarize: the full conversation is sent to the model with a structured 9-part prompt covering primary request, key technical concepts, files and code, errors and fixes, problem resolution, all user messages, pending tasks, current work, and optional next steps.
Placement: becomes a user message telling the model the session continues from a previous conversation that was interrupted due to context exhaustion.
Post-compression restore: up to 5 recently read files are re-attached to context after compression, subject to a token budget.
Summarizer safety: the model generates an analysis draft and a final summary in separate tagged sections; the draft is stripped before the summary enters context, improving quality without bloating the result.
Fallback for overlong prompts: if the compression call itself hits the context limit, a deterministic head-dropping strategy deletes the oldest API turn groups ( 20% of groups or enough to close the token gap).
Pre-query optimization runs before every model call, regardless of context pressure. A pipeline manages tool results without touching conversation text. Oversized tool results are persisted to disk and replaced with a 2 KB preview; per-tool cap is 50,000 characters, per-message aggregate cap is 200,000 characters, so a 60 KB grep result gets offloaded on the first turn of a new session.
Letta
Letta Code manages context through server-side compression and client-side memory consolidation.
Trigger: compression is handled by the Letta API server-side. The client receives compression events via the streaming API and tracks context token history locally with a 4 bytes per token heuristic for estimation.
Summarize: server runs LLM-driven summarization (default model letta/auto) and streams back a summary message containing compressed text and stats (pre/post token counts, message counts).
Placement: managed server-side. The client sees a completed compression event with summary text and token stats, then marks subsequent usage as post-compression.
Reflection sub-agent: when a compression event fires (default trigger) or a configurable step threshold is reached ( 25 user messages if enabled), Letta Code launches a background reflection sub-agent. It receives the recent conversation transcript and a snapshot of the parent agent's memory, then edits the git-backed memory repository in a worktree. On completion, it triggers a system-prompt recompile so the parent agent picks up the new memory. The reflection prompt has a budget of 16,000 tokens.
Knowledge persistence: important state migrates from ephemeral conversation history into persistent memory files, reducing what the server must retain via compression. Information that would be lost in other frameworks is persisted in files the agent can always access. This is the most ambitious approach to long-term agent memory among the evaluated frameworks.
Sub-Agent Context Management
In the frameworks studied, sub-agents are typically isolated from the parent conversation. None copy the full parent history into the child. The difference lies in what workspace context they inherit.
Pi spawns a new process with an in-memory session for each delegated task. The sub-agent receives only the task string as a single user message; no parent history is passed.
OpenClaw defaults to a fresh isolated session with no parent transcript. It has a fork mode that copies the parent transcript to the child, but only for same-agent generation scenarios. Workspace context is filtered to a minimal allow-list ( AGENTS.md, TOOLS.md, SOUL.md).
Claude Code has two paths. The default typed-agent path creates a blank conversation: the delegation prompt becomes the only user message, no parent history. The newer fork path passes the entire parent message history to the child to share prompt caching, plus a synthetic assistant message and placeholder tool results. Tools are rebuilt for the worker with their own permission modes; async agents get an explicit allow-list (Read, Grep, Glob, Shell, Edit, Write, WebSearch, and a few others). Skills referenced in the agent definition are eagerly pre-loaded; full skill content is injected as user messages into the initial conversation rather than loaded on demand.
Letta Code has seven built-in sub-agent types—the richest out-of-the-box taxonomy among the four. Fork sub-agents call the Letta API's conversation fork endpoint, which creates a server-side copy of the full parent message history, giving the child the complete conversation trajectory in context. Non-fork sub-agents (general-purpose, init, memory, recall, reflection) start as fresh headless instances with the task prompt as the only user message. Tool restrictions are handled per sub-agent via the tools field in the sub-agent definition; parent allow/deny permission rules propagate to children. Sub-agent results are capped at 30,000 characters; full run transcripts are written to disk. Skills can be pre-loaded into sub-agents; skill content is injected as tagged blocks before the user prompt. Existing Letta API agents can also be deployed as sub-agents by agent ID, bringing their own persistent memory into the local instance.
Where Designs Converge
Comparing these four codebases, the most striking finding is not how different they are, but how consistent.
All four enforce hard limits on file reads.
All four support offset/limit pagination.
All four cap tool result sizes.
All four isolate sub-agent sessions.
All four run LLM-driven compression triggered by token thresholds.
All four estimate context usage and detect pressure.
These are not coincidences. They are convergent solutions to the same engineering problem: a fixed-size working set that must feel infinite.
Fifty years of computing experience tell us the best memory management is the kind the programmer never has to think about. Registers, cache lines, page tables, swap space. Each layer is managed by the system, each layer invisible to the one above. The program just runs.
Agent frameworks are moving in the same direction. The goal is not to show the model everything, but to give it the right working set at the right time, and let it dynamically decide how to manage its own context.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
