Quick revision
The short answer
Spring AI wraps provider calls in a RetryTemplate configured by spring.ai.retry.* properties, retrying errors it classifies as transient (TransientAiException) and failing fast on NonTransientAiException. A 429 deserves a few retries with exponential backoff and jitter, honouring Retry-After, plus client-side rate limiting; a context length error is permanent, so we shrink the input or use a larger-window model instead of retrying; a two-minute hang needs connect and read timeouts on the underlying RestClient or WebClient and an overall deadline. In every case the user gets a clear fallback message and the error type is recorded as a metric.
What to remember
In this answer, we saw that Spring AI retries transient errors through a RetryTemplate configured with spring.ai.retry properties, whose defaults are long and should be tightened for user-facing calls. A 429 needs few retries with backoff, jitter, Retry-After and client-side rate limiting. Context length exceeded is permanent, so we trim the input or use a larger-window model. A hang needs connect and read timeouts on RestClient or WebClient plus an overall deadline. In all cases the user gets a clear message and the error type is recorded.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Spring AI wraps provider calls in a RetryTemplate configured by spring.ai.retry.* properties, retrying errors it classifies as transient (TransientAiException) and failing fast on NonTransientAiException. A 429 deserves a few retries with exponential backoff and jitter, honouring Retry-After, plus client-side rate limiting; a context length error is permanent, so we shrink the input or use a larger-window model instead of retrying; a two-minute hang needs connect and read timeouts on the underlying RestClient or WebClient and an overall deadline. In every case the user gets a clear fallback message and the error type is recorded as a metric.
Why this question is asked
Errors from model providers are not all alike, and treating them alike is how a small problem turns into a long outage or a big bill. Retrying a rate limit too fast makes the limit last longer; retrying a request that is too long wastes money every time; and waiting forever on a hung connection ties up threads until the whole service stops responding. Interviewers ask this question to see whether we know Spring AI's retry behaviour and can match each failure with the right response. In this answer, we will recap how Spring AI retries, then handle the 429, the context length error and the hang one by one, and finish with what the user sees.
1How Spring AI Retries
Most Spring AI provider modules call the provider through a Spring RetryTemplate. When a call fails, Spring AI classifies the error. Transient errors, which might succeed if tried again, such as server errors and, by default, rate limits, are thrown as TransientAiException and retried with exponential backoff. Non-transient errors, such as invalid requests, are thrown as NonTransientAiException and not retried. The retry settings come from properties.
| Error | Kind | Response |
|---|---|---|
| 429 rate limit | transient | few retries, backoff with jitter, Retry-After, client rate limit |
| 5xx or connection reset | transient | few retries with backoff, then fallback |
| Context length exceeded | permanent | shrink input or bigger-window model; no retry |
| Invalid request, bad key | permanent | fail fast, alert |
| Hang, no response | timeout | connect/read timeouts and an overall deadline |
Notice how large the defaults are: ten attempts with waits of 2, 10, 50 seconds and then up to 3 minutes. That suits a batch job but is far too long for a user waiting on a web page. For user-facing calls we lower them, for example three attempts starting at 500 milliseconds. Exact defaults and the treatment of 429 have varied between versions, so we read the reference for our version and set the values explicitly.
2Handling the 429 Rate Limit
HTTP 429 means we sent more requests or tokens than our quota allows in a time window. Retrying is right, because the window will move, but how we retry matters. Exponential backoff spreads the attempts out, and jitter, a random part of each wait, stops many instances from retrying at exactly the same moment and hitting the limit together again. If the provider sends a Retry-After header, it tells us how long to wait, and waiting less is pointless.
Retries treat the symptom. The cause is that we send more than the quota, so we also limit ourselves on the client side: a rate limiter per provider that keeps us under the requests-per-minute and tokens-per-minute quota, queueing or rejecting extra calls before they reach the provider. Then 429s become rare, and retries do not pile on top of an already exceeded limit.
3Handling Context Length Exceeded
The context window is the maximum number of tokens a model accepts in one request, input plus output. If the system prompt, chat history, retrieved chunks and question together exceed it, the provider rejects the request with a 400-type error saying the context length was exceeded. This is not transient. Sending the same request again gives the same error and, with some providers and settings, still costs time. So the error should be classified as non-transient and never retried as is.
The fix is to send less or use a model that accepts more. We trim the chat history to fewer messages or a summary, retrieve fewer or smaller chunks, shorten the system prompt, or summarise a long document in parts before asking about it. Better still, we prevent the error: count tokens before sending, and if the prompt is over a budget, trim it first. A model with a larger window is an option for features that genuinely need long inputs, at a higher price per call.
4Handling the Two-Minute Hang
A hang is the most dangerous of the three, because nothing fails. A request waits for a response that never comes, and the thread serving the user waits with it. If many requests hang at once, the server's thread pool fills and the whole service stops responding, including endpoints that have nothing to do with AI.
So we set timeouts on the HTTP client Spring AI uses underneath. Spring AI builds its provider clients on RestClient for blocking calls and WebClient for streaming, and it picks up builder customisations from the application context. We give the client a connect timeout of a few seconds and a read timeout that fits our longest expected response. On top of that, an overall deadline for the user request, including retries, guarantees an answer within a fixed time.
- Under the client rate limit?YesSend the requestNoWait briefly or return 'busy'
- Prompt within token budget?YesCall the modelNoTrim history and chunks first
- Answer before the read timeout?YesReturn the answerNoIs the error transient?
- Transient and deadline left?429, 5xx, timeoutYesBack off with jitter and retryNoFallback message and metric
4 steps in order. Play them, or step through them one at a time.
A read timeout counts as transient, so Spring AI may retry it. That is why the overall deadline matters: three attempts of 30 seconds each would make the user wait 90 seconds. For user-facing calls we keep attempts low and the deadline tight, for example 20 seconds in total.
5What the User Sees and What We Record
Whatever goes wrong, the user should get a clear message quickly, not a spinner for two minutes or a stack trace. For a rate limit or outage: The assistant is busy right now, please try again in a minute. For a request that is too long: This conversation is too long for me to process, please start a new one or shorten your question. We also record each error type as a metric, tagged by provider and model: ai.errors with type rate_limited, context_too_long, timeout, server_error.
6Follow-up questions
How many retries would you allow on a user-facing request?
Usually one or two retries, three attempts at most, inside an overall deadline of perhaps 10 to 20 seconds. Users will not wait longer, and each retry during a rate limit adds load to a provider that is already refusing us. Batch jobs can afford many more retries with long backoff, because nobody is waiting on a page.
Should streaming calls use the same settings?
They go through WebClient, so set its connect and response timeouts, and remember that retrying a stream after text has been sent would duplicate output; retry only before the first token.
7Conclusion
In this answer, we saw that Spring AI retries transient errors through a RetryTemplate configured with spring.ai.retry properties, whose defaults are long and should be tightened for user-facing calls. A 429 needs few retries with backoff, jitter, Retry-After and client-side rate limiting. Context length exceeded is permanent, so we trim the input or use a larger-window model. A hang needs connect and read timeouts on RestClient or WebClient plus an overall deadline. In all cases the user gets a clear message and the error type is recorded.
Open with
Spring AI calls providers through a RetryTemplate configured by spring.ai.retry properties: max attempts, backoff interval, multiplier and max interval, plus which HTTP codes to retry or exclude. Errors classified as transient become TransientAiException and are retried; non-transient ones become NonTransientAiException and fail fast.
Then, if they want more
The defaults are long, many attempts with minutes of backoff, which is fine for batch but not for a user, so I set two or three attempts and an overall deadline.
For the 429, retrying is right but with exponential backoff and jitter, respecting Retry-After, and I add a client-side rate limiter, for example Resilience4j, so we stay under the quota instead of retrying into it.
Context length exceeded is permanent; retrying the same request is useless, so I count tokens before sending and trim history, use fewer chunks or summarise, or route to a model with a larger window.
For the hang, I set connect and read timeouts on the RestClient or WebClient builder that Spring AI uses, and an overall deadline including retries, because a hang blocks threads and can take the whole service down. In each case the user gets a clear fallback message, and I record error types as metrics by provider and model.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring AI reference
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A Spring AI call fails in production with HTTP 429 from the provider, another with a...": https://scrivio-interview-library.netlify.app/spring-boot/spring-ai/e83ad665cc/