---
title: "How Compaction Works in Pi"
description: "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."
url: "https://swe.quest/articles/how-compaction-works-in-pi-fe49f7d01d8f597b"
canonical: "https://earendil.com/posts/compaction-in-pi/"
source: "Earendil"
published: "2026-08-13"
difficulty: "medium"
kind: "Deep dive"
reading_minutes: 4
tags: ["ai-agents","ai-coding","llms"]
---

# How Compaction Works in Pi

A post on [Earendil](https://swe.quest/sources/earendil.md), published 2026-08-13, 4 min read, Deep dive. Original: [https://earendil.com/posts/compaction-in-pi/](https://earendil.com/posts/compaction-in-pi/)

## Summary

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.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [AI agents](https://swe.quest/tags/ai-agents.md) (AI & ML)
- [AI for coding](https://swe.quest/tags/ai-coding.md) (AI & ML)
- [LLMs](https://swe.quest/tags/llms.md) (AI & ML)

## Prerequisites

- LLM context windows ([easy posts on LLMs](https://swe.quest/tags/llms.md?difficulty=easy))
- coding agent tool calls
- prompt caching ([easy posts on Caching](https://swe.quest/tags/caching.md?difficulty=easy))

## Keywords

Pi, compaction, prompt caching, Claude Code, Codex

## Related posts

- [Context Management for Deep Agents](https://swe.quest/articles/context-management-for-deep-agents-dc1d776470c6aad5.md): LangChain · 2026-10-13 · Medium · 5 min read · AI agents, LLMs, Evaluation
  The Deep Agents SDK manages long-running agent context with three compression techniques triggered at fractions of the model's context window.
- [How we make AI coding more cost efficient without sacrificing task quality](https://swe.quest/articles/how-we-make-ai-coding-more-cost-efficient-without-sacrificing-task-quality-07be31d835dcfb01.md): GitHub Engineering · 2026-09-02 · Medium · 10 min read · AI agents, AI for coding, LLMs, Evaluation, Cost optimization
  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.
- [How to solve the context size issues with context packing with Docker Model Runner and Agentic Compose](https://swe.quest/articles/how-to-solve-the-context-size-issues-with-context-packing-with-docker-model-e9177ed63d80433f.md): Docker · 2026-02-13 · Medium · 7 min read · LLMs, Inference & serving, JavaScript, Containers
  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.
- [Effective harnesses for long-running agents](https://swe.quest/articles/effective-harnesses-for-long-running-agents-987ba1b5b74b4050.md): Anthropic Engineering · 2025-11-26 · Medium · 9 min read · AI agents, AI for coding, LLMs, Testing
  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.
- [Context Engineering with Deep Agents: How To Remember Without Keeping Everything In Context](https://swe.quest/articles/context-engineering-with-deep-agents-how-to-remember-without-keeping-everything-1dc293ec006d2b0c.md): LangChain · 2026-11-10 · Medium · 9 min read · AI agents, LLMs
  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.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
