Token Economics โ Compaction, Caching & Context Budgets
Lesson 7: Token Economics โ Compaction, Caching & Context Budgets
LLMs have finite context windows, and every token you send costs money (or burns subscription quota). Pi's answer is compaction: when the conversation grows too long, older messages are summarized while recent work is preserved. This lesson covers exactly how that works and every knob that shapes your token bill.
When compaction fires
Auto-compaction triggers when contextTokens > contextWindow โ reserveTokens. The default reserveTokens is 16,384 โ headroom left for the model's response. You can also trigger it manually with /compact [instructions], where optional instructions focus the summary ("focus on decisions made and files touched").
The mechanics: find a cut point by walking backward from the newest message until keepRecentTokens (default 20,000) is accumulated; summarize everything older into a structured CompactionEntry (summary + firstKeptEntryId); rebuild the context as system + summary + kept messages. On repeated compactions, the summarized span starts at the previous compaction's kept boundary โ messages that survived one compaction get included in the next. Two related mechanisms share the same structured summary format: branch summarization (when you navigate with /tree, preserving context across branches) and cumulative file tracking (file operations are tracked across compactions so the summary stays accurate).
Prompt caching
Most providers cache the stable prefix of your request. The cost fields cacheRead/cacheWrite make that visible in the footer. Pi is cache-aware: compaction and branch-summary requests use fresh routing session IDs and disable prompt-cache writes, because those one-off prompts are unlikely to be reused โ no point paying cache-write prices for a prompt you'll never send again. The showCacheMissNotices setting surfaces significant cache misses in the transcript, and warnings.anthropicExtraUsage warns about subscription providers that bill extra usage on top of a plan.
Custom summarization via extensions
Extensions can hook session_before_compact, session_compact_failed, and session_before_tree to replace or tune the summarization โ your domain's important details (API names, decisions, TODOs) can be forced into every summary, which keeps later turns cheaper because the model doesn't re-derive lost context.
The full token knob table
| Setting | Default | Effect |
|---|---|---|
compaction.enabled | true | Master switch for auto-compaction |
compaction.reserveTokens | 16384 | Context headroom for the model's response |
compaction.keepRecentTokens | 20000 | Recent tokens exempt from summarization |
branchSummary.reserveTokens | 16384 | Reserve for branch summarization on /tree |
branchSummary.skipPrompt | false | Skip the "summarize branch?" prompt (defaults to no summary) |
defaultThinkingLevel | โ | off/minimal/low/medium/high/xhigh/max reasoning budget |
thinkingBudgets | 1024โฆ32768 | Custom token budget per thinking level |
hideThinkingBlock | false | Hide reasoning text in output (display only) |
showCacheMissNotices | false | Surface significant prompt-cache misses |
retry.maxRetries | 3 | Agent-level retries โ each retry re-sends the context |
- Pick the cheapest model that can do the job (Lesson 3) โ biggest lever by far.
- Run
/compact "keep API names and open decisions"before starting a new phase of work. - Keep sessions short; fork instead of piling unrelated tasks into one context.
- Use
!!commandso verification commands don't dump output into context. - Write skills and prompt templates so instructions are only paid for when used.
- Watch
showCacheMissNoticesโ a session that constantly misses cache is paying full input price. - Set thinking level to
minimal/lowfor mechanical tasks. - Trim
keepRecentTokensif you trust your summaries (less re-sent context per turn). - Use a custom
session_before_compactsummarizer for high-stakes repos. - Prefer subscription auth (flat-rate) for heavy daily use; API keys for sporadic use.
Further Reading
- Compaction & Branch Summarization โ full internals
- Settings โ all token, compaction, thinking, and retry knobs
- Extensions โ session_before_compact and friends
๐ง Knowledge Check
1. When does auto-compaction trigger?
2. What does keepRecentTokens (default 20,000) control?
3. Why does pi disable prompt-cache writes for compaction requests?