Decision Models
The DecisionModel API is experimental and may change in future releases.
Most applications start with Decision Services, which let you ask questions through
plain Java interfaces returning boolean, enums or records.
Use the DecisionModel API described on this page when the questions or options are only known at runtime,
or to build your own integrations.
A decision model answers typed questions about some input (state), instead of generating text. You give it an input (state), for example a support ticket, and a set of named questions, and it returns one typed answer per question, together with probabilities.
Typical uses are:
- Classification and routing: which team should handle this ticket? Which agent or retriever should handle this query? (see the decision router agentic pattern)
- Gating: is this message spam? Does this answer contain personal data? Does this query need retrieval at all?
- Grading: how urgent is this incident? How frustrated is the customer? How well does this answer address the question? (see the confidence cascade agentic pattern)
- Step-by-step orchestration: which agent would be the most useful to invoke next? Has the goal been reached? (see the decision scheduler agentic pattern)
- Decisions inside an agentic system: a classifier, a grader or a check whose typed answer other agents consume (see decision agents)
Decision models are built for these tasks: they return typed answers with probabilities, so there is no text to parse, and all questions of a request are answered against the same input in a single call.
When to use a decision model
A chat model can also answer such questions, for example through an AI Service
returning a boolean or an enum. Consider a decision model when:
- you need probabilities, for example to escalate uncertain cases to a human or to tune thresholds;
- you ask many questions about the same input, and want them answered in one call;
- the decision is on a hot path (every request, every message) where latency and cost matter.
Use a chat model when the task needs generated text, reasoning over several steps, or tools.
A TextClassifier is another option for classification: it learns the categories from
labeled examples, using an embedding model. Use a decision model when you would rather describe the categories in
words than collect examples, or when you need yes/no or scale answers.
Available implementations are listed here.
The DecisionModel API itself is part of langchain4j-core, which comes with every implementation:
requests and questions are in dev.langchain4j.model.decision.request, answers in
dev.langchain4j.model.decision.response and listeners in dev.langchain4j.model.decision.listener.
Asking questions
A DecisionRequest contains the input (state) and the questions, each registered under a name of your choice:
DecisionModel decisionModel = TypeSafeDecisionModel.builder()
.apiKey(System.getenv("TYPESAFE_API_KEY"))
.modelName("jev-1.13.0")
.build();
DecisionRequest request = DecisionRequest.builder()
.input("Help! My payouts have been failing for 3 days and nobody answers my emails.")
.question("team", ChoiceQuestion.builder()
.text("Which team should handle this ticket?")
.option("billing", "Payments, payouts, invoices, refunds")
.option("support", "Problems using the product")
.option("sales", "Pricing, upgrades, new accounts")
.build())
.question("urgent", YesNoQuestion.from("Does this need attention today?"))
.question("frustration", ScaleQuestion.builder()
.text("How frustrated is the customer?")
.level("Calm")
.level("Frustrated")
.level("Angry")
.build())
.build();
DecisionResponse response = decisionModel.decide(request);
Each question type has a builder, and a shorter factory method for the common case:
YesNoQuestion.from(text), ChoiceQuestion.from(text, options) and ScaleQuestion.from(text, levels).
The response contains one answer per question, under the same name:
ChoiceAnswer team = response.choice("team");
YesNoAnswer urgent = response.yesNo("urgent");
ScaleAnswer frustration = response.scale("frustration");
team.value(); // "billing"
team.probabilities(); // {billing=0.88, support=0.1, sales=0.02}
urgent.probability(); // 0.93
frustration.mean(); // 1.4
frustration.probabilities(); // [0.05, 0.5, 0.45]
The response also carries the name of the model that produced the answers and the token usage:
response.modelName() and response.tokenUsage().
Question types
Yes/no questions
A YesNoQuestion asks a yes/no question and is answered with a YesNoAnswer,
whose probability() is the probability that the answer is "yes", from 0 to 1.
isYes(threshold) turns it into a decision:
if (response.yesNo("urgent").isYes(0.8)) {
notifyOnCallTeam(ticket);
}
Optionally, describe when the answer should be "yes" and when it should be "no":
YesNoQuestion refundRequested = YesNoQuestion.builder()
.text("Does the customer ask for a refund?")
.yesWhen("The customer explicitly asks for their money back")
.noWhen("The customer only asks about a charge")
.build();
The criteria describe what "yes" and "no" mean for this question when the question alone is ambiguous. When only one
of them is set, how it combines with the question is up to the model: some models read a lone yesWhen as the only
reason to answer "yes". To split the cases clearly, set both.
Choice questions
A ChoiceQuestion selects exactly one option out of a named set (at least 2 options).
For options whose name says it all, the name is also used as the description:
ChoiceQuestion sentiment = ChoiceQuestion.from("What is the sentiment?", List.of("positive", "negative", "neutral"));
It is answered with a ChoiceAnswer:
value(): the name of the chosen optionprobabilities(): the probability of each option, keyed by option nameprobabilityOf(option)andmargin(): the probability of one option, and the difference between the two most likely optionsconfidence(): how confident the model is, ornullif the model does not report it (see below)
Scale questions
A ScaleQuestion places the input on an ordered scale.
Levels are added from lowest to highest, and a level's number is its index, starting at 0.
It is answered with a ScaleAnswer:
mean(): the probability-weighted mean of the level indexes, from 0 ton - 1. It can fall between two levels: with the levels "Calm", "Frustrated" and "Angry", a mean of 1.4 means "between frustrated and angry, closer to frustrated".probabilities(): the probability of each level, in the same order as the levelsconfidence(): how confident the model is, ornullif the model does not report it
Describing options and levels
Options, levels and the yesWhen/noWhen criteria are described with text.
A good description says what an option covers, what it does not cover, and can include examples:
ChoiceQuestion team = ChoiceQuestion.builder()
.text("Which team should handle this ticket?")
.option("billing", "Payments, payouts, invoices, refunds. Not for questions about pricing plans. "
+ "Examples: 'I was charged twice', 'Where is my payout?'")
.option("sales", "Pricing, plans, upgrades, new accounts")
.build();
Write questions and descriptions around criteria that can be observed in the input, such as "mentions a charge" or "asks for a refund", rather than around intentions or feelings the model has to guess.
If the input may fit none of the options, add an option for that case, for example
.option("other", "Anything else"): otherwise the model has to choose one of the options that do not fit, and its
probabilities look more confident than they are.
Levels can be described the same way, in addition to their label:
ScaleQuestion severity = ScaleQuestion.builder()
.text("How severe is the incident?")
.level("Minor", "No customer is affected")
.level("Major", "Some customers are affected")
.level("Critical", "No customer can use the product")
.build();
Describing the input (state)
The input is one of:
- text:
input(String); - named fields:
inputFields(Map); - a list of contents, such as text and images:
inputContents(List)(see Images); - a conversation:
inputMessages(List)(see Conversations).
Use named fields to give the model several pieces of information that belong together:
DecisionRequest request = DecisionRequest.builder()
.inputFields(Map.of(
"ticket", "My payouts have been failing for 3 days",
"customer_plan", "enterprise",
"open_tickets", 3))
.question("urgent", YesNoQuestion.from("Does this need attention today?"))
.build();
The values of the fields can be strings, numbers, booleans, nulls, maps and lists, and, directly as the value of a
field, contents such as images (see Images). Other objects are rejected, so that you decide what is sent to
the model provider: convert them to a Map that holds only what the decision needs.
Images
The input can also be a list of contents, such as text and images, if the decision model supports them (for example OpenAI, Google AI Gemini, or Ollama with a vision model such as Clef):
DecisionRequest request = DecisionRequest.builder()
.inputContents(List.of(
TextContent.from("The customer says the package arrived like this."),
ImageContent.from(base64Image,