Large language models are stateless by design, which makes chatbots feel forgetful without proper context management. Spring AI's chat memory support solves this by storing conversation messages and injecting them into each new prompt. The post covers the ChatMemory abstraction, MessageWindowChatMemory for sliding-window context, and how MessageChatMemoryAdvisor connects ChatClient to memory. It walks through setup with in-memory storage for development, then migrating to PostgreSQL via JDBC for persistence across restarts and multiple instances. Practical patterns include using UUID cookies to isolate per-browser conversations, schema initialization options, and production recommendations like Flyway migrations and separating chat memory from full chat history.

7m read timeFrom sivalabs.in
Post cover image
Table of contents
Table of ContentsIntroducing ChatMemoryAdding ChatMemory to the applicationLetting ChatClient manage the messagesChoosing where memory livesPersisting chat memory in PostgreSQLGiving each browser its own conversationWhat I would use in a real applicationRelated PostsSpring AI : Structured OutputSpring AI : Chat with LLMsSupport Me
594 Impressions