Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Pi Agent Core Concepts

Pi Agent Core Concepts

Before using Pi Agent for real, understanding its core concepts and how it works will help you get started faster.


Understanding Pi Agent in one sentence

Pi Agent is an AI coding assistant that runs in the terminal.

After launch you'll see a chat-like interface; enter a request in natural language and the AI can read your code, execute commands, and modify files.


Four running modes

Pi Agent supports four running modes for different scenarios:

Mode Command Description Applicable scenarios
Interactive mode pi (default) Terminal TUI interface, real-time conversation, all features supported Daily development, interactive programming
Print mode pi -p "prompt" One-shot Q&A, exits after outputting the result Script integration, quick queries
JSON mode pi --mode json All events output as JSON lines, suitable for program parsing Toolchain integration, data analysis
RPC mode pi --mode rpc Integration via the JSONL protocol over stdin/stdout Inter-process communication, editor plugins

Launch interactive mode directly to enter the TUI interface:



$ pi

Pi Agent 0.1.x  |  ~/projects/runoob-demo

claude-sonnet-4  |  上下文 2%  |  $0.0000

>

Print mode exits after answering, suitable for scripts:



$ pi -p "总结这个项目"

这是一个 TypeScript 命令行工具,核心逻辑位于 src/ 目录,包含会话管理、工具调度和扩展加载三个模块。

Tokens: 1.2k 输入 / 320 输出  |  费用 $0.0081

JSON mode outputs each event as one line of JSON for easy program parsing:



$ pi --mode json -p "总结这个项目"

{"type":"session","id":"a1b2c3d4"}

{"type":"message_start","role":"assistant"}

{"type":"message_end","usage":{"input":1200,"output":320}}

RPC mode sends and receives JSONL messages via stdin/stdout for editor plugins to call:



$ pi --mode rpc

{"id":1,"method":"prompt","params":{"message":"列出 src 目录"}}

{"id":1,"result":{"state":"idle","messages":2}}

As a beginner, you'll spend most of your time in interactive mode.

Once you're proficient, Print mode can be used for script automation.


Built-in Tools

On startup, Pi Agent gives the AI the following four built-in tools by default, with three additional read-only tools grep, find, and ls that can be optionally enabled:

Tool name Function Example Default state
read Read file contents AI reads src/main.ts to understand code logic Enabled by default
write Create or overwrite a file AI creates new files or rewrites entire files Enabled by default
edit Precisely replace content in files AI modifies certain lines of code in a function Enabled by default
bash Execute shell commands AI runs tests, installs dependencies, executes lint Enabled by default
grep Search for text in files AI searches which files a function is called in Optionally enabled
find Search files by name AI finds all files named *.test.ts Optionally enabled
ls List directory contents AI views the directory structure to locate modules Optionally enabled

The three read-only tools are enabled manually via the --tools option, e.g. pi --tools read,grep,find,ls.


Extension system

Pi Agent extends functionality in the following five ways:

Extension method File format Purpose Difficulty
Extensions TypeScript(.ts) Register custom tools, event listeners, commands, UI components Requires programming
Skills Markdown(SKILL.md) Provide workflow instructions and scripts for specialized domains Just need to know Markdown
Prompt Templates Markdown(.md) Reusable prompts with parameter support Simplest
Themes JSON(.json) Customize terminal interface color schemes Simple
Packages npm/git projects Package and share the above four types of resources As needed

As a beginner, you don't need to understand all extension methods right away.

Start with basic usage, then gradually explore the extension system as your needs grow.


File loading order

On startup, Pi Agent automatically loads project instruction files, letting you define the AI's behavior guidelines:

Level Path Scope Precedence
Global configuration ~/.pi/agent/AGENTS.md All projects Lowest, as a general baseline
Project configuration AGENTS.md or CLAUDE.md in the project directory and parent directories Current projects Highest, can override global instructions

Project-level configuration is loaded by traversing up from parent directories and finally merged with the current directory's configuration.

If you previously used Claude Code, you can reuse your existing CLAUDE.md file; Pi Agent will automatically recognize and load it.


Sessions and branching

Pi Agent's session system is one of its most distinctive features.

Each conversation is automatically saved as a session file in ~/.pi/agent/sessions/.

Sessions are tree-structured: you can 'fork' a new exploration direction from any historical node without losing previous conversations.

Schematic of the branching structure of the Pi Agent session tree

This is very useful when you need to experiment with different implementation approaches—each idea can be kept independently and revisited at any time.