alibaba/spring-ai-alibaba · error

Model call failed (attempted {}/{}): {}

Error message

Model call failed (attempted {}/{}): {}

What it means

ModelRetryInterceptor.interceptModel logs 'Model call failed (attempted N/max)' whenever the ChatModel call throws, records lastException, and either schedules a retry with backoff or — when attempt >= maxAttempts — throws RuntimeException('Model call failed, maximum number of retries reached.'). This is the per-attempt failure record for the retry loop.

Source

Thrown at spring-ai-alibaba-agent-framework/src/main/java/com/alibaba/cloud/ai/graph/agent/interceptor/modelretry/ModelRetryInterceptor.java:130

						continue;
					} else if (attempt >= maxAttempts) {
						log.error("The maximum number of retries has been reached {}, and the model call has failed.", maxAttempts);
						throw new RuntimeException("Model call failed, maximum number of retries reached:" + exceptionText);
					}

					// For non-retryable exceptions, return immediately.
					return modelResponse;
				}

				// Successful response
				if (attempt > 1) {
					log.info("The model call succeeded after the {}th attempt.", attempt);
				}
				return modelResponse;

			} catch (Exception e) {
				lastException = e;
				log.warn("Model call failed (attempted {}/{}): {}", attempt, maxAttempts, e.getMessage());

				if (attempt >= maxAttempts) {
					log.error("The maximum number of retries has been reached {}, and the model call has failed.", maxAttempts);
					throw new RuntimeException("Model call failed, maximum number of retries reached.", lastException);
				}

				// Determine if an exception can be retried.
				if (!retryableExceptionPredicate.test(e)) {
					log.warn("Exceptions cannot be retried and are thrown immediately: {}", e.getMessage());
					throw new RuntimeException("Model call failed (non-retryable exception)", e);
				}

				// Wait and try again
				if (currentDelay > 0) {
					try {
						log.info("Retry after {} ms", currentDelay);
						Thread.sleep(currentDelay);
					} catch (InterruptedException ie) {

View on GitHub (pinned to f82da0b50f)

Solutions

  1. Read the logged per-attempt message to classify the error before changing retry settings
  2. Fix permanent errors (credentials, oversized prompt) directly — retries cannot help
  3. Increase maxAttempts/backoff only for genuinely transient errors (timeouts, 429s)
  4. Catch the final RuntimeException at the call site, or wrap the model in ModelFallbackInterceptor for a last-resort model switch
  5. Verify retryablePredicate excludes non-retryable errors to fail fast

Example fix

// before
ModelRetryInterceptor.of(model); // throws after max retries, unhandled
// after
ModelRetryInterceptor retry = ModelRetryInterceptor.builder()
    .chatModel(model).maxAttempts(3)
    .initialBackoff(Duration.ofMillis(500)).build();
try {
    return retry.interceptModel(request, chain);
} catch (RuntimeException e) {
    return fallbackResponse(request); // graceful degradation
}
Defensive patterns

Strategy: retry

Validate before calling

if (promptTokensEstimate(messages) > modelContextLimit) { throw new IllegalArgumentException("Prompt too large for model; retrying cannot succeed"); }

Try / catch

try { return interceptor.interceptModel(request, chain); }
catch (RuntimeException e) {
    if (e.getCause() instanceof AuthException) throw e; // don't mask permanent failures
    return fallbackChain.interceptModel(request, chain);
}

Prevention

When it happens

Trigger: modelResponse = model.call(...) throws on any attempt: network errors, timeouts, 4xx/5xx from the provider, auth failures — retried until maxAttempts is consumed.

Common situations: Sustained provider outage (retries exhausted); persistent 401 from a bad API key (retrying is pointless); context length errors that will fail every attempt; maxAttempts set too low for a flaky network.

Understand the failure class

Background: "API request failed": what wrapped HTTP errors from external APIs mean and how to find the real cause — this error's family across 29 libraries.

Related errors


AI-assisted analysis of alibaba/spring-ai-alibaba@f82da0b50f (2026-09-09). Data as JSON: /api/errors/04d40b10a192d804. Report an issue: GitHub.