Spring AI utility library providing retry mechanisms for AI API interactions with comprehensive error handling and exception classification
This guide covers all configuration options and scenarios for Spring AI Retry.
Use pre-configured constants from RetryUtils:
import org.springframework.ai.retry.RetryUtils;
// Production configuration (10 attempts, exponential backoff)
RetryTemplate template = RetryUtils.DEFAULT_RETRY_TEMPLATE;
// Testing configuration (10 attempts, fixed 100ms backoff)
RetryTemplate testTemplate = RetryUtils.SHORT_RETRY_TEMPLATE;
// Error handler (4xx → non-transient, 5xx → transient)
ResponseErrorHandler errorHandler = RetryUtils.DEFAULT_RESPONSE_ERROR_HANDLER;Configure via application.yml or application.properties:
spring:
ai:
retry:
max-attempts: 5
on-client-errors: false
on-http-codes: [429, 503, 504]
exclude-on-http-codes: [401, 403]
backoff:
initial-interval: 1000ms
multiplier: 2
max-interval: 30000msBuild your own configuration:
import org.springframework.ai.retry.TransientAiException;
import org.springframework.retry.support.RetryTemplate;
import org.springframework.web.client.ResourceAccessException;
import java.time.Duration;
RetryTemplate customTemplate = RetryTemplate.builder()
.maxAttempts(5)
.retryOn(TransientAiException.class)
.retryOn(ResourceAccessException.class)
.exponentialBackoff(
Duration.ofMillis(1000), // initial interval
2.0, // multiplier
Duration.ofMillis(30000) // max interval
)
.build();Maximum number of retry attempts.
spring.ai.retry.max-attempts: 5Controls whether 4xx client errors should be retried.
false - 4xx errors are non-transient (not retried)true - 4xx errors are transient (retried)spring.ai.retry.on-client-errors: falseHTTP status codes that should always be retried (highest priority).
spring.ai.retry.on-http-codes: [429, 503, 504]HTTP status codes that should never be retried.
spring.ai.retry.exclude-on-http-codes: [401, 403]Initial delay before first retry.
1000ms, 1s, PT1Sspring.ai.retry.backoff.initial-interval: 1000msMultiplier applied to each subsequent retry delay.
2 - Gentle exponential growth3 - Moderate growth5 - Aggressive growth (default)10 - Very aggressive growthspring.ai.retry.backoff.multiplier: 2Maximum backoff duration (cap for exponential growth).
30000ms, 30s, PT30Sspring.ai.retry.backoff.max-interval: 30000msMinimize retry attempts and costs while handling genuine transient failures.
spring:
ai:
retry:
max-attempts: 3
on-client-errors: false
on-http-codes: [503, 504] # Only retry clear service issues
exclude-on-http-codes: [429] # Don't retry rate limits
backoff:
initial-interval: 500ms
multiplier: 2
max-interval: 5000msUse Case: Cost-sensitive applications, development environments
Behavior:
Maximize success rate with extensive retry attempts and long backoff.
spring:
ai:
retry:
max-attempts: 15
on-client-errors: false
on-http-codes: [429, 503, 504]
exclude-on-http-codes: [401, 403, 404]
backoff:
initial-interval: 5000ms
multiplier: 3
max-interval: 300000ms # 5 minutesUse Case: Critical production systems, high-value requests
Behavior:
Optimize for fast test execution while maintaining retry logic.
spring:
ai:
retry:
max-attempts: 3
on-client-errors: false
on-http-codes: [503]
backoff:
initial-interval: 10ms
multiplier: 2
max-interval: 100msUse Case: Unit tests, integration tests
Behavior:
Handle rate limits gracefully with appropriate backoff.
spring:
ai:
retry:
max-attempts: 10
on-client-errors: false
on-http-codes: [429, 503, 504]
backoff:
initial-interval: 10000ms # 10 seconds
multiplier: 1 # Fixed backoff
max-interval: 10000msUse Case: Applications hitting rate limits frequently
Behavior:
Quick failure to allow failover to alternate regions/providers.
spring:
ai:
retry:
max-attempts: 2
on-client-errors: false
on-http-codes: []
backoff:
initial-interval: 100ms
multiplier: 2
max-interval: 500msUse Case: Multi-provider or multi-region architectures
Behavior:
The auto-configured error handler evaluates HTTP errors in this order:
TransientAiException (retried)NonTransientAiException (not retried)NonTransientAiException (not retried)TransientAiException (retried)spring.ai.retry:
on-client-errors: false
on-http-codes: [429, 503, 504]
exclude-on-http-codes: [401, 403]| HTTP Code | Result | Reason |
|---|---|---|
| 429 | TransientAiException | In onHttpCodes (priority 1) |
| 400 | NonTransientAiException | 4xx + onClientErrors=false (priority 2) |
| 401 | NonTransientAiException | In excludeOnHttpCodes (priority 3) |
| 500 | TransientAiException | Default (priority 4) |
With initial-interval: 2s, multiplier: 5, max-interval: 180s:
| Attempt | Calculation | Delay | Cumulative |
|---|---|---|---|
| 1 | - | 0s | 0s |
| 2 | 2s × 1 | 2s | 2s |
| 3 | 2s × 5 | 10s | 12s |
| 4 | 10s × 5 | 50s | 62s |
| 5 | 50s × 5 = 250s → cap | 180s | 242s |
| 6-10 | capped | 180s each | up to 1142s |
Set multiplier: 1 for fixed delays:
backoff:
initial-interval: 5s
multiplier: 1
max-interval: 5sResults in: 5s, 5s, 5s, 5s... (all attempts same delay)
Override auto-configuration with custom beans:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.retry.support.RetryTemplate;
import org.springframework.web.client.ResponseErrorHandler;
@Configuration
public class CustomRetryConfig {
@Bean
public RetryTemplate retryTemplate() {
// Your custom RetryTemplate takes precedence
return RetryTemplate.builder()
.maxAttempts(3)
.fixedBackoff(1000)
.retryOn(TransientAiException.class)
.build();
}
@Bean
public ResponseErrorHandler responseErrorHandler() {
// Your custom error handler takes precedence
return new CustomAiErrorHandler();
}
}To disable auto-configuration entirely:
spring:
autoconfigure:
exclude:
- org.springframework.ai.retry.autoconfigure.SpringAiRetryAutoConfigurationOr via annotation:
@SpringBootApplication(exclude = {
SpringAiRetryAutoConfiguration.class
})
public class Application {
// ...
}spring:
profiles: dev
ai:
retry:
max-attempts: 2 # Fail fast in dev
backoff:
initial-interval: 100ms
max-interval: 1sspring:
profiles: prod
ai:
retry:
max-attempts: 10
on-http-codes: [429, 503, 504]
backoff:
initial-interval: 2s
multiplier: 5
max-interval: 180sspring:
profiles: test
ai:
retry:
max-attempts: 3
backoff:
initial-interval: 10ms
max-interval: 100ms| Feature | RetryUtils Constants | Auto-Configuration |
|---|---|---|
| Configuration Method | Hardcoded | Property-based |
| Max Attempts | Fixed: 10 | Configurable |
| Backoff | Fixed: 2s → 180s | Fully configurable |
| 4xx Handling | Always non-transient | Configurable |
| HTTP Code Lists | Not supported | Supported |
| WebFlux Support | No | Automatic |
| Bean Override | Manual | Automatic |
| Best For | Simple apps, testing | Production Spring Boot |
Install with Tessl CLI
npx tessl i tessl/maven-org-springframework-ai--spring-ai-retry@1.1.0