You are here
Home > java >

AI Agents in Spring Boot: Building Autonomous Workflows with Spring AI

AI Agents in Spring Boot: Building Autonomous Workflows with Spring AI

AI Agents in Spring BootYour Spring Boot application already talks to databases, APIs, and message queues. Now imagine it could think, decide, and act on its own. That is exactly what AI Agents do.

Most developers stop at chatbots, send a prompt, get a response. But a chatbot has no memory, no tools, and no goals. An AI Agent is different. It receives an objective, breaks it into steps, calls your Java methods when needed, evaluates the result, and keeps going until the job is done.

With Spring AI, you do not need Python, LangChain, or a completely new tech stack. You build agents right inside your existing Spring Boot project, using the same annotations, beans, and patterns you already know.

This article walks you through everything from setting up Spring AI to building real autonomous workflows using five proven agentic patterns. Code examples are included at every step. If you know Spring Boot, you are already halfway there.

What You Will Learn

By the end of this article, you will understand:

  • What AI Agents are and how they differ from regular chatbots

  • How Spring AI supports agentic workflows in Spring Boot

  • What Tool Calling is and how it enables autonomy

  • The five key agentic workflow patterns (with real-world use cases)

  • How to build a working AI Agent in Spring Boot using Spring AI

  • Common mistakes developers make when building agents

  • Interview questions on Spring AI Agents

Prerequisites

Before reading this article, make sure you are familiar with:

  • Core Spring Boot concepts (beans, dependency injection, REST controllers)

  • Basic Spring AI setup (ChatClient, Prompt API)

  • Java 21 or later

  • Maven or Gradle for project setup

Spring Boot version used: 3.4.x | Spring AI version: 1.0.x | Java version: 21

Why AI Agents Matter (And Why Java Developers Should Care)

Most developers start with chatbots. You send a prompt. You get a response. That is it.

AI Agents are different. An agent does not just generate text, it plans, decides, acts, and loops until a goal is achieved.

Think about it this way:

  • A chatbot answers: “What is the weather in Chicago?”

  • An agent fetches live weather data, checks your calendar, and then tells you: “It will rain tomorrow. You have a meeting scheduled. Want me to send a rescheduling email?”

That is the difference between a language model and an autonomous workflow.

Spring AI, the official AI integration framework from the Spring team, makes it straightforward to build production-ready AI agents inside your existing Spring Boot applications without leaving the Java ecosystem you already know.

What Is an AI Agent?

An AI Agent is an AI system that:

  1. Receives a high-level goal (not just a question)

  2. Breaks it down into steps

  3. Calls tools (APIs, databases, functions) to complete each step

  4. Uses the results to decide the next action

  5. Loops until the goal is done

The key capability that makes an agent autonomous is Tool Calling (also called Function Calling). It allows the LLM to request the execution of your Java methods based on context and Spring AI handles the entire plumbing.

Spring AI Architecture: How Agents Are Built

Spring AI 1.0.x provides the following key building blocks for agents:

Building Block Purpose
ChatClient Fluent API to interact with LLMs (OpenAI, Ollama, Azure, Gemini, etc.)
@Tool annotation Marks a Java method as a callable tool for the LLM
ToolCallback Registers tools so the ChatClient can pass them to the model
Advisor API Adds cross-cutting behaviors (memory, logging, retry loops)
ChatMemory Stores and retrieves conversation history for context

The flow is simple:

  1. User sends a message

  2. Spring AI forwards the message + available tool definitions to the LLM

  3. The LLM decides which tool to call (if any)

  4. Spring Boot executes the Java method

  5. The result is sent back to the LLM

  6. The LLM generates the final response

The model never runs your Java code directly. It only requests a call. Your application executes it in your own security context.

Setting Up Spring AI in Spring Boot

Step#1: Add Dependencies

Add these to your pom.xml:



    
        
            org.springframework.ai
            spring-ai-bom
            1.0.0
            pom
            import
        
    




    org.springframework.ai
    spring-ai-openai-spring-boot-starter
Step#2: Configure application.properties
spring.ai.openai.api-key=${OPENAI_API_KEY}
spring.ai.openai.chat.options.model=gpt-4o
spring.ai.openai.chat.options.temperature=0.2

Lower temperature (0.1–0.3) is recommended for agents. It makes decisions more deterministic.

Step#3: Verify the ChatClient Bean

Spring AI auto-configures ChatClient.Builder. You just inject it:

@Service
public class AgentService {

    private final ChatClient chatClient;

    public AgentService(ChatClient.Builder builder) {
        this.chatClient = builder.build();
    }
}

 

Building Your First AI Agent with Tool Calling

The Goal: A Smart Order Status Agent

This agent will:

  • Accept a natural language query like “Where is order #12345?”
  • Decide it needs to call an order lookup tool
  • Fetch order data from your database
  • Return a human-readable response

Step#1: Create the Tool

// Java 21 | Spring Boot 3.4.x | Spring AI 1.0.x
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.stereotype.Component;

@Component
public class OrderTools {

    private final OrderRepository orderRepository;

    public OrderTools(OrderRepository orderRepository) {
        this.orderRepository = orderRepository;
    }

    @Tool(description = "Fetches the current status of an order by its order ID")
    public String getOrderStatus(String orderId) {
        return orderRepository.findById(orderId)
            .map(order -> "Order " + orderId + " is currently: " + order.getStatus()
                + ". Expected delivery: " + order.getExpectedDelivery())
            .orElse("Order not found with ID: " + orderId);
    }
}

The @Tool annotation is the key. The description field tells the LLM when to use this tool. Write it clearly, the model reads this description to decide when to call the method.

Step#2: Register the Tool with ChatClient

@Service
public class OrderAgentService {

    private final ChatClient chatClient;
    private final OrderTools orderTools;

    public OrderAgentService(ChatClient.Builder builder, OrderTools orderTools) {
        this.chatClient = builder
            .defaultTools(orderTools)
            .build();
        this.orderTools = orderTools;
    }

    public String handleQuery(String userMessage) {
        return chatClient.prompt()
            .user(userMessage)
            .call()
            .content();
    }
}

Step#3: Expose it via REST Controller

@RestController
@RequestMapping("/api/agent")
public class OrderAgentController {

    private final OrderAgentService agentService;

    public OrderAgentController(OrderAgentService agentService) {
        this.agentService = agentService;
    }

    @PostMapping("/order")
    public ResponseEntity askAgent(@RequestBody String query) {
        return ResponseEntity.ok(agentService.handleQuery(query));
    }
}

Now when a user sends: “Can you tell me the status of order 10045?”, the agent will:

  1. Recognise the intent
  2. Call getOrderStatus(“10045”) automatically
  3. Return a natural language answer

Five Agentic Workflow Patterns in Spring AI

Spring AI supports five core agentic patterns, based on Anthropic’s research. Understanding these patterns helps you design the right architecture for your use case.

1. Chain Workflow

Steps run in sequence. Output of one step becomes input of the next.

Use case: Draft an email → Translate it → Send it

User Input → Step 1 (Draft) → Step 2 (Translate) → Step 3 (Send) → Output
public String chainWorkflow(String topic) {
    String draft = chatClient.prompt()
        .user("Write a professional email about: " + topic)
        .call().content();

    String translated = chatClient.prompt()
        .user("Translate this email to French:\n" + draft)
        .call().content();

    return translated;
}

2. Parallelization Workflow

Multiple tasks run simultaneously. Results are merged at the end.

Use case: Analyze a resume from multiple angles (skills, experience, culture fit) at the same time.

The LLM decides which path to take based on the input.

Use case: Route a customer query to the right department (billing, support, or sales).

@Tool(description = "Routes the customer query to billing department")
public String routeToBilling(String query) { /* handle billing */ return "Routed to billing"; }

@Tool(description = "Routes the customer query to technical support")
public String routeToSupport(String query) { /* handle support */ return "Routed to support"; }

@Tool(description = "Routes the customer query to the sales team")
public String routeToSales(String query) { /* handle sales */ return "Routed to sales"; }

Register all three tools. The agent picks the right one based on the user’s message. No if-else in your code. The LLM decides.

4. Orchestrator-Workers Workflow

A master agent breaks a big task into subtasks and delegates to worker agents.

Use case: Build a competitor analysis report. The orchestrator creates a plan. Workers research pricing, features, and reviews independently.

Orchestrator Agent
  ├── Worker 1: Research pricing
  ├── Worker 2: Analyze features
  └── Worker 3: Gather user reviews
       ↓
  Orchestrator combines results

This is the most powerful pattern for complex, multi-step business automation.

5. Evaluator-Optimizer Workflow

The agent generates output, then evaluates its own output and retries if the quality is not good enough.

Use case: Generate a code snippet. Evaluate if it compiles and passes unit tests. If not, retry with corrections.

public String generateWithEvaluation(String requirement) {
    String result = null;
    int attempts = 0;

    while (attempts < 3) {
        result = chatClient.prompt()
            .user("Write Java code for: " + requirement)
            .call().content();

        String evaluation = chatClient.prompt()
            .user("Does this code correctly implement the requirement? "
                + "Reply YES or NO and explain.\nRequirement: " + requirement
                + "\nCode:\n" + result)
            .call().content();

        if (evaluation.toUpperCase().startsWith("YES")) break;

        requirement = "Previous attempt failed evaluation. Feedback: "
            + evaluation + "\nRetry: " + requirement;
        attempts++;
    }
    return result;
}

Adding Memory to Your Agent

Without memory, every agent interaction starts fresh. Use ChatMemory to give your agent context across multiple turns:

@Service
public class MemoryAgentService {

    private final ChatClient chatClient;

    public MemoryAgentService(ChatClient.Builder builder) {
        this.chatClient = builder
            .defaultAdvisors(new MessageChatMemoryAdvisor(new InMemoryChatMemory()))
            .build();
    }

    public String chat(String conversationId, String userMessage) {
        return chatClient.prompt()
            .user(userMessage)
            .advisors(advisor -> advisor.param(
                AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY, conversationId))
            .call()
            .content();
    }
}

Each conversationId maintains its own independent memory. This is essential for multi-user agent applications.

Real-World Use Cases for Spring AI Agents

Use Case Pattern Used Tools Needed
Customer support bot Routing + Tool Calling CRM API, ticket system
Code review assistant Chain + Evaluator Static analysis, GitHub API
HR resume screener Parallelization Database, scoring function
E-commerce order manager Tool Calling Order DB, payment gateway
Automated report generator Orchestrator-Workers Data sources, email service

Common Mistakes to Avoid

  • Poor tool descriptions: The LLM uses the description field to decide when to call a tool. Vague descriptions lead to wrong or missed tool calls. Be specific: “Fetches current weather for a given city using OpenWeatherMap” is much better than “Gets weather”.

  • No temperature tuning: For agents, always set a low temperature (0.1–0.3). High temperature makes the agent unpredictable. It may hallucinate tool names or skip tool calls entirely.

  • Unbounded loops: In Evaluator-Optimizer patterns, always set a max retry limit. Without it, a failed evaluation can loop indefinitely and drain your API budget.

  • Putting business logic in prompts: If your prompt becomes 500 words long, something is wrong. Business logic belongs in your Java tool methods. The prompt should only set context and intent.

  • Exposing sensitive data via tools: Never return raw database records or credentials from a tool. Always sanitize and filter what the LLM receives back.

  • Ignoring error handling in tools: Tools must handle exceptions gracefully and return readable error messages. If a tool throws a runtime exception, the agent often cannot recover.

FAQs (Interview Questions on Spring AI Agents)

Q1. What is the difference between a chatbot and an AI Agent?

A chatbot generates responses based on a prompt. An AI Agent can also call tools, make decisions, loop on outcomes, and take actions to achieve a goal. An agent has autonomy a chatbot does not.

Q2. What is Tool Calling in Spring AI?

Tool Calling (or Function Calling) is the mechanism by which an LLM requests the execution of a Java method. The LLM does not run the code, it sends a structured request. Spring AI intercepts this, calls your @Tool-annotated method, and returns the result to the model.

Q3. How does the @Tool annotation work in Spring AI?

The @Tool annotation marks a method as a callable tool. Spring AI reads the method signature and the description attribute to build a JSON schema. This schema is sent to the LLM alongside your prompt so the model knows when and how to call it.

Q4. What is the role of the Advisor API in Spring AI agents?

Advisors are interceptors in the ChatClient pipeline. They can add memory, logging, retry logic, and output validation, all without modifying your core agent code. MessageChatMemoryAdvisor is the most commonly used advisor for persistent conversation context.

Q5. Which agentic pattern is best for complex multi-step tasks?

The Orchestrator-Workers pattern. The orchestrator agent plans and delegates. Worker agents execute individual sub-tasks. This allows parallelism, specialization, and better error isolation.

Q6. How do you prevent an infinite loop in the Evaluator-Optimizer pattern?

Always use a bounded loop (e.g., while (attempts < 3)). Define clear success/failure criteria that the evaluator must check against. Log each attempt so you can debug behavior later.

Q7. Can Spring AI agents work with local LLMs?

Yes. Replace the OpenAI starter with spring-ai-ollama-spring-boot-starter and update application.properties to point to your local Ollama server. The agent code remains unchanged. Spring AI abstracts the LLM provider.

Conclusion and What to Learn Next

AI Agents in Spring Boot are not experimental anymore. With Spring AI 1.0.x, you have a production-ready toolkit to build autonomous workflows using tools and patterns you already know as a Java developer.

Start small: build a single-tool agent, test it, then add routing and memory. Graduate to Orchestrator-Workers when you need to automate complex business pipelines.

What to learn next:

  • Spring AI RAG (Retrieval-Augmented Generation): Give your agents access to your own documents and knowledge bases

  • Spring AI MCP (Model Context Protocol): Connect Spring AI agents to external MCP-compatible tool servers

  • Embabel: The next-generation Spring AI orchestration framework using Goal-Oriented Action Planning (GOAP)

  • Spring AI Advisors: Deep dive into building custom advisors for retry, logging, and output validation

References:

Building Effective Agents with Spring AI (Official Spring Blog)

Spring AI Agentic Patterns (Explainable AI Spring Blog)


You may also like:

12 Essential AI Terms Java Developer Must Know in 2026

Top 10 AI Tools for Java Developers and Programmers in 2026

Best AI tools for Java Developers by Development Phase

Free AI Framework for Java Developers in 2026: Think Like a Technology PRO (Try It Now)

Leave a Reply


Top