Chapter 1- Build Production-Ready AI Agents with Spring AI, and Java
Introduction
Artificial Intelligence has evolved rapidly over the past few years. Initially, AI applications were limited to answering questions, summarizing text, translating languages, or generating content. These applications are commonly referred to as AI-powered chatbots, where the Large Language Model (LLM) acts as the central intelligence, generating responses based on the prompts it receives.
While chatbots are useful, they have a significant limitation—they cannot perform real-world tasks on their own.
Imagine asking a chatbot:
“Generate my monthly sales report, save it as an Excel file, and email it to my manager.”
A traditional chatbot may explain how to perform these steps, but it cannot actually access your company’s database, generate the report, save the file, or send the email.
This is where AI Agents come into the picture.
An AI agent doesn’t just answer questions—it understands goals, makes decisions, interacts with external systems, executes tasks, and returns results. Instead of acting as a conversational assistant, it behaves more like a digital employee capable of completing business processes.
For example, when a user requests:
“Analyze last month’s sales, identify the top five customers, create a PDF report, and send it to the finance team.”
An AI agent can:
- Connect to the enterprise database
- Execute SQL queries
- Analyze the retrieved data
- Generate a report
- Convert it into a PDF
- Send the report via email
- Notify the user when the task is complete
Notice that the language model itself is not performing these operations. Instead, the language model acts as the reasoning engine, while the AI agent coordinates various tools and enterprise systems to accomplish the task.
From Chatbots to AI Agents
A traditional chatbot simply sends the user’s prompt to an LLM and returns the generated response.
User
│
▼
Large Language Model
│
▼
Generated Response
An AI agent, however, can interact with external systems.
User
│
▼
AI Agent
│
┌───────────┼────────────┐
▼ ▼ ▼
Database APIs File System
│ │ │
└───────────┼────────────┘
▼
Final Response
The AI agent acts as an orchestrator. It determines what actions are needed, invokes the appropriate tools, gathers information, and combines everything into a meaningful response.
Why Build an AI Agent Platform?
As organizations adopt AI, they rarely need just one assistant. Different departments require specialized agents.
Examples include:
- HR Assistant
- Finance Assistant
- Customer Support Assistant
- DevOps Assistant
- Code Review Assistant
- Sales Analytics Assistant
Instead of rebuilding authentication, prompt management, memory, tool integration, logging, and monitoring for every project, organizations build a reusable AI Agent Platform.
Think of it as Spring Boot for AI applications.
Technology Stack
Throughout this series we’ll use:
- Java 21
- Spring Boot 3.5.x
- Spring AI
- OpenAI
- MCP (later chapters)
- PostgreSQL (later)
- Redis (later)
- Vector Database (later)
Step 1: Create the Spring Boot Project
Project Structure
ai-agent-platform
│
├── src
│ ├── main
│ │ ├── java
│ │ └── resources
│ └── test
│
├── pom.xml
└── README.md
pom.xml
Create a new Spring Boot project and use the following Maven configuration.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.5.4</version>
</parent>
<groupId>com.example</groupId>
<artifactId>ai-agent-platform</artifactId>
<version>1.0.0</version>
<properties>
<java.version>21</java.version>
<spring-ai.version>1.0.1</spring-ai.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bom</artifactId>
<version>${spring-ai.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-openai</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
At this stage, we’ve only added the dependencies required to connect to an LLM and expose REST APIs. We’ll introduce MCP, Redis, PostgreSQL, and Vector Stores in later chapters.
Step 2: Configure Spring AI
Create an application.yml file.
spring:
ai:
openai:
api-key: ${OPENAI_API_KEY}
chat:
options:
model: gpt-4.1-mini
Store your API key as an environment variable instead of hardcoding it.
export OPENAI_API_KEY=your-api-key
Step 3: Create the Main Class
@SpringBootApplication
public class AiAgentApplication {
public static void main(String[] args) {
SpringApplication.run(AiAgentApplication.class, args);
}
}
Run the application to verify that everything starts successfully.
Step 4: Configure the ChatClient
Spring AI automatically creates a ChatModel bean based on your configuration.
Now create a ChatClient bean.
@Configuration
public class AIConfig {
@Bean
ChatClient chatClient(ChatModel chatModel) {
return ChatClient.builder(chatModel)
.build();
}
}
At this point, our application can communicate with the configured LLM.
Step 5: Create Your First AI Agent
Let’s create a simple service that delegates requests to the LLM.
@Service
public class AssistantAgent {
private final ChatClient chatClient;
public AssistantAgent(ChatClient chatClient) {
this.chatClient = chatClient;
}
public String chat(String question) {
return chatClient.prompt()
.user(question)
.call()
.content();
}
}
This service receives the user’s prompt, sends it to the language model, and returns the generated response.
Although simple, this class forms the foundation of our AI agent.
Step 6: Create a REST Controller
Expose the AI agent using a REST endpoint.
@RestController
@RequestMapping("/agent")
public class AgentController {
private final AssistantAgent assistantAgent;
public AgentController(AssistantAgent assistantAgent) {
this.assistantAgent = assistantAgent;
}
@PostMapping
public String chat(@RequestBody String prompt) {
return assistantAgent.chat(prompt);
}
}
Run the application and send a request.
Request
POST /agent
Explain Spring AI.
Response
Spring AI is a framework that provides abstractions for integrating
Large Language Models into Spring applications.
Congratulations! You have built your first AI-powered REST API using Spring AI.
Current Request Flow
Our application currently works as follows:
Client
│
▼
AgentController
│
▼
AssistantAgent
│
▼
ChatClient
│
▼
Chat Model
│
▼
Response
Although functional, our AI behaves like a generic chatbot because it has no predefined role or personality.
Code : https://github.com/beladasharrow-netizen/FirstSpringAIProject-