Agent class runs the model ↔ tool loop, maintains conversation state, and emits events for streaming UIs.
Constructor
ModelProvider
required
LLM adapter —
createAnthropicProvider() or createOpenAIProvider().Tool[]
required
Tools available to the model (can include
handoff() tools).AgentContext
default:"new AgentContext()"
Optional conversation state — use a branched context for time-travel.
run(userInput, options?)
Runs to completion and returns AgentRunResult.
AgentRunOptions
number
default:"8"
Maximum model turns before stopping.
number
default:"2"
Retries when tool arguments fail Zod validation.
(event: AgentEvent) => void
Fires for every event:
turn_start, text_delta, tool_call, tool_result, rollback_start, run_end, etc.(text: string) => void
Shorthand — called on each
text_delta (token-level when the provider supports streaming).AgentRunResult
string
Plain text extracted from the final assistant message.
Message
Full final assistant
Message object.Message['content']
Alias for
message.content (backward compatible).Message[]
Full message history after the run.
stream(userInput, options?)
Async generator yielding AgentEvent on each step, then returning AgentRunResult.
run(). Internally, run() is implemented as a thin wrapper around stream().
See Streaming for all event types and UI patterns.
spawn(seedMessages?)
Creates a new Agent with the same provider and tools but a fresh context, optionally seeded with messages.
context
Public AgentContext instance — snapshots, branching, message history.
Other methods
ModelProvider
Returns the provider passed to the constructor.
Tool[]
Returns the tool list.