Token导航 LogoToken导航TokenDH.com
研究检索external-servicegithub未标认证来源可访问许可证需确认审计提醒

claude-agent-sdkClaude Agent SDK 搜索

Agent Skill

claude-agent-sdk 用于查找、检索和筛选相关信息,适合在 Codex、Claude、Cursor、Gemini CLI 中需要根据关键词、任务场景或来源线索快速定位候选结果时使用。可结合来源仓库、安装命令和原始 README 继续核验具体用法。安装前建议确认权限范围、维护状态,以及是否会触发联网、命令执行或文件读写。

总安装

297

周安装

12

GitHub Stars

18

下载量

93
CodexClaudeCursorGemini CLI

安装说明

本站只整理中文说明和来源信息,不托管安装包,也不代用户安装。

GitHub

来源数

2

许可证

unknown

最后核验

2026-05-01

来源状态

来源可访问

安装方式

通过对话安装

复制提示词发给支持本地命令或 Skills 的 AI 助手,先确认命令和权限,再让它执行。

请帮我安装这个 Agent Skill:claude-agent-sdk(Claude Agent SDK 搜索)
来源仓库:https://github.com/basher83/lunar-claude
仓库路径:skills/claude-agent-sdk
安装命令:
npx skills add https://github.com/basher83/lunar-claude --skill claude-agent-sdk
安装前请先检查当前环境是否支持对应 CLI,并向我确认将要执行的命令、安装目录、联网范围和文件读写权限;确认后再执行。

命令行安装

复制命令到本机终端执行。该命令会通过 npx skills 从第三方来源获取 Skill;本站只展示命令,不托管安装包,也不自动执行。

skills.shnpx skills
npx skills add https://github.com/basher83/lunar-claude --skill claude-agent-sdk

简介

claude-agent-sdk 提供基于 Claude Agent SDK 的应用程序构建模式和支持。

  • 支持 query() 和 ClaudeSDKClient 两种调用方式的选择指导。
  • 适用于构建生产级多代理协调应用,提供模板化项目结构和异步处理模式。
  • 目标版本为 claude-agent-sdk>=0.1.6 (Python),需配合 uv 脚本头使用。
  • 适用宿主包括 Codex、Claude、Cursor、Gemini CLI,接入前应确认版本、权限和运行环境要求。

SKILL.md

Claude Agent SDK

Build production-ready applications using the Claude Agent SDK for Python.

SDK Version: This skill targets claude-agent-sdk>=0.1.6 (Python)

Overview

This skill provides patterns, examples, and best practices for building SDK applications that orchestrate Claude agents.

Quick Start

Copy the template and customize:

cp assets/sdk-template.py my-app.py
# Edit my-app.py - customize agents and workflow
chmod +x my-app.py
./my-app.py

The template includes proper uv script headers, agent definitions, and async patterns.

Choosing Between query() and ClaudeSDKClient

The SDK provides two ways to interact with Claude: the query() function for simple one-shot tasks, and ClaudeSDKClient for continuous conversations.

Quick Comparison

Featurequery()ClaudeSDKClient
Conversation memoryNo - each call is independentYes - maintains context across queries
Use caseOne-off tasks, single questionsMulti-turn conversations, complex workflows
ComplexitySimple - one function callMore setup - context manager pattern
Hooks supportNoYes
Custom toolsNoYes
InterruptsNoYes - can interrupt ongoing operations
Session controlNew session each timeSingle persistent session
Important: Hooks and custom tools (SDK MCP servers) are only supported with ClaudeSDKClient, not with query(). If you need hooks or custom tools, you must use ClaudeSDKClient. Note on Async Runtimes: The SDK works with both asyncio and anyio. The official SDK examples prefer anyio.run() for better async library compatibility, but asyncio.run() works equally well. Use whichever fits your project's async runtime.

When to Use query()

Use query() for simple, independent tasks where you don't need conversation history:

import anyio  # or: import asyncio
from claude_agent_sdk import query, ClaudeAgentOptions

async def analyze_file():
    """One-shot file analysis - no conversation needed."""
    options = ClaudeAgentOptions(
        system_prompt="You are a code analyzer",
        allowed_tools=["Read", "Grep", "Glob"],
        permission_mode="acceptEdits"
    )

    async for message in query(
        prompt="Analyze /path/to/file.py for bugs",
        options=options
    ):
        print(message)

anyio.run(analyze_file)  # or: asyncio.run(analyze_file())

Best for:

  • Single analysis tasks
  • Independent file operations
  • Quick questions without follow-up
  • Scripts that run once and exit

Key limitation: Each query() call creates a new session with no memory of previous calls.

When to Use ClaudeSDKClient

Use ClaudeSDKClient when you need conversation context across multiple interactions:

import anyio  # or: import asyncio
from claude_agent_sdk import ClaudeSDKClient, ClaudeAgentOptions, AssistantMessage, TextBlock

async def interactive_debugging():
    """Multi-turn debugging conversation with context."""
    options = ClaudeAgentOptions(
        system_prompt="You are a debugging assistant",
        allowed_tools=["Read", "Grep", "Bash"],
        permission_mode="acceptEdits"
    )

    async with ClaudeSDKClient(options=options) as client:
        # First query
        await client.query("Find all TODO comments in /path/to/project")
        async for message in client.receive_response():
            if isinstance(message, AssistantMessage):
                for block in message.content:
                    if isinstance(block, TextBlock):
                        print(f"Claude: {block.text}")

        # Follow-up - Claude remembers the TODOs found above
        await client.query("Now prioritize them by complexity")
        async for message in client.receive_response():
            if isinstance(message, AssistantMessage):
                for block in message.content:
                    if isinstance(block, TextBlock):
                        print(f"Claude: {block.text}")

        # Another follow-up - still in same conversation
        await client.query("Create a plan to address the top 3")
        async for message in client.receive_response():
            if isinstance(message, AssistantMessage):
                for block in message.content:
                    if isinstance(block, TextBlock):
                        print(f"Claude: {block.text}")

anyio.run(interactive_debugging)  # or: asyncio.run(interactive_debugging())

Best for:

  • Multi-turn conversations
  • Interactive workflows
  • Tasks requiring context from previous responses
  • Applications with interrupt capability
  • Orchestrators managing complex workflows

Key advantage: Claude remembers all previous queries and responses in the session.

See: examples/streaming_mode.py - Comprehensive ClaudeSDKClient examples with all patterns

Advanced: Interrupts with ClaudeSDKClient

Only ClaudeSDKClient supports interrupting ongoing operations:

import anyio  # or: import asyncio
from claude_agent_sdk import ClaudeSDKClient

async def interruptible_task():
    async with ClaudeSDKClient() as client:
        await client.query("Run a long analysis on /large/codebase")

        # Start processing in background
        async with anyio.create_task_group() as tg:
            tg.start_soon(process_messages, client)

            # Simulate user interrupt after 5 seconds
            await anyio.sleep(5)
            await client.interrupt()

async def process_messages(client):
    async for message in client.receive_response():
        print(message)

anyio.run(interruptible_task)  # or: asyncio.run(interruptible_task())

Quick Decision Guide

Use query() if:

  • Task is self-contained
  • No follow-up questions needed
  • Each execution is independent
  • Simpler code is preferred

Use ClaudeSDKClient if:

  • Need conversation memory
  • Building interactive workflows
  • Require interrupt capability
  • Managing complex multi-step processes
  • Working with orchestrators and subagents

Core Patterns

1. Orchestrator with Subagents

Define a main orchestrator that delegates work to specialized subagents.

Critical requirements:

  • Orchestrator must use system_prompt={"type": "preset", "preset": "claude_code"} (provides Task tool knowledge)
  • Register agents programmatically via agents={} parameter (SDK best practice)
  • Orchestrator must include "Task" in allowed_tools
  • Match agent names exactly between definition and usage

Example:

from claude_agent_sdk import AgentDefinition, ClaudeAgentOptions

options = ClaudeAgentOptions(
    system_prompt={"type": "preset", "preset": "claude_code"},  # REQUIRED for orchestrators
    allowed_tools=["Bash", "Task", "Read", "Write"],
    agents={
        "analyzer": AgentDefinition(
            description="Analyzes code structure and patterns",
            prompt="You are a code analyzer...",
            tools=["Read", "Grep", "Glob"],
            model="sonnet"
        ),
        "fixer": AgentDefinition(
            description="Fixes identified issues",
            prompt="You are a code fixer...",
            tools=["Read", "Edit", "Bash"],
            model="sonnet"
        )
    },
    permission_mode="acceptEdits",
    model="claude-sonnet-4-5"
)

See:

  • references/agent-patterns.md - Complete agent definition patterns
  • examples/agents.py - Official SDK agent examples with different agent types

2. System Prompt Configuration

Choose the appropriate system prompt pattern:

# Orchestrator (use claude_code preset) - dict format (official examples prefer this)
system_prompt={"type": "preset", "preset": "claude_code"}

# Shorthand format (equivalent, but less explicit)
system_prompt="claude_code"

# Custom behavior
system_prompt="You are a Python expert..."

# Extend preset with additional instructions
system_prompt={
    "type": "preset",
    "preset": "claude_code",
    "append": "Additional domain-specific instructions"
}

Note: The shorthand system_prompt="claude_code" is equivalent to {"type": "preset", "preset": "claude_code"}. Both are valid. Official examples prefer the dict format for explicitness.

See:

  • references/system-prompts.md - Complete system prompt documentation
  • examples/system_prompt.py - Official SDK system prompt examples

3. Tool Restrictions

Limit subagent tools to minimum needed:

# Read-only analyzer
tools=["Read", "Grep", "Glob"]

# Code modifier
tools=["Read", "Edit", "Bash"]

# Test runner
tools=["Bash", "Read"]

See: references/agent-patterns.md for common tool combinations

4. Hooks

Intercept SDK events to control behavior:

from claude_agent_sdk import HookMatcher

options = ClaudeAgentOptions(
    hooks={
        "PreToolUse": [
            HookMatcher(matcher="Bash", hooks=[check_bash_command])
        ],
        "PostToolUse": [
            HookMatcher(matcher="Bash", hooks=[review_output])
        ]
    }
)

See:

  • references/hooks-guide.md - Complete hook patterns documentation
  • examples/hooks.py - Official SDK hook examples with all hook types

5. Permission Callbacks

Fine-grained control over tool usage:

async def permission_callback(tool_name, input_data, context):
    # Allow read operations
    if tool_name in ["Read", "Grep", "Glob"]:
        return PermissionResultAllow()

    # Block dangerous commands
    if tool_name == "Bash" and "rm -rf" in input_data.get("command", ""):
        return PermissionResultDeny(message="Dangerous command")

    return PermissionResultAllow()

options = ClaudeAgentOptions(
    can_use_tool=permission_callback,
    permission_mode="default"
)

See:

  • references/tool-permissions.md - Complete permission patterns and decision guide
  • examples/tool_permission_callback.py - Official SDK permission callback example

Workflow Templates

Building an Orchestrator

Follow these steps to build an effective orchestrator:

1. Define agent purposes

  • What specialized tasks need delegation?
  • What tools does each agent need?
  • What constraints should apply?

2. Create agent definitions

agents={
    "agent-name": AgentDefinition(
        description="When to use this agent",
        prompt="Agent's role and behavior",
        tools=["Tool1", "Tool2"],
        model="sonnet"
    )
}

3. Configure orchestrator

options = ClaudeAgentOptions(
    system_prompt={"type": "preset", "preset": "claude_code"},  # CRITICAL
    allowed_tools=["Bash", "Task", "Read", "Write"],
    agents=agents,
    permission_mode="acceptEdits"
)

4. Implement workflow

async with ClaudeSDKClient(options=options) as client:
    await client.query("Use 'agent-name' to perform task")

    async for message in client.receive_response():
        # Process responses
        pass

See: examples/basic-orchestrator.py for complete working example

Loading Agents from Files

While programmatic registration is recommended, agent content can be stored in markdown files:

import yaml

def load_agent_definition(path: str) -> AgentDefinition:
    """Load agent from markdown file with YAML frontmatter."""
    with open(path) as f:
        content = f.read()

    parts = content.split("---")
    frontmatter = yaml.safe_load(parts[1])
    prompt = parts[2].strip()

    # Parse tools (comma-separated string or array)
    tools = frontmatter.get("tools", [])
    if isinstance(tools, str):
        tools = [t.strip() for t in tools.split(",")]

    return AgentDefinition(
        description=frontmatter["description"],
        prompt=prompt,
        tools=tools,
        model=frontmatter.get("model", "inherit")
    )

# Load and register programmatically
agent = load_agent_definition(".claude/agents/my-agent.md")
options = ClaudeAgentOptions(agents={"my-agent": agent})

See: references/agent-patterns.md for complete loading pattern

Common Anti-Patterns

Avoid these common mistakes:

❌ Missing orchestrator system prompt

# Orchestrator won't know how to use Task tool
options = ClaudeAgentOptions(agents={...})

✅ Correct orchestrator configuration

options = ClaudeAgentOptions(
    system_prompt="claude_code",
    agents={...}
)

❌ Mismatched agent names

agents={"investigator": AgentDefinition(...)}
await client.query("Use 'markdown-investigator'...")  # Wrong name

✅ Exact name matching

agents={"investigator": AgentDefinition(...)}
await client.query("Use 'investigator'...")  # Matches

❌ Tool/prompt mismatch

system_prompt="Fix bugs you find"
allowed_tools=["Read", "Grep"]  # Can't fix, only read

✅ Aligned tools and behavior

system_prompt="Analyze code for bugs"
allowed_tools=["Read", "Grep", "Glob"]

See: references/best-practices.md for complete anti-patterns list

Resources

references/

In-depth documentation loaded as needed:

  • api-reference.md - Complete Python SDK API reference (types, functions, examples)
  • agent-patterns.md - Agent definition patterns, tool restrictions, best practices
  • subagents.md - Comprehensive subagent patterns and SDK integration
  • system-prompts.md - System prompt configuration (preset, custom, append)
  • hooks-guide.md - Hook patterns for all hook types with examples
  • tool-permissions.md - Permission callback patterns and examples
  • best-practices.md - SDK best practices, anti-patterns, debugging tips
  • custom-tools.md - Creating custom tools with SDK MCP servers (Python-only)
  • sessions.md - Session management and resumption patterns (Python-only)
  • skills.md - Using Agent Skills with the SDK (Python-only)
  • slash-commands.md - Slash commands and custom command creation (Python-only)

examples/

Ready-to-run code examples from official SDK:

Getting Started:

  • quick_start.py - Basic query() usage and message handling (start here!)
  • basic-orchestrator.py - Complete orchestrator with analyzer and fixer subagents

Core Patterns:

  • agents.py - Programmatic agent definitions with different agent types
  • hooks.py - Comprehensive hook patterns (PreToolUse, PostToolUse, UserPromptSubmit, etc.)
  • system_prompt.py - System prompt patterns (preset, custom, append)
  • streaming_mode.py - Complete ClaudeSDKClient patterns with multi-turn conversations

Advanced Features:

  • mcp_calculator.py - Custom tools with SDK MCP server (in-process tool server)
  • tool_permission_callback.py - Permission callbacks with logging and control
  • setting_sources.py - Settings isolation and loading (user/project/local)
  • plugin_example.py - Using plugins with the SDK (relevant for plugin marketplace!)

assets/

Templates and validation tools:

  • sdk-template.py - Project template with uv script headers and agent structure
  • sdk-validation-checklist.md - Comprehensive checklist for validating SDK applications against best practices

When to Use This Skill

Use this skill when:

  • Creating new Claude Agent SDK applications
  • Building orchestrators with multiple subagents
  • Implementing programmatic agent definitions
  • Configuring hooks or permission callbacks
  • Validating/reviewing SDK code (use assets/sdk-validation-checklist.md)
  • Migrating from filesystem agent discovery to programmatic registration
  • Debugging SDK applications (agent not found, Task tool not working)
  • Following SDK best practices

Do not use for:

  • Claude Code slash commands or skills (different system)
  • Direct API usage without SDK
  • Non-Python implementations (TypeScript SDK has different patterns)

Next Steps

For Beginners

  1. Start with examples/quick_start.py - Learn basic query() usage
  2. Try assets/sdk-template.py - Template for new projects
  3. Review examples/basic-orchestrator.py - See orchestrator pattern

For Intermediate Users

  1. Explore core patterns:

- examples/agents.py - Agent definitions - examples/system_prompt.py - System prompt patterns - examples/streaming_mode.py - Multi-turn conversations - examples/hooks.py - Hook patterns

For Advanced Users

  1. Study advanced features:

- examples/tool_permission_callback.py - Permission control - examples/mcp_calculator.py - Custom tools - examples/setting_sources.py - Settings management - examples/plugin_example.py - Plugin integration

Validation & Quality

  1. Validate your code with assets/sdk-validation-checklist.md
  2. Review against best practices in references/best-practices.md

Reference Documentation

  1. Consult references/ as needed for detailed patterns

适合场景

01

用户想查找某类 Agent Skill 时

02

需要根据任务场景推荐可安装能力包时

03

需要对比不同来源的安装命令和来源信息时

能力概览

能力 1

按任务关键词查找相关 Skills

能力 2

展示可复制的安装命令

能力 3

保留来源站点、仓库和原始说明,方便继续核验

能力 4

展示第三方安全扫描或审计结果

安装后应在对应宿主中按原始 README 的触发条件使用;具体调用方式请以来源页面和 README 为准。

平台分布

Codex

38.47%
按下载量换算36

Claude

31.08%
按下载量换算29

Cursor

18.92%
按下载量换算18

Gemini CLI

9.36%
按下载量换算9

安全审计

Gen Agent Trust Hub

可疑

Socket

通过

Snyk

可疑

权限和风险

external-service

该 Skill 可能调用第三方服务、云服务或外部模型 API,使用前需要确认账号、额度、数据发送范围和服务条款。

安装前确认

本站仅展示第三方公开信息,不托管安装包,不提供自动安装或运行环境。安装前应自行审查源码、依赖和命令行为。来源安全扫描存在 warning/failed 结果,不能写成本站确认安全。当前只有一个来源,正式发布前建议补源仓库或其他目录站核验。

来源信息

继续浏览同类 Skills