Agent is the stateful orchestration class in ai-query.
Use it when you need model configuration, tools, persistent state, message history, events, or transport/RPC behavior attached to a stable agent ID.
Import
Constructor
Parameters
str
required
Stable agent identifier. Used for storage keys, routing, and RPC.
LanguageModel | None
Default model for
chat(), stream(), run(), and turn().str
Default system prompt.
ToolSet | None
Default tools available to model calls.
Storage | None
Storage backend. Defaults to
MemoryStorage().StateT | None
State used when storage has no saved state for this agent.
StopCondition | list[StopCondition] | None
Default stop condition for tool loops.
ProviderOptions | None
Default provider-specific options.
ReasoningConfig | None
Default reasoning controls.
RetryPolicy | None
Default provider-call retry policy for
chat(), stream(), run(), and turn().OnReasoningEvent | None
Callback for provider reasoning events. Agent also emits these as
reasoning events.AgentHooks | None
Default runtime hooks for step control, tool policy, tool result overrides, and reasoning observation.
AgentTransport | None
Transport used for remote agent calls.
EventBus | None
Optional event bus used by server/transport layers.
Class Attributes
bool
When
True, emitted events are persisted for replay.dict | BaseModel
Optional subclass blueprint for initial or typed state.
Properties
str
Agent ID.
Storage
Storage backend.
StateT
Current loaded state. Raises
RuntimeError before start().list[Message]
Current message history.
start
stop
on_stop().
Context Manager
await agent.start() followed by await agent.stop().
chat
Exception
Raised if
self.model is not configured.Exception
Raised if
signal aborts.stream
chat().
run
AgentTurn and returns its structured result.
Use this when you need steps, usage, finish_reason, or output_message.
turn
events(), text_stream(), or result() is consumed.
Raises:
Exception
Raised if
self.model is not configured.set_state
state event.
update_state
state event.
For Pydantic state, uses model_copy(update=kwargs) when available.
clear
emit
str
required
Event type to deliver to connected clients.
dict[str, Any]
required
Event payload.
bool
When
True, the event is stored in the replay log and may be returned by replay_events(). When False, the event is delivered live to connected clients but omitted from replay persistence.int
Monotonic event ID for this agent instance.
enable_event_log = True, events are persisted to storage.
Notes:
- Event IDs remain monotonic even for
replay=Falseevents. - Replay streams may therefore contain gaps in event IDs.
- Use
replay=Falsefor ephemeral UI commands such asopen_documentthat should not re-run on reconnect.
replay_events
id > after_id from the in-memory/persisted event log.
clear_event_log
handle_request
chatactioninvokestate
handle_request_stream
call
handle_invoke
- registered
@actionmethod - legacy
on_call_{method}handler - error response
serve
AgentServer for this agent class.