Master AI Agent Workflows with Spring Boot 3: From Chains to Orchestrators
This article introduces the fundamentals of augmented large language model agents, explains six workflow patterns—including chain, parallel, routing, orchestrator‑workers, evaluator‑optimizer, and autonomous agents—and provides complete Spring Boot 3 code examples, configuration, and test results for each pattern.
1. Introduction
The core building block of an agent system is an enhanced LLM that integrates retrieval, tools, and memory. The model can generate its own search queries, select appropriate tools, and decide which information to retain.
Anthropic defines "agents" as systems that can autonomously run, use various tools, and handle complex tasks, while "workflows" are predefined code paths that coordinate LLMs and tools.
Workflow: A system that follows a predefined sequence to coordinate LLMs and tools.
Agent: An LLM that dynamically guides its own process and tool usage with autonomy.
The article then presents common patterns observed in production environments, starting from the basic augmented LLM and progressing to more complex autonomous agents.
1.1 Chain Workflow
Chain workflow breaks a complex task into simple, manageable steps. It is suitable when tasks have a clear sequential order, when higher accuracy is worth added latency, and when each step depends on the previous output.
1.2 Parallelization Workflow
LLMs can process multiple tasks simultaneously and aggregate their outputs programmatically. This pattern fits scenarios with many independent items, tasks requiring multiple perspectives, or when processing time is critical.
1.3 Routing Workflow
Routing intelligently assigns tasks based on input type, allowing specialized handling for different categories.
1.4 Orchestrator‑Workers Workflow
A central LLM dynamically decomposes a task, distributes sub‑tasks to worker LLMs, and aggregates their results.
1.5 Evaluator‑Optimizer Workflow
An LLM generates a response while another model provides evaluation and feedback in a loop, useful when clear evaluation criteria exist and iterative optimization adds measurable value.
1.6 Autonomous Agents
Agents emerge as LLMs mature, receiving user instructions, planning operations, receiving environmental feedback, and optionally pausing for human input. They are suited for open‑ended problems where steps cannot be predetermined, but they also bring higher cost and risk of error accumulation.
Note: Extensive sandbox testing and protective measures are recommended.
2. Practical Cases
2.1 Chain Workflow Example
<dependency>
<groupId>com.alibaba.cloud.ai</groupId>
<artifactId>spring-ai-alibaba-starter</artifactId>
<version>1.0.0-M6.1</version>
</dependency> spring:
ai:
dashscope:
api-key: sk-xxxooo
base-url: https://dashscope.aliyuncs.com/compatible-mode/v1
chat:
options:
stream: true
model: qwen-turbo @Component
public class ChainWorkflow {
public static final String[] DEFAULT_SYSTEM_PROMPTS = {
"""
从文本中仅提取数值及其对应的指标。
将每个数值和指标按照“数值: 指标”的格式逐行列出。
示例格式:
92: 客户满意度
45%: 收入增长率""",
"""
将所有数值尽可能转换为百分比形式。
若数值并非百分比或点数形式,则将其转换为小数形式后再以百分比呈现(例如,92 点 → 92%)。
每行只保留一个数值。
示例格式:
92%:客户满意度
45%:收入增长率""",
"""
将所有行按照数值大小进行降序排序。
每行保持“数值: 指标”的格式。
示例:
92%: 客户满意度
87%: 员工满意度""",
"""
将排序后的数据格式化为以下带有“指标”和“数值”两列的 Markdown 表格:
| 指标 | 数值 |
|:--|--:|
| 客户满意度 | 92% |"""
};
private final ChatClient chatClient;
public ChainWorkflow(ChatClient chatClient) { this.chatClient = chatClient; }
public String chain(String userInput, String[] systemPrompts) {
int step = 0;
String response = userInput;
System.out.println(String.format("
STEP %s:
%s", step++, response));
for (String prompt : systemPrompts) {
String input = String.format("{%s}
{%s}", prompt, response);
response = chatClient.prompt(input).call().content();
System.out.println(String.format("
STEP %s:
%s", step++, response));
}
return response;
}
} @GetMapping("")
public ResponseEntity<String> chain() {
String[] prompts = ChainWorkflow.DEFAULT_SYSTEM_PROMPTS;
String userInput = """
第三季度绩效总结:
本季度我们的客户满意度得分提升至92分。
与去年相比,收入增长了45%。
在我们的主要市场中,市场份额现已达到23%。
客户流失率从8%下降至5%。
新用户获取成本为每用户43美元。
产品采用率提升至78%。
员工满意度得分为87分。
营业利润率提高至34%。
""";
return ResponseEntity.ok(this.chainWorkflow.chain(userInput, prompts));
}2.2 Parallelization Workflow Example
@Component
public class ParalleWorkflow {
private final ChatClient chatClient;
public ParalleWorkflow(ChatClient chatClient) { this.chatClient = chatClient; }
/**
* Process multiple inputs in parallel using a fixed thread pool.
*/
public List<String> parallel(String prompt, List<String> inputs, int nWorkers) {
ExecutorService executor = Executors.newFixedThreadPool(nWorkers);
try {
List<CompletableFuture<String>> futures = inputs.stream()
.map(input -> CompletableFuture.supplyAsync(() -> {
try { return chatClient.prompt(prompt + "
Input: " + input).call().content(); }
catch (Exception e) { throw new RuntimeException("Failed to process input: " + input, e); }
}, executor))
.collect(Collectors.toList());
CompletableFuture.allOf(futures.toArray(CompletableFuture[]::new)).join();
return futures.stream().map(CompletableFuture::join).collect(Collectors.toList());
} finally { executor.shutdown(); }
}
} @GetMapping("")
public ResponseEntity<?> index() {
List<String> response = this.paralleWorkflow.parallel("""
分析市场变化将如何影响该利益相关者群体。
提供具体影响及建议措施。
格式应清晰划分章节,并明确优先级。""",
List.of(
"""客户群体:
- 对价格敏感
- 追求更先进的技术
- 关注环境问题""",
"""员工群体:
- 对工作保障的担忧
- 需要新技能
- 希望获得明确的方向指引""",
"""投资者:
- 期望增长
- 希望控制成本
- 关注风险问题""",
"""供应商:
- 产能限制
- 价格压力
- 技术转型"""
), 4);
System.err.println(response);
return ResponseEntity.ok(response);
}2.3 Routing Workflow Example
public class RoutingWorkflow {
private final ChatClient chatClient;
public RoutingWorkflow(ChatClient chatClient) { this.chatClient = chatClient; }
public String route(String input, Map<String, String> routes) {
String routeKey = determineRoute(input, routes.keySet());
String selectedPrompt = routes.get(routeKey);
if (selectedPrompt == null) {
throw new IllegalArgumentException("Selected route '" + routeKey + "' not found in routes map");
}
return chatClient.prompt(selectedPrompt + "
Input: " + input).call().content();
}
private String determineRoute(String input, Iterable<String> availableRoutes) {
System.out.println("
有效路由: " + availableRoutes);
String selectorPrompt = String.format("""
分析输入内容,并从以下选项中选择最合适的支持团队: %s
先阐述你的推理过程,然后以以下JSON格式提供你的选择:
{
\"reasoning\": \"...\",
\"selection\": \"...\"
}
Input: %s""",
availableRoutes, input);
RoutingResponse routingResponse = chatClient.prompt(selectorPrompt).call().entity(RoutingResponse.class);
System.out.println(String.format("路由分享:%s
选择的路由: %s", routingResponse.reasoning(), routingResponse.selection()));
return routingResponse.selection();
}
}2.4 Evaluator‑Optimizer Workflow Example
// Similar structure to the routing example, with an evaluator LLM providing feedback in a loop.2.5 Autonomous Agent Example
// Agent implementation that receives user instructions, plans actions, interacts with the environment, and can pause for human feedback.The article concludes that these patterns can be combined to build increasingly sophisticated AI agent systems, but developers should be aware of higher costs and potential error accumulation, and therefore perform extensive testing in sandbox environments.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Spring Full-Stack Practical Cases
Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
