Skip to main content
This catalog provides runnable code examples for every major AFK capability. Each example is self-contained and demonstrates a specific pattern — from the simplest possible agent run to advanced tool security configurations. The examples are ordered by complexity. If you are new to AFK, start at the top and work your way down. Each example builds on concepts introduced in the previous ones.

Which example should I start with?

  • First time using AFK? Start with Minimal Chat Agent. It shows the absolute simplest agent run in under 10 lines of code.
  • Need to gate dangerous actions? Go to Policy + HITL. It demonstrates how policy engines and human approval work together.
  • Building a multi-agent system? See Subagent Router. It shows how to delegate work to specialist subagents and merge results.
  • Need to persist and resume runs? Check Resume + Compact. It covers checkpoint-based resumption and memory compaction.
  • Using LLMs without the agent loop? See Structured Output. It shows how to use LLMBuilder directly with Pydantic models.
  • Locking down tool capabilities? Go to Tool Security. It demonstrates scoped tool registration and policy gates.

Complexity progression

LevelExampleWhat You Learn
BeginnerMinimal Chat AgentAgent + Runner basics, final_text access
BeginnerStructured OutputDirect LLM usage, Pydantic schema validation
IntermediatePolicy + HITLPolicy engine, approval flows, interaction modes
IntermediateTool SecurityTool scoping, sandbox profiles, policy gates
IntermediateHooks + MiddlewarePre/post tool execution hooks, middleware chains
IntermediateRuntime ToolsBuilt-in file, search, and command tools
IntermediatePrompt LoaderExternal prompt files, template variables
AdvancedSubagent RouterMulti-agent coordination, delegation patterns
AdvancedResume + CompactCheckpointing, memory management, long-running workflows
IntermediateStreaming + MemoryReal-time streaming with multi-turn thread persistence
IntermediateCost MonitoringBudget limits, real-time cost tracking, batch budgets
AdvancedMCP ClientConnect to external MCP servers, hybrid local+remote tools
AdvancedMulti-Model FallbackFallback chains, circuit breakers, model tier strategies

Scenario catalog

Minimal Chat Agent

The simplest possible AFK agent. Define an agent, run it synchronously, and read the output. Start here.

Policy + HITL

Gate sensitive actions through a policy engine and route approval requests to a human operator.

Subagent Router

Delegate workload to specialist subagents using a coordinator pattern. Merge outputs into a unified response.

Resume + Compact

Checkpoint a run, resume it later from the last known state, and compact memory to control storage growth.

Structured Output

Use the LLM builder directly (without the agent loop) to get schema-validated structured responses via Pydantic.

Tool Security

Register destructive tools with tight scoping, enforce sandbox profiles, and gate actions through policy.

Hooks + Middleware

Attach pre-execution and post-execution hooks to tools. Chain middleware for logging, validation, and transformation.

Runtime Tools

Use AFK’s built-in file reading, directory listing, and command execution tools with agents.

Prompt Loader

Load system prompts from external files with template variable substitution and automatic caching.

Streaming + Memory

Combine real-time streaming with thread-based memory for multi-turn chat UIs.

Cost Monitoring

Track and control agent costs using FailSafeConfig budgets and telemetry events.

MCP Client

Connect to external MCP servers, discover tools, and combine with local tools.

Multi-Model Fallback

Configure fallback model chains for LLM resilience, circuit breakers, and cost optimization.