AgentServer, and it exposes three lifecycle hooks:
@server.on_create@server.on_event@server.on_cancel
on_complete hook.
Start With tu init
Minimal Agent
The Handler Model
on_create
Runs once when the task is created. Use it for:- initializing state
- validating params
- sending the first message
on_event
Runs every time your app sends an event to the task. This is where most agent logic lives.on_cancel
Runs when the task is cancelled. Use it for cleanup or last-message behavior if needed. Important runtime detail:on_cancel does not run with the normal automatic filesystem sync wrapper. Do not rely on /workspace writes made in on_cancel being persisted automatically.
TaskContext
Every handler receives aTaskContext.
| Attribute | Purpose |
|---|---|
ctx.task | Current task metadata |
ctx.agent | Current agent metadata |
ctx.messages | Send or list task messages |
ctx.state | Read and update per-task state |
ctx.events | Send or list events |
TaskContext is the default path. The lower-level adk.* modules are still useful in helper code when you need to pass IDs explicitly.
Messages
The most common thing you do inside a handler is emit messages:State
State is persisted per task and agent.- conversation continuity
- IDs or handles you need on future turns
- lightweight workflow progress
Filesystem Access
If the task has an attached filesystem, your agent sees it at/workspace.
TerminalUse syncs that filesystem around on_create and on_event. If the caller only has read access to the filesystem, /workspace is mounted read-only for that task.
on_cancel is the exception: the runtime skips the automatic sync wrapper for cancel handlers.
See Filesystem Sync and Access Control.