GitHub - Kiz8-Team/pi-cwl: Reference implementation of Context Window Lifecycle (CWL) — structured context eviction for long-horizon LLM agents

3 min read Original article ↗

pi-cwl is the reference implementation of Context Window Lifecycle (CWL), a context-management scheme that gives long-horizon LLM agents an effectively unbounded working horizon.

It is a fork of the open-source pi.dev terminal coding agent, extended with the CWL protocol described in our paper:

Beyond Compaction: Structured Context Eviction for Long-Horizon Agents
Andrew Semenov, Svyatoslav Dorofeev — Kiz8
arXiv:2606.11213 [cs.CL], 2026


What is CWL?

As a long-running agent session accumulates history, the context window fills. The standard response is compaction: pause, summarize the transcript with an LLM, replace the original history with the summary. Compaction is simple but has four structural problems:

  • Unpredictable lossiness — the summarizer decides what matters, not the task.
  • Structural destruction — causal chains (tool call → output → decision → action) collapse into prose.
  • Blocking cost — a full LLM call fires mid-task, under token pressure.
  • Compression-induced hallucination — summarization under length pressure is a known LLM failure mode.

CWL addresses these by treating the transcript as a structured record of work rather than an opaque blob.

How it works

The agent annotates its trajectory in real time using a single delimiter tool. Annotations form a typed episode graph:

  • Exploratory episodes (expl) — information gathering: file reads, searches, directory listings. When closed, the agent supplies a one-line description of what was learned.
  • Action episodes (act) — durable work: edits, writes, shell commands. When opened, the agent declares which exploratory episodes it depends on.

When the token budget is exceeded, a deterministic, model-free eviction policy walks the graph and strips content in graduated levels — from reasoning traces, to bulk outputs, to intermediate artifacts, to full episode removal — starting with the oldest action episodes (whose effects are already persisted in the environment) and respecting dependency edges so that exploratory context is never dropped while an action that relied on it remains in context.

User turns are never evicted.

Results

A single agent session completing 89 sequential tasks across 80 million tokens with no measurable degradation in task accuracy relative to per-task isolated sessions (68.1% vs 67.5% on Terminal Bench 2.0). Inference cost reduction of 20–70% relative to uncapped sessions due to prefix stability near the token budget ceiling.


Installation

npm install       # install all dependencies
npm run build     # build all packages and expose `pi` on your global PATH

Then run:

Using the SDK in another project

# In this repo
npm run build

# In your project
npm link @mariozechner/pi-coding-agent
import { createAgentSession } from "@mariozechner/pi-coding-agent";

To switch back to the published npm version:

npm unlink @mariozechner/pi-coding-agent
npm install

Development commands

npm run check     # lint, format, and type-check
./test.sh         # run tests (skips LLM-dependent tests without API keys)
./pi-test.sh      # run pi from sources (can be run from any directory)

Repository structure

packages/
  ai/             # LLM provider abstraction layer
  agent/          # core agent loop
  coding-agent/   # coding-agent extension (tools, CWL integration)
  tui/            # terminal UI
  mom/            # multi-agent orchestration
  pods/           # sandboxed execution environments
  web-ui/         # web interface

Citation

If you use CWL or this reference implementation in your work, please cite:

@misc{semenov2026compactionstructuredcontexteviction,
  title         = {Beyond Compaction: Structured Context Eviction for Long-Horizon Agents},
  author        = {Andrew Semenov and Svyatoslav Dorofeev},
  year          = {2026},
  eprint        = {2606.11213},
  archivePrefix = {arXiv},
  primaryClass  = {cs.CL},
  url           = {https://arxiv.org/abs/2606.11213},
}

A standalone BibTeX file is available at CITATION.bib.