$npx -y skills add jackspace/ClaudeSkillz --skill claude-agent-sdkThis skill provides comprehensive knowledge for working with the Anthropic Claude Agent SDK. It should be used when building autonomous AI agents, creating multi-step reasoning workflows, orchestrating specialized subagents, integrating custom tools and MCP servers, or implementi
| 1 | # Claude Agent SDK |
| 2 | |
| 3 | **Status**: Production Ready |
| 4 | **Last Updated**: 2025-10-25 |
| 5 | **Dependencies**: @anthropic-ai/claude-agent-sdk, zod |
| 6 | **Latest Versions**: @anthropic-ai/claude-agent-sdk@0.1.0+, zod@3.23.0+ |
| 7 | |
| 8 | --- |
| 9 | |
| 10 | ## Quick Start (5 Minutes) |
| 11 | |
| 12 | ### 1. Install SDK |
| 13 | |
| 14 | ```bash |
| 15 | npm install @anthropic-ai/claude-agent-sdk zod |
| 16 | ``` |
| 17 | |
| 18 | **Why these packages:** |
| 19 | - `@anthropic-ai/claude-agent-sdk` - Main Agent SDK |
| 20 | - `zod` - Type-safe schema validation for tools |
| 21 | |
| 22 | ### 2. Set API Key |
| 23 | |
| 24 | ```bash |
| 25 | export ANTHROPIC_API_KEY="sk-ant-..." |
| 26 | ``` |
| 27 | |
| 28 | **CRITICAL:** |
| 29 | - API key required for all agent operations |
| 30 | - Never commit API keys to version control |
| 31 | - Use environment variables |
| 32 | |
| 33 | ### 3. Basic Query |
| 34 | |
| 35 | ```typescript |
| 36 | import { query } from "@anthropic-ai/claude-agent-sdk"; |
| 37 | |
| 38 | const response = query({ |
| 39 | prompt: "Analyze the codebase and suggest improvements", |
| 40 | options: { |
| 41 | model: "claude-sonnet-4-5", |
| 42 | workingDirectory: process.cwd(), |
| 43 | allowedTools: ["Read", "Grep", "Glob"] |
| 44 | } |
| 45 | }); |
| 46 | |
| 47 | for await (const message of response) { |
| 48 | if (message.type === 'assistant') { |
| 49 | console.log(message.content); |
| 50 | } |
| 51 | } |
| 52 | ``` |
| 53 | |
| 54 | --- |
| 55 | |
| 56 | ## The Complete Claude Agent SDK Reference |
| 57 | |
| 58 | ## Table of Contents |
| 59 | |
| 60 | 1. [Core Query API](#core-query-api) |
| 61 | 2. [Tool Integration](#tool-integration-built-in--custom) |
| 62 | 3. [MCP Servers](#mcp-servers-model-context-protocol) |
| 63 | 4. [Subagent Orchestration](#subagent-orchestration) |
| 64 | 5. [Session Management](#session-management) |
| 65 | 6. [Permission Control](#permission-control) |
| 66 | 7. [Filesystem Settings](#filesystem-settings) |
| 67 | 8. [Message Types & Streaming](#message-types--streaming) |
| 68 | 9. [Error Handling](#error-handling) |
| 69 | 10. [Known Issues](#known-issues-prevention) |
| 70 | |
| 71 | --- |
| 72 | |
| 73 | ## Core Query API |
| 74 | |
| 75 | ### The `query()` Function |
| 76 | |
| 77 | The primary interface for interacting with Claude Code CLI programmatically. |
| 78 | |
| 79 | ```typescript |
| 80 | import { query } from "@anthropic-ai/claude-agent-sdk"; |
| 81 | |
| 82 | const response = query({ |
| 83 | prompt: string | AsyncIterable<SDKUserMessage>, |
| 84 | options?: Options |
| 85 | }); |
| 86 | |
| 87 | // Response is AsyncGenerator<SDKMessage, void> |
| 88 | for await (const message of response) { |
| 89 | // Process streaming messages |
| 90 | } |
| 91 | ``` |
| 92 | |
| 93 | ### Basic Options |
| 94 | |
| 95 | ```typescript |
| 96 | const response = query({ |
| 97 | prompt: "Review this code for bugs", |
| 98 | options: { |
| 99 | model: "claude-sonnet-4-5", // or "haiku", "opus" |
| 100 | workingDirectory: "/path/to/project", |
| 101 | systemPrompt: "You are a security-focused code reviewer.", |
| 102 | allowedTools: ["Read", "Grep", "Glob"], |
| 103 | disallowedTools: ["Write", "Edit", "Bash"], |
| 104 | permissionMode: "default" // or "acceptEdits", "bypassPermissions" |
| 105 | } |
| 106 | }); |
| 107 | ``` |
| 108 | |
| 109 | ### Model Selection |
| 110 | |
| 111 | | Model | ID | Best For | Speed | Capability | |
| 112 | |-------|-----|----------|-------|------------| |
| 113 | | **Haiku** | `"haiku"` | Fast tasks, monitoring | Fastest | Basic | |
| 114 | | **Sonnet** | `"sonnet"` or `"claude-sonnet-4-5"` | Balanced | Medium | High | |
| 115 | | **Opus** | `"opus"` | Complex reasoning | Slowest | Highest | |
| 116 | | **Inherit** | `"inherit"` | Use parent model | - | - | |
| 117 | |
| 118 | **Default**: `"sonnet"` if not specified |
| 119 | |
| 120 | ### System Prompts |
| 121 | |
| 122 | ```typescript |
| 123 | const response = query({ |
| 124 | prompt: "Implement user authentication", |
| 125 | options: { |
| 126 | systemPrompt: `You are an expert backend developer. |
| 127 | |
| 128 | Follow these principles: |
| 129 | - Always use TypeScript with strict types |
| 130 | - Implement comprehensive error handling |
| 131 | - Add detailed logging for debugging |
| 132 | - Write unit tests for all functions |
| 133 | - Follow OWASP security guidelines` |
| 134 | } |
| 135 | }); |
| 136 | ``` |
| 137 | |
| 138 | **CRITICAL:** |
| 139 | - System prompt sets agent behavior for entire session |
| 140 | - Should be clear and specific |
| 141 | - Can be 1-10k tokens (affects context window) |
| 142 | |
| 143 | ### Working Directory |
| 144 | |
| 145 | ```typescript |
| 146 | const response = query({ |
| 147 | prompt: "Refactor the user service", |
| 148 | options: { |
| 149 | workingDirectory: "/Users/dev/projects/my-app", |
| 150 | // Agent operates within this directory |
| 151 | // Relative paths resolved from here |
| 152 | } |
| 153 | }); |
| 154 | ``` |
| 155 | |
| 156 | **Best Practices:** |
| 157 | - Use absolute paths for clarity |
| 158 | - Agent stays within this directory scope |
| 159 | - Critical for multi-project environments |
| 160 | |
| 161 | --- |
| 162 | |
| 163 | ## Tool Integration (Built-in + Custom) |
| 164 | |
| 165 | ### Built-in Tools |
| 166 | |
| 167 | The SDK provides access to Claude Code's |