SubAgentCapability API¶
subagents_pydantic_ai.capability
¶
Subagent capability for pydantic-ai agents.
Provides a SubAgentCapability that integrates the subagent toolset +
dynamic instructions via the pydantic-ai capabilities API.
Example
from pydantic_ai import Agent
from subagents_pydantic_ai import SubAgentCapability, SubAgentConfig
agent = Agent(
"openai:gpt-4.1",
capabilities=[SubAgentCapability(
default_model="openai:gpt-4.1",
subagents=[
SubAgentConfig(
name="researcher",
description="Researches topics",
instructions="You are a research assistant.",
),
],
)],
)
SubAgentCapability
dataclass
¶
Bases: AbstractCapability[Any]
Capability that provides subagent delegation tools and dynamic instructions.
Combines the subagent toolset (task, check_task, answer_subagent, etc.) with dynamic system prompt injection listing available subagents.
Example
from pydantic_ai import Agent
from subagents_pydantic_ai import SubAgentCapability, SubAgentConfig
cap = SubAgentCapability(
default_model="openai:gpt-4.1",
subagents=[
SubAgentConfig(
name="researcher",
description="Researches topics",
instructions="You are a research assistant.",
),
],
)
agent = Agent("openai:gpt-4.1", capabilities=[cap])
Attributes:
| Name | Type | Description |
|---|---|---|
subagents |
list[SubAgentConfig] | None
|
List of subagent configurations. |
default_model |
Any
|
Default model for subagents that name none, for the general-purpose subagent, and for a dynamic call that names no model. There is no implicit default: leave it unset and each of those is refused rather than run on a model the library picked, which resolves whatever provider credential the process environment happens to hold. |
include_general_purpose |
bool
|
Include general-purpose subagent. Needs
|
max_nesting_depth |
int
|
Max depth for nested subagents. |
toolsets_factory |
ToolsetFactory | None
|
Factory for subagent toolsets. |
registry |
DynamicAgentRegistry | None
|
Dynamic agent registry. |
descriptions |
dict[str, str] | None
|
Custom tool descriptions override. |
usage_limits |
UsageLimits | UsageLimitsFactory | None
|
Optional static or per-task usage limits for delegated subagent runs. |
delegation_configuration |
DelegationConfiguration
|
Select default, persisted, combined, or
one-shot-only delegation entry points. A mode that hides a tool
rejects that tool's configuration rather than ignoring it — see
|
allowed_models |
list[str] | None
|
Optional model allow-list for dynamic specialists. |
capabilities_map |
dict[str, CapabilityFactory] | None
|
Optional capability factories for dynamic specialists. |
default_agent_factory |
AgentFactory | None
|
Optional custom agent factory for dynamic
specialists, and for the general-purpose subagent when
|
max_agents |
int
|
Maximum number of persistent dynamic agents, applied to the
registry the toolset creates for |
max_chat_traces |
int
|
Maximum number of subagent conversations kept for
|
max_task_handles |
int
|
Maximum number of finished task handles retained for
status queries. Evicted usage still counts toward |
max_result_chars |
int | None
|
Character budget for a completed task's result in the
|
ask_user |
AskUserCallback | None
|
Callback backing |
event_stream_handler |
EventStreamHandler[Any] | None
|
Streams every delegation's events as they happen,
including for specialists created at run time. An agent supplied as
|
event_stream_handler_factory |
EventStreamHandlerFactory | None
|
The same, resolved per delegation from the
parent run context, the subagent config and the task id — which is
what lets a handler label the events of a fan-out. Mutually exclusive
with |
cancel_grace_seconds |
float
|
How long |
Raises:
| Type | Description |
|---|---|
ValueError
|
From |
Source code in src/subagents_pydantic_ai/capability.py
| Python | |
|---|---|
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 | |
task_manager
property
¶
The task manager behind the toolset, for observability.
__post_init__()
¶
Create the underlying subagent toolset.
Source code in src/subagents_pydantic_ai/capability.py
get_serialization_name()
classmethod
¶
get_toolset()
¶
wrap_run(ctx, *, handler)
async
¶
Run the agent, then stop every background task this run started.
A background delegation is an asyncio.Task the parent run does not await.
Without this finalizer it keeps executing after the run returns, against
deps the application has already torn down, and one blocked in ask_parent
waits for an answer that can never arrive.
Source code in src/subagents_pydantic_ai/capability.py
get_instructions()
¶
Return dynamic instructions listing available subagents.