ππ Agent
Based on pi source · 21 interactive chapters

Deep Dive
Pi Agent

These 21 chapters take you from the core agent loop to the full engineering picture — based on real source analysis.

SOURCE CODE WALKTHROUGH

Core Agent Loop

Every AI Agent is essentially a loop. Click each step to see the corresponding source and implementation details.

1User Input2Call LLM3Parse Response4Execute Tools5Loop Decisionloop
1

User Input · Harness

The user hits enter. The Agent switches from "idle" to "busy", then bundles the chat history, model config, and available tools into a frozen "work snapshot" — every decision in this turn is based on it; changing settings mid-run won't affect the current turn.

// packages/agent/src/harness/agent-harness.ts
async prompt(text: string) {
if (this.phase !== "idle")
throw new AgentHarnessError("busy")
this.phase = "turn"
const turnState = this.createTurnState()
const context = session.buildContext() // JSONL → messages[]
}
WHY DEEP DIVE

From 30 Lines to 920 Files

A teaching Agent's core loop is just ~30 lines, but how much engineering does production pi stack on top of the same loop?

Teaching~30 lines TypeScript
// A complete Agent core
const agent = new Agent()
while (true) {
r = await provider.stream(msgs)
if (r.stop !== "tool_use") break
msgs.push(await runTool(r.tool))
}
Synchronous API calls
Single provider
No session persistence
No compaction
Crash = data loss
No extensions
Production piSame loop + 21 layers of engineering
Ch09
Lazy Stream + Retry
lazyStream async-loads SDKs, RetryPolicy exponential backoff
Ch06
Provider Abstraction
Models route to ~40 providers, header priority chain
Ch08
7+ OAuth Flows
Anthropic/Copilot/Codex/OpenRouter/xAI/Kimi …
Ch04
Durable Session Tree
JSONL append-only, in-place branching without copying files
Ch03
Auto + Branch Compaction
findCutPoint, file tracking, branch summarization
Ch13
Extension System
~40 lifecycle events, jiti + VIRTUAL_MODULES
Ch16
6,046-line TUI
steer/follow-up queues, dialogs, hot-reload themes
Ch21
Supply-Chain Hardening
lockstep releases, shrinkwrap allowlist, --ignore-scripts
ARCHITECTURE LAYERS

Four Architecture Layers

From the core engine to the user interface, pi is organized into four layers by responsibility. Click to expand and see core files, design patterns, and corresponding chapters.

L1
Engine Core
5 chapters in depth
L2
Unified LLM API
4 chapters in depth
L3
Coding-Agent & Extensibility
6 chapters in depth
L4
Application & Engineering
6 chapters in depth
DISTINCTIVE DESIGN

8 Distinctive Design Choices in pi

pi's unusual engineering tradeoffs: minimal core + self-extension, Result types, durable session tree, lazy SDKs, supply-chain hardening — each maps to a specific chapter.

KEY SOURCE FILES

Key Source Files

Understanding these key files gives you a grasp of the entire system. File size reflects engineering complexity.

LEARNING PATH

21 Chapters of Source Deep Dive

Each chapter focuses on a core subsystem with source analysis + architecture visualization + runnable demos. Filter by layer, or follow the sequence.