Quick revision
The short answer
The common patterns are a sequential pipeline, where each agent's output is the next one's input; a router, where a classifier step picks which specialised agent handles a request; orchestrator-workers, where one agent splits a task and others handle the parts; and evaluator-optimizer, where one agent produces and another critiques in a loop with a limit. In Java, a router can be a ChatClient call that returns a structured enum through entity output, followed by an ordinary switch to the right service, and in general plain code should own the control flow while models make only the decisions that need them.
What to remember
In this answer, we went through four ways to combine agents: a sequential pipeline for known steps, a router that picks a specialist, orchestrator-workers for tasks that split differently each time, and evaluator-optimizer for critique loops with a limit. We built a router in Java with a ChatClient call returning a structured enum and a switch, and saw that plain code should own the control flow while models make only the decisions that need them.
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.
The common patterns are a sequential pipeline, where each agent's output is the next one's input; a router, where a classifier step picks which specialised agent handles a request; orchestrator-workers, where one agent splits a task and others handle the parts; and evaluator-optimizer, where one agent produces and another critiques in a loop with a limit. In Java, a router can be a ChatClient call that returns a structured enum through entity output, followed by an ordinary switch to the right service, and in general plain code should own the control flow while models make only the decisions that need them.
Why this question is asked
Once a single prompt is not enough, teams start combining several LLM calls, often called agents, each with its own instructions. A few patterns cover most real systems, and knowing them helps us pick a simple, predictable design instead of a tangle of agents talking freely. Interviewers ask this to check that we know the vocabulary and can implement it with ordinary Java. In this answer, we will recap what an agent is here, go through the four patterns with an example each, implement a router with Spring AI, and see why code should own the control flow.
1A Quick Recap: What an Agent Means Here
In this context an agent is an LLM call, or a small tool-calling loop, with its own instructions and possibly its own tools and model, focused on one job: a billing agent, a technical support agent, a writer, a reviewer. Combining agents means deciding which one runs when, and what each receives from the others. That decision can be made by fixed code, by a model, or by a mix.
2Sequential Pipeline and Router
In a sequential pipeline, each agent's output becomes the next agent's input, in a fixed order: for example, extract the facts from a contract, then draft a summary from the facts, then translate the summary. It is simple and predictable, easy to test step by step, and the right choice whenever the order of work is known.
A router adds one decision at the start. A classifier step, which can be a model or plain rules, looks at the request and picks which specialised agent handles it. A support assistant might route billing questions to a billing agent with invoice tools, technical questions to an agent with documentation search, and anything else to a general agent or a person. Each specialist gets short, focused instructions and only the tools it needs, which usually gives better answers than one agent with every tool.
3Orchestrator-Workers and Evaluator-Optimizer
In orchestrator-workers, one agent, the orchestrator, looks at a task and splits it into parts it could not know in advance, then hands the parts to worker agents and combines their results. For a question such as 'compare our three biggest customers' contract terms', the orchestrator decides which contracts to look at and assigns one worker per contract.
In evaluator-optimizer, one agent produces a draft and another evaluates it against criteria and sends feedback, and the producer revises. This loop can improve quality, for example for a translation or generated code, but it must have a limit, such as three rounds, because the two could otherwise disagree forever.
| Pattern | Who decides the next step | Good for | Main risk |
|---|---|---|---|
| Sequential pipeline | fixed code | known steps in order | rigid when cases vary |
| Router | classifier, then code | different request types | misrouting |
| Orchestrator-workers | orchestrator model | tasks split differently each time | cost, hard to debug |
| Evaluator-optimizer | evaluator model, round limit | quality through critique | endless loops, cost |
4Implementing a Router in Java With Spring AI
The router is the easiest pattern to build well. We ask the model for a structured answer, here a record holding an enum, so we never parse free text. Spring AI's entity() converts the model's JSON output into the Java type and adds format instructions to the prompt for us.
- Question arrives
- Does a rule match, such as an invoice number?YesRoute by ruleNoAsk the model for a RouteDecision
- switch on the routeBILLING, TECHNICAL or OTHER
- Specialist agent answersown prompt and tools
4 steps in order. Play them, or step through them one at a time.
Notice the division of work. The model does the one thing that needs language understanding: deciding what kind of question this is. Everything else, which agent runs, with which tools, what happens on failure, is ordinary Java that we can read, test and debug. If the model returns something that does not parse, entity() throws, and we can fall back to the general agent. Check your Spring AI version for structured output details; a cheap, fast model is usually enough for routing.
A rules step can sit in front of the model too: if the question contains an invoice number pattern, route to billing without a model call at all.
5Why Plain Code Should Own the Control Flow
It is tempting to let agents decide everything, passing messages among themselves until one says it is finished. That design is hard to predict, hard to test and can run up large costs. The more reliable approach is to write the control flow in code, sequences, switches, loops with limits, and use model calls only for the decisions that need judgement, such as classification, extraction, drafting and critique. Code is deterministic, cheap and testable; models are flexible but probabilistic. Combining them in that way gives us the benefits of both.
6Follow-up questions
How do you test a router?
Build a labelled set of a few hundred real questions with the correct route, run the router on it, and measure accuracy per route. Look at the confusion between routes, for example billing questions sent to technical, and improve the category descriptions or add rules.
When would you use orchestrator-workers instead of a pipeline?
When the number and kind of sub-tasks depend on the input and cannot be written in advance, such as research across a variable set of documents. If the sub-tasks are always the same, a pipeline or parallel fixed calls is simpler.
Can the specialists use different models?
Yes, and that is one advantage of the router. A small, cheap model can handle routing and simple questions, while a stronger model handles complex technical answers.
7Conclusion
In this answer, we went through four ways to combine agents: a sequential pipeline for known steps, a router that picks a specialist, orchestrator-workers for tasks that split differently each time, and evaluator-optimizer for critique loops with a limit. We built a router in Java with a ChatClient call returning a structured enum and a switch, and saw that plain code should own the control flow while models make only the decisions that need them.
Open with
There are four patterns I see most. A sequential pipeline chains agents so each output feeds the next, like extract facts, then summarise, then translate; it is simple and predictable.
Then, if they want more
A router puts a classifier first, a model or rules, that picks which specialised agent handles the request, so each specialist has focused instructions and only its own tools. Orchestrator-workers has one agent break a task into parts that differ from case to case and hand them to workers, then combine the results.
And evaluator-optimizer has one agent produce and another critique in a loop, always with a round limit. In Java I would build a router with Spring AI: a ChatClient call with a system prompt describing the categories, and .call().entity(RouteDecision.class), where RouteDecision wraps an enum like BILLING, TECHNICAL, OTHER.
Then a normal switch calls the right service, with a fallback if parsing fails, and maybe a rule in front for obvious cases like invoice numbers.
The principle I follow is that plain code owns the control flow, sequences, switches, limits, and models only make the decisions that need language judgement, because code is deterministic and testable while models are not.
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 "What are common patterns for combining several LLM agents (sequential pipeline, router...": https://scrivio-interview-library.netlify.app/spring-boot/ai-agents-and-mcp/074ba812e7/