6 Essential AI Agent Design Patterns Every Developer Should Master
This article explores six practical AI Agent design patterns—ReAct, Tool Use, Reflection, Planning, Multi‑Agent, and Human‑in‑the‑Loop—detailing their principles, Java Spring AI implementations, advantages, drawbacks, and suitable scenarios, and provides guidance on selecting and combining them for robust AI applications.
Introduction
Large‑model APIs have made it easy to start building AI agents, but uncontrolled prompt engineering often leads to token‑hungry loops. The decisive factor for a production‑grade AI system is how agents are structured, how tasks are planned, and how external tools are invoked.
Typical Agent Architecture
A mature agent system is composed of three core modules:
LLM inference node – calls the large language model and returns reasoning or action decisions.
Tool‑execution node – receives tool‑call specifications from the LLM and runs the corresponding function (API call, database query, file operation, etc.).
Memory component – persists conversation or workflow state so that the agent can retrieve context across turns.
On top of this backbone, six design patterns have emerged that address different levels of complexity and reliability.
Six Core Design Patterns
Pattern 1 – ReAct (Reasoning + Acting)
Positioning: The basic “think‑act‑observe” loop that underlies all more sophisticated patterns.
The agent first reasons about the current request, decides on an action, executes the action (via a tool or internal function), observes the result, and repeats until the goal is satisfied.
Java example (Spring AI Alibaba)
ReactAgent agent = ReactAgent.builder()
.name("customer_service_agent")
.model(chatModel)
.systemPrompt("You are a professional customer‑service assistant that solves problems by reasoning and acting.")
.tools(queryOrderTool, checkInventoryTool)
.build();
// Synchronous call
String response = agent.call("Check the status of order 12345");
System.out.println(response);
// Streaming call (typewriter effect)
Flux<String> stream = agent.stream("I want to return a product, what should I do?");
stream.subscribe(System.out::print);Core components used: AgentLlmNode – performs LLM inference and produces the reasoning/action JSON. AgentToolNode – dispatches tool calls generated by the LLM. MemorySaver – writes dialogue turns to persistent storage.
Pros: Simple, easy to debug, provides a clear foundation for extensions.
Cons: Each reasoning step requires a separate LLM request, increasing token usage; no built‑in long‑term planning.
Typical use cases: Basic Q&A, intelligent customer service, any workflow that needs multi‑step reasoning without extensive state.
Pattern 2 – Tool Use (Function Calling)
Positioning: Gives the agent “hands and feet” by connecting it to external functions.
LLMs can only generate text. Tool Use lets the agent invoke real‑time data sources, APIs, or system commands, turning textual intent into concrete actions.
Java example (annotation‑driven)
@Component
public class OrderTools {
@Tool(description = "Query the status of a specific order")
public String queryOrder(@P("Order ID") String orderId) {
Order order = orderService.findById(orderId);
return String.format("Order %s status: %s", orderId, order.getStatus());
}
@Tool(description = "Get weather information for a city")
public String getWeather(@P("City name") String city) {
return weatherApi.get(city);
}
@Tool(description = "Calculate the sum of two numbers")
public int add(@P("First number") int a, @P("Second number") int b) {
return a + b;
}
}
ReactAgent agent = ReactAgent.builder()
.model(chatModel)
.tools(new OrderTools())
.build();
String result = agent.call("Check the logistics status of order ORD‑123");Pros: Extends the agent’s capability frontier; annotation‑driven development is concise; tools can be reused across projects.
Cons: Each tool must have an accurate description; as the tool library grows the LLM may select an inappropriate tool.
Typical use cases: Real‑time information lookup (weather, stock, order status), data manipulation, system integration.
Pattern 3 – Reflection
Positioning: The agent “thinks twice before acting”, iteratively critiquing and improving its own output.
Reflection creates two cooperating agents: an executor that generates an answer and a critic that reviews the answer. The loop repeats until the critic returns a pass signal or a maximum iteration count is reached.
public class ReflectionService {
private final ReactAgent executor;
private final ReactAgent critic;
public ReflectionService(ChatModel chatModel) {
this.executor = ReactAgent.builder()
.name("executor")
.model(chatModel)
.systemPrompt("You are a professional assistant that answers user questions.")
.build();
this.critic = ReactAgent.builder()
.name("critic")
.model(chatModel)
.systemPrompt("You are a quality‑check expert. Review the following answer, point out errors and imperfections. If the answer is perfect, reply \"PASS\".")
.build();
}
public String generateWithReflection(String input, int maxIterations) {
String current = null;
String feedback = "";
for (int i = 0; i < maxIterations; i++) {
if (current == null) {
current = executor.call(input);
} else {
current = executor.call("Please improve the answer based on the feedback:
" +
"Original question: " + input + "
" +
"Previous answer: " + current + "
" +
"Feedback: " + feedback);
}
String critique = critic.call("Please review the following answer:
" + current);
if (critique.contains("PASS") || i == maxIterations - 1) {
return current;
}
feedback = critique;
}
return current;
}
}Pros: Significantly raises answer quality; automatically discovers logical or factual errors.
Cons: Multiple LLM calls increase token consumption and latency.
Typical use cases: Code review, content polishing, academic paper editing, high‑accuracy Q&A.
Pattern 4 – Planning
Positioning: Decomposes a complex goal into executable sub‑tasks and orchestrates their execution.
A Plan Agent first creates a task graph (sequential or parallel) and then runs each node with its own specialized tool set.
// Define three specialist agents
ReactAgent dataCollector = ReactAgent.builder()
.name("Data Collector")
.model(chatModel)
.tools(databaseTool, webSearchTool)
.build();
ReactAgent dataAnalyzer = ReactAgent.builder()
.name("Data Analyzer")
.model(chatModel)
.tools(statisticsTool)
.build();
ReactAgent reportGenerator = ReactAgent.builder()
.name("Report Generator")
.model(chatModel)
.tools(reportTool)
.build();
// Sequential orchestration
SequentialAgent pipeline = SequentialAgent.builder()
.name("Market Research Workflow")
.agents(dataCollector, dataAnalyzer, reportGenerator)
.build();
String finalReport = pipeline.call("Generate Q1 2026 sales analysis report");Core value: Turns a multi‑day, multi‑agent development effort into a few hours; standard paradigm for enterprise‑grade AI applications.
Pros: Handles ultra‑long, ultra‑complex tasks; improves determinism and predictability; supports parallel execution for efficiency.
Cons: Planning itself may contain errors; requires fallback strategies and careful task granularity design.
Typical use cases: End‑to‑end data analysis, automated research, report generation, project planning.
Pattern 5 – Multi‑Agent Collaboration
Positioning: Multiple specialized agents cooperate, each contributing its expertise (1 + 1 > 2).
Beyond linear pipelines, agents communicate via a message hub, enabling dynamic routing, escalation, and group discussion.
// Create specialist agents
ReactAgent orderAgent = ReactAgent.builder()
.name("Order Agent")
.model(chatModel)
.tools(queryOrderTool)
.build();
ReactAgent paymentAgent = ReactAgent.builder()
.name("Payment Agent")
.model(chatModel)
.tools(queryPaymentTool)
.build();
ReactAgent refundAgent = ReactAgent.builder()
.name("Refund Agent")
.model(chatModel)
.tools(processRefundTool)
.build();
// Build the collaboration hub
MsgHub hub = new MsgHub();
hub.subscribe("order:query", orderAgent);
hub.subscribe("payment:status", paymentAgent);
hub.subscribe("refund:process", refundAgent);
// Trigger a workflow
hub.publish(new Message("order:query", "Get detailed information for order ORD‑123"));
// Example of inter‑agent communication
orderAgent.onMessage("order.found", (msg) -> {
hub.publish(new Message("payment:status", "Check payment status for " + msg.getData()));
});Collaboration modes (selected):
Hierarchical command: A master agent decomposes tasks; sub‑agents execute them (enterprise task scheduling).
Nested: An agent contains child agents (complex hierarchical systems).
Transfer: If an agent cannot handle a request, it forwards it to another agent (customer‑service escalation).
Group‑chat: Agents discuss freely, useful for creative brainstorming.
Pros: Modular, easy to extend, parallel processing, natural fit for distributed deployment.
Cons: Higher architectural complexity; additional overhead for message routing and coordination.
Typical use cases: Large‑scale enterprise systems, cross‑department workflows, complex process automation.
Pattern 6 – Human‑in‑the‑Loop
Positioning: Inserts a human decision point before critical actions, improving safety and controllability.
This pattern is essential when the operation involves finance, permission changes, or sensitive data. The agent first analyses intent, then checks whether approval is required; if so, it sends an approval request to a human workflow service.
public class HumanInTheLoopAgent {
private final ReactAgent agent;
private final ApprovalService approvalService;
public String processWithApproval(UserRequest request) {
// 1. Analyze intent
String analysis = agent.call("Analyze intent and required operations: " + request.getText());
// 2. Determine if human approval is needed
if (requiresApproval(analysis)) {
ApprovalRequest ar = ApprovalRequest.builder()
.userId(request.getUserId())
.operation(extractOperation(analysis))
.details(analysis)
.build();
ApprovalResult result = approvalService.requestApproval(ar);
if (!result.isApproved()) {
return "Operation rejected, please contact an administrator";
}
}
// 3. Execute the operation
return agent.call("Execute the following operation: " + analysis);
}
private boolean requiresApproval(String analysis) {
String[] keywords = {"refund", "delete", "modify permission", "batch"};
for (String kw : keywords) {
if (analysis.contains(kw)) return true;
}
return false;
}
}Pros: Improves system safety, satisfies compliance requirements in finance, government, etc., and increases user trust.
Cons: Reduces automation level and adds latency dependent on human response time.
Typical use cases: Financial transactions, permission changes, data deletion, any high‑risk business process.
Pattern Comparison Summary
ReAct – Basic reasoning‑action loop; low development complexity, moderate token cost; ideal for chatbots and simple Q&A.
Tool Use – Adds external function calls; similar complexity to ReAct; expands capability boundary.
Reflection – Self‑review loop; higher token consumption and development effort; best for code review, content polishing.
Planning – Task decomposition and orchestration; higher complexity and token usage; recommended for data‑analysis pipelines and automated research.
Multi‑Agent – Collaborative agents; highest architectural complexity and token cost; suited for enterprise‑scale workflows.
Human‑in‑the‑Loop – Human approval checkpoints; moderate complexity, low token cost; essential for finance, permission, or any high‑risk operation.
Choosing a Pattern
Start with the simplest combination – ReAct + Tool Use – to validate feasibility quickly. As business requirements grow, layer on Reflection for quality, Planning for task orchestration, and Multi‑Agent for large‑scale coordination. Insert Human‑in‑the‑Loop wherever regulatory or safety concerns exist.
Combining Patterns in Practice
Intelligent Customer Service: ReAct + Tool Use (order lookup, inventory check) + Reflection (answer refinement).
Data‑Analysis Platform: Planning + Multi‑Agent + Human‑in‑the‑Loop (sensitive data approval).
Code Generation Assistant: ReAct + Reflection + Tool Use (execute generated code, run tests).
Conclusion
The six AI‑Agent design patterns – ReAct, Tool Use, Reflection, Planning, Multi‑Agent, and Human‑in‑the‑Loop – map directly to human problem‑solving strategies. Selecting the appropriate pattern(s) and incrementally adding more sophisticated layers transforms a prototype into an engineered, production‑ready AI system.
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.
Code Ape Tech Column
Former Ant Group P8 engineer, pure technologist, sharing full‑stack Java, job interview and career advice through a column. Site: java-family.cn
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.
