importasynciofromclaude_agent_sdkimportquery,ClaudeAgentOptionsasyncdefmain():options=ClaudeAgentOptions(cwd="/path/to/project",# 包含 .claude/skills/ 的项目目录setting_sources=["user","project"],# 从文件系统加载 Skillsallowed_tools=["Skill","Read","Write","Bash"],# 启用 Skill 工具)asyncformessageinquery(prompt="Help me process this PDF document",options=options):print(message)asyncio.run(main())
import{query}from"@anthropic-ai/claude-agent-sdk";forawait(constmessageofquery({prompt:"Help me process this PDF document",options:{cwd:"/path/to/project",// 包含 .claude/skills/ 的项目目录settingSources:["user","project"],// 从文件系统加载 SkillsallowedTools:["Skill","Read","Write","Bash"]// 启用 Skill 工具}})){console.log(message);}
options=ClaudeAgentOptions(setting_sources=["user","project"],allowed_tools=["Skill","Read","Grep","Glob"],# 只允许只读工具)asyncformessageinquery(prompt="Analyze the codebase structure",options=options):print(message)
importasynciofromclaude_agent_sdkimportquery,ClaudeAgentOptions,AgentDefinitionasyncdefmain():asyncformessageinquery(prompt="Review the authentication module for security issues",options=ClaudeAgentOptions(# Agent 工具是调用子 Agent 的必需条件allowed_tools=["Read","Grep","Glob","Agent"],agents={"code-reviewer":AgentDefinition(# description 告诉 Claude 何时使用这个子 Agentdescription="Expert code review specialist. Use for quality, security, and maintainability reviews.",# prompt 定义子 Agent 的行为和专业能力prompt="""You are a code review specialist with expertise in security, performance, and best practices.When reviewing code:- Identify security vulnerabilities- Check for performance issues- Verify adherence to coding standards- Suggest specific improvementsBe thorough but concise in your feedback.""",# tools 限制子 Agent 只能使用指定工具(此处为只读)tools=["Read","Grep","Glob"],# model 覆盖子 Agent 使用的模型model="sonnet",),"test-runner":AgentDefinition(description="Runs and analyzes test suites. Use for test execution and coverage analysis.",prompt="""You are a test execution specialist. Run tests and provide clear analysis of results.Focus on:- Running test commands- Analyzing test output- Identifying failing tests- Suggesting fixes for failures""",# Bash 访问让子 Agent 能运行测试命令tools=["Bash","Read","Grep"],),},),):ifhasattr(message,"result"):print(message.result)asyncio.run(main())
import{query}from"@anthropic-ai/claude-agent-sdk";forawait(constmessageofquery({prompt:"Review the authentication module for security issues",options:{// Agent 工具是调用子 Agent 的必需条件allowedTools:["Read","Grep","Glob","Agent"],agents:{"code-reviewer":{description:"Expert code review specialist. Use for quality, security, and maintainability reviews.",prompt:`You are a code review specialist with expertise in security, performance, and best practices.When reviewing code:- Identify security vulnerabilities- Check for performance issues- Verify adherence to coding standards- Suggest specific improvementsBe thorough but concise in your feedback.`,tools:["Read","Grep","Glob"],model:"sonnet"},"test-runner":{description:"Runs and analyzes test suites. Use for test execution and coverage analysis.",prompt:`You are a test execution specialist. Run tests and provide clear analysis of results.Focus on:- Running test commands- Analyzing test output- Identifying failing tests- Suggesting fixes for failures`,tools:["Bash","Read","Grep"]}}}})){if("result"inmessage)console.log(message.result);}
importasyncioimportjsonimportrefromclaude_agent_sdkimportquery,ClaudeAgentOptionsdefextract_agent_id(text:str)->str|None:"""从 Agent 工具结果文本中提取 agentId。"""match=re.search(r"agentId:\s*([a-f0-9-]+)",text)returnmatch.group(1)ifmatchelseNoneasyncdefmain():agent_id=Nonesession_id=None# 第一次调用——使用 Explore 子 Agentasyncformessageinquery(prompt="Use the Explore agent to find all API endpoints in this codebase",options=ClaudeAgentOptions(allowed_tools=["Read","Grep","Glob","Agent"]),):ifhasattr(message,"session_id"):session_id=message.session_idifhasattr(message,"content"):content_str=json.dumps(message.content,default=str)extracted=extract_agent_id(content_str)ifextracted:agent_id=extractedifhasattr(message,"result"):print(message.result)# 第二次调用——恢复会话,继续追问ifagent_idandsession_id:asyncformessageinquery(prompt=f"Resume agent {agent_id} and list the top 3 most complex endpoints",options=ClaudeAgentOptions(allowed_tools=["Read","Grep","Glob","Agent"],resume=session_id),):ifhasattr(message,"result"):print(message.result)asyncio.run(main())