Conversational Memory in Spring AI: Building Stateful AI Assistants
Conversational memory transforms a single-turn API into a true chat assistant. Without memory, each request is isolated. With memory, your assistant understands conversation history and maintains coherent dialogues.
Spring AI provides elegant mechanisms for managing conversation history and leveraging memory for intelligent multi-turn conversations.
Why Conversational Memory Matters
Without memory, the AI has no context of previous messages. With memory, it can reference and learn from the conversation flow.
Implementing Conversational Memory
Here's a production-ready implementation:
@Service
public class MemorizedChatAssistant {
private final ChatClient chatClient;
private final Map<String, ConversationContext> conversations = new ConcurrentHashMap<>();
public String chat(String userId, String userMessage) {
ConversationContext context = conversations.computeIfAbsent(
userId, k -> new ConversationContext(userId)
);
context.addMessage("user", userMessage);
String promptWithHistory = buildPromptWithMemory(context);
String response = this.chatClient
.prompt()
.user(promptWithHistory)
.call()
.content();
context.addMessage("assistant", response);
return response;
}
private String buildPromptWithMemory(ConversationContext context) {
StringBuilder prompt = new StringBuilder();
prompt.append("You are a helpful AI assistant.\n\n");
prompt.append("Conversation history:\n");
for (Message msg : context.getRecentMessages(10)) {
prompt.append(String.format("[%s]: %s\n", msg.role, msg.content));
}
prompt.append("\nContinue the conversation:");
return prompt.toString();
}
}
Persistent Storage
For production, store conversations in a database:
@Entity
@Table(name = "conversations")
public class Conversation {
@Id
private String id;
private String userId;
@OneToMany(cascade = CascadeType.ALL)
private List<ConversationMessage> messages;
private Long createdAt;
}
Best Practices
- Limit history size to control token usage
- Summarize long conversations to prevent context overflow
- Implement TTL (time-to-live) for auto-cleanup
- Allow users to clear their conversation history
- Monitor conversation lengths and costs
Conclusion
Conversational memory transforms Spring AI into a stateful assistant. Implement memory early, respect privacy, and monitor token consumption to build conversational experiences users love.
Top comments (0)