WorkflowAgent: no caller hook to stop a retry of a failure that cannot succeed (follow-up to #22379 / #22395)
Follow-up to #22379, split out because #22395 leaves it out of scope ("a caller veto requires broader timeout and retry-policy design").
What changes when #22395 lands. Today a model stream that fails after it opens is never retried, so a failure that can never succeed costs one call. With #22395 every failure the provider marks isRetryable is retried up to maxRetries with in-step backoff, and the caller has no say. @Adam13y's known-answer matrix on #22379 measured it (scenario S4: a content-filter refusal delivered as a retryable 500 on every attempt): main 1 call, 1 ms; #22395 3 calls, about 6 s, then AI_RetryError. onError fires once, after the retries, and its return value is ignored.
Why status-based isRetryable is not enough. Through a gateway, some deterministic failures arrive as 5xx: content-moderation refusals, a withdrawn model id, provider "invalid request" errors wrapped as 500. Retrying the identical prompt only adds dead air before the same error. In our production, before we had our own veto, two incidents had 7 and 11 runs a day each spending about 6 minutes retrying failures of this kind.
Ask. A caller-side decision that runs before each retry, for example onError returning { retry: false } (as streamText allows) or a shouldRetry(error, attempt) option. Because callbacks cannot cross the durable step boundary, the decision would need to run where modelCallIterator runs, or be expressible as data the step can evaluate (for example a list of error codes or message patterns that are never retried).
Until that exists, a note in the #22395 changeset that maxRetries now also governs post-open stream failures would let callers lower it deliberately.
Identify issue type
Identified as Feature
Confidence: High
Rationale
The primary request is a new caller-controlled retry decision hook for WorkflowAgent, which is an API and behavior enhancement rather than a failure of currently promised behavior.
Evidence
- WorkflowAgent retries provider-marked retryable failures up to
maxRetrieswithout allowing callers to veto individual retries. - The requested
shouldRetry(error, attempt)option or retry-controllingonErrorreturn value would add new retry-policy capability. - The issue acknowledges that this caller veto is outside the scope of #22395 and requests a documentation note only as an interim measure.
Feature Analysis
Feature is not available.
Feature Description
- Allow callers to inspect each otherwise-retryable model failure and veto the next attempt, while retaining maxRetries as the overall retry limit.
- On current main, doStreamStep applies maxRetries to dispatch failures through prepareRetries, while post-open stream errors are retained as terminal results rather than retried.
- WorkflowAgentOnErrorCallback returns only void and is invoked after retry processing or terminal stream completion; no shouldRetry callback or serializable never-retry policy is exported.
- Focused do-stream-step and workflow-agent-stream-error tests passed, confirming maxRetries-driven dispatch retries and one final onError notification without a per-attempt decision hook.
- The pending post-open retry implementation referenced by the issue also keeps retry decisions inside the durable model step and adds no caller veto.
AI SDK Fit
- A caller-controlled veto complements maxRetries and the SDK's normalized retry metadata by letting applications correct provider or gateway classifications without provider-specific forks.
- The capability belongs primarily in @ai-sdk/workflow because its central design problem is making retry policy compatible with durable step boundaries and replay.
- A separate shouldRetry-style policy is safer than changing onError because WorkflowAgent's existing onError contract is a final observer, and streamText currently supports only the positive { retry: true } request rather than a typed { retry: false } automatic-retry veto.
- The callback and serializable rule-list examples are alternative API designs for the same user goal, while the requested release-note warning is a temporary mitigation rather than an independently valuable feature.
Implications / Considerations
- The main implementation would affect packages/workflow, including WorkflowAgent option types and merging, modelCallIterator, ModelCallOptions, doStreamStep, and likely doGenerateStep so equivalent model-call operations do not have surprising retry-policy differences.
- A callback cannot simply be passed into a durable step; supporting arbitrary shouldRetry logic likely requires exposing each failed attempt to modelCallIterator and coordinating backoff and another step invocation, while a serializable code, status, or message policy could remain inside the existing model step.
- Moving retry decisions outside doStreamStep must preserve deterministic replay, absolute deadlines, Retry-After handling, abort behavior, RetryError aggregation, telemetry attempt counts, writer reset-step behavior, and the rule that workflow-runtime retries do not stack with model retries.
- The change is provider-independent and should not require provider-by-provider integrations or changes to @ai-sdk/provider because APICallError, GatewayError, and StreamProviderError already expose retry metadata; reliable provider code, type, status, and data propagation remains an external constraint.
- The public ai package need not gain a new API, although its internal prepareRetries utility may need a veto-capable predicate if shared; @ai-sdk/workflow would receive an additive public option and exported callback or policy type.
- Changing onError to run before every retry would be behaviorally risky for existing logging callbacks, so a new callback or explicitly scoped retry-policy option is preferable for backward compatibility.
- AI SDK UI message types and framework integrations should not require changes, but retries after partial output must continue resetting failed-attempt UI state, and vetoed attempts must preserve the terminal error without duplicating chunks or tool execution.
- Tests should cover pre-open and post-open errors, callback attempt numbering, veto and default behavior, maxRetries zero and exhaustion, Retry-After, timeout and abort, callback failures, replay, telemetry, partial output, provider-executed tool side effects, and final onError and RetryError semantics.
- Examples and WorkflowAgent documentation should demonstrate selective retry suppression and warn that provider-executed work can repeat; when post-open retries ship, release documentation should explicitly state that maxRetries governs them so callers can lower or disable retries.
- Error objects and provider payloads may contain sensitive request or response data and are recorded by durable workflows, so a callback contract should expose only necessary fields or clearly document persistence and privacy implications; message-pattern policies should avoid unsafe regular-expression evaluation.
- A material design uncertainty is whether arbitrary caller code can be made replay-safe without changing retry-step structure; if not, a constrained serializable policy may be the only durable implementation.
Relevant Documentation
Thanks for carrying the S4 row over. Whichever shape lands (a shouldRetry(error, attempt) callback or a serializable never retry list), I can turn the matrix into regression tests for it with three known answer rows:
- S4 with the veto: moderation refusal as a retryable 500, vetoed, so 1 call, no backoff, original
APICallErrorpreserved. - S2 control: a transient 503 that the veto does not match, so it still retries and succeeds on attempt 2.
- Cap still holds: a persistent 503 the veto never matches, so
maxRetries: 2still stops at 3 calls and the last error surfaces.
Happy to open them as a PR once a maintainer picks the API.