Prerequisites: Python 3.10+ and an LLM API key. Set
OPENAI_API_KEY in
your environment, or configure any provider via
LiteLLM.Setup
Step 1 — Minimal agent
The simplest possible agent: a model, a name, and instructions.What just happened?
What just happened?
Agent(...)is a configuration object — it describes what the agent is, not how it runs. -Runner()is the execution engine — it manages the LLM call loop and state. -run_sync()blocks until the run finishes. Under the hood, it creates an async loop, sends the user message to the LLM, and returns anAgentResult. -result.final_textalways contains the model’s final response.
Step 2 — Add a tool
Give your agent a capability by defining a typed tool function.How tool calling works
How tool calling works
- The runner sends the tool’s JSON schema to the LLM along with the user message.
- The LLM decides to call the tool and returns a
ToolCallwith the function name and arguments. - AFK validates the arguments against the Pydantic model, executes the function, sanitizes the output, and feeds the result back to the LLM.
- The LLM uses the tool result to compose its final answer.
Step 3 — Stream the response
For real-time UIs, use streaming to receive text as it’s generated.- Async streaming
- Sync (non-streaming)
Stream event types
Stream event types
| Event | When it fires | Key field | | --- | --- | --- | |
text_delta |
Incremental text from the model | event.text_delta | | step_started | New
step in the agent loop | event.step | | tool_started | A tool is about to
execute | event.tool_name | | tool_completed | A tool finished executing |
event.success | | error | Something went wrong | event.error | |
completed | Run finished | event.result |Step 4 — Delegate to subagents
Build a multi-agent system where a coordinator delegates to specialists.How delegation works
How delegation works
- The coordinator sees its subagents as available tools (automatically
generated
transfer_to_reviewer,transfer_to_writertools). - When the coordinator decides to delegate, AFK routes the task to the subagent, runs it through a full agent loop, and returns the result to the coordinator. - Each subagent uses its own model and instructions, and has access to its own tools. - The coordinator can delegate to multiple subagents and combine their results.
Step 5 — Add safety limits
Protect against runaway agents withFailSafeConfig.
What you just built
In five steps, you’ve covered the core AFK workflow:| Step | Concept | What you learned |
|---|---|---|
| 1 | Agent + Runner | Define an agent and execute it |
| 2 | Tools | Give agents typed capabilities |
| 3 | Streaming | Real-time text output for UIs |
| 4 | Multi-agent | Coordinate specialist subagents |
| 5 | Safety | Protect against runaway agents |
Next steps
Learn in 15 Minutes
Deep-dive tutorial covering memory, policies, observability, and more.
Building with AI
Production playbook: common patterns, anti-patterns, and deployment
guidance.
Examples
Runnable examples for every major AFK feature.
Core Concepts
Deep dive into agents, tools, runner, memory, and more.