Overview
The Pi bridge wraps thepi CLI (from the pi-mono project), a minimal terminal coding harness that is aggressively extensible. Pi supports multi-provider model selection, thinking levels, TypeScript extensions, skills, prompt templates, and fine-grained JSONL event streaming. It provides both token usage and cost data.
The bridge is implemented by PiBridge and configured through PiBridgeBuilder. Access the builder through the AgentCtrl facade:
Prerequisites
Install Pi globally via npm or bun:Basic Usage
Model Selection
Pi supports flexible model identification with optional provider prefix and thinking level shorthand:withProvider() to explicitly set the provider when the model name alone is ambiguous:
Thinking Levels
Pi supports six thinking levels that control how much the model reasons before responding:->withModel('sonnet:high').
System Prompts
Replace or extend the default system prompt:Tool Control
By default, Pi provides four tools:read, write, edit, and bash. Additional built-in tools include grep, find, and ls.
File Arguments
Attach files to the prompt usingwithFiles(). These are passed as @-prefixed arguments to Pi:
Extensions and Skills
Pi supports TypeScript extensions and skills that add custom tools, commands, and capabilities:Streaming with Pi
Pi streams output as JSONL with granular event types. The bridge normalizes these into the standard callback API:Event Normalization
Pi emits a rich set of JSONL events that are normalized:MessageUpdateEvent(text_delta) — Text deltas delivered throughonText().ToolExecutionEndEvent— Tool results delivered throughonToolUse()with tool name, call ID, result, and error flag.ErrorEvent— Errors delivered throughonError().SessionEvent,AgentStart/End,TurnStart/End,MessageStart/End,ToolExecutionStart— Lifecycle events available through thewiretap()event system.
Session Management
Pi maintains sessions as JSONL files. Agent-Ctrl extracts session IDs from the session header event:API Key Override
Override the API key for a specific execution without changing environment variables:Usage and Cost Data
Pi provides token usage and cost data from the message events:Data Availability
| Data Point | Available | Notes |
|---|---|---|
| Text output | Yes | Extracted from message_update text_delta events |
| Tool calls | Yes | Normalized from tool_execution_end with call IDs and error status |
| Session ID | Yes | Extracted from JSONL session header event |
| Token usage | Yes | Input, output, cache read, cache write tokens |
| Cost | Yes | Cost in USD from usage data |
| Parse diagnostics | Yes | Malformed JSON line counts and samples |
Complete Example
Comparison with Other Bridges
| Feature | Claude Code | Codex | OpenCode | Pi |
|---|---|---|---|---|
| System prompts | Yes (replace + append) | No | No | Yes (replace + append) |
| Permission modes | Yes (4 levels) | No | No | No |
| Turn limits | Yes | No | No | No |
| Sandbox modes | No | Yes (3 levels) | No | No |
| Image input | No | Yes | No | No |
| Thinking levels | No | No | No | Yes (6 levels) |
| Named agents | No | No | Yes | No |
| File attachments | No | No | Yes | Yes (@-prefix) |
| Extensions | No | No | No | Yes (TypeScript) |
| Skills | No | No | No | Yes |
| Tool control | No | No | No | Yes (select/disable) |
| Session sharing | No | No | Yes | No |
| Session titles | No | No | Yes | No |
| Ephemeral mode | No | No | No | Yes |
| API key override | No | No | No | Yes |
| Token usage | No | Yes (partial) | Yes (full) | Yes |
| Cost tracking | No | No | Yes | Yes |
| Multi-provider models | No | No | Yes | Yes |
Environment Variables
| Variable | Description |
|---|---|
ANTHROPIC_API_KEY | Anthropic API key |
OPENAI_API_KEY | OpenAI API key |
PI_CODING_AGENT_DIR | Override Pi config directory (default: ~/.pi/agent) |
PI_SKIP_VERSION_CHECK | Skip version check at startup |
PI_CACHE_RETENTION | Set to long for extended prompt cache |