Use this file to discover all available pages before exploring further.
Claude Code SDK は Claude Agent SDK に名前が変更されました。古い SDK から移行する場合は、移行ガイドを参照してください。
ファイルを自動的に読み取り、コマンドを実行し、ウェブを検索し、コードを編集するなど、さらに多くのことができる AI エージェントを構築します。Agent SDK は、Claude Code を強化する同じツール、エージェントループ、およびコンテキスト管理を提供し、Python と TypeScript でプログラム可能です。
Opus 4.7(claude-opus-4-7)には Agent SDK v0.2.111 以降が必要です。thinking.type.enabled API エラーが表示される場合は、トラブルシューティングを参照してください。
import asynciofrom claude_agent_sdk import query, ClaudeAgentOptionsasync def main(): async for message in query( prompt="Find and fix the bug in auth.py", options=ClaudeAgentOptions(allowed_tools=["Read", "Edit", "Bash"]), ): print(message) # Claude reads the file, finds the bug, edits itasyncio.run(main())
import asynciofrom claude_agent_sdk import query, ClaudeAgentOptionsasync def main(): async for message in query( prompt="What files are in this directory?", options=ClaudeAgentOptions(allowed_tools=["Bash", "Glob"]), ): if hasattr(message, "result"): print(message.result)asyncio.run(main())
import asynciofrom claude_agent_sdk import query, ClaudeAgentOptionsasync def main(): async for message in query( prompt="Find all TODO comments and create a summary", options=ClaudeAgentOptions(allowed_tools=["Read", "Glob", "Grep"]), ): if hasattr(message, "result"): print(message.result)asyncio.run(main())
import asynciofrom claude_agent_sdk import query, ClaudeAgentOptionsasync def main(): async for message in query( prompt="Review this code for best practices", options=ClaudeAgentOptions( allowed_tools=["Read", "Glob", "Grep"], ), ): if hasattr(message, "result"): print(message.result)asyncio.run(main())
複数の交換にわたってコンテキストを維持します。Claude は読み取ったファイル、実行した分析、および会話履歴を記憶します。後でセッションを再開するか、異なるアプローチを探索するためにフォークします。この例は、最初のクエリからセッション ID をキャプチャし、その後、完全なコンテキストで続行するために再開します。
import asynciofrom claude_agent_sdk import query, ClaudeAgentOptions, SystemMessage, ResultMessageasync def main(): session_id = None # First query: capture the session ID async for message in query( prompt="Read the authentication module", options=ClaudeAgentOptions(allowed_tools=["Read", "Glob"]), ): if isinstance(message, SystemMessage) and message.subtype == "init": session_id = message.data["session_id"] # Resume with full context from the first query async for message in query( prompt="Now find all places that call it", # "it" = auth module options=ClaudeAgentOptions(resume=session_id), ): if isinstance(message, ResultMessage): print(message.result)asyncio.run(main())
Claude Platform は Claude で構築するための複数の方法を提供しています。Agent SDK がどのように適合するかは次のとおりです。
Agent SDK vs Client SDK
Agent SDK vs Claude Code CLI
Agent SDK vs Managed Agents
Anthropic Client SDK は直接 API アクセスを提供します。プロンプトを送信し、ツール実行を自分で実装します。Agent SDK は、組み込みツール実行を備えた Claude を提供します。Client SDK では、ツールループを実装します。Agent SDK では、Claude がそれを処理します。
# Client SDK: You implement the tool loopresponse = client.messages.create(...)while response.stop_reason == "tool_use": result = your_tool_executor(response.tool_use) response = client.messages.create(tool_result=result, **params)# Agent SDK: Claude handles tools autonomouslyasync for message in query(prompt="Fix the bug in auth.py"): print(message)