Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017ic4b4RzcCS5JCAgcxLz7T
3.1 KiB
long-term-memory
Long-term memory for a Spring AI agent, three ways, on the same travel-desk agent: file memory
(AutoMemoryTools / AutoMemoryToolsAdvisor from spring-ai-agent-utils), vector memory (facts
extracted with entity(), kept in a PostgreSQL table and indexed in pgvector) and summary memory
(one rolling profile per user). The chat model is a hand-written ScriptedChatModel, not a language
model. The embeddings (all-MiniLM-L6-v2, in-process), PostgreSQL, pgvector, the files and Spring AI
are real. Nothing here measures how well a real model decides what to remember.
Companion code for Long-Term Memory for Spring AI Agents
on ankurm.com. Short-term (session) memory is the session-memory/ module.
Versions
| Component | Version |
|---|---|
| Spring Boot / Spring AI | 4.2.0 / 2.1.0 |
| spring-ai-agent-utils | 0.14.0 |
| Java | 25 (Temurin 25.0.5.1) |
| PostgreSQL / pgvector | 17.12 / 0.8.7 (pgvector/pgvector:pg17) |
| Embeddings | all-MiniLM-L6-v2, 384 dimensions, ONNX |
Run it
export JAVA_HOME=/path/to/jdk-25
./scripts/services-up.sh # Docker: PostgreSQL + pgvector on 127.0.0.1:5522
./scripts/run-all.sh # 12 tests, regenerate output/
No API key. The first run downloads the embedding model (about 90 MB) from Hugging Face. Without
PostgreSQL the tests that need it are skipped and only output/01-04 are rewritten. File tests use
/tmp/ltm-memory/. output/04 is timing dependent; token counts are cl100k_base estimates.
Code
| File | What it is |
|---|---|
Agents.java |
The one agent; memory is a per-request advisor |
FactExtractor.java |
Write path: entity() into UPSERT / FORGET changes |
PrivacyGate.java |
Category allow-list and a few secret shapes, in code |
JdbcFactStore.java |
System of record: upsert, expiry, forget, erase user |
FactIndex.java |
pgvector index with ids derived from (user, key) |
MemoryBlockAdvisor.java |
Read path: recalled text into the system message |
SummaryStore.java, SummaryUpdater.java |
Summary memory with a version column |
PromptCost.java |
Token estimate of a recorded Prompt |
Output
| File | Test |
|---|---|
01-04 |
FileMemoryTest |
05-10 |
FactMemoryTest |
11-13 |
SummaryAndCostTest |
Not covered
A live LLM; AutoDreamService consolidation (present in agent-utils 0.14.0, not run); encryption at
rest; multi-node file storage; hosted memory services.