voltagent icon indicating copy to clipboard operation
voltagent copied to clipboard

[BUG] A retryable model error in a later step re-runs the whole generateText, re-executing tools from earlier steps

Open zennicalles opened this issue 2 weeks ago • 1 comments

Versions: @voltagent/core 2.11.1, ai 6.0.326. Agent maxRetries at its default (3).

Describe the bug VoltAgent passes maxRetries: 0 to the AI SDK generateText and retries itself (executeWithModelFallback). The retried unit is the entire multi-step generateText call, started again from the messages prepared before step 1, not the model call that failed. So if the provider returns a retryable error (529/503/timeout) on step 2, the tool from step 1 runs again. For side-effecting tools (payments, emails, writes) this produces duplicates without any tool failure. The same happens when falling back to the next model in a model: [...] list.

The docs describe maxRetries as "the number of retry attempts for a single model call". For streaming they say that "if a stream fails after output starts … the stream is not restarted". generateText currently behaves differently.

To reproduce

let payments = 0, failuresLeft = 1
const model = new MockLanguageModelV3({ doGenerate: async ({ prompt }) => {
  if (!prompt.some(m => m.role === 'tool'))
    return { content: [{ type: 'tool-call', toolCallId: crypto.randomUUID(), toolName: 'send_payment', input: '{"amount":50}' }], finishReason: { unified: 'tool-calls', raw: 'tool_use' }, usage, warnings: [] }
  if (failuresLeft-- > 0)
    throw new APICallError({ message: 'Overloaded', url: 'https://api.example', requestBodyValues: {}, statusCode: 529, isRetryable: true })
  return { content: [{ type: 'text', text: 'Paid.' }], finishReason: { unified: 'stop', raw: 'stop' }, usage, warnings: [] }
}})
const agent = new Agent({ name: 'a', instructions: 'x', model,
  tools: [createTool({ name: 'send_payment', description: 'pay', parameters: z.object({ amount: z.number() }),
    execute: async () => ({ paymentId: `pay_${++payments}` }) })] })
await agent.generateText('pay 50')
console.log(payments) // 2

Observed: 4 model calls. Call 3 receives the original 2-message prompt again, send_payment executes twice, and the result reports only 2 steps.

Expected: only the failed model request is retried, with the messages already accumulated (step 1's tool call and result). send_payment executes once. This is what the AI SDK's own maxRetries does.

Cause: in Agent.generateText, messages is computed once by prepareExecution and captured by the run closure. executeWithModelFallback retries run, which calls generateText({ messages, …, maxRetries: 0 }) from scratch.

Suggested fix (any of)

  • Retry at the model-call level instead of around the whole loop, e.g. a wrapping LanguageModel / middleware that applies VoltAgent's backoff and fallback to each doGenerate / doStream, so tool steps are never repeated.
  • If a step has already completed, do not retry or fall back. Surface the error, as the streaming path does once output has started.
  • At minimum, document that a retry or fallback restarts the whole multi-step run and re-executes completed tool calls.

Found while testing VoltAgent with Shakedown (a fault-injection test harness for agent tool calls).

zennicalles avatar Nov 14 '26 01:11 zennicalles

I reproduced this locally and prepared a fix that prevents completed tool calls from being replayed on model retry or fallback. I’d like to submit it with regression tests.

Yin123-ybh avatar Nov 15 '26 14:11 Yin123-ybh