Learn Pi Agent
Real agent harness engineering. pi already turns the loop; you just hang mechanisms on it.
The core pattern
pi runs the same loop inside: call the model, run tools, feed results back. Your job is to hang mechanisms on that loop — tools, permission, events, knowledge, sessions, delegation, integration.
const { session } = await createAgentSession({ sessionManager: SessionManager.inMemory() })
// 观察循环的每一次呼吸
session.subscribe((event) => {
if (event.type === "message_update") process.stdout.write(event.assistantMessageEvent.delta)
})
// 一次 prompt = 一整个 agent loop
await session.prompt("看看当前目录,告诉我这是什么项目")Learning path
12 progressive chapters, from a single minimal session to many mechanisms in one AgentSession
AgentSession
One prompt() is a whole agent loop; pi already turns it for you.
Custom tools
Add a capability, add just one handler.
Permission gate
Draw the boundary first, then hand over freedom.
Events and hooks
Observe with events, intervene with hooks, never touch the main loop.
On-demand skills
Only names and descriptions are resident; the body is read on a hit.
Prompt templates
Standardize how to ask, too.
Sessions and branching
A session is a tree, not a straight line.
Subagents
Trade context budget for focus.
RPC integration
Commands in on stdin, events out on stdout.
Custom providers
pi's model layer is open.
Pi Package
One unit, one install.
The full harness
Every extension point ultimately flows into the same loop.
Mechanism layers
Four orthogonal concerns combine into a complete harness