Add Context Memory to Your AI in 5 Lines with Spring AI ChatMemory

The article demonstrates how to give a Spring Boot AI chatbot persistent context using Spring AI's ChatMemory, showing the problem of stateless requests, explaining the ChatMemory architecture, providing a five‑line code example, comparing storage options (InMemory, JDBC, Redis), and sharing practical tips and pitfalls.

Coder Circle
Coder Circle
Coder Circle
Add Context Memory to Your AI in 5 Lines with Spring AI ChatMemory

When an AI chatbot receives a request, it only sees the current turn, so asking "continue" leads to a confused response because the model has no memory of previous interactions. This stateless behavior is a classic multi‑turn dialogue problem.

Spring AI solves the issue with ChatMemory , which stores each exchange and automatically includes the conversation history in subsequent prompts, effectively creating an "AI memory palace".

The ChatMemory stack consists of three layers:

ChatMemoryRepository (storage layer interface)
    ↑
ChatMemory (manager)
    ↑
MessageChatMemoryAdvisor (interceptor registered to ChatClient)

Using Spring Boot style, the entire solution can be implemented in five lines of code inside a controller:

@RestController
@RequestMapping("chat/memory")
public class ChatMemoryController {
    @Resource
    private MiniMaxChatModel miniMaxChatModel;
    @Resource
    private ChatMemory chatMemory; // auto‑configured InMemory instance

    @GetMapping(value = "v1", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> streamEvent(@RequestParam("question") String question) {
        // 5 core lines
        ChatClient chatClient = ChatClient.builder(miniMaxChatModel)
            .defaultAdvisors(MessageChatMemoryAdvisor.builder(chatMemory).build())
            .build();
        return chatClient.prompt()
            .user(question)
            .stream()
            .content();
    }
}

The advisor automatically reads historical messages, appends them to the prompt, and writes the new user‑AI exchange back to the memory.

Different storage back‑ends can be chosen:

InMemory – zero‑configuration, suitable for development; loses data on restart.

JDBC – production‑ready; add the spring-ai-starter-model-chat-memory-repository-jdbc dependency and set

spring.ai.chat.memory.repository.jdbc.initialize-schema=embedded

to let Spring create the table.

Redis – high performance with advanced queries; configure spring.ai.chat.memory.redis.host, port, and time-to-live. It offers content search and is about ten times faster than JDBC.

The advisor works as follows:

User request
  ↓
MessageChatMemoryAdvisor (interceptor)
  ├─ Read: chatMemory.get(sessionId) → history
  ├─ Append: history + current question → full prompt
  ↓
Send to AI model
  ↓
AI response
  ↓
MessageChatMemoryAdvisor
  ├─ Write: chatMemory.add(sessionId, userMsg, aiReply)
  ↓
Return to user

Session isolation is achieved by supplying a unique sessionId per user, e.g., String sessionId = user.getId(); and constructing the advisor with that ID.

Because AI models have a token limit (MiniMax typically 32K), memory size must be controlled. Spring AI provides two window implementations: MessageWindowChatMemory.builder().maxMessages(10).build() – keeps the most recent N messages (10‑20 works for simple dialogs). TokenWindowChatMemory.builder().maxTokens(2000).build() – caps the total token count for longer conversations.

Practical tips gathered from experience:

Use maxMessages=10 for simple Q&A, maxMessages=20 for complex dialogs, and consider maxTokens=2000 for very long sessions.

Never share a single session ID across users; always isolate memory per user ID.

Avoid InMemory in production; switch to Redis or JDBC to persist memory across restarts.

Monitor token usage to prevent truncation when the context exceeds the model's limit.

In summary, ChatMemory is a fundamental building block for AI applications, and Spring AI makes its integration as straightforward as typical database operations, with the Advisor mechanism handling memory automatically and session IDs ensuring isolation.

JavaSpring BootSpring AIAI conversationChatMemory
Coder Circle
Written by

Coder Circle

Limited experience, continuously learning and summarizing knowledge, aiming to join a top tech company.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.