Keep replayed durable work out of traces and token and cost metrics on AWS Lambda and Temporal

Merged
DO
24d ago
+500-14

No issue: found preparing the AWS Lambda durable functions webinar with AWS. Other engines are tracked in #12752.

Why we make these changes

A durable engine resumes a run by replaying it. Completed model requests and tool calls return their recorded results, but Pydantic AI's instrumentation ran again, so every resumed invocation re-emitted their spans and recorded their token and cost metrics again. On AWS Lambda with real Bedrock, the first request's cost counted 3× in a run with three invocations, and Logfire's cost rollups were ~37% too high. On Temporal, #7006 made spans replay-safe through LogfirePlugin, but token metrics were still recorded on every history replay: 4× under forced replay.

Engines now tell instrumentation which work came from the journal. Every span keeps a tally of the durable units that completed under it, in the requesting task's context, so concurrent tool calls are told apart. A span whose units were all replayed is never ended, so it is never exported. Replayed model requests and failed attempts skip their metrics and spans. A reported span keeps its parents.

New public surface

  • pydantic_ai.durable_exec.replay_scope(*, is_replaying=None): wrap a durable agent run; is_replaying is for engines with a "replaying now" flag.
  • pydantic_ai.durable_exec.record_durable_unit(*, executed): report one durable unit, executed or served from the journal.

These are for engine authors and are documented in backends.md. AWS Lambda uses record_durable_unit from its step bridge. Temporal uses is_replaying=workflow.unsafe.is_replaying_history_events.

Verification

  • TestReplayTelemetry (Lambda) covers:

    • a fully replayed run emits no spans or metrics;
    • a partly replayed run emits only live work;
    • an attempt that ran before a suspension is reported;
    • a replayed ModelFallback emits no attempt spans;
    • parallel tools are told apart.

    All of them fail without the fix.

  • test_replay_records_no_model_request_metrics (Temporal) fails without the fix.

  • test_replay_telemetry.py covers the core hooks. _durable_replay.py has 100% branch coverage.

  • Real Bedrock demo on the AWS SDK's in-process runner, traced to Logfire: chat spans equal the real requests, and per-invocation cost rollups sum exactly to the real total.

What changes for existing users

Agents not running under a durable engine are unchanged. On Lambda and Temporal, resumed runs no longer report duplicate model request and tool spans or duplicate token and cost metrics. With LogfirePlugin(replay_safe=False) or plain OTel on Temporal, agent run spans and Temporal's own spans still repeat on replay, as documented.

PR decisions

2026-11-06 · Replay telemetry fixed in core for every engine, Lambda and Temporal first · iter -

  • Decision: Engines install a DurableReplay; instrumentation drops spans and skips model request metrics for work only replayed (Douwe chose the core 'served from journal' option over a Lambda-only tracer or instrumenting inside steps)
  • Why: Lambda re-emitted every replayed step's spans, tokens and cost (flaky demo run: first request's cost counted 3x); Temporal's #7006 fixed spans but metrics still recorded 4x under forced replay. Wrapping InstrumentationSettings.tracer was avoided because changing that attribute's value trips the API compatibility gate
  • Source: https://github.com/pydantic/pydantic-ai/tree/durable-replay-telemetry

2026-11-06 · pre-push review 1 of 3 at 805a1443e825791ca1dc069ad029eee44d2b9d80 · iter -

2026-11-06 · pre-push review 1 of 3 at 805a1443e825791ca1dc069ad029eee44d2b9d80 · iter -

2026-11-06 · pre-push review 2 of 3 at 568c333a97e6f5b1c44e4d9e07146e5234b0f38c · iter -

2026-11-06 · pre-push review 2 of 3 at 568c333a97e6f5b1c44e4d9e07146e5234b0f38c · iter -

2026-11-06 · push 5d8ee0ba89e55ca7d514872041194067676bdaa9 without pre-push review 3 · iter -

Checklist

  • No breaking changes in accordance with the version policy.
  • Any permitted compatibility impact has a label, warning, migration and release note, and an exact API-check waiver if the gate reports it.
  • PR title is fit for the release changelog.

Summary by cubic

Prevents AWS Lambda and Temporal replays from recording telemetry for completed durable work. Replayed model and tool spans and model token and cost metrics are omitted, so cost totals reflect actual requests.

  • Adds replay_scope() and record_durable_unit() to pydantic_ai.durable_exec for engine authors. Each span counts the durable units completed under it in the requesting task's context, so concurrent tool calls are told apart, and a span with only replayed units is never exported.
  • Temporal passes is_replaying=workflow.unsafe.is_replaying_history_events(); Lambda calls record_durable_unit(executed=...) from its step bridge.
  • Telemetry for work that runs during a resumed invocation, including a failed attempt awaiting its retry, is preserved; agents outside durable execution are unchanged.