Overview
Tools are LLM-callable functions that extend the agent’s capabilities. Pi includes built-in tools for file operations and command execution, and supports custom tools through the extension system.Built-in Tools
Pi provides six core tools for working with codebases:read
Read file contents with automatic truncation:- Automatic truncation (2000 lines or 50KB)
- Line-numbered output (
cat -nformat) - Binary file detection
- Full output saved to temp file if truncated
packages/coding-agent/src/core/tools/read.ts
bash
Execute shell commands:- Streams stdout/stderr combined
- Automatic truncation (last 2000 lines or 50KB)
- Full output saved to temp file if truncated
- Process tree cleanup on abort
- Custom shell configuration support
packages/coding-agent/src/core/tools/bash.ts
edit
Edit files using exact string replacement:- Validates that
oldStringexists exactly once (unlessreplaceAll) - Preserves file encoding and line endings
- Returns diff for verification
- Fails fast if
oldStringnot found or ambiguous
packages/coding-agent/src/core/tools/edit.ts
write
Create or overwrite files:- Creates parent directories automatically
- Overwrites existing files
- Returns confirmation with file size
packages/coding-agent/src/core/tools/write.ts
grep
Search file contents using regex:- Regex search across files
- Glob pattern filtering
- Sorted by modification time
- Line number display
packages/coding-agent/src/core/tools/grep.ts
find
Find files by name pattern:- Fast glob-based search
- Sorted by modification time
- Respects .gitignore by default
packages/coding-agent/src/core/tools/find.ts
Tool Interface
All tools implement theAgentTool interface:
packages/agent/src/types.ts:146
Creating Custom Tools
Basic Example
Create a simple tool using the extension API:Streaming Updates
Tools can stream partial results during execution:Abort Handling
Respond to user interrupts using the abort signal:Custom Tool Rendering
Tools can provide custom UI rendering for their calls and results:Tool Operations
Advanced tools can use custom operations for delegation:Bash Operations
Override how bash commands execute:packages/coding-agent/src/core/tools/bash.ts:33
Tool Hooks
Extensions can intercept and modify tool execution:Block Tool Calls
Modify Tool Results
Tool Selection
Control which tools are active:Best Practices
Use TypeBox for parameter validation
Use TypeBox for parameter validation
TypeBox provides runtime validation and automatic type inference:
Stream progress for long operations
Stream progress for long operations
Use
onUpdate to provide feedback during execution:Handle abort signals
Handle abort signals
Always check
signal.aborted in loops and clean up resources:Return structured details
Return structured details
The
details field is for UI/logging, not the LLM. Use it for rich information:Next Steps
Extensions
Learn about the extension system
Architecture
Understand the system architecture