Quick revision
The short answer
We add both providers' starters and build one ChatClient per role from the specific ChatModel beans, for example ChatClient.builder(openAiChatModel).build(), disabling the single auto-configured builder with spring.ai.chat.client.enabled=false if it gets in the way. Our own routing service picks the client by task, input size or confidence and records the choice in metrics. For outages, transient errors get a few retries, then the call falls back to the second provider, with a Resilience4j circuit breaker so we stop waiting on a provider that is down; prompts, evaluations and data approval must cover both providers.
What to remember
In this answer, we built separate ChatClients from specific ChatModel beans, disabling the shared auto-configured builder, and wrote a router that picks a client by task, input size or confidence and records the choice. Transient errors get a few retries, then a fallback provider, with a circuit breaker so we stop waiting on a failing provider. Finally, prompts, evaluations and structured output must be verified on both models, and the fallback must be approved for the same data.
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.
We add both providers' starters and build one ChatClient per role from the specific ChatModel beans, for example ChatClient.builder(openAiChatModel).build(), disabling the single auto-configured builder with spring.ai.chat.client.enabled=false if it gets in the way. Our own routing service picks the client by task, input size or confidence and records the choice in metrics. For outages, transient errors get a few retries, then the call falls back to the second provider, with a Resilience4j circuit breaker so we stop waiting on a provider that is down; prompts, evaluations and data approval must cover both providers.
Why this question is asked
Using one model for everything is either too expensive or not good enough. Classification of thousands of messages should cost fractions of a cent, while a complex customer answer deserves the best model we have, and neither should stop working when one provider has an outage. Interviewers ask this question because it combines Spring configuration, routing logic and resilience. In this answer, we will configure several ChatModels and ChatClients, write the router, add retries, fallback and a circuit breaker, and then cover the parts that are not code: prompts, evaluation and data approval.
1Configuring More Than One ChatModel
With one provider, Spring Boot's auto-configuration creates a ChatModel and a ChatClient.Builder, and we inject the builder. With two providers on the classpath, there are two ChatModel beans, and the auto-configured builder does not know which one to use. The clean answer is to switch off the shared builder and build our own clients from the concrete models.
Notice that the fast and strong clients share one provider and differ only in the model option. Recent Spring AI versions may also need a property that selects which chat model auto-configuration is active, such as spring.ai.model.chat, so check your version's reference for running two providers together.
2Routing in Our Own Service
Spring AI gives us the clients; the decision of which to use is business logic, so it lives in our service. We route by task type first, then by input size, and we can escalate based on the cheap model's own answer.
The metric is what keeps routing honest. If 80 percent of answers go to the strong model, the savings are small and the thresholds need another look. Another common rule is confidence: ask the fast model for an answer plus a confidence label, and send low-confidence cases to the strong model.
3Retries, Fallback and a Circuit Breaker
Errors fall into two groups. Transient errors, such as timeouts, HTTP 429 rate limits and 5xx server errors, may succeed if we try again. Permanent errors, such as an invalid request or a context that is too long, will fail again and should not be retried. Spring AI already retries transient errors with its RetryTemplate, configured by spring.ai.retry.* properties; the defaults allow many attempts with long backoff, so for a user-facing call we lower them to two or three attempts.
When the primary still fails, we call the fallback provider. And when the primary keeps failing, we do not want every request to wait through retries before falling back. A circuit breaker solves that: after, say, 50 percent of calls fail within a window, it opens and sends calls straight to the fallback for a while, then lets a few test calls through to see whether the provider has recovered.
- Circuit for the primary open?YesGo straight to the fallbackNoCall the primary
- Primary succeeded within the retries?2 to 3 attempts for transient errorsYesReturn the answerNoIs the error transient?
- Transient errortimeout, 429, 5xxYesCall the fallback providerNoReturn a clear error
- Record route and outcomemetrics per provider
4 steps in order. Play them, or step through them one at a time.
The fallback method must be in a Spring bean called through its proxy, like any annotation-driven feature.
4Prompts, Evaluation and Structured Output per Model
A fallback that answers badly is not much better than an error. Each model reacts differently to the same prompt, so we keep a prompt variant per model where needed and run our evaluation set against both the primary and the fallback. If the service uses entity() for structured output, we verify that the fallback produces valid objects for every type we use, because native JSON schema support and formatting habits differ between providers. The same applies to tool calling.
5Data and Contracts
The fallback provider receives the same customer data as the primary. Before it is configured, it must be approved for that data: the contract, the data processing terms, the region where data is processed, and retention rules. A fallback to a provider in another region can break a data residency promise at the exact moment nobody is watching, during an outage. If no second provider is approved, the fallback can be a smaller self-hosted model or a degraded answer without generation.
6Follow-up questions
How would you test the fallback path regularly?
Make it part of normal operations, not something that only runs in an outage. Integration tests force the primary client to fail, for example with a stubbed HTTP server returning 503, and assert that the fallback answers and the metric is recorded. The evaluation set runs on the fallback model on the same schedule as on the primary. In production, a small steady share of traffic, such as 1 percent, can go to the fallback so we know it works with real requests, and a game day can open the circuit breaker on purpose.
Why not let Spring AI's retry handle the fallback too?
RetryTemplate retries the same ChatModel. Switching to another provider is a different decision that needs routing logic, metrics and data rules, so it belongs in our service or in a resilience library.
Where should the routing rules live?
In configuration with sensible defaults, so thresholds and model names can be changed without a code change, and with the route tagged in metrics for every call.
7Conclusion
In this answer, we built separate ChatClients from specific ChatModel beans, disabling the shared auto-configured builder, and wrote a router that picks a client by task, input size or confidence and records the choice. Transient errors get a few retries, then a fallback provider, with a circuit breaker so we stop waiting on a failing provider. Finally, prompts, evaluations and structured output must be verified on both models, and the fallback must be approved for the same data.
Open with
I would add both providers' starters and build ChatClients myself from the concrete ChatModel beans, for example ChatClient.builder(openAiChatModel) with a small model as the fast client and a large model as the strong client, and a third client on the second provider for fallback.
Then, if they want more
With two models present, I set spring.ai.chat.client.enabled to false so there is no ambiguous auto-configured builder.
Routing is business logic in my own service: classification always uses the fast client, answers go to the strong client when the input is large or the fast model reports low confidence, and every choice is recorded as a metric so I can see the split.
For resilience, I lower Spring AI's retry settings to two or three attempts for transient errors like timeouts, 429 and 5xx, then fall back to the second provider, and I wrap the primary in a Resilience4j circuit breaker so during an outage we fail over in a second instead of waiting through retries. Permanent errors are not retried or masked.
I keep prompts and evaluation sets per model and test structured output and tools on the fallback, and I make sure the fallback provider is approved for the same data and region.
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 service must use a cheap fast model for classification, a stronger model for complex...": https://scrivio-interview-library.netlify.app/spring-boot/spring-ai/aa2fc790d6/