claude-agent-sdk-go / examples
CLAUDE.md176 starsChanged 7 months agoMITView on GitHub
Working examples demonstrating SDK usage patterns. Examples are numbered by complexity (01-20) from beginner to advanced, covering Query API, Client API, tools, MCP integration, and production patterns.
- Installs packages
In this repositoryOne other file it gives its agents.
- CLAUDE.mdCLAUDE.md
The file
We split this CLAUDE.md into the rules it gives an agent
6 rules. About 0 are common, 0 are distinctive, and 6 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈0
Distinctive · estimate
This author's own choices. Worth a look.
6
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 44
All examples have a main.go with runnable code
- Very specific: line 45
Run with go run main.go from the example directory
- Very specific: line 47
Use WithClient + client.Query + client.ReceiveMessages(ctx) as the standard streaming pattern
- Very specific: line 48
Use WithHook(eventName, toolFilter, callback) for hook events without convenience helpers (PostToolUseFailure, Notification, SubagentStart, …
- Very specific: line 49
Use a local ptrTo[T any] helper (func ptrToT any *T { return &v }) for constructing pointer fields.
- Very specific: line 50
Use toolUseID *string parameter for correlating pre/post hook calls (e.g., timing via a map keyed by tool use ID).
≈ “Common” is an estimate: at least 1 in 5 of the 12,729 CLAUDE.md files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Module: examples
<!-- AUTO-MANAGED: module-description -->Purpose
Working examples demonstrating SDK usage patterns. Examples are numbered by complexity (01-20) from beginner to advanced, covering Query API, Client API, tools, MCP integration, and production patterns.
<!-- END AUTO-MANAGED --><!-- AUTO-MANAGED: architecture -->Module Architecture
examples/
├── 01_quickstart/ # Basic Query API usage
├── 02_client_streaming/ # Real-time streaming responses
├── 03_client_multi_turn/ # Multi-turn conversations
├── 04_query_with_tools/ # File operations with Query API
├── 05_client_with_tools/ # Interactive file workflows; tool_use_result metadata, SetPermissionMode
├── 06_query_with_mcp/ # MCP server integration (Query)
├── 07_client_with_mcp/ # MCP server integration (Client)
├── 08_client_advanced/ # Error handling, model switching
├── 09_context_manager/ # WithClient pattern
├── 10_session_management/ # Session isolation
├── 11_permission_callback/ # Tool permission control
├── 12_hooks/ # Lifecycle hooks; PreToolUse logging, command blocking, PostToolUse context injection, PostToolUseFailure recovery via WithHook(), Notification observation via WithHook()
├── 13_file_checkpointing/ # File rewind capabilities
├── 14_sdk_mcp_server/ # In-process custom tools; 3 sub-examples: Calculator, Text Processor, Annotated Tool (circle_area with ReadOnlyHint/IdempotentHint/OpenWorldHint via WithToolAnnotations)
├── 15_programmatic_subagents/ # Agent definitions
├── 16_structured_output/ # JSON schema constraints
├── 17_plugins/ # Plugin configuration
├── 18_sandbox_security/ # Command isolation
├── 19_partial_streaming/ # Real-time delta updates
├── 20_debugging_and_diagnostics/ # Debug output, health monitoring
└── README.md # Example documentation<!-- END AUTO-MANAGED --><!-- AUTO-MANAGED: conventions -->Module-Specific Conventions
- Each example is self-contained in its own directory
- All examples have a
main.gowith runnable code - Run with
go run main.gofrom the example directory - Prerequisites noted in README.md (e.g., MCP servers need
uvx) - Use
WithClient+client.Query+client.ReceiveMessages(ctx)as the standard streaming pattern - Use
WithHook(eventName, toolFilter, callback)for hook events without convenience helpers (PostToolUseFailure, Notification, SubagentStart, PermissionRequest);WithPreToolUseHook/WithPostToolUseHookare the only convenience helpers - Use a local
ptrTo[T any]helper (func ptrTo[T any](v T) *T { return &v }) for constructing pointer fields; defined in examples 12 (forAdditionalContext *string) and 14 (forToolAnnotationspointer fields likeReadOnlyHint *bool) - Use
toolUseID *stringparameter for correlating pre/post hook calls (e.g., timing via a map keyed by tool use ID); always guard withif toolUseID == nilbefore dereferencing
<!-- END AUTO-MANAGED --><!-- AUTO-MANAGED: dependencies -->Key Dependencies
- Root
claudecodepackage - Claude CLI installed (
npm install -g @anthropic-ai/claude-code) - Go 1.18+
- Optional:
uvxfor MCP server examples
<!-- END AUTO-MANAGED --><!-- MANUAL -->Notes
<!-- END MANUAL -->Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

