ttttmr/pi-context

Letting AI Actively Manage Its Own Context

What it is

Pi Context is a tool that helps AI assistants (specifically "Pi") manage long conversations. When an AI agent has a very long chat history, it can get cluttered with old, finished threads. Pi Context keeps the conversation focused by letting the agent: save important moments as labeled "checkpoints," look at the structure of the conversation history, and compress old, completed parts into short summaries instead of keeping every detail.

The project is inspired by a feature from another tool called kimi-cli d-mail, and aims to give Pi the ability to "time travel" through its conversation tree without losing information.

The three core actions

  1. Anchor (context_checkpoint) — Label a meaningful point in the conversation with a unique name, like parser-investigation-start or parser-investigation-done. This marks a moment so you can return to it later.

  2. Inspect (context_timeline) — View the current conversation path as a visual map showing checkpoints, summaries, branch points, user messages, and where you are now. Useful when you need to understand the shape of the history before deciding what to compress.

  3. Compact (context_compact) — Compress the conversation from an earlier checkpoint (or from the very beginning) into a summarized continuation. The summary should capture the useful state: current task, decisions made, external effects (like changed files or remote updates), validation status, source references, and the explicit next step.

What it is NOT

Earlier versions used Git-like names (context_tag, context_log, context_checkout), but the current version deliberately uses conversation-native names instead. These tools manage conversation history only — they are not Git commands and do not replace git tag, git log, or git checkout. Using them does not modify or roll back files, running processes, browser state, tickets, databases, or remote services.

How humans use it

  • Works automatically with Pi version 1.2.0 or newer — no manual command needed.
  • The commands /acm and /acm <task> still work for compatibility.
  • After a compaction, the transcript shows a one-line "Context compacted" message. Press ctrl+o to expand the handoff summary. Earlier messages stay visible, and you can return to the original context via a backup checkpoint or /tree.
  • /context opens a visual dashboard showing context-window usage and token distribution (similar to claude code /context).

Checkpoint naming conventions

Names ending in -start, -done, -pivot, -pause, or -resume declare a task or phase change. Keep the scope prefix consistent within a phase's lifecycle (e.g., parser-investigation-start → parser-investigation-done). Other names are still valid as ordinary anchors. The timeline shows the parsed phase next to each label, folds hidden intervals with message counts and approximate token estimates, marks already-compacted history (which can't be compacted again), and marks off-path summaries. These estimates describe historical content, exclude internal context-management traffic, and are not exact current-model occupancy or reclaimable space.

Session analysis (developer tool)

There's a separate offline analysis tool that generates a token timeline from a saved Pi session file, including checkpoint markers, compaction events, cumulative usage, and a simulated threshold-only comparison:

npm run analyze:session -- /path/to/session.jsonl --out /path/to/report

Then open report/session-token-chart.html in a browser. This tool is not included in the npm extension package. Details on snapshot cutoffs, simulation parameters, measurement limits, and privacy notes are in the session analysis documentation.

Installation

pi install npm:pi-context

For agents

The extension adds a context-management skill that guides agents to keep the active conversation as the smallest sufficient working set for the next step. It provides the three tools described above.

Development

Contributor guidance is in AGENTS.md (commands, structure, conventions) and docs/compaction.md (how compaction works and why). Test instructions are in test/test.md.

Design philosophy

More background is available in the linked blog posts (English and Chinese versions).

Related

  • Project
  • Project
  • Dispatch
  • Project
  • Project