Spring AI Recipe: Enabling Long-Term Memory
Memory is a fundamental concept in agentic systems. Without memory, every interaction starts from scratch, limiting how useful an agent can be over time.
In previous recipes, we’ve explored two types of memory in Spring AI:
- Short-term (conversational/episodic) memory, implemented with
MessageChatMemoryAdvisor, which retains context within a single conversation - Procedural memory, provided by skills, which give the agent domain-specific knowledge and guidance
There is a third type of memory: long-term (learned) memory.
NOTE: This recipe has been updated since it’s original publication to reflect changes in Spring AI 2.0.0.
What is Long-Term Memory?
Long-term memory is not about storing everything — it’s about storing the right things.
Specifically, long-term memory is made up of facts that the agent has learned and are:
- Significant
- Durable
For example:
- A user’s name is both significant and durable, so it should be remembered
- Today’s weather may be significant in the moment, but it is temporary and should not be retained
Spring AI enables this capability through AutoMemoryToolsAdvisor.
Adding Long-Term Memory
Start with a simple Spring AI application, such as the chat loop from the first recipe.
Add the Spring AI Agent Utils dependency to the build:
implementation 'org.springaicommunity:spring-ai-agent-utils:0.7.0'Configuring AutoMemoryToolsAdvisor
Use a ChatClientCustomizer to register the advisor:
@Bean
ChatClientCustomizer longTermMemoryCustomizer(
@Value("${agent.memories.dir}") String rootMemoriesDirectory) {
return builder -> {
builder.defaultAdvisors(
AutoMemoryToolsAdvisor.builder()
.memoriesRootDirectory(rootMemoriesDirectory)
.build());
};
}The root directory for memories is configurable:
agent.memories.dir=/Users/habuma/.agent/memoriesUsing a configuration property instead of hardcoding the path provides flexibility across environments.
AutoMemoryToolsAdvisor is an implementation of Spring AI’s advisor mechanism. This allows it to not only add the tool that manages long-term memory, but also augment the prompt with a system message that tells the LLM how to identify significant and durable facts.
Trying It Out
Run the application and tell the agent a few things about yourself:
> My name is Craig
> I have a dog named Moose
> I'm a Denver Nuggets fan
> I prefer cashews over almondsNow inspect the contents of the memory directory:
MEMORY.mduser_craig.mduser_pets.md
You’ll see that the agent has persisted key pieces of information in a handful of Markdown files. The actual files written will vary depending on what you ask or tell the agent. The content of each file is human-readable, but is intended and structured for agent readability.
Querying the Agent
Now ask the agent questions:
> Who am I?
> Who is Moose?
> What should I have for a snack?The agent will use its stored memory to respond appropriately.
Persistence Across Sessions
Because the memories are stored on the filesystem, they persist across application restarts.
This means the agent can retain knowledge over time, making interactions more personalized and effective.
What’s Happening Behind the Scenes
AutoMemoryToolsAdvisor leverages the LLM to:
- determine which facts are important and durable
- extract those facts from conversation
- persist them as structured Markdown files
- reintroduce them into future prompts
This creates a feedback loop where the agent continuously learns from interactions.
Conclusion
With long-term memory, your agent evolves beyond a stateless responder into something that can learn and adapt over time.
This is a key step toward building more capable and personalized agent systems.
Find the source code for this and other Spring AI recipes at https://github.com/habuma/spring-ai-recipes.