Spring AI utility library providing retry mechanisms for AI API interactions with comprehensive error handling and exception classification
Spring AI Retry provides comprehensive retry mechanisms specifically designed for AI API interactions within the Spring AI framework. It offers pre-configured retry templates with exponential backoff strategies for handling transient failures when communicating with AI model providers, custom exception hierarchies for classifying retryable and non-retryable errors, and a specialized ResponseErrorHandler that automatically categorizes HTTP errors from AI services.
Installation:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-retry</artifactId>
<version>1.1.2</version>
</dependency>For Spring Boot with auto-configuration:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-autoconfigure-retry</artifactId>
<version>1.1.2</version>
</dependency>import org.springframework.ai.retry.RetryUtils;
import org.springframework.retry.support.RetryTemplate;
import org.springframework.web.client.RestTemplate;
// Configure RestTemplate with error handler
RestTemplate restTemplate = new RestTemplate();
restTemplate.setErrorHandler(RetryUtils.DEFAULT_RESPONSE_ERROR_HANDLER);
// Use retry template for API calls
RetryTemplate retryTemplate = RetryUtils.DEFAULT_RETRY_TEMPLATE;
String response = retryTemplate.execute(context -> {
return restTemplate.postForEntity(aiApiUrl, request, String.class);
});spring:
ai:
retry:
max-attempts: 5
on-http-codes: [429, 503, 504]
backoff:
initial-interval: 1s
multiplier: 2
max-interval: 30s@Service
public class AiService {
@Autowired
private RetryTemplate retryTemplate;
@Autowired
private ResponseErrorHandler responseErrorHandler;
public String callAi(String prompt) {
return retryTemplate.execute(context ->
aiClient.generate(prompt)
);
}
}Utility class providing pre-configured retry templates and error handlers.
Static Members:
DEFAULT_RETRY_TEMPLATE - Production retry template (10 attempts, exponential backoff: 2s → 180s)SHORT_RETRY_TEMPLATE - Testing retry template (10 attempts, fixed 100ms backoff)DEFAULT_RESPONSE_ERROR_HANDLER - HTTP error handler (4xx → non-transient, 5xx → transient)TransientAiException: Thrown for temporary failures that may succeed on retry (5xx errors, network issues, rate limits). Automatically retried by RetryTemplate.
NonTransientAiException: Thrown for permanent failures that require intervention (4xx errors, auth failures, invalid requests). Not retried.
SpringAiRetryAutoConfiguration: Automatically creates RetryTemplate and ResponseErrorHandler beans for Spring Boot applications, configurable via spring.ai.retry.* properties.
| HTTP Status | Exception Type | Retry Behavior |
|---|---|---|
| 4xx (Client Errors) | NonTransientAiException | Not retried |
| 5xx (Server Errors) | TransientAiException | Retried with backoff |
| Network errors | TransientAiException | Retried with backoff |
| Attempt | Delay | Cumulative Time |
|---|---|---|
| 1 | 0s | 0s |
| 2 | 2s | 2s |
| 3 | 10s | 12s |
| 4 | 50s | 62s |
| 5-10 | 180s each | up to 19 minutes |
| Property | Default | Description |
|---|---|---|
spring.ai.retry.max-attempts | 10 | Maximum retry attempts |
spring.ai.retry.on-client-errors | false | Whether to retry 4xx errors |
spring.ai.retry.on-http-codes | [] | HTTP codes to always retry |
spring.ai.retry.exclude-on-http-codes | [] | HTTP codes to never retry |
spring.ai.retry.backoff.initial-interval | 2000ms | Initial delay |
spring.ai.retry.backoff.multiplier | 5 | Backoff multiplier |
spring.ai.retry.backoff.max-interval | 180000ms | Maximum delay |
RetryTemplate template = RetryUtils.DEFAULT_RETRY_TEMPLATE;
String result = template.execute(context -> aiClient.generate(prompt));String result = retryTemplate.execute(
context -> aiClient.generate(prompt),
recoveryContext -> cachedResponseService.getLastKnownGood(prompt)
);spring:
ai:
retry:
max-attempts: 3
on-http-codes: [503, 504]
backoff:
initial-interval: 500ms
multiplier: 2
max-interval: 5sTransient: Temporary failures that may succeed on retry without changes (network issues, service outages, rate limits). The retry template automatically retries these.
Non-Transient: Permanent failures requiring code or configuration changes (invalid API key, malformed request, insufficient permissions). These fail immediately without retry.
Retry delays increase exponentially to avoid overwhelming failing services:
The ResponseErrorHandler automatically classifies HTTP errors:
All components are thread-safe and can be shared across threads:
RetryUtils.DEFAULT_RETRY_TEMPLATE - Thread-safeRetryUtils.SHORT_RETRY_TEMPLATE - Thread-safeRetryUtils.DEFAULT_RESPONSE_ERROR_HANDLER - Stateless and thread-safeEach execute() call creates a new RetryContext, so retry state is isolated per execution.
Required:
Optional:
Install with Tessl CLI
npx tessl i tessl/maven-org-springframework-ai--spring-ai-retry