Spring AI Essentials
Before jumping into specific examples, here's the stuff that shows up in every Spring AI project.
ChatClient
Everything starts with ChatClient. Think of it like RestTemplate but for LLMs.
@Bean
ChatClient chatClient(ChatClient.Builder builder) {
return builder
.defaultSystem("You are a helpful assistant.")
.build();
}
Inject it, call .prompt(), get a response. That's the whole pattern.
How every call works
Same shape every time:
- Build a prompt (system message + user message)
- Add advisors if you need them (memory, RAG, logging)
- Call the model —
.call()for sync,.stream()for streaming - Get the response — string, entity, or structured type
String answer = chatClient.prompt()
.user("What is Spring AI?")
.call()
.content();
The key pieces
| Thing | What it does |
|---|---|
ChatClient |
Sends prompts, gets responses |
@Tool |
Lets the LLM call your Java methods |
ChatMemory |
Remembers previous messages |
Advisor |
Middleware for requests/responses |
.entity() |
Maps LLM output to Java objects |
Each of these has its own page:
- Prompts — system prompts, guardrails, prompt catalogs
- Tools — function calling with
@ToolandToolContext - Chat Memory — conversation memory with MongoDB
- Structured Output — mapping responses to Java records
- RAG — retrieval-augmented generation