@@ -39,14 +39,88 @@ class ToolsPreset(TypedDict):
3939 preset : Literal ["claude_code" ]
4040
4141
42+ # Subagent execution modes
43+ SubagentExecutionMode = Literal ["sequential" , "parallel" , "auto" ]
44+
45+ # Multi-invocation handling modes
46+ MultiInvocationMode = Literal ["sequential" , "parallel" , "error" ]
47+
48+ # Error handling modes for subagent execution
49+ SubagentErrorHandling = Literal ["fail_fast" , "continue" ]
50+
51+
52+ @dataclass
53+ class SubagentExecutionConfig :
54+ """Configuration for subagent execution behavior.
55+
56+ This controls how the SDK handles multiple Task tool calls (subagent invocations)
57+ when they occur in the same turn.
58+
59+ Attributes:
60+ multi_invocation: How to handle multiple Task tool calls in the same turn.
61+ - "sequential": Execute subagents one at a time in order (default)
62+ - "parallel": Execute subagents concurrently
63+ - "error": Raise an error if multiple Task tools are invoked in same turn
64+ max_concurrent: Maximum number of subagents to run concurrently when
65+ multi_invocation is "parallel". Default is 3.
66+ error_handling: How to handle errors from individual subagents.
67+ - "fail_fast": Stop execution on first subagent error
68+ - "continue": Continue executing remaining subagents on error (default)
69+
70+ Example:
71+ >>> config = SubagentExecutionConfig(
72+ ... multi_invocation="parallel",
73+ ... max_concurrent=5,
74+ ... error_handling="fail_fast"
75+ ... )
76+ >>> options = ClaudeAgentOptions(subagent_execution=config)
77+ """
78+
79+ multi_invocation : MultiInvocationMode = "sequential"
80+ max_concurrent : int = 3
81+ error_handling : SubagentErrorHandling = "continue"
82+
83+
4284@dataclass
4385class AgentDefinition :
44- """Agent definition configuration."""
86+ """Agent definition configuration.
87+
88+ Defines a custom agent (subagent) that can be invoked via the Task tool.
89+
90+ Attributes:
91+ description: A short description of what this agent does. This is shown
92+ to the parent agent to help it decide when to use this subagent.
93+ prompt: The system prompt or instructions for this agent.
94+ tools: Optional list of tool names this agent can use. If None, inherits
95+ from parent agent.
96+ model: Optional model override for this agent.
97+ - "sonnet": Use Claude Sonnet
98+ - "opus": Use Claude Opus
99+ - "haiku": Use Claude Haiku (faster, lower cost)
100+ - "inherit": Use the same model as the parent agent
101+ - None: Use default model
102+ execution_mode: How this agent should be executed when invoked alongside
103+ other subagents in the same turn.
104+ - "sequential": This agent must complete before the next starts
105+ - "parallel": This agent can run concurrently with others
106+ - "auto": SDK decides based on global SubagentExecutionConfig (default)
107+ - None: Same as "auto"
108+
109+ Example:
110+ >>> agent = AgentDefinition(
111+ ... description="Analyzes code for security issues",
112+ ... prompt="You are a security analyst...",
113+ ... tools=["Read", "Grep", "Glob"],
114+ ... model="haiku",
115+ ... execution_mode="parallel"
116+ ... )
117+ """
45118
46119 description : str
47120 prompt : str
48121 tools : list [str ] | None = None
49122 model : Literal ["sonnet" , "opus" , "haiku" , "inherit" ] | None = None
123+ execution_mode : SubagentExecutionMode | None = None
50124
51125
52126# Permission Update types (matching TypeScript SDK)
@@ -368,18 +442,62 @@ class HookContext(TypedDict):
368442# Hook matcher configuration
369443@dataclass
370444class HookMatcher :
371- """Hook matcher configuration."""
445+ """Hook matcher configuration for matching tool invocations.
372446
373- # See https://docs.anthropic.com/en/docs/claude-code/hooks#structure for the
374- # expected string value. For example, for PreToolUse, the matcher can be
375- # a tool name like "Bash" or a combination of tool names like
376- # "Write|MultiEdit|Edit".
377- matcher : str | None = None
447+ The matcher field supports regex patterns for filtering which tools trigger
448+ the associated hooks. This is particularly useful for PreToolUse and
449+ PostToolUse hook events.
378450
379- # A list of Python functions with function signature HookCallback
380- hooks : list [HookCallback ] = field (default_factory = list )
451+ Attributes:
452+ matcher: Regex pattern for matching tool names. If None, matches all tools.
453+ hooks: List of async callback functions to execute when a tool matches.
454+ timeout: Timeout in seconds for all hooks in this matcher (default: 60).
455+
456+ Pattern Examples:
457+ Built-in tools:
458+ - "Bash" - Match only the Bash tool
459+ - "Read" - Match only the Read tool
460+ - "Write|Edit" - Match Write OR Edit tools
461+ - "Write|MultiEdit|Edit" - Match any file writing tool
462+
463+ MCP tools (format: mcp__<server>__<tool>):
464+ - "mcp__.*" - Match all MCP tools from any server
465+ - "mcp__slack__.*" - Match all tools from the slack MCP server
466+ - "mcp__github__create_issue" - Match specific github tool
467+ - "mcp__.*__delete.*" - Match any MCP tool with "delete" in the name
468+ - "mcp__db__.*write.*" - Match db server tools containing "write"
469+
470+ Combined patterns:
471+ - "Bash|mcp__.*__execute.*" - Match Bash or any MCP execute tools
472+ - None - Match all tools (universal matcher)
381473
382- # Timeout in seconds for all hooks in this matcher (default: 60)
474+ Example:
475+ >>> from claude_agent_sdk import HookMatcher, HookCallback
476+ >>>
477+ >>> async def log_mcp_calls(input, tool_use_id, context):
478+ ... print(f"MCP tool called: {input['tool_name']}")
479+ ... return {"continue_": True}
480+ >>>
481+ >>> # Match all MCP tools
482+ >>> mcp_matcher = HookMatcher(
483+ ... matcher="mcp__.*",
484+ ... hooks=[log_mcp_calls],
485+ ... timeout=30.0
486+ ... )
487+ >>>
488+ >>> # Match dangerous operations
489+ >>> dangerous_matcher = HookMatcher(
490+ ... matcher="mcp__.*__delete.*|mcp__.*__drop.*",
491+ ... hooks=[confirm_dangerous_operation],
492+ ... timeout=60.0
493+ ... )
494+
495+ See Also:
496+ https://docs.anthropic.com/en/docs/claude-code/hooks#structure
497+ """
498+
499+ matcher : str | None = None
500+ hooks : list [HookCallback ] = field (default_factory = list )
383501 timeout : float | None = None
384502
385503
@@ -660,6 +778,8 @@ class ClaudeAgentOptions:
660778 fork_session : bool = False
661779 # Agent definitions for custom agents
662780 agents : dict [str , AgentDefinition ] | None = None
781+ # Subagent execution configuration (controls parallel vs sequential execution)
782+ subagent_execution : SubagentExecutionConfig | None = None
663783 # Setting sources to load (user, project, local)
664784 setting_sources : list [SettingSource ] | None = None
665785 # Sandbox configuration for bash command isolation.
0 commit comments