Skip to main content

Overview

Tools allow agents to execute functions and interact with external systems. The AgentTool interface extends the base Tool interface from @mariozechner/pi-ai with execution capabilities and UI metadata.

AgentTool Interface

Properties

string
required
A human-readable label for the tool to be displayed in UI.
string
required
The function name used by the LLM to invoke the tool. Must be unique within the agent’s tool set.
string
required
Description of what the tool does. Used by the LLM to decide when to call the tool.
TSchema
required
TypeBox schema defining the tool’s input parameters. Must be a TypeBox object schema.
(toolCallId: string, params: Static<TParameters>, signal?: AbortSignal, onUpdate?: AgentToolUpdateCallback<TDetails>) => Promise<AgentToolResult<TDetails>>
required
Function that executes the tool logic.Parameters:
  • toolCallId: Unique identifier for this tool call
  • params: Validated parameters matching the schema
  • signal: Optional AbortSignal for cancellation
  • onUpdate: Optional callback for streaming partial results
Returns: An AgentToolResult containing content blocks and details.

AgentToolResult

(TextContent | ImageContent)[]
required
Content blocks that will be sent back to the LLM. Supports text and images.
T
required
Arbitrary data to be displayed in UI or logged. Not sent to the LLM.

Creating Tools

Simple Tool

Tool with Detailed Output

Tool with Streaming Updates

Tool with Cancellation Support

Error Handling

Tools can throw errors which will be caught by the agent loop and sent back to the LLM:
When a tool throws an error:
  1. The agent loop catches it
  2. Creates a tool result message with isError: true
  3. Sends the error message back to the LLM
  4. The LLM can decide how to handle it (retry, give up, etc.)

Registering Tools

Tools are registered with the agent via the tools property:

Tool Execution Events

The agent emits events during tool execution that can be used for UI updates:

Best Practices

1. Clear Descriptions

Write clear, concise descriptions that help the LLM understand when to use the tool:

2. Descriptive Parameters

Add descriptions to all parameters:

3. Meaningful Labels

Use human-friendly labels for UI display:

4. Structured Details

Use the details field for structured data that UIs can render:

5. Handle Cancellation

Respect the AbortSignal for long-running operations:

6. Stream Progress

Use onUpdate for operations that take time: