Loading...
Loading...
This skill should be used when the user asks to "build an AI agent with Claude", "use the Claude Agent SDK", "integrate claude-agent-sdk into a project", "set up an autonomous agent with tools", or needs guidance on the Anthropic Claude Agent SDK best practices for Python and TypeScript.
npx skill4agent add the-perfect-developer/the-perfect-opencode claude-agent-sdkclaude-agent-sdk@anthropic-ai/claude-agent-sdk# TypeScript
npm install @anthropic-ai/claude-agent-sdk
# Python (uv — recommended)
uv add claude-agent-sdk
# Python (pip)
pip install claude-agent-sdkexport ANTHROPIC_API_KEY=your-api-keyCLAUDE_CODE_USE_BEDROCK=1CLAUDE_CODE_USE_VERTEX=1CLAUDE_CODE_USE_FOUNDRY=1query()query()import asyncio
from claude_agent_sdk import query, ClaudeAgentOptions
async def main():
async for message in query(
prompt="Find and fix the bug in auth.py",
options=ClaudeAgentOptions(
allowed_tools=["Read", "Edit", "Glob"],
permission_mode="acceptEdits",
),
):
if hasattr(message, "result"):
print(message.result)
asyncio.run(main())import { query } from "@anthropic-ai/claude-agent-sdk";
for await (const message of query({
prompt: "Find and fix the bug in auth.py",
options: { allowedTools: ["Read", "Edit", "Glob"], permissionMode: "acceptEdits" }
})) {
if ("result" in message) console.log(message.result);
}| Tool | Purpose |
|---|---|
| Read any file in the working directory |
| Create new files |
| Make precise edits to existing files |
| Run terminal commands, scripts, git operations |
| Find files by pattern ( |
| Search file contents with regex |
| Search the web for current information |
| Fetch and parse web page content |
| Ask the user clarifying questions |
| Spawn subagents (required when using subagents) |
| Use case | Tools |
|---|---|
| Read-only analysis | |
| Code modification | |
| Full automation | |
| Web-augmented | Add |
| Subagent orchestration | Add |
permissionModepermission_mode| Mode | Behavior | Best for |
|---|---|---|
| Delegates unresolved requests to | Custom approval flows |
| Auto-approves file edits and filesystem ops | Trusted dev workflows |
| Runs all tools without prompts | CI/CD pipelines |
| No tool execution — planning only | Pre-review before changes |
acceptEditsbypassPermissionsbypassPermissionsq = query(prompt="Refactor auth module", options=ClaudeAgentOptions(permission_mode="plan"))
await q.set_permission_mode("acceptEdits") # Switch after plan is approved
async for message in q:
...settings.jsoncanUseTooldefaultsession_id = None
async for message in query(prompt="Analyze auth module", options=ClaudeAgentOptions(...)):
if hasattr(message, "subtype") and message.subtype == "init":
session_id = message.data.get("session_id")let sessionId: string | undefined;
for await (const message of query({ prompt: "Analyze auth module", options: { ... } })) {
if (message.type === "system" && message.subtype === "init") {
sessionId = message.session_id;
}
}resumeresumeasync for message in query(
prompt="Now find all callers of that function",
options=ClaudeAgentOptions(resume=session_id),
):
...fork_session=TrueforkSession: true# Explore a different approach without losing the original session
async for message in query(
prompt="Redesign this as GraphQL instead",
options=ClaudeAgentOptions(resume=session_id, fork_session=True),
):
...options = ClaudeAgentOptions(
mcp_servers={
"playwright": {"command": "npx", "args": ["@playwright/mcp@latest"]}
}
)mcp__{server_name}__{tool_name}allowed_toolsallowed_tools=["mcp__playwright__browser_click", "mcp__playwright__browser_screenshot"]from claude_agent_sdk import AssistantMessage, ResultMessage
async for message in query(...):
if isinstance(message, AssistantMessage):
for block in message.content:
if hasattr(block, "text"):
print(block.text) # Claude's reasoning
elif hasattr(block, "name"):
print(f"Tool: {block.name}") # Tool being called
elif isinstance(message, ResultMessage):
print(f"Result: {message.subtype}") # "success" or "error"for await (const message of query({ ... })) {
if (message.type === "assistant") {
for (const block of message.message.content) {
if ("text" in block) console.log(block.text);
else if ("name" in block) console.log(`Tool: ${block.name}`);
}
} else if (message.type === "result") {
console.log(`Result: ${message.subtype}`);
}
}system_promptsystemPromptoptions = ClaudeAgentOptions(
system_prompt="You are a senior Python developer. Always follow PEP 8. Prefer explicit error handling over bare excepts.",
allowed_tools=["Read", "Edit", "Glob"],
permission_mode="acceptEdits",
)BashTaskallowed_tools"Review auth.py""Review some code""Use the code-reviewer agent to..."ResultMessagesubtype == "error"async forexceptbypassPermissionsPreToolUse.env/etccleanupPeriodDaysasync forplanreferences/hooks.mdreferences/subagents.mdreferences/custom-tools.mdcreateSdkMcpServer