Agent is a stateful wrapper around model calls.
Use an agent when your app needs identity, message history, tools, persistent state, or server routes.
What An Agent Owns
An agent owns:- an ID
- a model
- a system prompt
- tools
- message history
- state
- storage
- emitted events
Minimal Agent
async with block starts the agent, loads state, and stops background processing when done.
Conversation Memory
The agent stores messages between calls.Persistence
UseSQLiteStorage for local durable history.
agent.stateagent.messages- event logs when
enable_event_log = True
Agent State
State is app-owned data attached to the agent.Typed State
You can define state shape on a subclass.Tools On Agents
Tools work the same way as core generation, but agent history persists tool calls and tool results.Three Ways To Run An Agent
chat
Usechat() when you need final text.
run
Userun() when you need a structured result.
turn
Useturn() when you need events, cancellation, or steering.
Events
Agents can emit application events.Serving Agents
UseAgentServer to expose agents over HTTP, SSE, WebSocket, and RPC.
When To Use Agents
Use agents for:- user sessions
- persistent chatbots
- coding agents
- research agents
- tool-using assistants
- apps that need event streams or replay
- multi-agent systems
generate_text() or stream_text() instead.