home / skills / trotsky1997 / my-claude-agent-skills / claude-agent-sdk

claude-agent-sdk skill

/claude-agent-sdk

This skill helps you build autonomous Claude agents that read files, edit code, run commands, and manage complex workflows.

npx playbooks add skill trotsky1997/my-claude-agent-skills --skill claude-agent-sdk

Review the files below or copy the command above to add this skill to your agents.

Files (5)
SKILL.md
9.9 KB
---
name: claude-agent-sdk
description: Comprehensive guide for using Claude Agent SDK to build AI agents that can read files, execute commands, edit code, and perform complex workflows. Use when (1) Building autonomous AI agents with Claude, (2) Creating agents that interact with codebases and file systems, (3) Integrating custom tools and MCP servers with Claude, (4) Managing conversation sessions and context with Claude, (5) Implementing file operations, command execution, and code editing in agents, (6) Setting up permission controls and security for agent tools, (7) Using file checkpointing and session management features, (8) Building production-ready agents with error handling and monitoring
metadata:
  short-description: Build AI agents with Claude Agent SDK
---

# Claude Agent SDK

Comprehensive toolkit for building autonomous AI agents with Claude that can understand codebases, edit files, run commands, and execute complex workflows.

## Quick Start

**Installation:**

```bash
# Python
pip install claude-agent-sdk

# TypeScript
npm install @anthropic-ai/claude-agent-sdk
```

**Simple query example:**

```python
from claude_agent_sdk import query, ClaudeAgentOptions
import asyncio

async def main():
    async for message in query(
        prompt="Explain the authentication flow",
        options=ClaudeAgentOptions(
            max_turns=1,
            allowed_tools=["Read", "Grep"]
        )
    ):
        if hasattr(message, 'result'):
            print(message.result)

asyncio.run(main())
```

**Session-based conversation:**

```python
from claude_agent_sdk import ClaudeSDKClient, ClaudeAgentOptions
import asyncio

async def main():
    async with ClaudeSDKClient() as client:
        # First query
        await client.query("What's the capital of France?")
        async for message in client.receive_response():
            print(message)
        
        # Follow-up - Claude remembers context
        await client.query("What's the population of that city?")
        async for message in client.receive_response():
            print(message)

asyncio.run(main())
```

## Core Workflows

### 1. Basic Agent Setup

**Using query() for one-shot requests:**

```python
from claude_agent_sdk import query, ClaudeAgentOptions

async for message in query(
    prompt="Analyze this codebase",
    options=ClaudeAgentOptions(
        allowed_tools=["Read", "Grep", "Bash"],
        max_turns=5
    )
):
    if hasattr(message, 'result'):
        print(message.result)
```

**Using ClaudeSDKClient for conversations:**

```python
from claude_agent_sdk import ClaudeSDKClient, ClaudeAgentOptions

options = ClaudeAgentOptions(
    allowed_tools=["Read", "Write", "Edit", "Bash"],
    permission_mode="acceptEdits"
)

async with ClaudeSDKClient(options=options) as client:
    await client.query("Create a hello.py file")
    async for message in client.receive_response():
        print(message)
```

### 2. Custom Tools

**Define tools with @tool decorator:**

```python
from claude_agent_sdk import tool, create_sdk_mcp_server, ClaudeAgentOptions

@tool("add", "Add two numbers", {"a": float, "b": float})
async def add(args):
    return {
        "content": [{"type": "text", "text": f"Sum: {args['a'] + args['b']}"}]
    }

@tool("multiply", "Multiply two numbers", {"a": float, "b": float})
async def multiply(args):
    return {
        "content": [{"type": "text", "text": f"Product: {args['a'] * args['b']}"}]
    }

# Create MCP server
calculator = create_sdk_mcp_server(
    name="calculator",
    version="2.0.0",
    tools=[add, multiply]
)

# Use with Claude
options = ClaudeAgentOptions(
    mcp_servers={"calc": calculator},
    allowed_tools=["mcp__calc__add", "mcp__calc__multiply"]
)
```

### 3. Tool Permissions

**Basic permission control:**

```python
options = ClaudeAgentOptions(
    allowed_tools=["Read", "Write"],  # Whitelist
    disallowed_tools=["Bash"],        # Blacklist
    permission_mode="acceptEdits"     # Auto-accept edits
)
```

**Advanced permission handler:**

```python
async def custom_permission_handler(tool_name: str, input_data: dict, context: dict):
    # Block writes to system directories
    if tool_name == "Write" and input_data.get("file_path", "").startswith("/system/"):
        return {
            "behavior": "deny",
            "message": "System directory write not allowed",
            "interrupt": True
        }
    
    # Redirect sensitive operations
    if tool_name in ["Write", "Edit"] and "config" in input_data.get("file_path", ""):
        safe_path = f"./sandbox/{input_data['file_path']}"
        return {
            "behavior": "allow",
            "updatedInput": {**input_data, "file_path": safe_path}
        }
    
    return {"behavior": "allow", "updatedInput": input_data}

options = ClaudeAgentOptions(
    can_use_tool=custom_permission_handler,
    allowed_tools=["Read", "Write", "Edit"]
)
```

### 4. Session Management

**Continuous conversation:**

```python
class ConversationSession:
    def __init__(self, options: ClaudeAgentOptions = None):
        self.client = ClaudeSDKClient(options)
        self.turn_count = 0

    async def start(self):
        await self.client.connect()
        
        while True:
            user_input = input(f"\n[Turn {self.turn_count + 1}] You: ")
            
            if user_input.lower() == 'exit':
                break
            elif user_input.lower() == 'interrupt':
                await self.client.interrupt()
                continue
            
            await self.client.query(user_input)
            self.turn_count += 1
            
            async for message in self.client.receive_response():
                if isinstance(message, AssistantMessage):
                    for block in message.content:
                        if isinstance(block, TextBlock):
                            print(block.text, end="")
            print()
        
        await self.client.disconnect()
```

### 5. File Checkpointing

**Enable checkpointing:**

```python
options = ClaudeAgentOptions(
    enable_file_checkpointing=True,
    permission_mode="acceptEdits",
    extra_args={"replay-user-messages": None}
)

checkpoint_id = None
session_id = None

async with ClaudeSDKClient(options) as client:
    await client.query("Refactor the authentication module")
    
    async for message in client.receive_response():
        if isinstance(message, UserMessage) and message.uuid:
            checkpoint_id = message.uuid
        if isinstance(message, ResultMessage):
            session_id = message.session_id

# Later: rewind to checkpoint
if checkpoint_id and session_id:
    async with ClaudeSDKClient(ClaudeAgentOptions(
        enable_file_checkpointing=True,
        resume=session_id
    )) as client:
        await client.query("")
        async for message in client.receive_response():
            await client.rewind_files(checkpoint_id)
            break
```

## Configuration

### ClaudeAgentOptions

**Essential options:**

```python
options = ClaudeAgentOptions(
    # Tool management
    allowed_tools=["Read", "Write", "Edit", "Bash"],
    disallowed_tools=["Delete"],
    
    # Conversation control
    max_turns=10,
    continue_conversation=True,
    
    # Permissions
    permission_mode="acceptEdits",  # or "prompt" or "deny"
    can_use_tool=custom_handler,
    
    # MCP servers
    mcp_servers={"my-server": custom_mcp_server},
    
    # Environment
    cwd="/path/to/working/directory",
    env={"API_KEY": "value"},
    
    # File checkpointing
    enable_file_checkpointing=True,
    
    # Model selection
    model="claude-sonnet-4.5",
    
    # System prompt
    system_prompt="You are a helpful coding assistant."
)
```

## Common Patterns

### Pattern 1: Code Analysis Agent

```python
options = ClaudeAgentOptions(
    allowed_tools=["Read", "Grep", "Bash"],
    cwd="./project",
    max_turns=5
)

async with ClaudeSDKClient(options) as client:
    await client.query("Analyze the codebase structure and identify main components")
    async for message in client.receive_response():
        print(message)
```

### Pattern 2: Code Editing Agent

```python
options = ClaudeAgentOptions(
    allowed_tools=["Read", "Write", "Edit"],
    permission_mode="acceptEdits",
    cwd="./project"
)

async with ClaudeSDKClient(options) as client:
    await client.query("Add error handling to all API endpoints")
    async for message in client.receive_response():
        print(message)
```

### Pattern 3: Multi-Tool Agent

```python
# Define multiple tools
@tool("calculate", "Perform calculations", {"expression": str})
async def calculate(args):
    result = eval(args["expression"])
    return {"content": [{"type": "text", "text": f"Result: {result}"}]}

@tool("translate", "Translate text", {"text": str, "target_lang": str})
async def translate(args):
    # Translation logic
    return {"content": [{"type": "text", "text": f"Translated: {args['text']}"}]}

server = create_sdk_mcp_server(
    name="utilities",
    version="1.0.0",
    tools=[calculate, translate]
)

options = ClaudeAgentOptions(
    mcp_servers={"utilities": server},
    allowed_tools=["mcp__utilities__calculate", "mcp__utilities__translate"]
)
```

## Troubleshooting

**Common issues:**

1. **Connection errors**: Ensure API key is set in environment or options
2. **Tool not found**: Check tool name format (`mcp__server__tool` for MCP tools)
3. **Permission denied**: Verify `allowed_tools` includes the tool
4. **Session not continuing**: Use `continue_conversation=True` or `ClaudeSDKClient`
5. **File checkpointing not working**: Set `enable_file_checkpointing=True` and `extra_args={"replay-user-messages": None}`

## References

For detailed information, see:

- [api-reference.md](references/api-reference.md) - Complete API reference
- [tools-reference.md](references/tools-reference.md) - Built-in tools and custom tool creation
- [advanced-features.md](references/advanced-features.md) - Hooks, file checkpointing, and production patterns
- [configuration.md](references/configuration.md) - Complete configuration options

Overview

This skill is a practical guide to the Claude Agent SDK for building autonomous AI agents that read and edit files, run shell commands, manage sessions, and integrate custom tools. It focuses on concrete workflows, permission controls, file checkpointing, and production readiness for agents working with codebases and file systems.

How this skill works

The SDK exposes a one-shot query API and a session-based client that preserves context across turns. It provides built-in tools (Read, Write, Edit, Bash, Grep), decorators to register custom MCP tools, and options to control allowed/disallowed tools, permission handling, and file checkpointing. Agents operate by issuing queries and receiving streamed messages; tools are invoked by the agent and validated by configurable permission handlers.

When to use it

  • Building autonomous agents that need to read, modify, or create files in a repository
  • Creating agents that must run shell commands or analyze a codebase
  • Integrating custom services or domain-specific tools via MCP servers
  • Managing multi-turn conversations where context and session state matter
  • Enforcing tool permission rules and sandboxing sensitive operations
  • Implementing file checkpointing and rewind for safe iterative edits

Best practices

  • Whitelist only the tools the agent needs using allowed_tools; blacklist destructive tools via disallowed_tools
  • Implement a custom permission handler to block or sanitize file paths and sensitive operations
  • Enable file checkpointing for workflows that edit files so you can rewind changes on error
  • Use ClaudeSDKClient for multi-turn sessions and query() for simple one-shot tasks
  • Run agents within a controlled working directory (cwd) and inject minimal required env vars
  • Log tool invocations and session activity for monitoring and debugging in production

Example use cases

  • Code analysis agent that scans a project with Read and Grep to produce an architecture summary
  • Refactor agent that edits multiple files with Write/Edit and uses checkpointing to revert if needed
  • Automation agent that runs build or test commands via Bash and reports results
  • Custom calculator or translation service exposed as MCP tools and consumed by agents
  • Session-based assistant that remembers prior queries and continues a long-running debugging workflow

FAQ

How do I restrict an agent from writing to system paths?

Provide a can_use_tool permission handler that denies or rewrites Write/Edit inputs that target system directories, returning behavior: 'deny' or an updatedInput with a safe path.

When should I use file checkpointing?

Enable checkpointing when agents will perform file edits across multiple steps so you can rewind to a known good state if changes are incorrect or harmful.