Overview
Pi is a modular AI agent toolkit framework built as a monorepo with clear separation of concerns. The architecture follows a layered approach where higher-level packages build on foundational abstractions.Package Structure
Core Layers
The framework is organized into three main layers:Key Classes
Agent (@mariozechner/pi-agent-core)
The Agent class orchestrates LLM interactions with tool execution:
- Execute the agent loop (LLM → tool calls → tool results → LLM)
- Manage streaming state
- Handle tool execution
- Support steering (interrupt mid-run) and follow-up messages
- Provider-agnostic LLM interaction
packages/agent/src/agent.ts:96
AgentSession (@mariozechner/pi-coding-agent)
Builds on Agent to add session persistence, compaction, and extension support:
- Session lifecycle (new, resume, fork, branch)
- Automatic message persistence to JSONL
- Context compaction when approaching context limits
- Extension system integration
- Model/thinking level management
- Resource loading (skills, prompts, themes)
packages/coding-agent/src/core/agent-session.ts:213
SessionManager (@mariozechner/pi-coding-agent)
Manages append-only session trees stored as JSONL:
- Append-only JSONL file
- Tree structure via
id/parentIdfields - Leaf pointer tracks current position
- Supports branching without modifying history
packages/coding-agent/src/core/session-manager.ts:663
Message Flow
User Input → Agent Response
Event System
The framework emits events at multiple levels:Agent Events
packages/agent/src/types.ts:177
Extension Events
Extensions can hook into additional lifecycle events:packages/coding-agent/src/core/extensions/types.ts:797
Dependency Flow
Packages depend on each other as follows:- ai package is provider-agnostic and reusable
- agent package has pure agent loop logic
- coding-agent brings it all together with tools, sessions, and UI
- tui provides terminal components for interactive mode
Configuration
Configuration flows from multiple sources:packages/coding-agent/src/core/settings-manager.ts
Next Steps
Tools
Learn about the tool system
Extensions
Build custom extensions
Sessions
Session management and branching