liveBook · Manning

ChatClient

As explained in Spring AI in Action

ChatClient is a Spring AI API component for sending conversational requests to language models and receiving responses.

ChatClient is a Spring AI component for the common case of sending conversational text to a language model and receiving generated text. A Spring Boot service injects ChatClient.Builder, builds a client, and uses its fluent interface to construct and submit prompts. section 1.1.2 section 1.2.1

A basic request follows this sequence:

  • Build the client from the injected ChatClient.Builder. section 1.1.2
  • Start prompt construction with prompt(). section 1.1.2
  • Place the question in the user role with user(). section 1.1.2
  • Submit the completed prompt with call() and retrieve response text with content(). section 1.1.2

A service can wrap the returned text in an application-specific Answer record and expose the operation through a REST controller. ChatClient also supports tools supplied when the client is built or when an individual prompt is created. section 1.1.2 section 6.2.3

Board-game question service

A SpringAiBoardGameService receives a ChatClient.Builder, builds its client, sends question.question() through user(), calls the model, and returns new Answer(answerText). A controller accepts Question JSON at /ask and delegates to the service.

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 complete service implementation showing constructor injection, client creation, fluent prompt construction, and response wrapping. section 1.1.2

The same abstraction supports summarization, translation, sentiment analysis, and agentic workflows; the prompt determines the task. Tools and workflow patterns extend its use beyond question answering. section 11.4 section 12.2

How Spring AI in Action covers it

How deep it goes
Explains construction, fluent prompting, response extraction, tools, and broader applications, while deferring lower-level model internals.
Examples
Uses a board-game question service, REST endpoint, game-complexity tools, summarization, translation, sentiment analysis, and workflow patterns.
What it assumes
Assumes Spring Boot dependency and configuration familiarity while demonstrating ChatClient.Builder injection and prompt construction explicitly.
What you can do after
Build it from scratch as a Spring Boot service that submits prompts, returns model responses, exposes REST operations, and supplies tools.
How it connects
Connects prompts, language models, REST APIs, tool calling, Spring Boot, and agentic workflow patterns.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring AI in Action. The section links point to the parts of the book it draws on.