Loading...
    • 開発者ガイド
    • API リファレンス
    • MCP
    • リソース
    • リリースノート
    Search...
    ⌘K
    はじめに
    Claude の紹介クイックスタート
    モデルと料金
    モデル概要モデルの選び方Claude 4.6 の新機能移行ガイドモデルの廃止料金
    Claude で構築する
    機能概要Messages API の使用停止理由の処理プロンプトのベストプラクティス
    コンテキスト管理
    コンテキストウィンドウコンパクションコンテキスト編集
    機能
    プロンプトキャッシング拡張思考適応型思考エフォートメッセージのストリーミングバッチ処理引用多言語サポートトークンカウントエンベディングビジョンPDF サポートFiles API検索結果構造化出力
    ツール
    概要ツール使用の実装方法きめ細かいツールストリーミングBash ツールコード実行ツールプログラムによるツール呼び出しコンピュータ使用ツールテキストエディタツールWeb フェッチツールWeb 検索ツールメモリツールツール検索ツール
    Agent Skills
    概要クイックスタートベストプラクティスエンタープライズ向け SkillsAPI での Skills の使用
    Agent SDK
    概要クイックスタートTypeScript SDKTypeScript V2(プレビュー)Python SDK移行ガイド
    API での MCP
    MCP コネクタリモート MCP サーバー
    サードパーティプラットフォームの Claude
    Amazon BedrockMicrosoft FoundryVertex AI
    プロンプトエンジニアリング
    概要プロンプトジェネレータープロンプトテンプレートの使用プロンプト改善ツール明確かつ直接的に例を使う(マルチショットプロンプティング)Claude に考えさせる(CoT)XML タグを使うClaude に役割を与える(システムプロンプト)複雑なプロンプトを連鎖させる長文コンテキストのヒント拡張思考のヒント
    テストと評価
    成功基準の定義テストケースの開発評価ツールの使用レイテンシの削減
    ガードレールの強化
    ハルシネーションの削減出力の一貫性を高めるジェイルブレイクの軽減ストリーミング拒否プロンプト漏洩の防止Claude をキャラクターに保つ
    管理とモニタリング
    Admin API 概要データレジデンシーワークスペースUsage and Cost APIClaude Code Analytics APIゼロデータリテンション
    Console
    Log in
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...

    Solutions

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

    Partners

    • Amazon Bedrock
    • Google Cloud's Vertex AI

    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

    Learn

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

    Help and security

    • Availability
    • Status
    • Support
    • Discord

    Terms and policies

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

    Agent SDKリファレンス - Python

    Python Agent SDKの完全なAPIリファレンス。すべての関数、型、クラスを含みます。

    インストール

    pip install claude-agent-sdk

    query() と ClaudeSDKClient の選択

    Python SDKは、Claude Codeと対話するための2つの方法を提供します:

    クイック比較

    機能query()ClaudeSDKClient
    セッション毎回新しいセッションを作成同じセッションを再利用
    会話単一のやり取り同じコンテキストで複数のやり取り
    接続自動的に管理手動制御
    ストリーミング入力✅ サポート✅ サポート
    中断❌ 非サポート✅ サポート
    フック❌ 非サポート✅ サポート
    カスタムツール❌ 非サポート✅ サポート
    チャット継続❌ 毎回新しいセッション✅ 会話を維持
    ユースケース単発タスク継続的な会話

    query() を使用する場合(毎回新しいセッション)

    最適な用途:

    • 会話履歴が不要な単発の質問
    • 以前のやり取りからのコンテキストを必要としない独立したタスク
    • シンプルな自動化スクリプト
    • 毎回新しく始めたい場合

    ClaudeSDKClient を使用する場合(継続的な会話)

    最適な用途:

    • 会話の継続 - Claudeにコンテキストを記憶させる必要がある場合
    • フォローアップの質問 - 以前の応答に基づいて構築する場合
    • インタラクティブなアプリケーション - チャットインターフェース、REPL
    • 応答駆動のロジック - 次のアクションがClaudeの応答に依存する場合
    • セッション制御 - 会話のライフサイクルを明示的に管理する場合

    関数

    query()

    Claude Codeとの各インタラクションごとに新しいセッションを作成します。メッセージが到着するたびにそれを生成する非同期イテレータを返します。query() の各呼び出しは、以前のインタラクションの記憶なしに新しく開始されます。

    async def query(
        *,
        prompt: str | AsyncIterable[dict[str, Any]],
        options: ClaudeAgentOptions | None = None
    ) -> AsyncIterator[Message]

    パラメータ

    パラメータ型説明
    promptstr | AsyncIterable[dict]文字列としての入力プロンプト、またはストリーミングモード用の非同期イテラブル
    optionsClaudeAgentOptions | Noneオプションの設定オブジェクト(Noneの場合は ClaudeAgentOptions() がデフォルト)

    戻り値

    会話からのメッセージを生成する AsyncIterator[Message] を返します。

    例 - オプション付き

    
    import asyncio
    from claude_agent_sdk import query, ClaudeAgentOptions
    
    async def main():
        options = ClaudeAgentOptions(
            system_prompt="You are an expert Python developer",
            permission_mode='acceptEdits',
            cwd="/home/user/project"
        )
    
        async for message in query(
            prompt="Create a Python web server",
            options=options
        ):
            print(message)
    
    
    asyncio.run(main())

    tool()

    型安全性を備えたMCPツールを定義するためのデコレータ。

    def tool(
        name: str,
        description: str,
        input_schema: type | dict[str, Any]
    ) -> Callable[[Callable[[Any], Awaitable[dict[str, Any]]]], SdkMcpTool[Any]]

    パラメータ

    パラメータ型説明
    namestrツールの一意の識別子
    descriptionstrツールの機能を説明する人間が読める説明
    input_schematype | dict[str, Any]ツールの入力パラメータを定義するスキーマ(以下参照)

    入力スキーマオプション

    1. シンプルな型マッピング(推奨):

      {"text": str, "count": int, "enabled": bool}
    2. JSONスキーマ形式(複雑なバリデーション用):

      {
          "type": "object",
          "properties": {
              "text": {"type": "string"},
              "count": {"type": "integer", "minimum": 0}
          },
          "required": ["text"]
      }

    戻り値

    ツールの実装をラップし、SdkMcpTool インスタンスを返すデコレータ関数。

    例

    from claude_agent_sdk import tool
    from typing import Any
    
    @tool("greet", "Greet a user", {"name": str})
    async def greet(args: dict[str, Any]) -> dict[str, Any]:
        return {
            "content": [{
                "type": "text",
                "text": f"Hello, {args['name']}!"
            }]
        }

    create_sdk_mcp_server()

    Pythonアプリケーション内で実行されるインプロセスMCPサーバーを作成します。

    def create_sdk_mcp_server(
        name: str,
        version: str = "1.0.0",
        tools: list[SdkMcpTool[Any]] | None = None
    ) -> McpSdkServerConfig

    パラメータ

    パラメータ型デフォルト説明
    namestr-サーバーの一意の識別子
    versionstr"1.0.0"サーバーバージョン文字列
    toolslist[SdkMcpTool[Any]] | NoneNone@tool デコレータで作成されたツール関数のリスト

    戻り値

    ClaudeAgentOptions.mcp_servers に渡すことができる McpSdkServerConfig オブジェクトを返します。

    例

    from claude_agent_sdk import tool, create_sdk_mcp_server
    
    @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']}"
            }]
        }
    
    calculator = create_sdk_mcp_server(
        name="calculator",
        version="2.0.0",
        tools=[add, multiply]  # Pass decorated functions
    )
    
    # Use with Claude
    options = ClaudeAgentOptions(
        mcp_servers={"calc": calculator},
        allowed_tools=["mcp__calc__add", "mcp__calc__multiply"]
    )

    クラス

    ClaudeSDKClient

    複数のやり取りにわたって会話セッションを維持します。 これは、TypeScript SDKの query() 関数が内部的に動作する方法のPython版に相当します - 会話を継続できるクライアントオブジェクトを作成します。

    主な機能

    • セッションの継続性: 複数の query() 呼び出しにわたって会話コンテキストを維持
    • 同一会話: Claudeはセッション内の以前のメッセージを記憶
    • 中断サポート: 実行中のClaudeを停止可能
    • 明示的なライフサイクル: セッションの開始と終了を制御
    • 応答駆動フロー: 応答に反応してフォローアップを送信可能
    • カスタムツール&フック: カスタムツール(@tool デコレータで作成)とフックをサポート
    class ClaudeSDKClient:
        def __init__(self, options: ClaudeAgentOptions | None = None)
        async def connect(self, prompt: str | AsyncIterable[dict] | None = None) -> None
        async def query(self, prompt: str | AsyncIterable[dict], session_id: str = "default") -> None
        async def receive_messages(self) -> AsyncIterator[Message]
        async def receive_response(self) -> AsyncIterator[Message]
        async def interrupt(self) -> None
        async def rewind_files(self, user_message_uuid: str) -> None
        async def disconnect(self) -> None

    メソッド

    メソッド説明
    __init__(options)オプションの設定でクライアントを初期化
    connect(prompt)オプションの初期プロンプトまたはメッセージストリームでClaudeに接続
    query(prompt, session_id)ストリーミングモードで新しいリクエストを送信
    receive_messages()Claudeからのすべてのメッセージを非同期イテレータとして受信
    receive_response()ResultMessageを含むまでメッセージを受信
    interrupt()中断シグナルを送信(ストリーミングモードでのみ動作)
    rewind_files(user_message_uuid)指定されたユーザーメッセージの状態にファイルを復元。enable_file_checkpointing=True が必要。ファイルチェックポイントを参照
    disconnect()Claudeから切断

    コンテキストマネージャーサポート

    クライアントは、自動接続管理のための非同期コンテキストマネージャーとして使用できます:

    async with ClaudeSDKClient() as client:
        await client.query("Hello Claude")
        async for message in client.receive_response():
            print(message)

    重要: メッセージをイテレートする際、break を使用して早期に終了することは避けてください。asyncioのクリーンアップの問題が発生する可能性があります。代わりに、イテレーションを自然に完了させるか、必要なものが見つかったことを追跡するフラグを使用してください。

    例 - 会話の継続

    import asyncio
    from claude_agent_sdk import ClaudeSDKClient, AssistantMessage, TextBlock, ResultMessage
    
    async def main():
        async with ClaudeSDKClient() as client:
            # 最初の質問
            await client.query("What's the capital of France?")
    
            # 応答を処理
            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}")
    
            # フォローアップの質問 - Claudeは以前のコンテキストを記憶している
            await client.query("What's the population of that city?")
    
            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}")
    
            # さらにフォローアップ - まだ同じ会話内
            await client.query("What are some famous landmarks there?")
    
            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}")
    
    asyncio.run(main())

    例 - ClaudeSDKClientでのストリーミング入力

    import asyncio
    from claude_agent_sdk import ClaudeSDKClient
    
    async def message_stream():
        """メッセージを動的に生成。"""
        yield {"type": "text", "text": "Analyze the following data:"}
        await asyncio.sleep(0.5)
        yield {"type": "text", "text": "Temperature: 25°C"}
        await asyncio.sleep(0.5)
        yield {"type": "text", "text": "Humidity: 60%"}
        await asyncio.sleep(0.5)
        yield {"type": "text", "text": "What patterns do you see?"}
    
    async def main():
        async with ClaudeSDKClient() as client:
            # Claudeに入力をストリーミング
            await client.query(message_stream())
    
            # 応答を処理
            async for message in client.receive_response():
                print(message)
    
            # 同じセッションでフォローアップ
            await client.query("Should we be concerned about these readings?")
    
            async for message in client.receive_response():
                print(message)
    
    asyncio.run(main())

    例 - 中断の使用

    import asyncio
    from claude_agent_sdk import ClaudeSDKClient, ClaudeAgentOptions
    
    async def interruptible_task():
        options = ClaudeAgentOptions(
            allowed_tools=["Bash"],
            permission_mode="acceptEdits"
        )
    
        async with ClaudeSDKClient(options=options) as client:
            # 長時間実行タスクを開始
            await client.query("Count from 1 to 100 slowly")
    
            # しばらく実行させる
            await asyncio.sleep(2)
    
            # タスクを中断
            await client.interrupt()
            print("Task interrupted!")
    
            # 新しいコマンドを送信
            await client.query("Just say hello instead")
    
            async for message in client.receive_response():
                # 新しい応答を処理
                pass
    
    asyncio.run(interruptible_task())

    例 - 高度な権限制御

    from claude_agent_sdk import (
        ClaudeSDKClient,
        ClaudeAgentOptions
    )
    from claude_agent_sdk.types import PermissionResultAllow, PermissionResultDeny
    
    async def custom_permission_handler(
        tool_name: str,
        input_data: dict,
        context: dict
    ) -> PermissionResultAllow | PermissionResultDeny:
        """ツール権限のカスタムロジック。"""
    
        # システムディレクトリへの書き込みをブロック
        if tool_name == "Write" and input_data.get("file_path", "").startswith("/system/"):
            return PermissionResultDeny(
                message="System directory write not allowed",
                interrupt=True
            )
    
        # 機密ファイル操作をリダイレクト
        if tool_name in ["Write", "Edit"] and "config" in input_data.get("file_path", ""):
            safe_path = f"./sandbox/{input_data['file_path']}"
            return PermissionResultAllow(
                updated_input={**input_data, "file_path": safe_path}
            )
    
        # その他はすべて許可
        return PermissionResultAllow(updated_input=input_data)
    
    async def main():
        options = ClaudeAgentOptions(
            can_use_tool=custom_permission_handler,
            allowed_tools=["Read", "Write", "Edit"]
        )
    
        async with ClaudeSDKClient(options=options) as client:
            await client.query("Update the system config file")
    
            async for message in client.receive_response():
                # サンドボックスパスを代わりに使用
                print(message)
    
    asyncio.run(main())

    型

    SdkMcpTool

    @tool デコレータで作成されたSDK MCPツールの定義。

    @dataclass
    class SdkMcpTool(Generic[T]):
        name: str
        description: str
        input_schema: type[T] | dict[str, Any]
        handler: Callable[[T], Awaitable[dict[str, Any]]]
    プロパティ型説明
    namestrツールの一意の識別子
    descriptionstr人間が読める説明
    input_schematype[T] | dict[str, Any]入力バリデーション用スキーマ
    handlerCallable[[T], Awaitable[dict[str, Any]]]ツール実行を処理する非同期関数

    ClaudeAgentOptions

    Claude Codeクエリ用の設定データクラス。

    @dataclass
    class ClaudeAgentOptions:
        tools: list[str] | ToolsPreset | None = None
        allowed_tools: list[str] = field(default_factory=list)
        system_prompt: str | SystemPromptPreset | None = None
        mcp_servers: dict[str, McpServerConfig] | str | Path = field(default_factory=dict)
        permission_mode: PermissionMode | None = None
        continue_conversation: bool = False
        resume: str | None = None
        max_turns: int | None = None
        max_budget_usd: float | None = None
        disallowed_tools: list[str] = field(default_factory=list)
        model: str | None = None
        fallback_model: str | None = None
        betas: list[SdkBeta] = field(default_factory=list)
        output_format: OutputFormat | None = None
        permission_prompt_tool_name: str | None = None
        cwd: str | Path | None = None
        cli_path: str | Path | None = None
        settings: str | None = None
        add_dirs: list[str | Path] = field(default_factory=list)
        env: dict[str, str] = field(default_factory=dict)
        extra_args: dict[str, str | None] = field(default_factory=dict)
        max_buffer_size: int | None = None
        debug_stderr: Any = sys.stderr  # Deprecated
        stderr: Callable[[str], None] | None = None
        can_use_tool: CanUseTool | None = None
        hooks: dict[HookEvent, list[HookMatcher]] | None = None
        user: str | None = None
        include_partial_messages: bool = False
        fork_session: bool = False
        agents: dict[str, AgentDefinition] | None = None
        setting_sources: list[SettingSource] | None = None
        max_thinking_tokens: int | None = None
    プロパティ型デフォルト説明
    toolslist[str] | ToolsPreset | NoneNoneツール設定。Claude Codeのデフォルトツールには {"type": "preset", "preset": "claude_code"} を使用
    allowed_toolslist[str][]許可されたツール名のリスト
    system_promptstr | SystemPromptPreset | NoneNoneシステムプロンプト設定。カスタムプロンプトには文字列を渡すか、Claude Codeのシステムプロンプトには {"type": "preset", "preset": "claude_code"} を使用。プリセットを拡張するには "append" を追加
    mcp_serversdict[str, McpServerConfig] | str | Path{}MCPサーバー設定または設定ファイルへのパス
    permission_modePermissionMode | NoneNoneツール使用の権限モード
    continue_conversationboolFalse最新の会話を継続
    resumestr | NoneNone再開するセッションID
    max_turnsint | NoneNone最大会話ターン数
    max_budget_usdfloat | NoneNoneセッションの最大予算(USD)
    disallowed_toolslist[str][]禁止されたツール名のリスト
    enable_file_checkpointingboolFalse巻き戻し用のファイル変更追跡を有効化。ファイルチェックポイントを参照
    modelstr | NoneNone使用するClaudeモデル
    fallback_modelstr | NoneNoneプライマリモデルが失敗した場合に使用するフォールバックモデル
    betaslist[SdkBeta][]有効にするベータ機能。利用可能なオプションについては SdkBeta を参照
    output_formatOutputFormat | NoneNoneエージェント結果の出力形式を定義。詳細は構造化出力を参照
    permission_prompt_tool_namestr | NoneNone権限プロンプト用のMCPツール名
    cwdstr | Path | NoneNone現在の作業ディレクトリ
    cli_pathstr | Path | NoneNoneClaude Code CLI実行ファイルへのカスタムパス
    settingsstr | NoneNone設定ファイルへのパス
    add_dirslist[str | Path][]Claudeがアクセスできる追加ディレクトリ
    envdict[str, str]{}環境変数
    extra_argsdict[str, str | None]{}CLIに直接渡す追加のCLI引数
    max_buffer_sizeint | NoneNoneCLI stdoutをバッファリングする際の最大バイト数
    debug_stderrAnysys.stderr非推奨 - デバッグ出力用のファイルライクオブジェクト。代わりに stderr コールバックを使用
    stderrCallable[[str], None] | NoneNoneCLIからのstderr出力用コールバック関数
    can_use_toolCanUseTool | NoneNoneツール権限コールバック関数。詳細は権限タイプを参照
    hooksdict[HookEvent, list[HookMatcher]] | NoneNoneイベントをインターセプトするためのフック設定
    userstr | NoneNoneユーザー識別子
    include_partial_messagesboolFalse部分メッセージストリーミングイベントを含める。有効にすると、StreamEvent メッセージが生成される
    fork_sessionboolFalseresume で再開する際、元のセッションを継続する代わりに新しいセッションIDにフォーク
    agentsdict[str, AgentDefinition] | NoneNoneプログラムで定義されたサブエージェント
    pluginslist[SdkPluginConfig][]ローカルパスからカスタムプラグインを読み込む。詳細はプラグインを参照
    sandboxSandboxSettings | NoneNoneサンドボックスの動作をプログラムで設定。詳細はサンドボックス設定を参照
    setting_sourceslist[SettingSource] | NoneNone(設定なし)読み込むファイルシステム設定を制御。省略時は設定が読み込まれない。注意: CLAUDE.mdファイルを読み込むには "project" を含める必要がある
    max_thinking_tokensint | NoneNone思考ブロックの最大トークン数

    OutputFormat

    構造化出力バリデーション用の設定。

    class OutputFormat(TypedDict):
        type: Literal["json_schema"]
        schema: dict[str, Any]
    フィールド必須説明
    typeはいJSONスキーマバリデーション用に "json_schema" でなければならない
    schemaはい出力バリデーション用のJSONスキーマ定義

    SystemPromptPreset

    Claude Codeのプリセットシステムプロンプトをオプションの追加付きで使用するための設定。

    class SystemPromptPreset(TypedDict):
        type: Literal["preset"]
        preset: Literal["claude_code"]
        append: NotRequired[str]
    フィールド必須説明
    typeはいプリセットシステムプロンプトを使用するには "preset" でなければならない
    presetはいClaude Codeのシステムプロンプトを使用するには "claude_code" でなければならない
    appendいいえプリセットシステムプロンプトに追加する追加の指示

    SettingSource

    SDKが設定を読み込むファイルシステムベースの設定ソースを制御します。

    SettingSource = Literal["user", "project", "local"]
    値説明場所
    "user"グローバルユーザー設定~/.claude/settings.json
    "project"共有プロジェクト設定(バージョン管理対象).claude/settings.json
    "local"ローカルプロジェクト設定(gitignore対象).claude/settings.local.json

    デフォルトの動作

    setting_sources が省略または**Noneの場合、SDKはファイルシステム設定を読み込みません**。これにより、SDKアプリケーションの分離が提供されます。

    setting_sourcesを使用する理由

    すべてのファイルシステム設定を読み込む(レガシー動作):

    # SDK v0.0.xのようにすべての設定を読み込む
    from claude_agent_sdk import query, ClaudeAgentOptions
    
    async for message in query(
        prompt="Analyze this code",
        options=ClaudeAgentOptions(
            setting_sources=["user", "project", "local"]  # すべての設定を読み込む
        )
    ):
        print(message)

    特定の設定ソースのみを読み込む:

    # プロジェクト設定のみを読み込み、ユーザーとローカルは無視
    async for message in query(
        prompt="Run CI checks",
        options=ClaudeAgentOptions(
            setting_sources=["project"]  # .claude/settings.json のみ
        )
    ):
        print(message)

    テストおよびCI環境:

    # ローカル設定を除外してCIでの一貫した動作を確保
    async for message in query(
        prompt="Run tests",
        options=ClaudeAgentOptions(
            setting_sources=["project"],  # チーム共有設定のみ
            permission_mode="bypassPermissions"
        )
    ):
        print(message)

    SDK専用アプリケーション:

    # すべてをプログラムで定義(デフォルトの動作)
    # ファイルシステム依存なし - setting_sourcesはデフォルトでNone
    async for message in query(
        prompt="Review this PR",
        options=ClaudeAgentOptions(
            # setting_sources=None がデフォルト、指定不要
            agents={ /* ... */ },
            mcp_servers={ /* ... */ },
            allowed_tools=["Read", "Grep", "Glob"]
        )
    ):
        print(message)

    CLAUDE.mdプロジェクト指示の読み込み:

    # CLAUDE.mdファイルを含めるためにプロジェクト設定を読み込む
    async for message in query(
        prompt="Add a new feature following project conventions",
        options=ClaudeAgentOptions(
            system_prompt={
                "type": "preset",
                "preset": "claude_code"  # Claude Codeのシステムプロンプトを使用
            },
            setting_sources=["project"],  # プロジェクトからCLAUDE.mdを読み込むために必要
            allowed_tools=["Read", "Write", "Edit"]
        )
    ):
        print(message)

    設定の優先順位

    複数のソースが読み込まれる場合、設定は以下の優先順位でマージされます(高い順):

    1. ローカル設定(.claude/settings.local.json)
    2. プロジェクト設定(.claude/settings.json)
    3. ユーザー設定(~/.claude/settings.json)

    プログラムオプション(agents、allowed_tools など)は常にファイルシステム設定を上書きします。

    AgentDefinition

    プログラムで定義されたサブエージェントの設定。

    @dataclass
    class AgentDefinition:
        description: str
        prompt: str
        tools: list[str] | None = None
        model: Literal["sonnet", "opus", "haiku", "inherit"] | None = None
    フィールド必須説明
    descriptionはいこのエージェントをいつ使用するかの自然言語による説明
    toolsいいえ許可されたツール名の配列。省略時はすべてのツールを継承
    promptはいエージェントのシステムプロンプト
    modelいいえこのエージェントのモデルオーバーライド。省略時はメインモデルを使用

    PermissionMode

    ツール実行を制御するための権限モード。

    PermissionMode = Literal[
        "default",           # 標準の権限動作
        "acceptEdits",       # ファイル編集を自動承認
        "plan",              # 計画モード - 実行なし
        "bypassPermissions"  # すべての権限チェックをバイパス(注意して使用)
    ]

    CanUseTool

    ツール権限コールバック関数の型エイリアス。

    CanUseTool = Callable[
        [str, dict[str, Any], ToolPermissionContext],
        Awaitable[PermissionResult]
    ]

    コールバックは以下を受け取ります:

    • tool_name: 呼び出されるツールの名前
    • input_data: ツールの入力パラメータ
    • context: 追加情報を含む ToolPermissionContext

    PermissionResult(PermissionResultAllow または PermissionResultDeny)を返します。

    ToolPermissionContext

    ツール権限コールバックに渡されるコンテキスト情報。

    @dataclass
    class ToolPermissionContext:
        signal: Any | None = None  # Future: abort signal support
        suggestions: list[PermissionUpdate] = field(default_factory=list)
    フィールド型説明
    signalAny | None将来のアボートシグナルサポート用に予約済み
    suggestionslist[PermissionUpdate]CLIからの権限更新の提案

    PermissionResult

    権限コールバック結果のユニオン型。

    PermissionResult = PermissionResultAllow | PermissionResultDeny

    PermissionResultAllow

    ツール呼び出しを許可すべきことを示す結果。

    @dataclass
    class PermissionResultAllow:
        behavior: Literal["allow"] = "allow"
        updated_input: dict[str, Any] | None = None
        updated_permissions: list[PermissionUpdate] | None = None
    フィールド型デフォルト説明
    behaviorLiteral["allow"]"allow""allow" でなければならない
    updated_inputdict[str, Any] | NoneNone元の入力の代わりに使用する変更された入力
    updated_permissionslist[PermissionUpdate] | NoneNone適用する権限の更新

    PermissionResultDeny

    ツール呼び出しを拒否すべきことを示す結果。

    @dataclass
    class PermissionResultDeny:
        behavior: Literal["deny"] = "deny"
        message: str = ""
        interrupt: bool = False
    フィールド型デフォルト説明
    behaviorLiteral["deny"]"deny""deny" でなければならない
    messagestr""ツールが拒否された理由を説明するメッセージ
    interruptboolFalse現在の実行を中断するかどうか

    PermissionUpdate

    プログラムによる権限更新の設定。

    @dataclass
    class PermissionUpdate:
        type: Literal[
            "addRules",
            "replaceRules",
            "removeRules",
            "setMode",
            "addDirectories",
            "removeDirectories",
        ]
        rules: list[PermissionRuleValue] | None = None
        behavior: Literal["allow", "deny", "ask"] | None = None
        mode: PermissionMode | None = None
        directories: list[str] | None = None
        destination: Literal["userSettings", "projectSettings", "localSettings", "session"] | None = None
    フィールド型説明
    typeLiteral[...]権限更新操作の種類
    ruleslist[PermissionRuleValue] | None追加/置換/削除操作のルール
    behaviorLiteral["allow", "deny", "ask"] | Noneルールベース操作の動作
    modePermissionMode | NonesetMode操作のモード
    directorieslist[str] | Noneディレクトリの追加/削除操作のディレクトリ
    destinationLiteral[...] | None権限更新を適用する場所

    SdkBeta

    SDKベータ機能のリテラル型。

    SdkBeta = Literal["context-1m-2025-08-07"]

    ClaudeAgentOptions の betas フィールドと共に使用してベータ機能を有効にします。

    McpSdkServerConfig

    create_sdk_mcp_server() で作成されたSDK MCPサーバーの設定。

    class McpSdkServerConfig(TypedDict):
        type: Literal["sdk"]
        name: str
        instance: Any  # MCP Server instance

    McpServerConfig

    MCPサーバー設定のユニオン型。

    McpServerConfig = McpStdioServerConfig | McpSSEServerConfig | McpHttpServerConfig | McpSdkServerConfig

    McpStdioServerConfig

    class McpStdioServerConfig(TypedDict):
        type: NotRequired[Literal["stdio"]]  # Optional for backwards compatibility
        command: str
        args: NotRequired[list[str]]
        env: NotRequired[dict[str, str]]

    McpSSEServerConfig

    class McpSSEServerConfig(TypedDict):
        type: Literal["sse"]
        url: str
        headers: NotRequired[dict[str, str]]

    McpHttpServerConfig

    class McpHttpServerConfig(TypedDict):
        type: Literal["http"]
        url: str
        headers: NotRequired[dict[str, str]]

    SdkPluginConfig

    SDKでプラグインを読み込むための設定。

    class SdkPluginConfig(TypedDict):
        type: Literal["local"]
        path: str
    フィールド型説明
    typeLiteral["local"]"local" でなければならない(現在ローカルプラグインのみサポート)
    pathstrプラグインディレクトリへの絶対パスまたは相対パス

    例:

    plugins=[
        {"type": "local", "path": "./my-plugin"},
        {"type": "local", "path": "/absolute/path/to/plugin"}
    ]

    プラグインの作成と使用に関する完全な情報については、プラグインを参照してください。

    メッセージ型

    Message

    すべての可能なメッセージのユニオン型。

    Message = UserMessage | AssistantMessage | SystemMessage | ResultMessage | StreamEvent

    UserMessage

    ユーザー入力メッセージ。

    @dataclass
    class UserMessage:
        content: str | list[ContentBlock]

    AssistantMessage

    コンテンツブロックを含むアシスタント応答メッセージ。

    @dataclass
    class AssistantMessage:
        content: list[ContentBlock]
        model: str

    SystemMessage

    メタデータを含むシステムメッセージ。

    @dataclass
    class SystemMessage:
        subtype: str
        data: dict[str, Any]

    ResultMessage

    コストと使用量情報を含む最終結果メッセージ。

    @dataclass
    class ResultMessage:
        subtype: str
        duration_ms: int
        duration_api_ms: int
        is_error: bool
        num_turns: int
        session_id: str
        total_cost_usd: float | None = None
        usage: dict[str, Any] | None = None
        result: str | None = None
        structured_output: Any = None

    StreamEvent

    ストリーミング中の部分的なメッセージ更新のためのストリームイベント。ClaudeAgentOptions で include_partial_messages=True の場合にのみ受信されます。

    @dataclass
    class StreamEvent:
        uuid: str
        session_id: str
        event: dict[str, Any]  # The raw Anthropic API stream event
        parent_tool_use_id: str | None = None
    フィールド型説明
    uuidstrこのイベントの一意識別子
    session_idstrセッション識別子
    eventdict[str, Any]生のAnthropic APIストリームイベントデータ
    parent_tool_use_idstr | Noneこのイベントがサブエージェントからのものである場合の親ツール使用ID

    コンテンツブロック型

    ContentBlock

    すべてのコンテンツブロックのユニオン型。

    ContentBlock = TextBlock | ThinkingBlock | ToolUseBlock | ToolResultBlock

    TextBlock

    テキストコンテンツブロック。

    @dataclass
    class TextBlock:
        text: str

    ThinkingBlock

    思考コンテンツブロック(思考機能を持つモデル用)。

    @dataclass
    class ThinkingBlock:
        thinking: str
        signature: str

    ToolUseBlock

    ツール使用リクエストブロック。

    @dataclass
    class ToolUseBlock:
        id: str
        name: str
        input: dict[str, Any]

    ToolResultBlock

    ツール実行結果ブロック。

    @dataclass
    class ToolResultBlock:
        tool_use_id: str
        content: str | list[dict[str, Any]] | None = None
        is_error: bool | None = None

    エラー型

    ClaudeSDKError

    すべてのSDKエラーの基底例外クラス。

    class ClaudeSDKError(Exception):
        """Base error for Claude SDK."""

    CLINotFoundError

    Claude Code CLIがインストールされていないか見つからない場合に発生します。

    class CLINotFoundError(CLIConnectionError):
        def __init__(self, message: str = "Claude Code not found", cli_path: str | None = None):
            """
            Args:
                message: Error message (default: "Claude Code not found")
                cli_path: Optional path to the CLI that was not found
            """

    CLIConnectionError

    Claude Codeへの接続が失敗した場合に発生します。

    class CLIConnectionError(ClaudeSDKError):
        """Failed to connect to Claude Code."""

    ProcessError

    Claude Codeプロセスが失敗した場合に発生します。

    class ProcessError(ClaudeSDKError):
        def __init__(self, message: str, exit_code: int | None = None, stderr: str | None = None):
            self.exit_code = exit_code
            self.stderr = stderr

    CLIJSONDecodeError

    JSONパースが失敗した場合に発生します。

    class CLIJSONDecodeError(ClaudeSDKError):
        def __init__(self, line: str, original_error: Exception):
            """
            Args:
                line: The line that failed to parse
                original_error: The original JSON decode exception
            """
            self.line = line
            self.original_error = original_error

    フック型

    フックの使用方法に関する包括的なガイド(例と一般的なパターンを含む)については、フックガイドを参照してください。

    HookEvent

    サポートされるフックイベント型。セットアップの制限により、Python SDKはSessionStart、SessionEnd、およびNotificationフックをサポートしていないことに注意してください。

    HookEvent = Literal[
        "PreToolUse",      # Called before tool execution
        "PostToolUse",     # Called after tool execution
        "UserPromptSubmit", # Called when user submits a prompt
        "Stop",            # Called when stopping execution
        "SubagentStop",    # Called when a subagent stops
        "PreCompact"       # Called before message compaction
    ]

    HookCallback

    フックコールバック関数の型定義。

    HookCallback = Callable[
        [dict[str, Any], str | None, HookContext],
        Awaitable[dict[str, Any]]
    ]

    パラメータ:

    • input_data: フック固有の入力データ(フックガイドを参照)
    • tool_use_id: オプションのツール使用識別子(ツール関連のフック用)
    • context: 追加情報を含むフックコンテキスト

    以下を含む可能性のある辞書を返します:

    • decision: アクションをブロックする場合は "block"
    • systemMessage: トランスクリプトに追加するシステムメッセージ
    • hookSpecificOutput: フック固有の出力データ

    HookContext

    フックコールバックに渡されるコンテキスト情報。

    @dataclass
    class HookContext:
        signal: Any | None = None  # Future: abort signal support

    HookMatcher

    特定のイベントやツールにフックをマッチングするための設定。

    @dataclass
    class HookMatcher:
        matcher: str | None = None        # Tool name or pattern to match (e.g., "Bash", "Write|Edit")
        hooks: list[HookCallback] = field(default_factory=list)  # List of callbacks to execute
        timeout: float | None = None        # Timeout in seconds for all hooks in this matcher (default: 60)

    HookInput

    すべてのフック入力型のユニオン型。実際の型は hook_event_name フィールドに依存します。

    HookInput = (
        PreToolUseHookInput
        | PostToolUseHookInput
        | UserPromptSubmitHookInput
        | StopHookInput
        | SubagentStopHookInput
        | PreCompactHookInput
    )

    BaseHookInput

    すべてのフック入力型に存在する基本フィールド。

    class BaseHookInput(TypedDict):
        session_id: str
        transcript_path: str
        cwd: str
        permission_mode: NotRequired[str]
    フィールド型説明
    session_idstr現在のセッション識別子
    transcript_pathstrセッショントランスクリプトファイルへのパス
    cwdstr現在の作業ディレクトリ
    permission_modestr(オプション)現在の権限モード

    PreToolUseHookInput

    PreToolUse フックイベントの入力データ。

    class PreToolUseHookInput(BaseHookInput):
        hook_event_name: Literal["PreToolUse"]
        tool_name: str
        tool_input: dict[str, Any]
    フィールド型説明
    hook_event_nameLiteral["PreToolUse"]常に "PreToolUse"
    tool_namestr実行されようとしているツールの名前
    tool_inputdict[str, Any]ツールの入力パラメータ

    PostToolUseHookInput

    PostToolUse フックイベントの入力データ。

    class PostToolUseHookInput(BaseHookInput):
        hook_event_name: Literal["PostToolUse"]
        tool_name: str
        tool_input: dict[str, Any]
        tool_response: Any
    フィールド型説明
    hook_event_nameLiteral["PostToolUse"]常に "PostToolUse"
    tool_namestr実行されたツールの名前
    tool_inputdict[str, Any]使用された入力パラメータ
    tool_responseAnyツール実行からの応答

    UserPromptSubmitHookInput

    UserPromptSubmit フックイベントの入力データ。

    class UserPromptSubmitHookInput(BaseHookInput):
        hook_event_name: Literal["UserPromptSubmit"]
        prompt: str
    フィールド型説明
    hook_event_nameLiteral["UserPromptSubmit"]常に "UserPromptSubmit"
    promptstrユーザーが送信したプロンプト

    StopHookInput

    Stop フックイベントの入力データ。

    class StopHookInput(BaseHookInput):
        hook_event_name: Literal["Stop"]
        stop_hook_active: bool
    フィールド型説明
    hook_event_nameLiteral["Stop"]常に "Stop"
    stop_hook_activeboolストップフックがアクティブかどうか

    SubagentStopHookInput

    SubagentStop フックイベントの入力データ。

    class SubagentStopHookInput(BaseHookInput):
        hook_event_name: Literal["SubagentStop"]
        stop_hook_active: bool
    フィールド型説明
    hook_event_nameLiteral["SubagentStop"]常に "SubagentStop"
    stop_hook_activeboolストップフックがアクティブかどうか

    PreCompactHookInput

    PreCompact フックイベントの入力データ。

    class PreCompactHookInput(BaseHookInput):
        hook_event_name: Literal["PreCompact"]
        trigger: Literal["manual", "auto"]
        custom_instructions: str | None
    フィールド型説明
    hook_event_nameLiteral["PreCompact"]常に "PreCompact"
    triggerLiteral["manual", "auto"]コンパクションをトリガーしたもの
    custom_instructionsstr | Noneコンパクション用のカスタム指示

    HookJSONOutput

    フックコールバック戻り値のユニオン型。

    HookJSONOutput = AsyncHookJSONOutput | SyncHookJSONOutput

    SyncHookJSONOutput

    制御フィールドと決定フィールドを含む同期フック出力。

    class SyncHookJSONOutput(TypedDict):
        # Control fields
        continue_: NotRequired[bool]      # Whether to proceed (default: True)
        suppressOutput: NotRequired[bool] # Hide stdout from transcript
        stopReason: NotRequired[str]      # Message when continue is False
    
        # Decision fields
        decision: NotRequired[Literal["block"]]
        systemMessage: NotRequired[str]   # Warning message for user
        reason: NotRequired[str]          # Feedback for Claude
    
        # Hook-specific output
        hookSpecificOutput: NotRequired[dict[str, Any]]

    Pythonコードでは continue_(アンダースコア付き)を使用してください。CLIに送信される際に自動的に continue に変換されます。

    AsyncHookJSONOutput

    フック実行を遅延させる非同期フック出力。

    class AsyncHookJSONOutput(TypedDict):
        async_: Literal[True]             # Set to True to defer execution
        asyncTimeout: NotRequired[int]    # Timeout in milliseconds

    Pythonコードでは async_(アンダースコア付き)を使用してください。CLIに送信される際に自動的に async に変換されます。

    フック使用例

    この例では2つのフックを登録しています:rm -rf / のような危険なbashコマンドをブロックするものと、監査のためにすべてのツール使用をログに記録するものです。セキュリティフックは(matcher を介して)Bashコマンドに対してのみ実行され、ログフックはすべてのツールに対して実行されます。

    from claude_agent_sdk import query, ClaudeAgentOptions, HookMatcher, HookContext
    from typing import Any
    
    async def validate_bash_command(
        input_data: dict[str, Any],
        tool_use_id: str | None,
        context: HookContext
    ) -> dict[str, Any]:
        """Validate and potentially block dangerous bash commands."""
        if input_data['tool_name'] == 'Bash':
            command = input_data['tool_input'].get('command', '')
            if 'rm -rf /' in command:
                return {
                    'hookSpecificOutput': {
                        'hookEventName': 'PreToolUse',
                        'permissionDecision': 'deny',
                        'permissionDecisionReason': 'Dangerous command blocked'
                    }
                }
        return {}
    
    async def log_tool_use(
        input_data: dict[str, Any],
        tool_use_id: str | None,
        context: HookContext
    ) -> dict[str, Any]:
        """Log all tool usage for auditing."""
        print(f"Tool used: {input_data.get('tool_name')}")
        return {}
    
    options = ClaudeAgentOptions(
        hooks={
            'PreToolUse': [
                HookMatcher(matcher='Bash', hooks=[validate_bash_command], timeout=120),  # 2 min for validation
                HookMatcher(hooks=[log_tool_use])  # Applies to all tools (default 60s timeout)
            ],
            'PostToolUse': [
                HookMatcher(hooks=[log_tool_use])
            ]
        }
    )
    
    async for message in query(
        prompt="Analyze this codebase",
        options=options
    ):
        print(message)

    ツール入出力型

    すべての組み込みClaude Codeツールの入出力スキーマのドキュメント。Python SDKはこれらを型としてエクスポートしませんが、メッセージ内のツール入出力の構造を表しています。

    Task

    ツール名: Task

    入力:

    {
        "description": str,      # A short (3-5 word) description of the task
        "prompt": str,           # The task for the agent to perform
        "subagent_type": str     # The type of specialized agent to use
    }

    出力:

    {
        "result": str,                    # Final result from the subagent
        "usage": dict | None,             # Token usage statistics
        "total_cost_usd": float | None,  # Total cost in USD
        "duration_ms": int | None         # Execution duration in milliseconds
    }

    AskUserQuestion

    ツール名: AskUserQuestion

    実行中にユーザーに明確化のための質問をします。使用方法の詳細については、承認とユーザー入力の処理を参照してください。

    入力:

    {
        "questions": [                    # Questions to ask the user (1-4 questions)
            {
                "question": str,          # The complete question to ask the user
                "header": str,            # Very short label displayed as a chip/tag (max 12 chars)
                "options": [              # The available choices (2-4 options)
                    {
                        "label": str,         # Display text for this option (1-5 words)
                        "description": str    # Explanation of what this option means
                    }
                ],
                "multiSelect": bool       # Set to true to allow multiple selections
            }
        ],
        "answers": dict | None            # User answers populated by the permission system
    }

    出力:

    {
        "questions": [                    # The questions that were asked
            {
                "question": str,
                "header": str,
                "options": [{"label": str, "description": str}],
                "multiSelect": bool
            }
        ],
        "answers": dict[str, str]         # Maps question text to answer string
                                          # Multi-select answers are comma-separated
    }

    Bash

    ツール名: Bash

    入力:

    {
        "command": str,                  # The command to execute
        "timeout": int | None,           # Optional timeout in milliseconds (max 600000)
        "description": str | None,       # Clear, concise description (5-10 words)
        "run_in_background": bool | None # Set to true to run in background
    }

    出力:

    {
        "output": str,              # Combined stdout and stderr output
        "exitCode": int,            # Exit code of the command
        "killed": bool | None,      # Whether command was killed due to timeout
        "shellId": str | None       # Shell ID for background processes
    }

    Edit

    ツール名: Edit

    入力:

    {
        "file_path": str,           # The absolute path to the file to modify
        "old_string": str,          # The text to replace
        "new_string": str,          # The text to replace it with
        "replace_all": bool | None  # Replace all occurrences (default False)
    }

    出力:

    {
        "message": str,      # Confirmation message
        "replacements": int, # Number of replacements made
        "file_path": str     # File path that was edited
    }

    Read

    ツール名: Read

    入力:

    {
        "file_path": str,       # The absolute path to the file to read
        "offset": int | None,   # The line number to start reading from
        "limit": int | None     # The number of lines to read
    }

    出力(テキストファイル):

    {
        "content": str,         # File contents with line numbers
        "total_lines": int,     # Total number of lines in file
        "lines_returned": int   # Lines actually returned
    }

    出力(画像):

    {
        "image": str,       # Base64 encoded image data
        "mime_type": str,   # Image MIME type
        "file_size": int    # File size in bytes
    }

    Write

    ツール名: Write

    入力:

    {
        "file_path": str,  # The absolute path to the file to write
        "content": str     # The content to write to the file
    }

    出力:

    {
        "message": str,        # Success message
        "bytes_written": int,  # Number of bytes written
        "file_path": str       # File path that was written
    }

    Glob

    ツール名: Glob

    入力:

    {
        "pattern": str,       # The glob pattern to match files against
        "path": str | None    # The directory to search in (defaults to cwd)
    }

    出力:

    {
        "matches": list[str],  # Array of matching file paths
        "count": int,          # Number of matches found
        "search_path": str     # Search directory used
    }

    Grep

    ツール名: Grep

    入力:

    {
        "pattern": str,                    # The regular expression pattern
        "path": str | None,                # File or directory to search in
        "glob": str | None,                # Glob pattern to filter files
        "type": str | None,                # File type to search
        "output_mode": str | None,         # "content", "files_with_matches", or "count"
        "-i": bool | None,                 # Case insensitive search
        "-n": bool | None,                 # Show line numbers
        "-B": int | None,                  # Lines to show before each match
        "-A": int | None,                  # Lines to show after each match
        "-C": int | None,                  # Lines to show before and after
        "head_limit": int | None,          # Limit output to first N lines/entries
        "multiline": bool | None           # Enable multiline mode
    }

    出力(contentモード):

    {
        "matches": [
            {
                "file": str,
                "line_number": int | None,
                "line": str,
                "before_context": list[str] | None,
                "after_context": list[str] | None
            }
        ],
        "total_matches": int
    }

    出力(files_with_matchesモード):

    {
        "files": list[str],  # Files containing matches
        "count": int         # Number of files with matches
    }

    NotebookEdit

    ツール名: NotebookEdit

    入力:

    {
        "notebook_path": str,                     # Absolute path to the Jupyter notebook
        "cell_id": str | None,                    # The ID of the cell to edit
        "new_source": str,                        # The new source for the cell
        "cell_type": "code" | "markdown" | None,  # The type of the cell
        "edit_mode": "replace" | "insert" | "delete" | None  # Edit operation type
    }

    出力:

    {
        "message": str,                              # Success message
        "edit_type": "replaced" | "inserted" | "deleted",  # Type of edit performed
        "cell_id": str | None,                       # Cell ID that was affected
        "total_cells": int                           # Total cells in notebook after edit
    }

    WebFetch

    ツール名: WebFetch

    入力:

    {
        "url": str,     # The URL to fetch content from
        "prompt": str   # The prompt to run on the fetched content
    }

    出力:

    {
        "response": str,           # AI model's response to the prompt
        "url": str,                # URL that was fetched
        "final_url": str | None,   # Final URL after redirects
        "status_code": int | None  # HTTP status code
    }

    WebSearch

    ツール名: WebSearch

    入力:

    {
        "query": str,                        # The search query to use
        "allowed_domains": list[str] | None, # Only include results from these domains
        "blocked_domains": list[str] | None  # Never include results from these domains
    }

    出力:

    {
        "results": [
            {
                "title": str,
                "url": str,
                "snippet": str,
                "metadata": dict | None
            }
        ],
        "total_results": int,
        "query": str
    }

    TodoWrite

    ツール名: TodoWrite

    入力:

    {
        "todos": [
            {
                "content": str,                              # The task description
                "status": "pending" | "in_progress" | "completed",  # Task status
                "activeForm": str                            # Active form of the description
            }
        ]
    }

    出力:

    {
        "message": str,  # Success message
        "stats": {
            "total": int,
            "pending": int,
            "in_progress": int,
            "completed": int
        }
    }

    BashOutput

    ツール名: BashOutput

    入力:

    {
        "bash_id": str,       # The ID of the background shell
        "filter": str | None  # Optional regex to filter output lines
    }

    出力:

    {
        "output": str,                                      # New output since last check
        "status": "running" | "completed" | "failed",       # Current shell status
        "exitCode": int | None                              # Exit code when completed
    }

    KillBash

    ツール名: KillBash

    入力:

    {
        "shell_id": str  # The ID of the background shell to kill
    }

    出力:

    {
        "message": str,  # Success message
        "shell_id": str  # ID of the killed shell
    }

    ExitPlanMode

    ツール名: ExitPlanMode

    入力:

    {
        "plan": str  # The plan to run by the user for approval
    }

    出力:

    {
        "message": str,          # Confirmation message
        "approved": bool | None  # Whether user approved the plan
    }

    ListMcpResources

    ツール名: ListMcpResources

    入力:

    {
        "server": str | None  # Optional server name to filter resources by
    }

    出力:

    {
        "resources": [
            {
                "uri": str,
                "name": str,
                "description": str | None,
                "mimeType": str | None,
                "server": str
            }
        ],
        "total": int
    }

    ReadMcpResource

    ツール名: ReadMcpResource

    入力:

    {
        "server": str,  # The MCP server name
        "uri": str      # The resource URI to read
    }

    出力:

    {
        "contents": [
            {
                "uri": str,
                "mimeType": str | None,
                "text": str | None,
                "blob": str | None
            }
        ],
        "server": str
    }

    ClaudeSDKClientを使用した高度な機能

    継続的な会話インターフェースの構築

    from claude_agent_sdk import ClaudeSDKClient, ClaudeAgentOptions, AssistantMessage, TextBlock
    import asyncio
    
    class ConversationSession:
        """Maintains a single conversation session with Claude."""
    
        def __init__(self, options: ClaudeAgentOptions = None):
            self.client = ClaudeSDKClient(options)
            self.turn_count = 0
    
        async def start(self):
            await self.client.connect()
            print("Starting conversation session. Claude will remember context.")
            print("Commands: 'exit' to quit, 'interrupt' to stop current task, 'new' for new session")
    
            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()
                    print("Task interrupted!")
                    continue
                elif user_input.lower() == 'new':
                    # Disconnect and reconnect for a fresh session
                    await self.client.disconnect()
                    await self.client.connect()
                    self.turn_count = 0
                    print("Started new conversation session (previous context cleared)")
                    continue
    
                # Send message - Claude remembers all previous messages in this session
                await self.client.query(user_input)
                self.turn_count += 1
    
                # Process response
                print(f"[Turn {self.turn_count}] Claude: ", end="")
                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()  # New line after response
    
            await self.client.disconnect()
            print(f"Conversation ended after {self.turn_count} turns.")
    
    async def main():
        options = ClaudeAgentOptions(
            allowed_tools=["Read", "Write", "Bash"],
            permission_mode="acceptEdits"
        )
        session = ConversationSession(options)
        await session.start()
    
    # Example conversation:
    # Turn 1 - You: "Create a file called hello.py"
    # Turn 1 - Claude: "I'll create a hello.py file for you..."
    # Turn 2 - You: "What's in that file?"
    # Turn 2 - Claude: "The hello.py file I just created contains..." (remembers!)
    # Turn 3 - You: "Add a main function to it"
    # Turn 3 - Claude: "I'll add a main function to hello.py..." (knows which file!)
    
    asyncio.run(main())

    フックを使用した動作の変更

    from claude_agent_sdk import (
        ClaudeSDKClient,
        ClaudeAgentOptions,
        HookMatcher,
        HookContext
    )
    import asyncio
    from typing import Any
    
    async def pre_tool_logger(
        input_data: dict[str, Any],
        tool_use_id: str | None,
        context: HookContext
    ) -> dict[str, Any]:
        """Log all tool usage before execution."""
        tool_name = input_data.get('tool_name', 'unknown')
        print(f"[PRE-TOOL] About to use: {tool_name}")
    
        # You can modify or block the tool execution here
        if tool_name == "Bash" and "rm -rf" in str(input_data.get('tool_input', {})):
            return {
                'hookSpecificOutput': {
                    'hookEventName': 'PreToolUse',
                    'permissionDecision': 'deny',
                    'permissionDecisionReason': 'Dangerous command blocked'
                }
            }
        return {}
    
    async def post_tool_logger(
        input_data: dict[str, Any],
        tool_use_id: str | None,
        context: HookContext
    ) -> dict[str, Any]:
        """Log results after tool execution."""
        tool_name = input_data.get('tool_name', 'unknown')
        print(f"[POST-TOOL] Completed: {tool_name}")
        return {}
    
    async def user_prompt_modifier(
        input_data: dict[str, Any],
        tool_use_id: str | None,
        context: HookContext
    ) -> dict[str, Any]:
        """Add context to user prompts."""
        original_prompt = input_data.get('prompt', '')
    
        # Add timestamp to all prompts
        from datetime import datetime
        timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    
        return {
            'hookSpecificOutput': {
                'hookEventName': 'UserPromptSubmit',
                'updatedPrompt': f"[{timestamp}] {original_prompt}"
            }
        }
    
    async def main():
        options = ClaudeAgentOptions(
            hooks={
                'PreToolUse': [
                    HookMatcher(hooks=[pre_tool_logger]),
                    HookMatcher(matcher='Bash', hooks=[pre_tool_logger])
                ],
                'PostToolUse': [
                    HookMatcher(hooks=[post_tool_logger])
                ],
                'UserPromptSubmit': [
                    HookMatcher(hooks=[user_prompt_modifier])
                ]
            },
            allowed_tools=["Read", "Write", "Bash"]
        )
    
        async with ClaudeSDKClient(options=options) as client:
            await client.query("List files in current directory")
    
            async for message in client.receive_response():
                # Hooks will automatically log tool usage
                pass
    
    asyncio.run(main())

    リアルタイム進捗モニタリング

    from claude_agent_sdk import (
        ClaudeSDKClient,
        ClaudeAgentOptions,
        AssistantMessage,
        ToolUseBlock,
        ToolResultBlock,
        TextBlock
    )
    import asyncio
    
    async def monitor_progress():
        options = ClaudeAgentOptions(
            allowed_tools=["Write", "Bash"],
            permission_mode="acceptEdits"
        )
    
        async with ClaudeSDKClient(options=options) as client:
            await client.query(
                "Create 5 Python files with different sorting algorithms"
            )
    
            # Monitor progress in real-time
            files_created = []
            async for message in client.receive_messages():
                if isinstance(message, AssistantMessage):
                    for block in message.content:
                        if isinstance(block, ToolUseBlock):
                            if block.name == "Write":
                                file_path = block.input.get("file_path", "")
                                print(f"🔨 Creating: {file_path}")
                        elif isinstance(block, ToolResultBlock):
                            print(f"✅ Completed tool execution")
                        elif isinstance(block, TextBlock):
                            print(f"💭 Claude says: {block.text[:100]}...")
    
                # Check if we've received the final result
                if hasattr(message, 'subtype') and message.subtype in ['success', 'error']:
                    print(f"\n🎯 Task completed!")
                    break
    
    asyncio.run(monitor_progress())

    使用例

    基本的なファイル操作(queryを使用)

    from claude_agent_sdk import query, ClaudeAgentOptions, AssistantMessage, ToolUseBlock
    import asyncio
    
    async def create_project():
        options = ClaudeAgentOptions(
            allowed_tools=["Read", "Write", "Bash"],
            permission_mode='acceptEdits',
            cwd="/home/user/project"
        )
    
        async for message in query(
            prompt="Create a Python project structure with setup.py",
            options=options
        ):
            if isinstance(message, AssistantMessage):
                for block in message.content:
                    if isinstance(block, ToolUseBlock):
                        print(f"Using tool: {block.name}")
    
    asyncio.run(create_project())

    エラーハンドリング

    from claude_agent_sdk import (
        query,
        CLINotFoundError,
        ProcessError,
        CLIJSONDecodeError
    )
    
    try:
        async for message in query(prompt="Hello"):
            print(message)
    except CLINotFoundError:
        print("Please install Claude Code: npm install -g @anthropic-ai/claude-code")
    except ProcessError as e:
        print(f"Process failed with exit code: {e.exit_code}")
    except CLIJSONDecodeError as e:
        print(f"Failed to parse response: {e}")

    クライアントを使用したストリーミングモード

    from claude_agent_sdk import ClaudeSDKClient
    import asyncio
    
    async def interactive_session():
        async with ClaudeSDKClient() as client:
            # Send initial message
            await client.query("What's the weather like?")
    
            # Process responses
            async for msg in client.receive_response():
                print(msg)
    
            # Send follow-up
            await client.query("Tell me more about that")
    
            # Process follow-up response
            async for msg in client.receive_response():
                print(msg)
    
    asyncio.run(interactive_session())

    ClaudeSDKClientでカスタムツールを使用する

    from claude_agent_sdk import (
        ClaudeSDKClient,
        ClaudeAgentOptions,
        tool,
        create_sdk_mcp_server,
        AssistantMessage,
        TextBlock
    )
    import asyncio
    from typing import Any
    
    # Define custom tools with @tool decorator
    @tool("calculate", "Perform mathematical calculations", {"expression": str})
    async def calculate(args: dict[str, Any]) -> dict[str, Any]:
        try:
            result = eval(args["expression"], {"__builtins__": {}})
            return {
                "content": [{
                    "type": "text",
                    "text": f"Result: {result}"
                }]
            }
        except Exception as e:
            return {
                "content": [{
                    "type": "text",
                    "text": f"Error: {str(e)}"
                }],
                "is_error": True
            }
    
    @tool("get_time", "Get current time", {})
    async def get_time(args: dict[str, Any]) -> dict[str, Any]:
        from datetime import datetime
        current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        return {
            "content": [{
                "type": "text",
                "text": f"Current time: {current_time}"
            }]
        }
    
    async def main():
        # Create SDK MCP server with custom tools
        my_server = create_sdk_mcp_server(
            name="utilities",
            version="1.0.0",
            tools=[calculate, get_time]
        )
    
        # Configure options with the server
        options = ClaudeAgentOptions(
            mcp_servers={"utils": my_server},
            allowed_tools=[
                "mcp__utils__calculate",
                "mcp__utils__get_time"
            ]
        )
    
        # Use ClaudeSDKClient for interactive tool usage
        async with ClaudeSDKClient(options=options) as client:
            await client.query("What's 123 * 456?")
    
            # Process calculation response
            async for message in client.receive_response():
                if isinstance(message, AssistantMessage):
                    for block in message.content:
                        if isinstance(block, TextBlock):
                            print(f"Calculation: {block.text}")
    
            # Follow up with time query
            await client.query("What time is it now?")
    
            async for message in client.receive_response():
                if isinstance(message, AssistantMessage):
                    for block in message.content:
                        if isinstance(block, TextBlock):
                            print(f"Time: {block.text}")
    
    asyncio.run(main())

    サンドボックス設定

    SandboxSettings

    サンドボックスの動作に関する設定です。コマンドのサンドボックス化を有効にし、ネットワーク制限をプログラムで設定するために使用します。

    class SandboxSettings(TypedDict, total=False):
        enabled: bool
        autoAllowBashIfSandboxed: bool
        excludedCommands: list[str]
        allowUnsandboxedCommands: bool
        network: SandboxNetworkConfig
        ignoreViolations: SandboxIgnoreViolations
        enableWeakerNestedSandbox: bool
    プロパティ型デフォルト説明
    enabledboolFalseコマンド実行のサンドボックスモードを有効にする
    autoAllowBashIfSandboxedboolFalseサンドボックスが有効な場合にbashコマンドを自動承認する
    excludedCommandslist[str][]サンドボックス制限を常にバイパスするコマンド(例:["docker"])。これらはモデルの関与なしに自動的にサンドボックス外で実行されます
    allowUnsandboxedCommandsboolFalseモデルがサンドボックス外でのコマンド実行をリクエストすることを許可する。Trueの場合、モデルはツール入力でdangerouslyDisableSandboxを設定でき、パーミッションシステムにフォールバックします
    networkSandboxNetworkConfigNoneネットワーク固有のサンドボックス設定
    ignoreViolationsSandboxIgnoreViolationsNone無視するサンドボックス違反の設定
    enableWeakerNestedSandboxboolFalse互換性のためにより弱いネストされたサンドボックスを有効にする

    ファイルシステムとネットワークのアクセス制限はサンドボックス設定では設定しません。代わりに、パーミッションルールから導出されます:

    • ファイルシステム読み取り制限:読み取り拒否ルール
    • ファイルシステム書き込み制限:編集許可/拒否ルール
    • ネットワーク制限:WebFetch許可/拒否ルール

    コマンド実行のサンドボックス化にはサンドボックス設定を使用し、ファイルシステムとネットワークのアクセス制御にはパーミッションルールを使用してください。

    使用例

    from claude_agent_sdk import query, ClaudeAgentOptions, SandboxSettings
    
    sandbox_settings: SandboxSettings = {
        "enabled": True,
        "autoAllowBashIfSandboxed": True,
        "network": {
            "allowLocalBinding": True
        }
    }
    
    async for message in query(
        prompt="Build and test my project",
        options=ClaudeAgentOptions(sandbox=sandbox_settings)
    ):
        print(message)

    Unixソケットのセキュリティ:allowUnixSocketsオプションは強力なシステムサービスへのアクセスを許可する可能性があります。例えば、/var/run/docker.sockを許可すると、Docker APIを通じてホストシステムへの完全なアクセスが事実上許可され、サンドボックスの分離がバイパスされます。厳密に必要なUnixソケットのみを許可し、各ソケットのセキュリティ上の影響を理解してください。

    SandboxNetworkConfig

    サンドボックスモードのネットワーク固有の設定です。

    class SandboxNetworkConfig(TypedDict, total=False):
        allowLocalBinding: bool
        allowUnixSockets: list[str]
        allowAllUnixSockets: bool
        httpProxyPort: int
        socksProxyPort: int
    プロパティ型デフォルト説明
    allowLocalBindingboolFalseプロセスがローカルポートにバインドすることを許可する(例:開発サーバー用)
    allowUnixSocketslist[str][]プロセスがアクセスできるUnixソケットパス(例:Dockerソケット)
    allowAllUnixSocketsboolFalseすべてのUnixソケットへのアクセスを許可する
    httpProxyPortintNoneネットワークリクエスト用のHTTPプロキシポート
    socksProxyPortintNoneネットワークリクエスト用のSOCKSプロキシポート

    SandboxIgnoreViolations

    特定のサンドボックス違反を無視するための設定です。

    class SandboxIgnoreViolations(TypedDict, total=False):
        file: list[str]
        network: list[str]
    プロパティ型デフォルト説明
    filelist[str][]違反を無視するファイルパスパターン
    networklist[str][]違反を無視するネットワークパターン

    サンドボックス外コマンドのパーミッションフォールバック

    allowUnsandboxedCommandsが有効な場合、モデルはツール入力でdangerouslyDisableSandbox: Trueを設定することで、サンドボックス外でのコマンド実行をリクエストできます。これらのリクエストは既存のパーミッションシステムにフォールバックし、can_use_toolハンドラーが呼び出されるため、カスタム認可ロジックを実装できます。

    excludedCommands vs allowUnsandboxedCommands:

    • excludedCommands:常にサンドボックスを自動的にバイパスするコマンドの静的リスト(例:["docker"])。モデルはこれを制御できません。
    • allowUnsandboxedCommands:モデルがツール入力でdangerouslyDisableSandbox: Trueを設定することで、実行時にサンドボックス外での実行をリクエストするかどうかを決定できるようにします。
    from claude_agent_sdk import query, ClaudeAgentOptions
    
    async def can_use_tool(tool: str, input: dict) -> bool:
        # Check if the model is requesting to bypass the sandbox
        if tool == "Bash" and input.get("dangerouslyDisableSandbox"):
            # The model wants to run this command outside the sandbox
            print(f"Unsandboxed command requested: {input.get('command')}")
    
            # Return True to allow, False to deny
            return is_command_authorized(input.get("command"))
        return True
    
    async def main():
        async for message in query(
            prompt="Deploy my application",
            options=ClaudeAgentOptions(
                sandbox={
                    "enabled": True,
                    "allowUnsandboxedCommands": True  # Model can request unsandboxed execution
                },
                permission_mode="default",
                can_use_tool=can_use_tool
            )
        ):
            print(message)

    このパターンにより、以下のことが可能になります:

    • モデルリクエストの監査:モデルがサンドボックス外での実行をリクエストした際にログを記録する
    • 許可リストの実装:特定のコマンドのみサンドボックス外での実行を許可する
    • 承認ワークフローの追加:特権操作に対して明示的な認可を要求する

    dangerouslyDisableSandbox: Trueで実行されるコマンドは、完全なシステムアクセス権を持ちます。can_use_toolハンドラーでこれらのリクエストを慎重に検証してください。

    permission_modeがbypassPermissionsに設定され、allow_unsandboxed_commandsが有効な場合、モデルは承認プロンプトなしにサンドボックス外でコマンドを自律的に実行できます。この組み合わせにより、モデルはサンドボックスの分離を暗黙的に回避できるようになります。

    関連情報

    • Python SDKガイド - チュートリアルと例
    • SDK概要 - 一般的なSDKの概念
    • TypeScript SDKリファレンス - TypeScript SDKドキュメント
    • CLIリファレンス - コマンドラインインターフェース
    • 一般的なワークフロー - ステップバイステップガイド

    Was this page helpful?

    • query() と ClaudeSDKClient の選択
    • query() を使用する場合(毎回新しいセッション)
    • ClaudeSDKClient を使用する場合(継続的な会話)
    • query()
    • tool()
    • create_sdk_mcp_server()
    • ClaudeSDKClient
    • SdkMcpTool
    • ClaudeAgentOptions
    • OutputFormat
    • SystemPromptPreset
    • SettingSource
    • AgentDefinition
    • PermissionMode
    • CanUseTool
    • ToolPermissionContext
    • PermissionResult
    • PermissionResultAllow
    • PermissionResultDeny
    • PermissionUpdate
    • SdkBeta
    • McpSdkServerConfig
    • McpServerConfig
    • SdkPluginConfig
    • Message
    • UserMessage
    • AssistantMessage
    • SystemMessage
    • ResultMessage
    • StreamEvent
    • ContentBlock
    • TextBlock
    • ThinkingBlock
    • ToolUseBlock
    • ToolResultBlock
    • ClaudeSDKError
    • CLINotFoundError
    • CLIConnectionError
    • ProcessError
    • CLIJSONDecodeError
    • HookEvent
    • HookCallback
    • HookContext
    • HookMatcher
    • HookInput
    • BaseHookInput
    • PreToolUseHookInput
    • PostToolUseHookInput
    • UserPromptSubmitHookInput
    • StopHookInput
    • SubagentStopHookInput
    • PreCompactHookInput
    • HookJSONOutput
    • Task
    • AskUserQuestion
    • Bash
    • Edit
    • Read
    • Write
    • Glob
    • Grep
    • NotebookEdit
    • WebFetch
    • WebSearch
    • TodoWrite
    • BashOutput
    • KillBash
    • ExitPlanMode
    • ListMcpResources
    • ReadMcpResource
    • ClaudeSDKClientを使用した高度な機能
    • 基本的なファイル操作(queryを使用)
    • ClaudeSDKClientでカスタムツールを使用する
    • SandboxSettings
    • SandboxNetworkConfig
    • SandboxIgnoreViolations