Overview
TheAgent 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 onceone-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 onceone-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
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
thinkingBudgets
transport
'sse' or 'responses').
maxRetryDelayMs
Methods
subscribe
prompt
Throws an error if the agent is already streaming. Use
steer() or followUp() to queue messages during execution.