In this article
Place capture boundaries around the run whose failure you need to recover, and keep nested agent histories explicitly associated with their own runs.
Capture the run whose failure matters
A service invokes a planning agent and then a research agent inside one request handler. If research fails, the service wants its partially collected evidence. Pydantic AI’s agent API documents a specific capture boundary: calling run methods repeatedly inside one capture_run_messages context captures the messages of the first call only. One broad context is therefore not automatically a combined transcript of both agents.
Align the context with the invocation
Place a capture context around the particular invocation whose history must be retained. Keep its captured messages linked to the corresponding agent and application task. If separate runs need recovery, give them separate explicit records. Otherwise an error handler may offer planning messages as though they were the research agent’s interrupted state, losing the source observations that the user expected the service to preserve.
Understand nested capture ownership
The reference permits nested contexts and assigns a run to its innermost active capture context. An inner agent invoked from a tool can therefore be inspected independently, while its messages are not also captured by an enclosing context. This is useful separation when retained deliberately. Do not infer that an outer captured history contains every internal sub-agent conversation merely because the outer agent awaited the tool that invoked it.
Preserve messages before relying on an exception
The agent guide explains that a failed run returns no normal result and most run exceptions do not carry its messages. Capture records them as execution progresses, including interrupted state around a failed step. Set up the boundary before invoking the run. Tool observations can associate external operations with their originating run, especially when a partial conversation ends after real tools already completed.
Resume with the right conversation frontier
The history guide explains how interrupted histories are interpreted during continuation. Inspect the captured final request or response and pending calls before resuming. Task contracts identify which task remains unfinished. A useful recovery record holds the correct run’s messages and its application state, rather than assuming that the existence of any captured list proves the failed research conversation was retained.
Earn rental income from GPUs
Have idle GPUs or looking to fund AI infrastructure? Retrivium finds a rental platform, sets up the server and helps you assess income and costs with a pilot.