Loading...
    • Developer Guide
    • API Reference
    • MCP
    • Resources
    • Release Notes
    Search...
    ⌘K

    First steps

    Intro to ClaudeQuickstart

    Models & pricing

    Models overviewChoosing a modelWhat's new in Claude 4.5Migrating to Claude 4.5Model deprecationsPricing

    Build with Claude

    Features overviewUsing the Messages APIContext windowsPrompting best practices

    Capabilities

    Prompt cachingContext editingExtended thinkingStreaming MessagesBatch processingCitationsMultilingual supportToken countingEmbeddingsVisionPDF supportFiles APISearch resultsGoogle Sheets add-on

    Tools

    OverviewHow to implement tool useToken-efficient tool useFine-grained tool streamingBash toolCode execution toolComputer use toolText editor toolWeb fetch toolWeb search toolMemory tool

    Agent Skills

    OverviewQuickstartBest practicesUsing Skills with the API

    Agent SDK

    OverviewTypeScript SDKPython SDK

    Guides

    Streaming InputHandling PermissionsSession ManagementHosting the Agent SDKModifying system promptsMCP in the SDKCustom ToolsSubagents in the SDKSlash Commands in the SDKAgent Skills in the SDKTracking Costs and UsageTodo ListsPlugins in the SDK

    MCP in the API

    MCP connectorRemote MCP servers

    Claude on 3rd-party platforms

    Amazon BedrockVertex AI

    Prompt engineering

    OverviewPrompt generatorUse prompt templatesPrompt improverBe clear and directUse examples (multishot prompting)Let Claude think (CoT)Use XML tagsGive Claude a role (system prompts)Prefill Claude's responseChain complex promptsLong context tipsExtended thinking tips

    Test & evaluate

    Define success criteriaDevelop test casesUsing the Evaluation ToolReducing latency

    Strengthen guardrails

    Reduce hallucinationsIncrease output consistencyMitigate jailbreaksStreaming refusalsReduce prompt leakKeep Claude in character

    Administration and monitoring

    Admin API overviewUsage and Cost APIClaude Code Analytics API
    Console
    Guides

    Subagents in the SDK

    Working with subagents in the Claude Agent SDK

    Subagents in the Claude Agent SDK are specialized AIs that are orchestrated by the main agent. Use subagents for context management and parallelization.

    This guide explains how to define and use subagents in the SDK using the agents parameter.

    Overview

    Subagents can be defined in two ways when using the SDK:

    1. Programmatically - Using the agents parameter in your query() options (recommended for SDK applications)
    2. Filesystem-based - Placing markdown files with YAML frontmatter in designated directories (.claude/agents/)

    This guide primarily focuses on the programmatic approach using the agents parameter, which provides a more integrated development experience for SDK applications.

    Benefits of Using Subagents

    Context Management

    Subagents maintain separate context from the main agent, preventing information overload and keeping interactions focused. This isolation ensures that specialized tasks don't pollute the main conversation context with irrelevant details.

    Example: A research-assistant subagent can explore dozens of files and documentation pages without cluttering the main conversation with all the intermediate search results - only returning the relevant findings.

    Parallelization

    Multiple subagents can run concurrently, dramatically speeding up complex workflows.

    Example: During a code review, you can run style-checker, security-scanner, and test-coverage subagents simultaneously, reducing review time from minutes to seconds.

    Specialized Instructions and Knowledge

    Each subagent can have tailored system prompts with specific expertise, best practices, and constraints.

    Example: A database-migration subagent can have detailed knowledge about SQL best practices, rollback strategies, and data integrity checks that would be unnecessary noise in the main agent's instructions.

    Tool Restrictions

    Subagents can be limited to specific tools, reducing the risk of unintended actions.

    Example: A doc-reviewer subagent might only have access to Read and Grep tools, ensuring it can analyze but never accidentally modify your documentation files.

    Creating Subagents

    Programmatic Definition (Recommended)

    Define subagents directly in your code using the agents parameter:

    import { query } from '@anthropic-ai/claude-agent-sdk';
    
    const result = query({
      prompt: "Review the authentication module for security issues",
      options: {
        agents: {
          'code-reviewer': {
            description: 'Expert code review specialist. Use for quality, security, and maintainability reviews.',
            prompt: `You are a code review specialist with expertise in security, performance, and best practices.
    
    When reviewing code:
    - Identify security vulnerabilities
    - Check for performance issues
    - Verify adherence to coding standards
    - Suggest specific improvements
    
    Be thorough but concise in your feedback.`,
            tools: ['Read', 'Grep', 'Glob'],
            model: 'sonnet'
          },
          'test-runner': {
            description: 'Runs and analyzes test suites. Use for test execution and coverage analysis.',
            prompt: `You are a test execution specialist. Run tests and provide clear analysis of results.
    
    Focus on:
    - Running test commands
    - Analyzing test output
    - Identifying failing tests
    - Suggesting fixes for failures`,
            tools: ['Bash', 'Read', 'Grep'],
          }
        }
      }
    });
    
    for await (const message of result) {
      console.log(message);
    }

    AgentDefinition Configuration

    FieldTypeRequiredDescription
    descriptionstringYesNatural language description of when to use this agent
    promptstringYesThe agent's system prompt defining its role and behavior
    toolsstring[]NoArray of allowed tool names. If omitted, inherits all tools
    model'sonnet' | 'opus' | 'haiku' | 'inherit'NoModel override for this agent. Defaults to main model if omitted

    Filesystem-Based Definition (Alternative)

    You can also define subagents as markdown files in specific directories:

    • Project-level: .claude/agents/*.md - Available only in the current project
    • User-level: ~/.claude/agents/*.md - Available across all projects

    Each subagent is a markdown file with YAML frontmatter:

    ---
    name: code-reviewer
    description: Expert code review specialist. Use for quality, security, and maintainability reviews.
    tools: Read, Grep, Glob, Bash
    ---
    
    Your subagent's system prompt goes here. This defines the subagent's
    role, capabilities, and approach to solving problems.

    Note: Programmatically defined agents (via the agents parameter) take precedence over filesystem-based agents with the same name.

    How the SDK Uses Subagents

    When using the Claude Agent SDK, subagents can be defined programmatically or loaded from the filesystem. Claude will:

    1. Load programmatic agents from the agents parameter in your options
    2. Auto-detect filesystem agents from .claude/agents/ directories (if not overridden)
    3. Invoke them automatically based on task matching and the agent's description
    4. Use their specialized prompts and tool restrictions
    5. Maintain separate context for each subagent invocation

    Programmatically defined agents (via agents parameter) take precedence over filesystem-based agents with the same name.

    Example Subagents

    For comprehensive examples of subagents including code reviewers, test runners, debuggers, and security auditors, see the main Subagents guide. The guide includes detailed configurations and best practices for creating effective subagents.

    SDK Integration Patterns

    Automatic Invocation

    The SDK will automatically invoke appropriate subagents based on the task context. Ensure your agent's description field clearly indicates when it should be used:

    const result = query({
      prompt: "Optimize the database queries in the API layer",
      options: {
        agents: {
          'performance-optimizer': {
            description: 'Use PROACTIVELY when code changes might impact performance. MUST BE USED for optimization tasks.',
            prompt: 'You are a performance optimization specialist...',
            tools: ['Read', 'Edit', 'Bash', 'Grep'],
            model: 'sonnet'
          }
        }
      }
    });

    Explicit Invocation

    Users can request specific subagents in their prompts:

    const result = query({
      prompt: "Use the code-reviewer agent to check the authentication module",
      options: {
        agents: {
          'code-reviewer': {
            description: 'Expert code review specialist',
            prompt: 'You are a security-focused code reviewer...',
            tools: ['Read', 'Grep', 'Glob']
          }
        }
      }
    });

    Dynamic Agent Configuration

    You can dynamically configure agents based on your application's needs:

    import { query, type AgentDefinition } from '@anthropic-ai/claude-agent-sdk';
    
    function createSecurityAgent(securityLevel: 'basic' | 'strict'): AgentDefinition {
      return {
        description: 'Security code reviewer',
        prompt: `You are a ${securityLevel === 'strict' ? 'strict' : 'balanced'} security reviewer...`,
        tools: ['Read', 'Grep', 'Glob'],
        model: securityLevel === 'strict' ? 'opus' : 'sonnet'
      };
    }
    
    const result = query({
      prompt: "Review this PR for security issues",
      options: {
        agents: {
          'security-reviewer': createSecurityAgent('strict')
        }
      }
    });

    Tool Restrictions

    Subagents can have restricted tool access via the tools field:

    • Omit the field - Agent inherits all available tools (default)
    • Specify tools - Agent can only use listed tools

    Example of a read-only analysis agent:

    const result = query({
      prompt: "Analyze the architecture of this codebase",
      options: {
        agents: {
          'code-analyzer': {
            description: 'Static code analysis and architecture review',
            prompt: `You are a code architecture analyst. Analyze code structure,
    identify patterns, and suggest improvements without making changes.`,
            tools: ['Read', 'Grep', 'Glob']  // No write or execute permissions
          }
        }
      }
    });

    Common Tool Combinations

    Read-only agents (analysis, review):

    tools: ['Read', 'Grep', 'Glob']

    Test execution agents:

    tools: ['Bash', 'Read', 'Grep']

    Code modification agents:

    tools: ['Read', 'Edit', 'Write', 'Grep', 'Glob']

    Related Documentation

    • Main Subagents Guide - Comprehensive subagent documentation
    • SDK Overview - Overview of Claude Agent SDK
    • Settings - Configuration file reference
    • Slash Commands - Custom command creation
    • Overview
    • Benefits of Using Subagents
    • Context Management
    • Parallelization
    • Specialized Instructions and Knowledge
    • Tool Restrictions
    • Creating Subagents
    • Programmatic Definition (Recommended)
    • AgentDefinition Configuration
    • Filesystem-Based Definition (Alternative)
    • How the SDK Uses Subagents
    • Example Subagents
    • SDK Integration Patterns
    • Automatic Invocation
    • Explicit Invocation
    • Dynamic Agent Configuration
    • Tool Restrictions
    • Common Tool Combinations
    • Related Documentation
    © 2025 ANTHROPIC PBC

    Products

    • Claude
    • Claude Code
    • Max plan
    • Team plan
    • Enterprise plan
    • Download app
    • Pricing
    • Log in

    Features

    • Claude and Slack
    • Claude in Excel

    Models

    • Opus
    • Sonnet
    • Haiku

    Solutions

    • AI agents
    • Code modernization
    • Coding
    • Customer support
    • Education
    • Financial services
    • Government
    • Life sciences

    Claude Developer Platform

    • Overview
    • Developer docs
    • Pricing
    • Amazon Bedrock
    • Google Cloud’s Vertex AI
    • Console login

    Learn

    • Blog
    • Catalog
    • Courses
    • Use cases
    • Connectors
    • Customer stories
    • Engineering at Anthropic
    • Events
    • Powered by Claude
    • Service partners
    • Startups program

    Company

    • Anthropic
    • Careers
    • Economic Futures
    • Research
    • News
    • Responsible Scaling Policy
    • Security and compliance
    • Transparency

    Help and security

    • Availability
    • Status
    • Support center

    Terms and policies

    • Privacy policy
    • Responsible disclosure policy
    • Terms of service: Commercial
    • Terms of service: Consumer
    • Usage policy

    Products

    • Claude
    • Claude Code
    • Max plan
    • Team plan
    • Enterprise plan
    • Download app
    • Pricing
    • Log in

    Features

    • Claude and Slack
    • Claude in Excel

    Models

    • Opus
    • Sonnet
    • Haiku

    Solutions

    • AI agents
    • Code modernization
    • Coding
    • Customer support
    • Education
    • Financial services
    • Government
    • Life sciences

    Claude Developer Platform

    • Overview
    • Developer docs
    • Pricing
    • Amazon Bedrock
    • Google Cloud’s Vertex AI
    • Console login

    Learn

    • Blog
    • Catalog
    • Courses
    • Use cases
    • Connectors
    • Customer stories
    • Engineering at Anthropic
    • Events
    • Powered by Claude
    • Service partners
    • Startups program

    Company

    • Anthropic
    • Careers
    • Economic Futures
    • Research
    • News
    • Responsible Scaling Policy
    • Security and compliance
    • Transparency

    Help and security

    • Availability
    • Status
    • Support center

    Terms and policies

    • Privacy policy
    • Responsible disclosure policy
    • Terms of service: Commercial
    • Terms of service: Consumer
    • Usage policy
    © 2025 ANTHROPIC PBC