A useful answer can still be incomplete
An agent inspects synthetic compiler diagnostics through a tool. Each tool response suggests another file worth checking. Without a stopping policy, the run can consume its entire deadline and fail before describing anything it learned. Reserving time for a final response changes the experience: the caller receives the completed observations and an explicit account of unfinished work. It must not be mistaken for successful completion of the original task.
Pydantic AI 2.78.0, released on December 2, 2026, extends the opt-in WrapUpAtLimits capability to projected cost and run time. This is a behavior change for applications already using that capability with the relevant limits. A caller that previously handled only an exception may now receive an answer. The migration question is therefore about result semantics as much as timeout configuration.
The release and its underlying pull request are the primary evidence for the change. The example for this guide uses a scripted FunctionModel, synthetic diagnostics and timed local sleeps. It is designed to exercise framework control flow without credentials or a provider request. Such a run can test whether wrap-up activates; it cannot establish model quality, billed cost or reliability under real network latency.
Separate hard limits from graceful stopping
A hard deadline ends the run when time is exhausted. A graceful stopping policy tries to finish before that deadline by estimating whether another work step and a final response will fit. Prediction cannot replace enforcement. A tool can suddenly take longer than previous tools, and the final model response can exceed its estimate. Preserve the hard deadline and test the exception path even when graceful stopping is enabled.
The implementation described in the upstream change observes completed steps and model response durations. It reserves room for another work step plus a final response, with an optional margin. timeout_margin=None disables the time-based wrap-up trigger. The release also keeps structured output tools available for the final request while preventing further ordinary tool work. These details are specific to the cited version and should be rechecked when upgrading.
Treat the projection as a control heuristic. It does not mean the remaining duration is known, nor does it make a cost limit an exact prepaid account. Pricing data can be incomplete, and a newly enlarged prompt can change the next request's cost. If an operation requires a strict spending boundary, enforce reservations outside the conversational loop and reconcile actual usage afterward.
Design the result contract before enabling the capability
Return a structured result with separate fields for findings, completeness, remaining work and stop reason. A valid schema is not sufficient evidence of completion. A final answer may be syntactically perfect while covering only two of five requested diagnostics. Downstream automation should check the completeness field and the application-owned list of required operations rather than searching the prose for apologetic language.
The application should derive completed operation identifiers from its execution record. Let the model describe their meaning, but do not let it declare that an unexecuted inspection happened. The same distinction applies to unresolved errors: a fluent final summary is useful only if the consumer can tell which claims have corresponding tool evidence. Preserve links to synthetic fixture outputs in tests and durable result references in deployed systems.
A stopped run can still be valuable. For an exploratory diagnostic assistant, partial findings may be the intended response. For a deterministic verification job, partial work may require a failed or pending state. Make that choice in the service contract. A capability designed to produce an answer should not silently convert a mandatory verification workflow into best-effort prose.
A reproducible offline experiment
Create a scripted model that asks for one diagnostic on each request until ordinary tools are disallowed. At that point it returns a summary of the calls actually executed. Add a fixed delay to the model response and another delay to the tool. Run with a short deadline, then repeat with a positive timeout margin and with the time trigger disabled.
The expected comparison is qualitative: a margin can make the run finish earlier with fewer diagnostics, while disabling the trigger preserves deadline exceptions when the scripted model keeps asking for work. Do not assert an exact number of calls from wall-clock sleeps. Scheduling jitter makes that brittle. For exact counts, use the framework's supported test clock or isolate the projection arithmetic in a deterministic test.
# Author: Sandeep Belgavi. Configuration fragment for Pydantic AI 2.78.0.
from pydantic_ai import Agent
from pydantic_ai.models.function import FunctionModel
from pydantic_ai_harness import WrapUpAtLimits
def configured_agent(scripted_response):
return Agent(
FunctionModel(scripted_response),
capabilities=[WrapUpAtLimits(timeout_margin=0.5)],
)The fragment deliberately accepts a scripted response function. It does not select a real provider or imply that a provider call was made. The local companion run used pydantic-ai-slim 2.78.0 and pydantic-ai-harness 0.78.0. With a 0.8-second deadline and 0.1-second margin it returned an answer marked with the timeout wrap-up reason after three completed synthetic inspections. With the trigger disabled it raised the deadline exception after four completed inspections. Those counts are observed fixture results, not timing guarantees or provider measurements.
Structured output needs its own test
An agent that returns a typed object may use an output tool to deliver that object. Blocking all tools on the final request could therefore block the only valid result path. The cited change distinguishes output tools from ordinary work tools. Verify that distinction with the actual output mode used by the application rather than assuming a passing plain-text test covers structured output.
Construct a result with observed_ids, unfinished_ids and complete. Validate it against the execution ledger. Reject unknown identifiers even if the model provides a convincing explanation. Also test a final output that fails validation: the remaining budget may be too small for another repair request. The application needs an honest fallback object that reports unavailable synthesis while retaining completed raw findings.
Streaming introduces another boundary. A user may see partial text before the final result is validated. Do not let a downstream system act on streamed prose as though it were the accepted structured result. Mark terminal status separately and only publish the machine-consumed result after validation. This is a general application design requirement, independent of whether graceful stopping happened.
Migration failure modes
The first migration trap is exception-only monitoring. A service counts RunTimedOut as failure and everything returned as success. Enabling wrap-up lowers the exception count while increasing incomplete answers. Add a separate completion measure before interpreting that change as an improvement. Record stop reason, completed operations and elapsed time without high-cardinality prompt contents in metric labels.
The second trap is a long first step. A projection based on observed behavior has limited evidence before a step completes. Give expensive tools their own timeouts and bounded inputs. A model deciding to inspect a large artifact should not gain an unlimited operation merely because the overall agent has a graceful stopping capability. Tool contracts remain the first line of control.
The third trap is replay or retry confusion. A final summary should describe the operations belonging to that run and input revision. If a retry reuses stored findings, mark them as reused evidence and verify that their input digest still matches. Do not count an old observation twice when computing completeness. Resume state and conversational history have different responsibilities.
Verification before rollout
Test the configured margin, the disabled trigger, a tool that exceeds the hard deadline, malformed structured output and a run that completes naturally before any limit. Confirm that the application distinguishes natural completion from graceful wrap-up. Include a test where no useful observation exists yet; the fallback must not invent a finding to make the response look helpful.
Roll out with a small controlled workload and compare complete results, partial results, deadline exceptions and actual provider usage separately. A shorter mean duration can simply mean the agent performed less work. Report that tradeoff directly. The capability is useful when partial progress is a legitimate product outcome, while deterministic tasks with a fixed checklist may be clearer as an ordinary bounded workflow.