Loading...
Loading...
Comprehensive knowledge of Claude Agent SDK architecture, tools, hooks, skills, and production patterns. Auto-activates for agent building, SDK integration, tool design, and MCP server tasks.
npx skill4agent add rysweet/amplihack agent-sdkclaude-agents@anthropics/agent-sdkpip install claude-agentsnpm install @anthropics/agent-sdkexport ANTHROPIC_API_KEY="your-api-key-here"from claude_agents import Agent
agent = Agent(api_key="your-api-key-here")from claude_agents import Agent
# Create agent with default settings
agent = Agent(
model="claude-sonnet-4-5-20250929",
system="You are a helpful assistant focused on accuracy."
)
# Run simple task
result = agent.run("What is 2+2?")
print(result.response)import { Agent } from "@anthropics/agent-sdk";
const agent = new Agent({
model: "claude-sonnet-4-5-20250929",
system: "You are a helpful assistant focused on accuracy.",
});
const result = await agent.run("What is 2+2?");
console.log(result.response);from claude_agents import Agent
from claude_agents.tools import Tool
# Define custom tool
def get_weather(location: str) -> dict:
"""Get weather for a location."""
return {"location": location, "temp": 72, "condition": "sunny"}
weather_tool = Tool(
name="get_weather",
description="Get current weather for a location",
input_schema={
"type": "object",
"properties": {
"location": {"type": "string", "description": "City name"}
},
"required": ["location"]
},
function=get_weather
)
# Agent with custom tool
agent = Agent(
model="claude-sonnet-4-5-20250929",
tools=[weather_tool]
)
result = agent.run("What's the weather in San Francisco?")
print(result.response)# Spawn subagent with isolated context
with agent.subagent(
system="You are a code reviewer focused on security.",
tools=[security_scan_tool]
) as reviewer:
review = reviewer.run("Review this code for vulnerabilities: ...")
# Subagent context doesn't pollute parentbashread_filewrite_fileedit_fileglobgrepTool(
name="tool_name", # Unique identifier
description="What it does", # Clear capability description
input_schema={ # JSON Schema for parameters
"type": "object",
"properties": {...},
"required": [...]
},
function=callable # Python function or async function
)from claude_agents import Agent
from claude_agents.mcp import MCPClient
# Connect to MCP server
mcp_client = MCPClient("npx", ["-y", "@modelcontextprotocol/server-filesystem"])
agent = Agent(
model="claude-sonnet-4-5-20250929",
mcp_clients=[mcp_client]
)agent = Agent(
model="claude-sonnet-4-5-20250929",
tools=[tool1, tool2, tool3, tool4],
allowed_tools=["tool1", "tool2"] # Only these can be used
)agent = Agent(
model="claude-sonnet-4-5-20250929",
tools=[tool1, tool2, tool3],
disallowed_tools=["tool3"] # All except tool3
)"strict""permissive"PreToolUseHookPostToolUseHookPreSubagentStartHookPostSubagentStopHookfrom claude_agents.hooks import PreToolUseHook
class LoggingHook(PreToolUseHook):
async def execute(self, context):
print(f"Tool: {context.tool_name}")
print(f"Args: {context.tool_input}")
return context # Allow execution
agent = Agent(
model="claude-sonnet-4-5-20250929",
hooks=[LoggingHook()]
)class ValidationHook(PreToolUseHook):
async def execute(self, context):
if context.tool_name == "bash" and "rm -rf" in context.tool_input.get("command", ""):
raise PermissionError("Destructive command blocked")
return contextfrom claude_agents import Agent
agent = Agent(
model="claude-sonnet-4-5-20250929",
allowed_tools=["read_file", "write_file", "glob"]
)
result = agent.run(
"Read all Python files in ./src and create a summary in summary.md"
)agent = Agent(
model="claude-sonnet-4-5-20250929",
allowed_tools=["bash"]
)
result = agent.run(
"Run the test suite and analyze any failures"
)# Agent automatically gathers information iteratively
search_agent = Agent(
model="claude-sonnet-4-5-20250929",
tools=[web_search_tool, read_url_tool]
)
result = search_agent.run(
"Research the latest developments in quantum computing and summarize key papers"
)main_agent = Agent(model="claude-sonnet-4-5-20250929")
# Delegate specialized task to subagent
with main_agent.subagent(
system="You are an expert data analyzer.",
tools=[analyze_csv_tool, plot_tool]
) as analyzer:
analysis = analyzer.run("Analyze sales_data.csv and create visualizations")
# Results available to main agent
main_agent.run(f"Based on this analysis: {analysis.response}, what actions should we take?")from claude_agents import Agent, AgentError
agent = Agent(model="claude-sonnet-4-5-20250929")
try:
result = agent.run("Your task here", max_turns=10)
except AgentError as e:
print(f"Agent failed: {e}")
print(f"Turns completed: {e.turns_completed}")
print(f"Last message: {e.last_message}")# Agent can self-verify results
agent = Agent(
model="claude-sonnet-4-5-20250929",
tools=[calculator_tool, verify_tool]
)
result = agent.run(
"Calculate the compound interest for $10000 at 5% for 10 years. "
"Verify your calculation by computing it a second way."
)# In .claude/agents/amplihack/specialized/my_agent.md
# Use Agent SDK patterns for tool-using agents
from claude_agents import Agent
from claude_agents.tools import Tool
def create_specialized_agent():
return Agent(
model="claude-sonnet-4-5-20250929",
system="<agent_role_from_md>",
tools=[...], # Custom tools for this agent
hooks=[...] # Logging, validation hooks
)# Integrate MCP tools into Amplihack workflow
from claude_agents.mcp import MCPClient
mcp_client = MCPClient("npx", ["-y", "@modelcontextprotocol/server-github"])
agent = Agent(
model="claude-sonnet-4-5-20250929",
mcp_clients=[mcp_client]
)
# Agent can now use GitHub MCP tools
result = agent.run("Create a GitHub issue for the bug we just found")# Log all agent actions to Amplihack runtime logs
class AmplihackLoggingHook(PreToolUseHook):
async def execute(self, context):
log_to_amplihack_runtime(
session_id=get_current_session(),
tool=context.tool_name,
input=context.tool_input
)
return context# Basic agent
agent = Agent(model="claude-sonnet-4-5-20250929")
result = agent.run("task")
# With tools
agent = Agent(model="...", tools=[tool1, tool2])
# With permissions
agent = Agent(model="...", allowed_tools=["tool1"])
# With hooks
agent = Agent(model="...", hooks=[LogHook()])
# Subagent
with agent.subagent(system="...") as sub:
result = sub.run("subtask")
# MCP integration
from claude_agents.mcp import MCPClient
mcp = MCPClient("npx", ["-y", "mcp-server-name"])
agent = Agent(model="...", mcp_clients=[mcp])# File operations
tools=["read_file", "write_file", "glob"]
# Code execution
tools=["bash"]
# All built-in
tools=["bash", "read_file", "write_file", "edit_file", "glob", "grep"]reference.mdexamples.mdpatterns.md