Step 07 - Dynamic Model Selection
New Requirement: Smarter Decisions for High-Value Vehicles
In Step 6, you enhanced the system with multimodal image analysis, giving the workflow visual context alongside textual feedback. The system now makes well-informed disposition decisions, but there's a catch: every decision uses the same LLM model, regardless of how much is at stake.
The Miles of Smiles management team has noticed that disposition proposals for high-value vehicles deserve more careful reasoning. A $50,000 car scrapped by mistake is far more costly than a $3,000 one. They want the system to automatically use a more capable (and more expensive) model when the stakes are high, while keeping costs down for routine decisions on lower-value vehicles.
In this step you'll implement dynamic model selection: the DispositionProposalAgent will automatically switch to an advanced LLM when the vehicle's estimated value exceeds $30,000.
What You'll Learn
In this step, you will:
- Configure multiple named AI models in a single Quarkus application
- Turn
DispositionProposalAgentinto a bean class that injects both models and chooses one of them based on runtime data - Use the
@ChatModelSupplierannotation to dynamically select a model per agent invocation - Understand how
@ModelNamequalifies named model injection in Quarkus LangChain4j
Understanding Dynamic Model Selection
Why Not Use the Same Model for Everything?
Using a single model for all decisions is simple, but it forces a trade-off between cost and quality. A cheaper model handles most routine decisions perfectly well, but may miss nuances on complex, high-stakes cases. A more capable model produces better reasoning, but running it for every request is wasteful when most cars in the fleet are low-to-mid-value vehicles.
Dynamic model selection lets you have both: route high-value decisions to a stronger model and keep everything else on the cost-effective default.
How It Works in LangChain4j
LangChain4j's @ChatModelSupplier annotation marks the method that provides the ChatModel of an agent. This method can take parameters, which are resolved from the AgenticScope, so it can receive the outputs of agents that ran earlier in the workflow. Here it receives carValue, the estimate produced by the PricingAgent, and it is invoked at each invocation of the agent. The model choice can therefore depend on the actual data flowing through the workflow at runtime, not just on static configuration.
In an agent interface the supplier is a static method, whereas in an agent class it is an instance method, which can return one of the models that the class injects. This second form is the one DispositionProposalAgent takes in this step.
graph LR
Call["Agent method called<br/>with carValue"] --> Supplier["@ChatModelSupplier<br/>inspects carValue"]
Supplier -->|"≤ $30k"| Base["gpt-4.1<br/>(default model)"]
Supplier -->|"> $30k"| Advanced["gpt-5.1<br/>(advanced model)"]
Base --> LLM["LLM processes<br/>disposition proposal"]
Advanced --> LLM
Prerequisites
Before starting:
- Completed Step 06 — This step builds on Step 6's architecture
- Application from Step 06 is stopped (Ctrl+C)
- Understanding of Step 5's disposition workflow (DispositionProposalAgent, HumanApprovalAgent)
Configure the Named Models
The first change is in application.properties. In previous steps, the application had a single unnamed model configuration that every agent shared. Now we configure two models: a default model used by all agents unless overridden, and a named advancedModel used selectively for high-value decisions.
Open src/main/resources/application.properties and update the AI model configuration:
application.properties
# Default AI model configuration
quarkus.langchain4j.chat-model.provider=openai
quarkus.langchain4j.openai.api-key=${OPENAI_API_KEY}
quarkus.langchain4j.openai.chat-model.model-name=gpt-4.1
quarkus.langchain4j.openai.chat-model.temperature=0
quarkus.langchain4j.openai.timeout=PT180S
# Advanced model for high-value vehicle disposition (>$30,000)
quarkus.langchain4j.advancedModel.chat-model.provider=openai
quarkus.langchain4j.openai.advancedModel.api-key=${OPENAI_API_KEY}
quarkus.langchain4j.openai.advancedModel.chat-model.model-name=gpt-5.1
quarkus.langchain4j.openai.advancedModel.chat-model.temperature=0
quarkus.langchain4j.openai.advancedModel.timeout=PT180S
The default (unnamed) model uses gpt-4.1 — a cost-effective model that handles routine disposition decisions well. The named advancedModel uses gpt-5.1, which provides stronger reasoning for complex cases. The naming convention follows the Quarkus LangChain4j pattern: quarkus.langchain4j.openai.<modelName>.* defines a model you can later inject with @ModelName("<modelName>").
All other agents in the application (FeedbackAnalysisAgent, PricingAgent, FleetSupervisorAgent, etc.) continue to use the default model automatically — no changes needed anywhere else.
Update the DispositionProposalAgent
The DispositionProposalAgent is the agent that creates disposition proposals — SCRAP, SELL, DONATE, or KEEP — based on the vehicle's value, condition, age, and damage. It is the natural place for dynamic model selection because the quality of its reasoning directly determines whether a valuable car is handled correctly.
To choose between the two models, the agent first needs a reference to both of them. An interface has no fields to inject them into, so DispositionProposalAgent becomes a bean class, in the same way as FleetSupervisorAgent in Step 05, and gains a supplier method that returns one of the injected models.
Open src/main/java/com/carmanagement/agentic/agents/DispositionProposalAgent.java and update the highlighted lines:
DispositionProposalAgent.java
package com.carmanagement.agentic.agents;
import dev.langchain4j.agentic.Agent;
import dev.langchain4j.agentic.declarative.ChatModelSupplier;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.service.SystemMessage;
import dev.langchain4j.service.UserMessage;
import io.quarkiverse.langchain4j.ModelName;
import io.quarkus.logging.Log;
import jakarta.inject.Inject;
/**
* Agent that creates disposition proposals for vehicles requiring disposition.
* This agent analyzes the vehicle and creates a proposal that will be reviewed
* by the HumanApprovalAgent if the vehicle value exceeds the threshold.
*/
public class DispositionProposalAgent {
private static final int HIGH_VALUE_THRESHOLD = 30000;
@Inject
ChatModel baseModel;
@Inject
@ModelName("advancedModel")
ChatModel advancedModel;
@SystemMessage("""
You are a car disposition specialist for a car rental company.
Your job is to create a disposition proposal based on the car's value, condition, age, and damage.
Disposition Options:
- SCRAP: Car is beyond economical repair or has severe safety concerns
- SELL: Car has value but is aging out of the fleet or has moderate damage
- DONATE: Car has minimal value but could serve a charitable purpose
- KEEP: Car is worth keeping in the fleet
Decision Criteria:
- If estimated repair cost > 50% of car value: Consider SCRAP or SELL
- If car is over 5 years old with significant damage: SCRAP
- If car is 3-5 years old in fair condition: SELL
- If car has low value (<$5,000) but functional: DONATE
- If car is valuable and damage is minor: KEEP
Your response must include:
1. Proposed Action with unique marker: __SCRAP__ or __SELL__ or __DONATE__ or __KEEP__
2. Reasoning: Clear explanation of your recommendation
Format your response as:
Proposed Action: __[SCRAP/SELL/DONATE/KEEP]__
Reasoning: [Your detailed explanation]
CRITICAL: Use double underscores around the action (e.g., __KEEP__ not KEEP)
""")
@UserMessage("""
Create a disposition proposal for this vehicle:
- Make: {carMake}
- Model: {carModel}
- Year: {carYear}
- Car Number: {carNumber}
- Current Condition: {carCondition}
- Estimated Value: {carValue}
- Damage/Feedback: {feedback}
Provide your disposition proposal with clear reasoning.
""")
@Agent(outputKey = "dispositionProposal", description = "Creates disposition proposals for vehicles requiring disposition")
public native String createDispositionProposal(
String carMake,
String carModel,
Integer carYear,
Integer carNumber,
String carCondition,
String carValue,
String feedback);
@ChatModelSupplier
ChatModel chatModel(String carValue) {
if (parseValue(carValue) > HIGH_VALUE_THRESHOLD) {
Log.info("Selecting advanced model for high-value car estimated " + carValue);
return advancedModel;
}
return baseModel;
}
private static int parseValue(String carValue) {
if (carValue == null) {
return 0;
}
String digits = carValue.replaceAll("[^0-9]", "");
if (digits.isEmpty()) {
return 0;
}
return Integer.parseInt(digits);
}
}
The agent is now declared as a class and createDispositionProposal is public native, while its prompts and its @Agent annotation are unchanged. The other highlighted lines select the model:
- The two
ChatModelfields are ordinary injections. The field without a qualifier receives the default model (gpt-4.1), and@ModelName("advancedModel")selects the named configuration (gpt-5.1) for the second one. Injecting a model does not make the agent use it, since the model of this agent is the one that its supplier returns. chatModel()is the@ChatModelSuppliermethod. It is invoked at each invocation ofcreateDispositionProposaland, as an instance method, it can return either of the two injected fields.- The
carValueparameter of the supplier is resolved from the AgenticScope, so it receives the estimate that thePricingAgentwrote there under itscarValueoutput key earlier in the workflow. parseValue()extracts the digits from the estimate, which arrives formatted as something like"$42,500", and the supplier compares the result againstHIGH_VALUE_THRESHOLD. A value above $30,000 selects the advanced model and writes a line to the log, while everything else stays on the default.
A $10,000 car therefore goes to gpt-4.1 and a $45,000 car to gpt-5.1, with the same system message, user message template and output key in both cases.
Try It Out
Start the Application
- Navigate to the step-07 directory:
cd section-2/step-07
- Start the application:
Linux / macOS
./mvnw quarkus:dev
Windows
mvnw quarkus:dev
Test with a Standard-Value Vehicle
Find the Honda Civic in the Fleet Status grid and enter feedback describing severe damage:
The car was involved in a severe collision, with heavy front-end damage, rear bumper damage, and possible frame damage. It may not be safe to drive.
Click Return.
This feedback is intentionally severe so the disposition analysis is activated. The Honda Civic should still be below the $30,000 advanced-model threshold, so the DispositionProposalAgent will use the default gpt-4.1 model. Check the application logs. You should see the disposition proposal generated without the advanced model being invoked.
Test with a High-Value Vehicle
Now try a scenario where the PricingAgent estimates a value above $30,000. Find the Mercedes-Benz C-Class in the Fleet Status grid and enter feedback describing severe damage:
The car was involved in a severe multi-car rear-end collision, with heavy damage to both the front and rear bumpers and possible structural damage.
Click Return.
This time, when the PricingAgent returns a high estimated value (above $30,000), the DispositionProposalAgent will automatically switch to the gpt-5.1 model. The more capable model provides stronger reasoning for the disposition decision on this valuable vehicle. In this case the agent also logs its choice, in a line whose message ends with the value estimated by the PricingAgent:
Selecting advanced model for high-value car estimated <carValue>
Verify the Model Switch
Enable request logging to see which model is being used:
quarkus.langchain4j.log-requests=true
In the logs, look for the model name in the request payload. You'll see gpt-4.1 for low-value vehicles and gpt-5.1 for high-value ones.
How It All Works Together
sequenceDiagram
participant Supervisor as FleetSupervisorAgent
participant Pricing as PricingAgent
participant Proposal as DispositionProposalAgent
participant LLM as LLM
Supervisor->>Pricing: Estimate vehicle value
Pricing-->>Supervisor: carValue = "$45,000"
Supervisor->>Proposal: createDispositionProposal(..., carValue)
rect rgb(212, 237, 218)
Note over Proposal: Model Selection
Proposal->>Proposal: @ChatModelSupplier chatModel("$45,000")
Note over Proposal: returns the injected<br/>advancedModel (gpt-5.1)
end
Proposal->>LLM: Generate proposal using gpt-5.1
LLM-->>Proposal: Detailed disposition proposal
Proposal-->>Supervisor: dispositionProposal
Key Takeaways
- Named models in
application.propertieslet you configure multiple LLM backends in the same application, each with its own model, temperature, and timeout settings @ModelNamequalifies which named model to inject into a CDI bean@ChatModelSuppliermarks the method that provides theChatModelof an agent. In an agent class it is an instance method- An agent declared as a bean class injects the models it chooses from. An injected
ChatModelfield never selects the model of the agent by itself, since this is what the supplier's return value does - Supplier parameters are resolved from the AgenticScope, so the supplier, invoked at each invocation of the agent, can read the outputs of earlier agents and select the model from them without extra plumbing
- The model selection sits in a single method of the agent class, and the agent's prompts and output handling don't change at all
Experiment Further
1. Adjust the Threshold
Change the HIGH_VALUE_THRESHOLD in DispositionProposalAgent to a lower value (e.g., 10000) and observe how more vehicles get routed to the advanced model. Watch the logs to confirm the switch.
2. Add a Third Model Tier
Configure a third named model, for example premiumModel, for vehicles above $100,000, replacing the placeholder with a model of your choice:
quarkus.langchain4j.premiumModel.chat-model.provider=openai
quarkus.langchain4j.openai.premiumModel.chat-model.model-name=<your-premium-model>
quarkus.langchain4j.openai.premiumModel.api-key=${OPENAI_API_KEY}
quarkus.langchain4j.openai.premiumModel.timeout=PT180S
Then inject it into DispositionProposalAgent with @ModelName("premiumModel") and extend the supplier to a three-tier selection.
3. Apply Dynamic Selection to Other Agents
Try adding @ChatModelSupplier to other agents in the system. For example, the FleetSupervisorAgent could use a more capable model when the overall disposition involves multiple damaged vehicles.
4. Make the Threshold Configurable
Replace the hardcoded HIGH_VALUE_THRESHOLD with a Quarkus configuration property using @ConfigProperty, as FleetSupervisorAgent does for its approval threshold since Step 05, so it can be tuned without recompilation:
@ConfigProperty(name = "car-management.advanced-model.threshold", defaultValue = "30000")
int highValueThreshold;
Troubleshooting
Bean resolution error for advancedModel
If you see UnsatisfiedResolutionException for the @ModelName("advancedModel") injection, verify that the named model is fully configured in application.properties. The name in @ModelName("advancedModel") must match the segment in quarkus.langchain4j.openai.advancedModel.* exactly.
All requests using the same model
If you see the same model in all requests regardless of car value, check that:
- The
carValueparameter reaching the agent is correctly formatted (e.g.,"$42,500") - The
parseValuemethod can extract digits from the format used by your PricingAgent - The threshold in
DispositionProposalAgentmatches your test data
ChatModelSupplier method not being called
In an agent class the @ChatModelSupplier method is an instance method, which may be package-private but not private or final. Verify that it returns ChatModel and that the agentic method carries no @ModelName annotation, because @ModelName on the agentic method and a @ChatModelSupplier cannot be used on the same agent. The ChatModel fields injected into the class never select the model by themselves, so the agent only switches when the supplier returns the advanced one.
Cleanup
Before moving to the next step:
-
Stop the running server by pressing
Ctrl+Cin the terminal where Quarkus is running -
Return to the root project directory:
cd ..
What's Next?
You've successfully implemented dynamic model selection, giving the system cost-effective reasoning for routine decisions and more capable analysis for high-value vehicles.
The system now:
- Configures multiple named AI models in a single application
- Automatically selects the appropriate model based on the vehicle's estimated value
- Uses a stronger model for high-value disposition decisions without changing agent prompts or workflow structure
Key Progression:
- Step 4: Sophisticated local orchestration with Supervisor Pattern
- Step 5: Human-in-the-Loop for safe, controlled autonomous decisions
- Step 6: Multimodal image analysis for enriched feedback
- Step 7: Dynamic model selection for cost-effective, risk-aware decisions
In Step 08, you'll learn about Agent-to-Agent (A2A) communication — converting the local PricingAgent into a remote service that runs in a separate system, demonstrating how to distribute agent workloads across multiple applications!