A large language model (LLM) generates natural-language responses from prompts. Applications can send questions, documents, classifications, sentiment requests, or other instructions through provider APIs. section 1.1
Spring AI provides a consistent client abstraction over different providers and models. Its ChatClient builds a prompt, submits it, and returns generated content without requiring application code to manage the underlying HTTP exchange. section 1.1 section 1.1.2
A basic LLM integration follows this path:
- Accept a user question through a REST endpoint. section 1.1.2
- Place the question in a user message within a prompt. section 1.1.2
- Call the model and wrap its returned text in an application response. section 1.1.2
- Supply conversation history or tool results when the application needs context beyond one prompt. section 5.0 section 6.4
Question-answering service
A Spring service injects ChatClient.Builder, builds a ChatClient, submits question.question() through user(), calls the LLM, and returns the resulting content as an Answer. section 1.1.2
Listing 1.2 The Spring AI implementation of BoardGameService
package com.example.boardgamebuddy;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.prompt.ChatOptions;
import org.springframework.stereotype.Service;
@Service
public class SpringAiBoardGameService implements BoardGameService {
private final ChatClient chatClient;
public SpringAiBoardGameService(ChatClient.Builder chatClientBuilder) { #1
this.chatClient = chatClientBuilder.build(); #2
}
@Override
public Answer askQuestion(Question question) {
var answerText = chatClient.prompt()
.user(question.question()) #3
.call()
.content();
return new Answer(answerText);
}
}
Inspect the compact Spring service that turns a Java question record into an LLM-generated answer. section 1.1.2


































































