Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

Earendil 4 min read Deep dive mediumAdept

How Compaction Works in Pi

Pi handles context-window overflow in long coding-agent sessions by compacting: it keeps recent messages within a configurable token budget (default 20k tokens, roughly 5–20 turns) and replaces older history with an LLM-generated structured summary. The summary comes from a standalone request with its own system prompt ("context summarization assistant") and sections for goal, progress and key decisions, so a different, cheaper model can produce it. Auto-compaction runs after a turn ends, or mid-turn on an overflow error. The plain-text summary stays portable across models, but compaction changes the prompt prefix and invalidates the provider's prompt cache for the retained turns.

Assumes: LLM context windowscoding agent tool callsprompt caching

Related posts

  • Context Management for Deep Agents

    LangChain mediumAdept

    The Deep Agents SDK manages long-running agent context with three compression techniques triggered at fractions of the model's context window. Tool results over 20,000 tokens are offloaded to a filesystem and replaced with a path plus a 10-line preview. Old write/edit tool inputs are replaced with file pointers once context passes 85%, and summarization runs as a last resort, producing a structured summary while saving the full history to disk for later retrieval. To evaluate these mechanisms, the team forces compression early (10-25% of the window) to generate more events and uses targeted evals such as needle-in-the-haystack recovery to catch goal drift and lost details.

  • How we make AI coding more cost efficient without sacrificing task quality

    GitHub Engineering mediumAdept

    GitHub Copilot cut agent costs by optimizing for whole-task outcomes rather than per-tool-call token counts, after finding that aggressive shell-output compression (RTK) made agents reread or rerun commands and raised end-to-end cost. Four changes were validated with offline benchmarks and online A/B tests. They were selective output compression that preserves source-like output such as git diff and keeps a recovery path (5.5%), removing line-number prefixes from the view tool (3.1%), a meta-prompted task-tool prompt saving about 1,300 tokens per turn (2.9%), and batching background-task completion results to avoid retrieval turns (2.3%). The prompt rewrite initially serialized parallel agents, which shows that prompt behaviour needs regression tests and that savings must be measured in each workflow where a change ships.

  • How to solve the context size issues with context packing with Docker Model Runner and Agentic Compose

    Docker mediumAdept

    Context packing works around small local LLMs' context window limits by having a second model summarize the conversation history once an estimated token count crosses a threshold, then replacing the history with that summary. The example configures Docker Model Runner models in compose.yml, using a Qwen2.5-Coder 3B chat model and Qwen2.5 1.5B as the summarizer. It estimates tokens as characters/4 with a 5120-token trigger, and implements the packing step in a LangChain.js conversation loop. In the demo, history shrinks from 4886 to 153 tokens (about 97% reduction). Raising context_size helps only partially because not all models support it and generation slows as the context fills.

  • Effective harnesses for long-running agents

    Anthropic Engineering mediumAdept

    Long-running coding agents lose all memory between context windows, and compaction alone doesn't stop them from trying to one-shot an app, leaving half-finished work, or declaring the project done early. The proposed harness for the Claude Agent SDK splits work between an initializer agent and a coding agent. The initializer writes an init.sh script, a claude-progress.txt log, an initial git commit and a JSON feature list (200+ end-to-end features, all marked failing). Each later coding session reads git logs and progress notes, smoke-tests the app, implements one feature, verifies it end-to-end with browser automation via Puppeteer MCP, then commits and updates progress. JSON proved less prone to inappropriate edits than Markdown, and browser-based testing caught bugs that unit tests and curl missed.

  • Context Engineering with Deep Agents: How To Remember Without Keeping Everything In Context

    LangChain mediumAdept

    Long-running agents suffer "context rot" as tool output, history and instructions accumulate, so Deep Agents moves context out of the active window via middleware hooks in the agent loop rather than deleting it. FilesystemMiddleware offloads tool results over ~20,000 tokens (and user messages over 50,000) to files and shows only a preview. SummarizationMiddleware condenses older messages at 85% of the context window while keeping recent ones verbatim and writing the originals to recoverable files. SkillsMiddleware uses progressive disclosure to load task instructions only when relevant, subagents isolate noisy work in separate contexts, and MemoryMiddleware persists learned preferences in a plain markdown file that survives switching model providers.

The quest for software engineering knowledge.

Look:
Layout: