AWS Lambda Durability
AWSLambdaDurability makes an agent resumable on AWS Lambda durable
functions. Every model
request, function tool call, MCP call, and dynamic-toolset resolution is checkpointed as a durable
step, so an invocation that times out, fails, or is retried continues from the last completed step
instead of repeating the work already paid for.
Lambda keeps a log of durable operations. When an execution resumes, the handler runs again from the top and completed steps return their stored results rather than executing. Without checkpointing, a resumed run would repeat every model request and tool call.
While Pydantic AI Harness is on 0.x releases, the API may change between minor releases; when it does, deprecation warnings and release-note migration guidance tell you (or your agent) exactly how to upgrade. See the version policy.
sequenceDiagram
accTitle: AWS Lambda Durability: resume an agent run from its last completed step
accDescr: AWSLambdaDurability runs each model request and tool call as a durable step. Invocation 1 records a model request and a get_weather call in the checkpoint log, then its next model request times out. Invocation 2 gets both replayed from the log without running them again, then a model request runs now and is recorded.
participant one as Invocation 1
participant log as Checkpoint log
participant two as Invocation 2
Note over one,two: AWSLambdaDurability() runs each model request<br/>and tool call as a durable step
one->>log: model request<br/>recorded
one->>log: get_weather call<br/>recorded
one->>one: model request<br/>times out
log-->>two: model request<br/>replayed, not run again
log-->>two: get_weather call<br/>replayed, not run again
two->>log: model request<br/>runs now and is recorded
pip install "pydantic-ai-harness[aws-lambda]"
uv add "pydantic-ai-harness[aws-lambda]"
The quick start below uses a Bedrock provider model, which needs the Bedrock SDK from
pydantic-ai-slim[bedrock]:
pip install "pydantic-ai-harness[aws-lambda]" "pydantic-ai-slim[bedrock]"
uv add "pydantic-ai-harness[aws-lambda]" "pydantic-ai-slim[bedrock]"
Attach the capability when you build the agent, then adapt an async handler body with
durable_agent_handler:
from typing import Any
from aws_durable_execution_sdk_python import DurableContext, durable_execution
from pydantic_ai import Agent
from pydantic_ai_harness.aws_lambda import AWSLambdaDurability, durable_agent_handler
agent = Agent(
'bedrock:global.anthropic.claude-sonnet-5-5',
name='support',
capabilities=[AWSLambdaDurability()],
)
@agent.tool_plain
def get_weather(city: str) -> str:
return f'It is sunny in {city}.'
@durable_execution
@durable_agent_handler
async def handler(event: dict[str, Any], context: DurableContext) -> str:
result = await agent.run(str(event['prompt']))
return result.output
Attach the capability at construction rather than per run, so the toolset wrapping and the deployed step shape stay the same across invocations.
Deploy with a durable configuration and invoke a published version, since in-flight executions are pinned to the version that started them:
aws lambda create-function \
--function-name support-agent \
--runtime python3.13 \
--handler handler.handler \
--role <ROLE_ARN> \
--zip-file fileb://support-agent.zip \
--timeout 300 --memory-size 1024 \
--durable-config '{"ExecutionTimeout":3600,"RetentionPeriodInDays":7}'
aws lambda publish-version --function-name support-agent
The agent needs a name (or AWSLambdaDurability(name=...)), and every leaf toolset needs a unique
id. Both are part of every step name, so both are checked when the agent is constructed: an agent
without a name raises a UserError from Agent(...), as does a toolset that has no id or shares
one with another toolset. Tools registered directly on the agent live in a toolset whose id renders
as <agent>, so @agent.tool_plain def get_weather is checkpointed as
{name}__function_toolset__<agent>.call_tool:get_weather.
Lambda’s durable API is synchronous: context.step(...) blocks, and every step has to be created
on the thread Lambda invoked. An agent run is async. durable_agent_handler uses run_durable to
bridge the two: it hosts the async handler body on a background event loop and services its steps
on the Lambda handler thread, so all steps are created in one continuous sequence. A step body
hands its work back to the agent loop and blocks until it finishes, which keeps the loop free while
the handler thread waits.
The async body can await several agent runs and do async work between them, and all of those
sections share the same bridge and step sequence. Await the runs one after another, not
concurrently with asyncio.gather or tasks: a resumed execution matches checkpoints by the order
steps are reached, so concurrent runs that reach their steps in a different order on replay are
served each other’s recorded results. A synchronous handler must call run_durable separately for
each async section, and concurrent calls are rejected.
Rules for the handler:
@durable_executionmust be the outermost decorator because its wrapper is what Lambda invokes. Reversing the order raises aUserErrorwhen the handler is defined.run_durableblocks the calling thread, so it cannot be called from inside a running event loop. Call it directly from a synchronous handler. It remains available when the bridge needs to be entered somewhere other than the top of a handler.- Durable steps cannot nest. A tool that starts another durable agent run is rejected with an explanatory error rather than deadlocking.
The loop is reused across invocations of a warm execution environment, so loop-bound resources like
a provider’s cached HTTP client stay valid between them. A run abandoned by a suspension or an error
is therefore cancelled before the handler returns, and run_durable waits cancel_timeout seconds
(5 by default) for it to unwind. Raise that for a workload whose cleanup is genuinely slow. If the
timeout expires, the abandoned cleanup keeps running on a retired loop for at most the retired
loop’s grace period and can overlap the next warm invocation. Do not share mutable module-global
state between the agent run and the handler. An external side effect from cleanup, such as writing
to a store, releasing a shared lock, or emitting a metric, can also land during a later invocation.
Loop-bound resources are isolated: the next invocation gets a fresh loop, so the abandoned cleanup
cannot touch resources such as that invocation’s provider HTTP client.
At the end of the grace period the abandoned cleanup is cancelled again, and the retired loop stops a few iterations later (on Python 3.11, after jobs already submitted to its default executor finish). A cleanup still running by then is left unfinished. A cleanup that cancels every task on its loop is not cancelled again; the loop stops at the deadline either way.
Loop reuse has one more consequence: do not detach background work from a tool with
asyncio.create_task() or by leaving executor work unawaited. Detached work is not checkpointed and
is not covered by durable execution’s guarantees, and it can outlive the invocation that started it.
To have a person approve a tool call, add a HandleDeferredToolCalls whose handler takes a resume token next to AWSLambdaDurability.
When the model calls a tool that needs approval (requires_approval=True or ApprovalRequired),
external execution (CallDeferred) or an answer to a question, the run creates a Lambda callback and
calls your ask handler with the callback ID inside a step. ask can answer right away, and the
step records its answer, so the run continues without suspending and a replay reuses the answer. To
let a person answer, ask writes the callback ID where the approver will see it and returns None,
and the run suspends: the invocation returns, and no compute is billed while the execution waits.
The answer comes back through your approval endpoint, which sends the decision to the callback.
Lambda then invokes the function again, the handler replays from its checkpoints without calling the
model or ask again, and the run continues with the approved calls executed and the denied ones
reported to the model.
import json
from datetime import timedelta
from typing import Any
import boto3
from aws_durable_execution_sdk_python import DurableContext, durable_execution
from pydantic_ai import Agent, DeferredToolRequests, RunContext
from pydantic_ai.capabilities import HandleDeferredToolCalls
from pydantic_ai_harness.aws_lambda import AWSLambdaDurability, durable_agent_handler
lambda_client = boto3.client('lambda')
def ask(ctx: RunContext, requests: DeferredToolRequests, callback_id: str) -> None:
... # Write the callback ID and `requests` where the approver will see them.
agent = Agent(
'bedrock:global.anthropic.claude-sonnet-5-5',
name='support',
capabilities=[AWSLambdaDurability(), HandleDeferredToolCalls(ask, timeout=timedelta(days=1))],
)
@agent.tool_plain(requires_approval=True)
def issue_refund(order_id: str, amount: int) -> str:
return f'Refunded ${amount} for order {order_id}'
@durable_execution
@durable_agent_handler
async def handler(event: dict[str, Any], context: DurableContext) -> str:
result = await agent.run(str(event['prompt']))
return result.output
def decide(callback_id: str, tool_call_ids: list[str], approved: bool) -> None:
# Runs in your approval endpoint, with permission to call `lambda:SendDurableExecutionCallbackSuccess`.
decision = {'approvals': {tool_call_id: approved for tool_call_id in tool_call_ids}}
lambda_client.send_durable_execution_callback_success(
CallbackId=callback_id, Result=json.dumps(decision)
)
From the command line, send the same decision with:
aws lambda send-durable-execution-callback-success \
--callback-id <CALLBACK_ID> \
--cli-binary-format raw-in-base64-out \
--result '{"approvals": {"<TOOL_CALL_ID>": true}}'
The decision is the JSON form of DeferredToolResults keyed by tool call ID: an approval can also
deny the call with a message for the model, or approve it with different arguments, and
The answer lists every form. send-durable-execution-callback-failure ends
the wait without a decision, like a timeout: every pending approval is denied and every pending
external call fails, with a message saying why, and the run continues.
The callback’s timeout is set from HandleDeferredToolCalls(timeout=...), rounded up to whole seconds, and
counts from when the callback is created, just before ask runs. Without one, the execution waits
until its ExecutionTimeout. When ask answers right away, nothing waits on the callback: it stays
open until its timeout, and resolving it has no effect on the run. Without a timeout, that callback
has none, so it is never closed; when ask may answer in place, set timeout so it expires. The ask step is a step like any
other: it is not run again on replay, step_config applies to it, and the default retry policy can
run it again when it fails, so make it idempotent. Both are named after HandleDeferredToolCalls(name=...),
deferred_tool_calls by default. An approved call’s tool appears twice in the history as
call_tool:<tool> steps: once for the attempt that asked for approval, and once for the approved
run. A decision must cover every pending call unless DeferredToolRequests is in the agent’s
output_type. A checkpointed tool cannot wait for a decision itself, because it runs inside a step;
that is rejected with a UserError.
Questions from Pydantic AI Harness’s AskUser wait the same way: its answerer runs in the ask
step, and an answerer that takes a resume token can hand the question on and let the answer arrive
through the callback.
Outside a durable handler, as in local development, the same agent waits in process instead, and
resolve_external_event from pydantic_ai.external_events resolves its token: see
A round trip in one process. A handler without a token
that waited for the approver itself would hold the invocation, and its 15-minute timeout, for the
whole wait, and would ask again on every replay; ending the run with DeferredToolRequests output and
starting a second run with the results also still works.
Step names are built from the agent’s name and each toolset’s id:
| Step name | Operation |
|---|---|
{name}__model.request | one model request segment |
{name}__model.request_stream | one streamed model request segment |
{name}__model.compact_messages | one model message-compaction operation |
{name}__model.count_tokens | one token count ahead of a model request, with count_tokens_before_request |
{name}__model.cancel_suspended_response | tearing down a suspended response |
{name}__capability__{capability_id}.{operation} | an operation contributed by another capability |
{name}__function_toolset__{id}.validate_args | validating a function tool call’s arguments |
{name}__function_toolset__{id}.call_tool:{tool} | a function tool call |
{name}__mcp_server__{id}.get_tools | listing an MCP server’s tools |
{name}__mcp_server__{id}.get_instructions | an MCP server’s instructions |
{name}__mcp_server__{id}.call_tool | an MCP tool call |
{name}__dynamic_toolset__{id}.get_tools | resolving a dynamic toolset |
{name}__dynamic_toolset__{id}.validate_args | validating a dynamic toolset call’s arguments |
{name}__dynamic_toolset__{id}.call_tool:{tool} | a dynamic toolset’s tool call |
{name}__event_stream_handler | one event delivered to an event_stream_handler |
{name}__external_event.deferred_tool_calls | the callback a HandleDeferredToolCalls wait suspends on |
{name}__external_event.deferred_tool_calls.ask | calling ask with that callback’s ID, and its answer |
{name}__external_event.ask_user_question | the callback an AskUser question waits on |
{name}__external_event.ask_user_question.ask | calling the AskUser answerer, and its answer |
A model operation that does not use the agent’s default model records its model id in the step name
(for example, {name}__model.request.{model_id}), so the execution history shows which model each
checkpoint came from. The default model keeps the plain, suffix-less name.
A step served from its checkpoint emits no telemetry: instrumentation records a model request’s or tool call’s span, and a model request’s token and cost metrics, in the invocation that ran it, and an agent run’s span in each invocation that did work for it. A resumed execution’s traces show only the work that invocation did, and costs are counted once.
-
Steps are at least once, and retried by default. A step is checkpointed after it runs, so an interruption between a tool’s side effect and its checkpoint re-runs the tool when the execution resumes. On top of that, the SDK’s default retry policy is six attempts with exponential backoff (5s to 60s), applied to every model request and every tool call. Keep tool side effects idempotent.
AT_MOST_ONCE_PER_RETRYalone is not enough to make a tool run once: it prevents re-execution after an interruption within an attempt, but the retry policy still starts further attempts that do execute the body. For a tool that must not repeat, set both:from aws_durable_execution_sdk_python.config import StepSemantics from aws_durable_execution_sdk_python.retries import RetryPresets metadata={'aws_lambda': {'step_semantics': StepSemantics.AT_MOST_ONCE_PER_RETRY, 'retry_strategy': RetryPresets.none()}} -
Retries stack. Pydantic AI and provider clients have their own retry logic. Leaving those enabled alongside the step retry policy multiplies the attempts, and a retry inside the step waits out the provider’s
Retry-Afterwhile the invocation runs. Disable one side, for example withmodel_settings={'max_retries': 0}on the agent. -
A model step’s retry waits the provider’s
Retry-After. When a model request fails with a model error that has aretry_after(read from aRetry-Afterorretry-after-msheader, for example) and the step’s retry strategy retries it, the execution suspends for that long, rounded up to whole seconds (at least one), instead of the strategy’s delay. No compute is billed while it waits. The strategy still decides whether to retry, so its attempt limit and error filters apply. A strategy is a function with no maximum to read, so the wait is capped at the strategy’s own delay for that attempt or a minute, whichever is longer. -
Tool calls run one at a time. A step’s identity comes from the order steps are reached, so concurrently scheduled tool calls could claim each other’s checkpoints when the execution resumes. Inside a durable handler the run is switched to sequential tool execution. Outside one the agent keeps its configured parallelism.
DynamicWorkflowsub-agents are the exception: they run concurrently, each in a branch of a Lambdaparalleloperation, which numbers its steps on its own. -
Changing the shape of the run breaks in-flight executions. A resumed execution matches checkpoints by the order operations are reached, so anything that changes the number or order of steps breaks executions started under the old code: adding or removing a tool or MCP server, flipping a
metadata={'aws_lambda': False}opt-out, or adding anevent_stream_handler(which switches the model step tomodel.request_streamand adds handler steps). SDK 2.0 and later compare each step’s type and name on replay, so these changes, and renaming the agent, a toolsetidor a tool, or changing the id a non-default model runs under (amodels=key or themodel=passed toagent.run), which is part of its step names, fail the execution withNonDeterministicExecutionError. On SDK 1.x a mismatch is not detected: a step is served another step’s recorded result, which usually fails to decode but can return the wrong data. Either way, deploy under a new published version and let in-flight executions drain on the old one. -
Step results must survive the SDK serializer. Results are checkpointed through the Lambda SDK’s serializer. Tool results are encoded by Pydantic first, so structured returns such as
ToolReturnandBinaryContentround-trip; a value Pydantic cannot serialize does not. -
Events cannot leave a running durable execution.
run_streamanditerdo work inside the handler and are checkpointed normally, but a durable execution returns a single value when it completes, so there is no channel to stream tokens to a caller while it runs. Anevent_stream_handlerworks: model events are handled live inside the model step and each agent-level event is checkpointed in its own step. -
ctx.enqueue()is not available inside a durable step, whether a checkpointed tool or anevent_stream_handler(which runs inside the model step for model events and its own step for agent events), because a resumed execution serves the recorded step output and would drop the enqueued messages. Enqueue from handler-level code instead. -
Checkpoints hold run data. Model responses, tool results, and the error messages of failed steps (which can include a provider’s error response body) are stored in the execution’s durable state for the
RetentionPeriodInDaysof the durable configuration. Anyone who can read the execution history can read them, so keep the retention period short for sensitive workloads. -
Budgets. A durable execution allows 30,000 operations and 100 MB of cumulative checkpointed state. A turn costs one model step plus one step per tool call, so the operation budget is generous, but large tool results consume the state budget: return a reference (an S3 key, say) rather than a blob.
-
Batched requests wait in the invocation. Under
BatchModelRequests, submitting a request and each check on its batch are durable steps, but the waits between checks are plain sleeps: the invocation has to stay up for as long as the batch takes, within its timeout, and a recovered run sleeps again through the waits it already did.
Tool metadata under the aws_lambda key configures that tool’s step. It accepts the StepConfig
fields retry_strategy, step_semantics, and serdes:
from aws_durable_execution_sdk_python.config import StepSemantics
from pydantic_ai.toolsets import FunctionToolset
toolset = FunctionToolset(id='billing')
@toolset.tool_plain(metadata={'aws_lambda': {'step_semantics': StepSemantics.AT_MOST_ONCE_PER_RETRY}})
def charge_card(amount: int) -> str:
return f'charged {amount}'
metadata={'aws_lambda': False} opts a tool out of checkpointing entirely, so it runs inline on
every attempt. Use it for cheap, side-effect-free tools whose result is not worth a checkpoint. MCP
tools cannot opt out, because they perform I/O that must not re-run when the execution resumes.
The engine-neutral durable=False opts an async
function tool out the same way on every engine; an 'aws_lambda' key on the same tool wins.
AWSLambdaDurability(step_config=...) sets the base configuration for every step. Per-tool metadata
overrides it key by key, so a tool that sets only step_semantics keeps the base retry_strategy.
AWSLambdaDurability orders itself innermost, so any other capability’s contribution to a model
request is already applied inside the durable step. Attach it alongside other capabilities as usual.
- Durable execution in Pydantic AI agents with AWS Lambda Durability (Pydantic)
- AWS Lambda durable functions
- AWS Durable Execution SDK for Python
- Pydantic AI durable execution
- AWS Lambda Durability source code
Bases: BaseDurabilityCapability[AgentDepsT]
Capability that checkpoints an agent’s I/O into AWS Lambda durable steps.
Attach it with capabilities=[AWSLambdaDurability()] and decorate an async handler with
durable_agent_handler: every model request, function tool call, MCP call, and dynamic-toolset
resolution is wrapped in DurableContext.step(...). A completed step is served from its
checkpoint when the execution resumes, so finished work is not repeated.
A step is checkpointed after it runs, so an interruption between a tool’s side effect and its
checkpoint re-runs the tool when the execution resumes: keep tool side effects idempotent, or
set step_semantics to AT_MOST_ONCE_PER_RETRY for the tools that cannot tolerate it.
Outside a durable handler the capability is transparent and the run is an ordinary agent run.
Step results are checkpointed through the Lambda SDK’s serializer, so a checkpointed tool’s
return value must survive that round trip. Control-flow signals (ModelRetry,
ApprovalRequired, CallDeferred, ToolFailed) cross the boundary as values rather than
exceptions, so approval and deferred-tool flows work inside a durable execution. With a
HandleDeferredToolCalls handler that takes a resume token or harness AskUser attached, a call
that needs an answer from outside waits on a Lambda callback instead of ending the run.
def __init__(
*,
models: Mapping[str, Model] | None = None,
event_stream_handler: EventStreamHandler[AgentDepsT] | None = None,
name: str | None = None,
step_config: Mapping[str, Any] | None = None,
) -> None
Create an AWSLambdaDurability capability.
The agent’s model, name, and toolsets are discovered when the capability is bound.
Optional additional models keyed by ID for run-time model switching via
agent.run(model='<id>'). The ID is folded into the step name so the execution
history shows which model each checkpoint came from.
event_stream_handler : EventStreamHandler[AgentDepsT] | None Default: None
Optional event stream handler. Model events are handled live inside the model-request step; each agent-level event is handled in its own checkpointed step.
Unique agent name used as the prefix for every step name. Defaults to the
agent’s name when the capability is bound.
Base StepConfig fields applied to every step, as a mapping of
retry_strategy, step_semantics, and serdes. Per-tool
metadata={'aws_lambda': {...}} overrides it key by key for that tool.
@async
def wrap_run(
ctx: RunContext[AgentDepsT],
*,
handler: WrapRunHandler,
) -> AgentRunResult[Any]
Let the run wait for external events, such as HandleDeferredToolCalls answers, through Lambda callbacks.
def durable_agent_handler(
func: Callable[[Any, ContextT], Coroutine[Any, Any, T]],
/,
) -> Callable[[Any, ContextT], T]
def durable_agent_handler(
func: None = None,
/,
*,
cancel_timeout: float = DEFAULT_CANCEL_TIMEOUT_SECONDS,
) -> Callable[[Callable[[Any, ContextT], Coroutine[Any, Any, T]]], Callable[[Any, ContextT], T]]
Adapt an async handler body for the AWS durable execution decorator.
@durable_execution must be outermost because its synchronous wrapper is what Lambda invokes.
Callable[[Any, ContextT], T] | Callable[[Callable[[Any, ContextT], Coroutine[Any, Any, T]]], Callable[[Any, ContextT], T]]
Async durable handler body.
cancel_timeout : float Default: DEFAULT_CANCEL_TIMEOUT_SECONDS
Seconds to wait for an abandoned handler body to unwind. See run_durable.
def run_durable(
agent_run: Callable[[], Coroutine[Any, Any, T]],
*,
context: DurableStepContext,
cancel_timeout: float = DEFAULT_CANCEL_TIMEOUT_SECONDS,
) -> T
Run an async agent call from a synchronous Lambda durable handler.
Hosts agent_run() on a background event loop and services its durable steps on the calling
(handler) thread, so every context.step(...) is created in one continuous sequence on the
thread Lambda invoked. Returns whatever agent_run() returns.
T
Callable returning the coroutine to run, e.g. lambda: agent.run(prompt).
It is called once per handler invocation, including each replay.
The DurableContext the durable handler was invoked with.
cancel_timeout : float Default: DEFAULT_CANCEL_TIMEOUT_SECONDS
Seconds to wait for a run abandoned by a suspension or an error to finish unwinding before returning. Raise it for a workload whose cleanup is genuinely slow. When it expires, the background event loop is retired so the next invocation builds a fresh loop and the cleanup cannot touch its loop-bound resources, such as a provider’s cached HTTP client. The cleanup can still run during that invocation for the retired loop’s grace period, so it can still affect module-global state or external systems.
Bases: BaseException
The agent loop stopped while a durable step was in flight, so its result can never arrive.
A BaseException rather than an Exception on purpose: consume() routes ordinary step
failures back into the agent run so it can handle them, and that is precisely what cannot work
here — the loop that would receive them is the thing that is gone. Like the SDK’s own control
flow, this has to leave the handler instead.