Skip to main content

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:
Key responsibilities:
  • 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
Location: packages/agent/src/agent.ts:96

AgentSession (@mariozechner/pi-coding-agent)

Builds on Agent to add session persistence, compaction, and extension support:
Key responsibilities:
  • 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)
Location: packages/coding-agent/src/core/agent-session.ts:213

SessionManager (@mariozechner/pi-coding-agent)

Manages append-only session trees stored as JSONL:
Session structure:
  • Append-only JSONL file
  • Tree structure via id/parentId fields
  • Leaf pointer tracks current position
  • Supports branching without modifying history
Location: 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

Location: packages/agent/src/types.ts:177

Extension Events

Extensions can hook into additional lifecycle events:
Location: packages/coding-agent/src/core/extensions/types.ts:797

Dependency Flow

Packages depend on each other as follows:
This ensures:
  • 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:
Each layer overrides the previous. Extensions can register custom CLI flags:
Location: 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