What if your AI agent could remember not just the current conversation, but facts about users across sessions? A few months ago, I built a custom three-tier memory system with PostgreSQL, Testcontainers, and AI-powered summarization. It worked, but required significant code and infrastructure.
Now there’s a simpler way. The Spring AI Amazon Bedrock AgentCore starter I’ve been contributing to integrates with Amazon Bedrock AgentCore Memory - a fully managed service that handles both short-term conversation history and long-term knowledge extraction for you.
In this post, I’ll show you how to build an AI agent with persistent memory in under 50 lines of Java using JBang - no Maven project setup required. Your agent will remember previous messages and automatically extract facts from conversations - all without managing a database.
Why Amazon Bedrock AgentCore Memory?
AI models are stateless - ask “What’s my name?” after introducing yourself, and the model has no idea. AgentCore Memory solves this with:
- Fully Managed: AWS handles storage, extraction, and retention. No Redis or PostgreSQL to configure for memory.
- Two Memory Types: Short-term for conversation context, long-term for extracted facts and preferences.
- Auto-Extraction: Long-term memory runs asynchronously - it automatically extracts semantic facts, user preferences, and summaries from conversations.
Short-Term Memory (STM):
- Stores raw conversation events within sessions
- Maintains context for multi-turn conversations
- Enables conversation continuation even after service restarts
Long-Term Memory (LTM) Strategies:
| Strategy | Purpose | Example |
|---|---|---|
| Semantic | Extracts factual information | “User lives in Berlin” |
| UserPreference | Identifies preferences and choices | “Prefers eco-friendly travel” |
| Summary | Generates conversation summaries | “Discussed trip planning to Vegas” |
| Episodic | Captures meaningful interaction slices | “User booked flight on March 15” |
Custom vs Managed: A Comparison
Here’s what I had to build for custom memory vs what Amazon Bedrock AgentCore Memory provides out of the box:
| Aspect | Custom Implementation | AgentCore Memory |
|---|---|---|
| Infrastructure | PostgreSQL + Testcontainers setup | None - fully managed |
| Session Memory | MessageWindowChatMemory + JDBC repository |
Auto-configured from environment |
| Long-Term Extraction | Custom ConversationSummaryService with AI prompts |
Automatic - runs asynchronously |
| Preferences Storage | Custom tier with manual extraction logic | Built-in UserPreference strategy |
| Summarization | Custom prompts parsing ===PREFERENCES=== sections |
Built-in Summary strategy |
| Code Required | ~300 lines across multiple services | ~50 lines total |
| Database Management | Schema migrations, connection pooling | None |
| Scaling | Manual PostgreSQL scaling | Managed by AWS |
The custom approach gives you full control and works without AWS dependencies. But if you’re already on AWS and want to move fast, Amazon Bedrock AgentCore Memory handles the infrastructure so you can focus on what your agent actually does.
Prerequisites
You’ll need:
- Java 21+ (Amazon Corretto 21)
- AWS CLI configured with Amazon Bedrock AgentCore access
- JBang installed
AWS Credentials
Configure AWS CLI with credentials that have Amazon Bedrock AgentCore access:
1 | aws configure |
Verify access:
1 | aws bedrock-agentcore-control list-memories --no-cli-pager --query 'memories[0].id' --output text |
Create the Memory Resource
Let’s create an Amazon Bedrock AgentCore Memory resource with LTM strategies. Copy and run in terminal:
1 | # Create memory resource |
This creates:
- Amazon Bedrock AgentCore Memory resource with 90-day event retention
- Semantic strategy for extracting facts from conversations
- UserPreference strategy for identifying user preferences
The memory resource takes 2-5 minutes to become active.
The Application
Here’s the complete AI agent with memory - under 50 lines of Java:
1 | cat <<'EOF' > MemoryAgent.java |
The JBang header declares dependencies, Spring AI auto-configures memory beans from environment variables, and the advisors handle both STM and LTM automatically.
Run the Application
Export the environment variables from the setup step, then run:
1 | export AGENTCORE_MEMORY_MEMORY_ID=${MEMORY_ID} |
JBang downloads dependencies on first run, then starts the Spring Boot application.
Test the Application
In another terminal, test conversation memory:
1 | # First message - introduce yourself |
Expected output:
1 | User: Hi, my name is Alex and I live in Berlin |
The agent remembers your name and location from the previous message. Long-term memory will extract “User’s name is Alex” and “User lives in Berlin” as semantic facts for future sessions.
Test Long-Term Memory
Wait a few minutes for LTM extraction, then start a new session:
1 | # New session - agent should remember facts from LTM |
The agent retrieves extracted facts from long-term memory.
Cleanup
1 | MEMORY_ID=$(aws bedrock-agentcore-control list-memories \ |
Going Further
This demo uses semantic and preference strategies. AgentCore Memory also supports Summary and Episodic strategies, custom namespaces, and retention policies.
Memory pairs well with RAG - memory handles personal context while RAG provides domain knowledge. Check out my post on Amazon Bedrock Knowledge Base to add RAG to the same agent.
Conclusion
One Java file, a few shell commands, and you have persistent memory with automatic fact extraction. No database to manage, no extraction pipelines to build. The same code works in any Maven or Gradle project - JBang just keeps the demo simple.
I’m excited to contribute to the Spring AI Amazon Bedrock AgentCore starter that makes this integration possible. The source is on GitHub if you want to see how it works.
Learn More
- Spring AI Amazon Bedrock AgentCore Starter
- Amazon Bedrock AgentCore Memory Documentation
- Custom Memory Implementation with PostgreSQL - My previous post on building memory from scratch




