Learn the SDK

Ten short, focused examples — one capability per example, 60–150 lines each. Start here after Example 01 — hello agent. Read them in dependency order (tools → memory → workflows → safety → directives → routing → app structure), or jump straight to the one that matches what you're building.

You don't have to read all ten. Each is independent.


The ten examples

Tool calling

Example 02 — tool_agent

Define a tool, register it with an agent, watch the agent call it. The simplest demonstration of tool use without any framework scaffolding.

When to read: right after hello-world, before any production use.

Multi-model swap

Example 03 — multi_model

Same agent code, swap Anthropic ↔ OpenAI ↔ local. Demonstrates the SDK's LLM-agnostic interface end-to-end.

When to read: when you're choosing your initial LLM provider, or when you need to confirm the SDK isn't locked to a single vendor.

Memory basics

Example 04 — memory_agent

Agent with memory. Basic vector memory: store and retrieve. The starting point for the deeper memory examples in Memory and retrieval.

When to read: when you need conversation context retained across turns.

Multi-step workflows

Example 05 — workflow

A two-step workflow that chains agent calls: plan, then execute. The smallest workflow surface in the SDK.

When to read: when single-turn agents stop being enough.

Safety filters

Example 06 — guardrails

A guardrail that intercepts unsafe outputs and rejects them. The minimal demonstration of the safety surface.

When to read: before you expose an agent to users.

MCP integration

Example 07 — mcp_tools

An agent calling tools served by an MCP (Anthropic's open standard) server. Third-party MCP-compliant tool servers work out of the box.

When to read: when integrating third-party MCP-compliant tool servers.

Directives

Prompt directives that expand at resolution time: @datetime, @context, @memory, /tool.name.

Example 08 — directives

Directives let the same agent code work well on Opus and on local llama3. They resolve inline in the prompt before the LLM sees the message, so a cheap model gets the same grounded context a frontier model would.

When to read: when you want consistent agent quality across different LLM tiers.

Model routing

Example 13 — model_routing

Routing rules: pick a different model per task class — cheap for simple, frontier for complex. The primary cost-reduction primitive in the SDK.

When to read: when you want a tier router in front of your agent.

Local LLM routing

Example 18 — local_llm_routing

The same code running on Ollama and LM Studio. Confirms the LLM-agnostic claim works in practice with locally-hosted models.

When to read: when you're moving off paid LLM providers.

App-factory pattern

Example 27 — app_factory

Production project structure: factory function, dependency injection, structured config. The shape a real application should grow into once it outgrows a single file.

When to read: when your example has outgrown a single file.


See also

  • Tutorials — once you've covered the basics you need, the tutorials show production-grade, end-to-end compositions.
  • Production patterns — longer reference examples for harder problems: orchestration shapes, multi-agent topologies, approval gates.
  • Platform — capability deep-dives for each major area of the SDK.
  • Reference — examples — the full numbered example list.