Skip to main content

Overview

The Agent class provides a high-level interface for creating AI agents that can:
  • Stream responses from LLM providers
  • Execute tools and handle multi-turn conversations
  • Support steering and follow-up messages during execution
  • Manage conversation state and event subscriptions

Constructor

AgentOptions

Partial<AgentState>
Initial state for the agent including system prompt, model, thinking level, tools, and messages.
(messages: AgentMessage[]) => Message[] | Promise<Message[]>
Converts AgentMessage[] to LLM-compatible Message[] before each LLM call. Default filters to user/assistant/toolResult messages.
(messages: AgentMessage[], signal?: AbortSignal) => Promise<AgentMessage[]>
Optional transform applied to context before convertToLlm. Use for context pruning, injecting external context, etc.
'all' | 'one-at-a-time'
default:"one-at-a-time"
Controls how steering messages are delivered:
  • all: Send all steering messages at once
  • one-at-a-time: Send one steering message per turn
'all' | 'one-at-a-time'
default:"one-at-a-time"
Controls how follow-up messages are delivered:
  • all: Send all follow-up messages at once
  • one-at-a-time: Send one follow-up message per turn
StreamFn
Custom stream function for proxy backends or custom LLM routing. Default uses streamSimple from @mariozechner/pi-ai.
string
Optional session identifier forwarded to LLM providers. Used by providers that support session-based caching (e.g., OpenAI Codex).
(provider: string) => Promise<string | undefined> | string | undefined
Resolves an API key dynamically for each LLM call. Useful for expiring tokens (e.g., GitHub Copilot OAuth).
ThinkingBudgets
Custom token budgets for thinking levels (token-based providers only).
Transport
default:"sse"
Preferred transport for providers that support multiple transports ('sse' or 'responses').
number
default:"60000"
Maximum delay in milliseconds to wait for a retry when the server requests a long wait. If the server’s requested delay exceeds this value, the request fails immediately. Set to 0 to disable the cap.

Properties

state

Returns the current agent state containing:
string
The system prompt used for LLM calls
Model<any>
The LLM model to use for generation
ThinkingLevel
Thinking/reasoning level: 'off' | 'minimal' | 'low' | 'medium' | 'high' | 'xhigh'
AgentTool<any>[]
Available tools the agent can execute
AgentMessage[]
Conversation history including user, assistant, and tool result messages
boolean
Whether the agent is currently streaming a response
AgentMessage | null
The partial message being streamed (null when not streaming)
Set<string>
Set of tool call IDs currently being executed
string | undefined
Error message from the last failed operation

sessionId

Get or set the session ID used for provider caching. Call this when switching sessions (new session, branch, resume).

thinkingBudgets

Get or set custom thinking budgets for token-based providers.

transport

Get the current preferred transport ('sse' or 'responses').

maxRetryDelayMs

Get or set the maximum delay to wait for server-requested retries. Set to 0 to disable the cap.

Methods

subscribe

Subscribe to agent events. Returns an unsubscribe function.

prompt

Send a prompt to the agent and stream the response. Supports text, images, or custom AgentMessage objects.
Throws an error if the agent is already streaming. Use steer() or followUp() to queue messages during execution.

continue

Continue from current context (used for retries and resuming queued messages). The last message in context must be a user or toolResult message.

steer

Queue a steering message to interrupt the agent mid-run. Delivered after current tool execution, skips remaining tools.

followUp

Queue a follow-up message to be processed after the agent finishes. Delivered only when agent has no more tool calls or steering messages.

abort

Abort the current streaming operation.

waitForIdle

Wait for the agent to finish the current operation.

reset

Reset the agent to initial state. Clears messages, streaming state, pending tool calls, errors, and message queues.

State Mutators

Update agent configuration and state.

hasQueuedMessages

Check if there are any steering or follow-up messages in the queue.

Complete Example

Advanced: Proxy Stream Function

For applications that need to route LLM calls through a backend server:
See the Proxy Stream Function documentation for details.