Examples

August 26, 2026 · View on GitHub

Runnable examples live in examples/. Each is a standalone main package:

export OPENAI_API_KEY=sk-...
go run ./examples/hello
ExampleShows
examples/helloThe minimal agent: provider, instructions, one Run
examples/toolsTyped function tools via NewTool
examples/toolimageA tool returning image content (ToolOutputImage)
examples/handoffsA triage agent delegating to specialists with HandoffTo
examples/multiagentAgent-as-tool orchestration: a manager keeps control while specialists run as nested runs
examples/guardrailsOne Guardrail across three stages, plus a Blocking gate that trips before any tokens are spent
examples/streamingRanging a RunStream
examples/hitlHuman-in-the-loop: interrupt, approve/reject, resume
examples/errorhandlersRunOptions.Exec.ErrorHandlers: fallback final outputs for max-turns and invalid structured output
examples/loggingLogConfig: the SDK's own logging, and the separate SensitiveData switch
examples/testingA scripted Model: test an agent with no API key — the tools run for real
examples/tracingThe tracing pipeline: tracer → batch processor → console exporter, plus TraceGroupID/TraceMetadata
examples/fallbackRetry + fallback model decorators, with WithShouldFallback classification
examples/anthropicStreaming an agent on Claude via the Anthropic Messages provider — tool loop plus token deltas (separate module)
examples/compactionopenai.CompactionSession: server-side history compaction via responses.compact
examples/toolstreamToolContext.Emit: a running tool's progress on the stream, and why it is not the answer
examples/steeringRunControl's three queues: steer, next-turn, follow-up
examples/branchingA session is a tree: branch from an earlier point without deleting the attempt
examples/projectorEntryProjector: deciding what the model gets to read
examples/tasksBackground sub-agents: spawn, the wake-up debt, and the parent woken with the result
examples/middlewareRun middleware: Retry + Approval policy + evaluator-driven Loop, stacked
examples/planmodePlan mode + todo list: read-only exploration, a submit_plan approval pause, then execution in the same run
examples/runcompactionRun-level compaction: a compaction.Strategy folding tool results mid-run, at the turn boundary
examples/conversationsopenai.ConversationsSession: history stored server-side via the Conversations API
examples/promptBinding an OpenAI stored prompt via Agent.Prompt
examples/sandboxAn agent that writes and runs code in a local sandbox
sandbox/docker/exampleThe Docker sandbox backend (separate module)
sessions/exampleSQLite-backed session persistence (separate module)
skills/exampleWiring SKILL.md skills into an agent (separate module)

Extra setup

Most examples only need OPENAI_API_KEY. The exceptions:

  • examples/prompt — a stored prompt ID: OPENAI_PROMPT_ID=pmpt_... go run ./examples/prompt
  • examples/anthropicANTHROPIC_API_KEY=... go run . (from its directory; separate module)
  • examples/sandbox — the host needs python3
  • examples/testingno key needed: go run ./examples/testing, go test ./examples/testing
  • Examples in the optional submodules run from their module directory:
(cd sessions && go run ./example)        # SQLite-backed session
(cd skills && go run ./example)          # Agent Skills (SKILL.md)
(cd sandbox/docker && go run ./example)  # needs a running Docker daemon

examples/testing is the worked version of Testing your agents: a scripted Model, the agent's real tool, and a test asserting both the answer and that the script was consumed.