Retry API¶
Auto-retry for transient model and gateway failures. Each retry resumes from the accumulated message history rather than starting over. See Retries.
RetryConfig¶
subagents_pydantic_ai.RetryConfig
dataclass
¶
Resolved retry policy for a subagent run.
Attributes:
| Name | Type | Description |
|---|---|---|
max_retries |
int
|
Number of additional attempts after the first
failure. Defaults to |
initial_delay |
float
|
Seconds to wait before the first retry. |
max_delay |
float
|
Upper bound for the backoff delay, in seconds. |
backoff_multiplier |
float
|
The delay is multiplied by this each attempt. |
jitter |
bool
|
When |
retry_on |
RetryPredicate | None
|
Predicate deciding whether an exception is transient.
|
Source code in src/subagents_pydantic_ai/retry.py
from_config(config)
classmethod
¶
Build a :class:RetryConfig from a :class:SubAgentConfig.
Missing keys fall back to the dataclass defaults, so a config
without any retry_* keys yields the default policy (3
retries with exponential backoff).
Source code in src/subagents_pydantic_ai/retry.py
should_retry(exc)
¶
Return whether exc is retryable under this policy.
run_with_retry¶
subagents_pydantic_ai.run_with_retry(agent, user_prompt, *, run_kwargs, retry, on_retry=None, sleep=asyncio.sleep, event_stream_handler=None, cancel_check=None, inject_messages=None)
async
¶
Run agent with auto-retry on transient errors.
When retry.max_retries <= 0 this is exactly agent.run(...) —
the legacy path, unchanged. Otherwise the agent is driven via
agent.iter() so that, on a transient failure, the accumulated
message history from the failed attempt is replayed via
message_history on the next attempt and the subagent resumes
instead of restarting from scratch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent
|
Any
|
The pydantic-ai |
required |
user_prompt
|
str | None
|
Initial prompt. After a retry that captured history
it is set to |
required |
run_kwargs
|
dict[str, Any]
|
Extra kwargs forwarded to |
required |
retry
|
RetryConfig
|
The resolved retry policy. |
required |
on_retry
|
OnRetryCallback | None
|
Optional callback invoked before each retry sleep with
|
None
|
sleep
|
Callable[[float], Awaitable[None]]
|
Async sleep function, injectable for tests. |
sleep
|
event_stream_handler
|
Any | None
|
Optional override for the agent's configured
|
None
|
cancel_check
|
Callable[[], bool] | None
|
Optional callable polled between graph nodes for
cooperative (soft) cancellation. When it returns |
None
|
inject_messages
|
Callable[[], Awaitable[list[str]]] | None
|
Optional async callable awaited before each model
request; its returned strings are appended to that request as
user instructions (unprompted parent -> child steering). Like
|
None
|
Returns:
| Type | Description |
|---|---|
Any
|
The |
Raises:
| Type | Description |
|---|---|
Exception
|
The last exception when retries are exhausted or the error
is not transient. |
Source code in src/subagents_pydantic_ai/retry.py
| Python | |
|---|---|
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 | |
is_transient_error¶
subagents_pydantic_ai.is_transient_error(exc)
¶
Return True if exc looks like a transient networking failure.
Treated as transient (worth retrying):
ModelHTTPErrorwith a 408/429/5xx status code — gateway hiccups, rate limits or upstream overload, typical with proxies such as LiteLLM.ModelAPIErrorthat is not an HTTP error — connection resets, read timeouts and other transport-level problems surfaced by the model client.
Everything else (auth/4xx, UnexpectedModelBehavior,
UsageLimitExceeded, UserError, validation errors, task
cancellation, ...) is treated as non-transient and is not retried.
Source code in src/subagents_pydantic_ai/retry.py
compute_backoff_delay¶
subagents_pydantic_ai.compute_backoff_delay(attempt, cfg, rng=random.uniform)
¶
Compute the delay (seconds) before retry attempt (1-based).
Exponential backoff (initial_delay * multiplier ** (attempt - 1))
capped at cfg.max_delay. With cfg.jitter the result is
randomised in [0, delay] (full jitter). rng is injectable for
deterministic tests.