liveBook · Manning

Chapter 5: Enabling conversational memory

What you will learn in chapter 5 of Spring AI in Action

This introduction explains why an LLM’s lack of memory makes follow-up conversation difficult.

After this chapter you will be able to

What it builds on

Section by section

Introduction

This introduction explains why an LLM’s lack of memory makes follow-up conversation difficult.

Key terms: LLMs, stateless services, conversational state

5.1 Making memories in AI

The section explains how Spring AI preserves conversation history and presents three advisors that add that history to later prompts.

Key terms: ChatMemory, user message, assistant message, chat history, prompt context, MessageChatMemoryAdvisor, PromptChatMemoryAdvisor, role-based messages, system message template, VectorStoreChatMemoryAdvisor

5.2 Adding conversational memory

Explains why follow-up questions need conversational context and how Spring AI chat memory advisors supply it.

Key terms: memory of the conversation, follow-up questions, MessageChatMemoryAdvisor, PromptChatMemoryAdvisor, LLM

5.2.1 Enabling an in-memory chat advisor

This section shows how one default advisor enables in-memory chat memory in Board Game Buddy, then illustrates the result with a follow-up question.

Key terms: ChatClient, defaultAdvisors(), MessageChatMemoryAdvisor, ChatMemory, MessageWindowChatMemory, PromptChatMemoryAdvisor, ChatMemoryRepository, Map, conversation history, follow-up question

5.2.2 Inspecting the prompt for chat memory

The section shows how two chat memory advisors place conversation history in prompts and why growing history can become costly.

Key terms: chat history, user message, assistant message, system message, MessageChatMemoryAdvisor, PromptChatMemoryAdvisor, chat memory, system prompt, usage tokens, token cost

5.2.3 Configuring chat memory size

This section explains how Spring AI limits chat memory to recent messages and how to change that limit.

Key terms: chat memory, messages, prompt, ChatMemory, MessageWindowChatMemory, maxMessages(), @Bean, token usage

5.3 Specifying the conversation ID

This section shows how to give requests distinct conversation IDs so chat memory stays within the right conversation, then explains why the default in-memory storage is unsuitable for production.

Key terms: conversation ID, default, conversation history, X_AI_CONVERSATION_ID, @RequestHeader, AdvisorSpec, CONVERSATION_ID, MessageWindowChatMemory, ChatMemoryRepository, in-memory

5.4 Enabling persistent chat memory

Explains why application-managed chat memory enables conversational recall but needs persistence to survive an application restart.

Key terms: chat memory advisor, MessageWindowChatMemory, persistent chat memory

5.4.1 Persisting chat memory to a database

The section shows how to replace in-memory chat storage with Cassandra, Neo4j, or JDBC-backed storage so conversation history survives application restarts.

Key terms: ChatMemoryRepository, MessageWindowChatMemory, persistent chat memory, CassandraChatMemoryRepository, Neo4jChatMemoryRepository, autoconfiguration, starter dependency, session/conversation entity, user and assistant messages, metadata node

5.4.2 Storing chat memory in a vector store

This section explains how VectorStoreChatMemoryAdvisor retrieves relevant past messages from a vector store and how to configure it on a ChatClient.

Key terms: VectorStoreChatMemoryAdvisor, vector store, embeddings, similarity search, RAG pattern, system prompt, ChatClient, VectorStore, ChatMemory, CONVERSATION_ID

5.5 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 4: Talking with your documents Chapter 6: Activating tool-driven generation →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.