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

Pi Agent Non-Interactive Mode

Pi Agent Non-Interactive Mode

Besides interactive chat, Pi Agent also offers three non-interactive modes for script integration, automated workflows, and inter-process communication.


Print mode (-p)

Print mode is the most commonly used non-interactive mode; it performs a one-off Q&A, outputs the result, and exits.

Basic Usage

The simplest form is to put a question directly after -p; the AI exits after answering.



$ pi -p "帮我总结一下这个项目的结构"

这是一个 TypeScript + Vite 项目,源码集中在 src/ 目录下。

入口文件是 src/main.ts,工具函数位于 src/utils/,共 18 个模块。

Use @ to reference a file so the AI reads the specified file's content:



$ pi -p @README.md "这段文档的主要内容是什么"

这份 README 介绍了安装步骤、环境变量配置和三条常用命令。

Pipe command output directly to the AI, suitable for processing logs, documents, and other text:



$ cat README.md | pi -p "用三句话总结这段文字"

1. 项目要求 Node 20 以上版本。

2. 安装依赖后先把 .env.example 复制为 .env。

3. 日常调试用 npm run dev,发布构建用 npm run build。

@ can also reference images so the AI analyzes image content:



$ pi -p @screenshot.png "图片中显示的错误信息是什么"

截图中的报错是 Module not found: Can't resolve './utils/format',

说明 src/index.ts 里的相对路径拼写有误。

Use --name to name a session so you can resume that thread in interactive mode:



$ pi --name "快速审查" -p "审查 src/ 目录下的代码质量"

The overall code quality is good, there are 3 places that can be improved:

1. src/api/user.ts 缺少对分页参数的校验。

2. src/utils/format.ts 的日期格式化未处理时区。

3. src/db/query.ts 的裸抛异常建议补充错误码。

Specify model

Use --provider plus --model to specify a model, or combine the two into the provider/model form.



$ pi --provider openai --model gpt-4o -p "帮我重构这段代码的逻辑"

重构建议:把 src/service/order.ts 中 120 行的 createOrder 拆成

校验参数、计算金额、落库三个函数,并为每个函数补充单元测试。

The combined form below is completely equivalent to the previous one and shorter to write:



$ pi --model openai/gpt-4o -p "帮我重构这段代码的逻辑"

Adding a colon and reasoning level after the model name controls the thinking depth of this call:



$ pi --model sonnet:high -p "这个复杂的算法有什么潜在问题"

主要风险有三点:递归深度没有上限会导致栈溢出;

缓存键未包含用户身份,存在越权读取的可能;

批量写入缺少事务包裹,失败时会出现半提交状态。

in --model :thinking the suffix (such as sonnet:high) and --thinking parameter can both set the reasoning level.

It's recommended to choose one of the two to avoid difficulty troubleshooting when the two forms are inconsistent.

Use --models to limit the available models; separate multiple patterns with commas:



$ pi --models "claude-*,gpt-4o" -p "审查代码"

审查完成:共发现 2 个阻塞问题和 5 个改进建议,

详细列表已按严重程度排序。

Tool control

Controlling which tools the AI can use via command-line arguments is key to running non-interactive mode safely in scripts.

On non-Windows platforms, Pi Agent has 7 built-in tools: read, write, edit, bash, grep, find, ls.

On Windows, bash is replaced by the Power Shell tool.

Use --tools as a whitelist to keep only specified tools, e.g. a read-only mode that changes no files:



$ pi --tools read,grep,find,ls -p "审查代码不修改"

审查结果:共 4 个问题,其中 1 个涉及并发写入,

完整报告已输出到终端,本次未修改任何文件。

Use --exclude-tools to exclude specified tools:



$ pi --exclude-tools bash -p "分析代码但不执行命令"

分析结论:该模块的性能瓶颈在循环内的重复文件读取,

建议把目录列表提到循环外,预计可减少 80% 的 I/O。

Use --no-builtin-tools to disable all built-in tools; the AI can only answer based on the conversation content, while extensions and custom tools remain available:



$ pi --no-builtin-tools -p "纯问答"

Quicksort 的平均时间复杂度是 O(n log n),

最坏情况是 O(n^2),出现在已排序数组上选取首元素为主元时。

To disable extensions and custom tools as well, use --no-tools instead.


JSON mode (--mode json)

JSON mode outputs all events in JSON Lines (JSONL) format, suitable for program parsing and processing.



$ pi --mode json "总结项目结构"

{"type":"session","version":3,"id":"019f8c2e-7a41-7b3d-9e5f-2c6a1d84b0e7","timestamp":"2026-08-31T02-40-11-000Z","cwd":"/Users/runoob/projects/runoob-demo"}

{"type":"agent_start"}

{"type":"turn_start"}

{"type":"message_start","message":{...}}

{"type":"message_update","usage":{...},"assistantMessageEvent":{...}}

{"type":"message_end","message":{...}}

{"type":"turn_end","message":{...},"toolResults":[]}

{"type":"agent_end","messages":[...]}

The first line is a session metadata header recording the format version, session ID, timestamp, and working directory.

Each subsequent line is an independent JSON event containing the event type and corresponding data.

This mode suits the following scenarios:

Scenario Description
Data processing pipelines Feed the event stream into post-processing scripts, parsing and processing line by line
Behavior monitoring and analysis Record and analyze every tool call and decision process of the AI
Custom toolchains Build your own automated workflows on top of the event stream

RPC mode (--mode rpc)

RPC mode implements inter-process communication via a JSONL protocol over stdin/stdout.

It allows another program to send commands via standard input and read responses from standard output.

RPC mode supports an extended UI protocol, enabling remote clients to present interactive elements such as confirm dialogs and selection lists.

This mode suits the following scenarios:

Scenario Description
Editor plugins Integrate Pi Agent in editor extensions such as VS Code
Custom frontends Build your own Pi Agent interface based on the RPC protocol
Background services Embed Pi Agent as a subprocess in a larger system

RPC mode and JSON mode are advanced features for developers.

As a beginner, you most likely only need interactive mode and Print mode.

When you need to integrate Pi Agent into a toolchain, you can come back and study these two modes in depth.


Mode comparison

The differences among the three non-interactive modes lie mainly in interactivity and output format; choose according to the table below.

Input/output comparison of the four runtime modes of Pi Agent: Interactive, Print, JSON, and RPC

Mode Command Interactivity Output formats Applicable scenarios
Interactive mode pi (default) Real-time chat Terminal TUI rendering Daily development, interactive programming
Print mode pi -p One-off answer Plain text Script integration, quick queries
JSON mode pi --mode json Event stream (non-interactive) JSONL event stream Program parsing, pipeline processing
RPC mode pi --mode rpc Continuous communication JSONL bidirectional protocol Editor integration, inter-process communication

CLI argument quick reference

The table below lists common pi command arguments by purpose; they can be combined with commands in interactive mode.

Classification Parameters Description
Model options --provider name Specify AI provider
Model options --model pattern Specify the model ID; supports provider/id and :thinking formats
Model options --api-key key Pass the API Key directly (highest priority)
Model options --thinking level Reasoning level: off/minimal/low/medium/high/xhigh/max
Model options --models patterns Comma-separated model list, limiting the Ctrl+P cycle range
Session options -c, --continue Continue the most recent session
Session options -r, --resume Browse and select a history session
Session options --session path|id Use the specified session file or UUID
Session options --fork path|id Fork a new session from a specified session
Session options --name, -n name Set session display name
Session options --no-session Don't record the session
Session options --export [Output] Export the session as HTML (output path can be omitted)
Tool options --tools, -t list Whitelist specific tools
Tool options --exclude-tools, -xt list Disable specific tools
Tool options --no-builtin-tools, -nbt Disable all built-in tools
Tool options --no-tools, -nt Disable all tools (pure chat mode)
Resource options -e, --extension source Load an extension (can be repeated)
Resource options --skill path Load a Skill (can be repeated)
Resource options --theme path Load a theme (can be repeated)
Other Options -a, --approve Trust project local files for this run
Other Options --no-context-files, -nc Disable automatic loading of AGENTS.md/CLAUDE.md