mirror of
https://github.com/anthropics/claude-code-sdk-python.git
synced 2025-12-23 09:19:52 +00:00
## Summary - Add support for custom agent definitions via `agents` option - Add support for controlling setting sources via `setting_sources` option - Add `/commit` slash command to project - Add examples demonstrating both features - Add e2e tests for verification ## Changes ### Core Implementation - Add `AgentDefinition` and `SettingSource` types to `types.py` - Add `agents` and `setting_sources` fields to `ClaudeCodeOptions` - Update subprocess CLI transport to pass `--agents` and `--setting-sources` flags - **Default behavior**: When `setting_sources` is not provided, pass empty string (no settings loaded) - Handle empty `setting_sources` array correctly (pass empty string to CLI) ### Examples - `examples/agents.py`: Demonstrates custom agent definitions with different tools and models - `examples/setting_sources.py`: Shows how setting sources control which settings are loaded - Default behavior (no settings) - User-only settings - User + project settings ### Tests - Add e2e tests verifying agents and setting_sources functionality - Test default behavior (no settings loaded) - Test filtering by setting source - Use `output_style` checking to verify settings loaded/not loaded - Tests use temporary directories for isolated testing ### Project Config - Add `.claude/commands/commit.md` slash command for git commits ## Test Plan - [x] E2E tests added for all new functionality - [ ] CI tests pass - [ ] Examples run successfully 🤖 Generated with [Claude Code](https://claude.ai/code) --------- Co-authored-by: Claude <noreply@anthropic.com>
124 lines
4.2 KiB
Python
124 lines
4.2 KiB
Python
#!/usr/bin/env python3
|
|
"""Example of using custom agents with Claude Code SDK.
|
|
|
|
This example demonstrates how to define and use custom agents with specific
|
|
tools, prompts, and models.
|
|
|
|
Usage:
|
|
./examples/agents.py - Run the example
|
|
"""
|
|
|
|
import anyio
|
|
|
|
from claude_code_sdk import (
|
|
AgentDefinition,
|
|
AssistantMessage,
|
|
ClaudeCodeOptions,
|
|
ResultMessage,
|
|
TextBlock,
|
|
query,
|
|
)
|
|
|
|
|
|
async def code_reviewer_example():
|
|
"""Example using a custom code reviewer agent."""
|
|
print("=== Code Reviewer Agent Example ===")
|
|
|
|
options = ClaudeCodeOptions(
|
|
agents={
|
|
"code-reviewer": AgentDefinition(
|
|
description="Reviews code for best practices and potential issues",
|
|
prompt="You are a code reviewer. Analyze code for bugs, performance issues, "
|
|
"security vulnerabilities, and adherence to best practices. "
|
|
"Provide constructive feedback.",
|
|
tools=["Read", "Grep"],
|
|
model="sonnet",
|
|
),
|
|
},
|
|
)
|
|
|
|
async for message in query(
|
|
prompt="Use the code-reviewer agent to review the code in src/claude_code_sdk/types.py",
|
|
options=options,
|
|
):
|
|
if isinstance(message, AssistantMessage):
|
|
for block in message.content:
|
|
if isinstance(block, TextBlock):
|
|
print(f"Claude: {block.text}")
|
|
elif isinstance(message, ResultMessage) and message.total_cost_usd and message.total_cost_usd > 0:
|
|
print(f"\nCost: ${message.total_cost_usd:.4f}")
|
|
print()
|
|
|
|
|
|
async def documentation_writer_example():
|
|
"""Example using a documentation writer agent."""
|
|
print("=== Documentation Writer Agent Example ===")
|
|
|
|
options = ClaudeCodeOptions(
|
|
agents={
|
|
"doc-writer": AgentDefinition(
|
|
description="Writes comprehensive documentation",
|
|
prompt="You are a technical documentation expert. Write clear, comprehensive "
|
|
"documentation with examples. Focus on clarity and completeness.",
|
|
tools=["Read", "Write", "Edit"],
|
|
model="sonnet",
|
|
),
|
|
},
|
|
)
|
|
|
|
async for message in query(
|
|
prompt="Use the doc-writer agent to explain what AgentDefinition is used for",
|
|
options=options,
|
|
):
|
|
if isinstance(message, AssistantMessage):
|
|
for block in message.content:
|
|
if isinstance(block, TextBlock):
|
|
print(f"Claude: {block.text}")
|
|
elif isinstance(message, ResultMessage) and message.total_cost_usd and message.total_cost_usd > 0:
|
|
print(f"\nCost: ${message.total_cost_usd:.4f}")
|
|
print()
|
|
|
|
|
|
async def multiple_agents_example():
|
|
"""Example with multiple custom agents."""
|
|
print("=== Multiple Agents Example ===")
|
|
|
|
options = ClaudeCodeOptions(
|
|
agents={
|
|
"analyzer": AgentDefinition(
|
|
description="Analyzes code structure and patterns",
|
|
prompt="You are a code analyzer. Examine code structure, patterns, and architecture.",
|
|
tools=["Read", "Grep", "Glob"],
|
|
),
|
|
"tester": AgentDefinition(
|
|
description="Creates and runs tests",
|
|
prompt="You are a testing expert. Write comprehensive tests and ensure code quality.",
|
|
tools=["Read", "Write", "Bash"],
|
|
model="sonnet",
|
|
),
|
|
},
|
|
setting_sources=["user", "project"],
|
|
)
|
|
|
|
async for message in query(
|
|
prompt="Use the analyzer agent to find all Python files in the examples/ directory",
|
|
options=options,
|
|
):
|
|
if isinstance(message, AssistantMessage):
|
|
for block in message.content:
|
|
if isinstance(block, TextBlock):
|
|
print(f"Claude: {block.text}")
|
|
elif isinstance(message, ResultMessage) and message.total_cost_usd and message.total_cost_usd > 0:
|
|
print(f"\nCost: ${message.total_cost_usd:.4f}")
|
|
print()
|
|
|
|
|
|
async def main():
|
|
"""Run all agent examples."""
|
|
await code_reviewer_example()
|
|
await documentation_writer_example()
|
|
await multiple_agents_example()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
anyio.run(main)
|