Pydantic AI Code Mode can make a failed script easy to rerun without making its external effects safe to repeat. The Pydantic AI and Agent Sandboxes documentation says the script runs in a sandbox, but your tools still run in your process. A type-checked script and a recorded run therefore do not answer the recovery question: did a payment, message, update, or other consequential action already happen?
Before deployment, review each consequential tool for an operation identifier, a durable side-effect record, and a tested way to reconcile or compensate an uncertain outcome. Gate the release on fault-injection tests, not on whether the model can produce another valid script after an error.
What Code Mode isolates, and what remains your responsibility
Code Mode turns the agent’s tools into functions the model can call from Python. The model receives one run_code tool, and Agent Sandboxes executes each script. Code Mode generates type stubs from your tools, checks scripts against them, returns errors to the model, and records runs in Logfire, according to the vendor documentation.
Those are useful correctness and observability features. They address different questions from recovery:
- Type checking: does the script match the advertised tool interfaces?
- Sandbox execution: where does the model-written Python execute?
- Error feedback: can the model respond to a script failure?
- Run recording: can a developer inspect what happened?
- External recovery: can the team establish the state of each real-world action before trying again?
The last question remains open because the tool calls reach code running in your process. The sandbox boundary around the script is not a transaction boundary around the tool’s effects.
There are two further coverage limits in the documented design. Logfire retains the snapshot identifier for seven days; resuming after that starts a fresh session. Some tools, including lazily loaded tools, are called directly rather than through run_code, as described in the same documentation. Neither observation proves that recovery is impossible. It does mean a reviewer should not treat session resumption or a run_code-only inventory as evidence that every external action is covered.
Review the error path separately from the side effect
A failed script can contain several different outcomes. Python may have failed before calling a tool. A tool may have returned an error without changing anything. It may also have completed an external action before a timeout, connection failure, or later script exception made the overall run look unsuccessful.
Only the last case creates an ambiguous repeat, but the visible script error alone cannot reliably distinguish these outcomes. The review should therefore follow the action rather than stop at the exception:
- Identify which tool invocations the script could have reached.
- Determine which invocations can change external state.
- Find the identifier that connects the script attempt to the external operation.
- Establish whether an authoritative read-back can resolve the result.
- Define what happens when it cannot: escalation, an unresolved case, or compensation.
This is where “idempotent” needs scrutiny. A tool may safely accept an identical retry under its normal contract while the surrounding workflow still lacks evidence that the first attempt and retry refer to the same operation. The fault-injection preprint “Idempotency Is Not Recovery” makes that distinction directly. In one configuration, a capture-persistence failure left 75 of 100 actions per platform unresolved, the preprint reports, rather than allowing them to be finalized without evidence.
That unresolved pile is not a recovery success story by itself. It is conservative behavior: the system declines to declare completion when it cannot support that declaration. Somebody still needs a queue, an owner, and a way to settle those cases.
A pre-deployment recovery worksheet
The following worksheet is a reviewer-supplied control, not a recovery contract stated by Pydantic AI. It maps the documented components to evidence that the documentation does not itself supply.
| Review item | What the cited material establishes | Evidence to require before shipping |
|---|---|---|
run_code scripts | Scripts are checked against generated type stubs, errors are returned to the model, and runs are recorded in Logfire. | An attempt identifier that can be linked to every consequential tool action, plus error-path tests showing where execution stopped. |
| Tools in the user process | Only the script is sandboxed; your tools execute in your process. | A per-tool side-effect ledger containing the operation identifier, attempt status, external result when available, and reconciliation state. |
| Lazily loaded or directly called tools | Some tools bypass run_code. | An explicit inventory check showing that these tools receive the same recovery review rather than disappearing from a run_code-centric audit. |
| Logfire records and snapshots | Runs are recorded, and the documented snapshot identifier has a seven-day retention window. | Independent retention rules for operation and side-effect evidence, based on how long reconciliation may take rather than on session resumption. |
| Retry and deduplication behavior | The supplied RetryBench contract is time-bounded and key-scoped; it is not a Pydantic AI contract. | A verified contract for each actual tool, including when the key is created, how long it remains valid, and what happens after expiry. |
| Partial completion | Saga-style compensation is a separate operation after partial execution. | A tested reconciliation path and, where appropriate, a separately tracked compensation operation with its own failure handling. |
For each consequential tool, the completed worksheet should answer a short set of operational questions:
- What external state can this action change?
- What identifier is created before the first attempt?
- Where is the attempt recorded, and how long does that record survive?
- Can the external service or another authoritative source confirm the outcome?
- Which retry uses the same operation identity, and which retry would create a new action?
- Who owns an unresolved case?
- If compensation is required, what proves that compensation completed?
A genuinely read-only tool may not need the full side-effect gate. The exception depends on verified behavior, however, not on a tool name such as get or lookup. For actions whose repetition would matter to a customer or another system, I would not accept “the script is rerunnable” as a release argument.
Design operation identifiers for their actual limits
An operation identifier is useful only if the tool contract gives it meaning. The RetryBench paper supplies a concrete example of why reviewers need to read that contract rather than assume deduplication. In its tool contract, keys are retained for 24 hours, different keys do not deduplicate one another, and a key cannot protect an earlier request that omitted it. Its probe calls used the same task, target, and payload within 30 seconds.
Those details belong to RetryBench, not to Pydantic AI or to every external service. They nevertheless expose three checks that transfer to a review:
Create the identity before the first consequential request. Adding a key only when retrying cannot retroactively group the original keyless request with the new one under that contract.
Keep the identity stable for the same logical operation. A fresh key may represent a fresh operation rather than protection against duplication. The ledger should make that distinction visible.
Test behavior beyond the retention window. A retry after key expiry needs a reconciliation decision. It should not inherit safety assumptions from a retry made while the key was still valid.
RetryBench also describes itself as a scoring protocol and four-task probe suite, not a leaderboard or a general estimate of agent reliability. Its key semantics are consequently a warning against vague assumptions, not a universal specification to copy into your tools.
Test the faults that make rerunning dangerous
A recovery acceptance test should inject failure at the service boundary, where the client’s view can diverge from the external action’s state. Merely raising a Python exception before a tool call does not exercise that gap.
The Limbo benchmark paper describes twelve injected service-boundary fault modes, including three that it says prior agent benchmarks omitted: late commits, redelivery, and partial batches. These provide a useful minimum acceptance set.
Late commit: let a request time out while remaining in flight, then allow it to take effect after the client has moved on. The acceptance evidence should show whether the agent retried, what operation identity it used, and whether reconciliation detected the delayed result. A test passes only if the resulting behavior matches the tool’s stated recovery policy, not simply if the final script reports success.
Redelivery: deliver the same request twice. Inspect the external operation record and side-effect ledger, not just the model’s response. The test should establish whether the two deliveries were recognized as one operation under the actual contract.
Partial batch: allow only part of a multi-item action to complete. Require an item-level accounting of completed, unresolved, and compensatable effects. Rerunning the whole batch is a separate decision that needs its own justification.
Limbo also reports that when no read-back can resolve a fault, the correct model-side response is to decline to act on uncertain information, escalate, or report uncertainty. Some models do this, but none does so reliably in the reported probes. Human escalation therefore needs to be part of the workflow design rather than a hoped-for model behavior.
Reconcile, compensate, or leave unresolved
These are different outcomes and should not share a generic “recovered” status.
Reconciliation establishes what happened and connects that result to the original operation. Compensation responds to an effect after partial execution. The banking delegation paper places compensation in the saga tradition and identifies it as a separate operation. Unresolved means the available evidence cannot support either safe completion or a justified retry.
Consider a hypothetical script that creates an invoice and then times out. Finding the original invoice through an authoritative lookup is reconciliation. Voiding a confirmed duplicate is compensation. Retrying without knowing whether the first request committed is neither. If the void operation itself can fail, it also needs an identifier and recovery record.
Evidence preservation has an operational price. In “Idempotency Is Not Recovery”, the configuration using durable authorization and logging achieved roughly half the measured throughput of configuration B1. That result concerns the study’s own configurations, not a Pydantic AI deployment, so it cannot predict your overhead. It does support budgeting for durable writes, unresolved-case handling, and slower finalization instead of presenting recovery evidence as free.
What this review cannot claim about Code Mode
No cited source tests Code Mode, and no cited source documents an independent production recovery failure involving it. The vendor page documents execution, feedback, recording, and resumption behavior; its silence on recovery semantics does not prove that no other recovery feature exists. The worksheet is an inference from the documented boundary between sandboxed scripts and user-process tools.
The supporting research also has limits. “Idempotency Is Not Recovery” is a non-peer-reviewed, single-host preprint and explicitly says that it does not inherit distributed guarantees, and none of the cited preprints measures Code Mode. Their role is to supply failure modes and review distinctions, not to establish a production failure rate.
The documentation snapshot lists minimum installation versions of logfire[agent-sandboxes]>=5.6.0 and pydantic-ai-harness[codemode]>=0.67.0. Those floors, the seven-day snapshot window, and current component behavior should be checked against the documentation and release notes at installation time.
The deployment decision is still clear: for each consequential tool, require a logged operation identifier, durable side-effect evidence, and a tested reconciliation or compensation path. If a late commit, redelivery, or partial batch can leave the team unable to explain external state, the workflow is not ready to be silently rerun.
Frequently Asked Questions
How long does Logfire retain the snapshot identifier for resuming a session?
Logfire retains the snapshot identifier for seven days; resuming after that starts a fresh session.
Which tools bypass the run_code tool in Pydantic AI Code Mode?
Some tools, including lazily loaded tools, are called directly rather than through run_code, as described in the same documentation.
Join the discussion
Share a useful perspective or ask a question about this article.