AWS Builder Center

Agentic Memory Patterns: ElastiCache in the Age of AI Agents

The Core Problem: Agents Have Amnesia Traditional AI applications are stateless — every conversation starts from zero. But agentic AI (systems that take actions, use tools, and complete multi-step tasks) fundamentally requires memory to be useful. Github URL : https://github.com/aws-samples/amazon-elasticache-samples/tree/main/agentic-memory-with-valkey

Solving LLM Amnesia: Agentic Memory Patterns with Amazon ElastiCache for Valkey

Introduction — Your Agent Forgets Everything. Here's How to Fix It.

You've built an AI agent. It reasons well, uses tools effectively, and completes multi-step tasks. But every time a user comes back, it starts from zero. It asks the same questions. It re-fetches the same data. It has no idea who it's talking to.
This is LLM Amnesia — and it's the defining infrastructure challenge of the agentic AI era.
Agentic AI applications are fundamentally different from chatbots. They use external tools, APIs, and multi-step reasoning to complete complex tasks autonomously. But without persistent memory, they can't:
  • Deliver personalized experiences across sessions
  • Complete multi-step tasks that span hours or days
  • Avoid redundant tool calls that inflate latency and token costs
  • Learn from past interactions to improve future responses
The fix isn't in the model. It's in the memory infrastructure. And Amazon ElastiCache for Valkey is purpose-built for exactly this.



The 5 Memory Types Every Production Agent Needs

Before picking a storage layer, it helps to understand what agents actually need to remember. AWS has formally documented five distinct memory types for agentic applications:
Memory Type
What It Stores
Example
Short-term
Current session context, conversation flow, intermediate reasoning
"The user just asked about pricing — keep that in context"
Long-term
Cross-session preferences, past decisions, historical context
"This user prefers concise answers and works in fintech"
Episodic
Records of specific past interactions and events
"Last Tuesday, the agent resolved a billing issue for this user"
Semantic
General knowledge and facts extracted from interactions
"This user's company uses Kubernetes on AWS"
Procedural
How to perform tasks and use tools
"To check order status, call the Orders API with these parameters"
Most teams building agents today handle short-term memory (session context) and stop there. The result: agents that feel smart in a single conversation but dumb across sessions.
The goal is to cover all five — and do it without stitching together five different storage systems.



Why ElastiCache for Valkey Covers All 5 in One Store

Here's what makes ElastiCache for Valkey uniquely suited for agentic memory: it's not just a cache. It's a multi-model in-memory data store with native data structures that map directly to each memory type.

The Data Structure → Memory Pattern Map

Memory Type
ElastiCache Data Structure
How It Works
Short-term (session state)
Hashes
Store session metadata, current conversation state per user
Short-term (conversation history)
Lists
Ordered chat history with automatic TTL expiration
Short-term (tool result caching)
Strings + TTL
Cache tool outputs — avoid redundant API calls within a session
Long-term (cross-session)
Vectors (Valkey 8.2+)
Semantic retrieval of user preferences and past decisions
Episodic (past events)
Vectors
Vector search over stored conversation embeddings
Semantic (knowledge/facts)
Vectors (HNSW or FLAT index)
Similarity search over extracted knowledge
Procedural (tool know-how)
Hashes
Tool configurations and workflow definitions
Event logs
Streams
Ordered event history for audit and replay
A single ElastiCache for Valkey instance handles all of this. No separate vector database. No separate session store. No separate cache layer. One connection, one operational surface, all five memory types.

The 5 Capabilities That Make It Work

1. Sub-millisecond latency Agents make dozens of memory reads per reasoning step. At microsecond-level latency, memory lookups add zero perceptible delay to the user experience. This matters more than it sounds — latency compounds across multi-step reasoning chains.
2. Vector similarity search (Valkey 8.2+) Starting with Valkey 8.2, ElastiCache supports vector similarity search via the
 valkey-search 
module. This enables semantic memory retrieval — agents find relevant memories based on meaning, not just keyword match. A user asking "what did we discuss about my infrastructure?" retrieves the right memories even if the exact words don't match.
3. Real-time index updates New memories are immediately searchable after being written. This is critical for within-session recall — an agent that stores a fact at step 2 of a reasoning chain needs to retrieve it at step 5, within the same session.
4. Built-in memory lifecycle management TTL (time-to-live),
 allkeys-lru 
eviction policies, and atomic operations handle memory expiration automatically. Short-term memories expire. Long-term memories persist. No manual cleanup code.
5. Scales to millions of ops/sec ElastiCache handles hundreds of millions of operations per second with consistent low latency — supporting large numbers of concurrent users and agents without degradation.



The Reference Architecture

Here's the full stack for a production-grade memory-enabled agent:
┌──────────────────────────────────────────────────┐
│ Amazon Bedrock AgentCore Runtime │
│ (LLM hosting + embedding model access) │
└──────────────────────┬───────────────────────────┘
│
┌──────────────────────▼───────────────────────────┐
│ Agent Framework (Strands Agents) │
│ LLM invocations · tool execution · dialogue │
│ Supports: Bedrock, Anthropic, Gemini, OpenAI │
└──────────────────────┬───────────────────────────┘
│
┌──────────────────────▼───────────────────────────┐
│ Mem0 (Memory Orchestration) │
│ Extract → Store → Retrieve → Decay │
└──────────────────────┬───────────────────────────┘
│
┌──────────────────────▼───────────────────────────┐
│ Amazon ElastiCache for Valkey │
│ Vectors · Hashes · Lists · Strings · Streams │
│ Sub-millisecond reads · Semantic retrieval │
└──────────────────────────────────────────────────┘

Key components:
  • Amazon Bedrock AgentCore Runtime — hosts the agent and provides access to LLM and embedding models
  • Strands Agents — manages LLM invocations, tool execution, and user conversations (supports Bedrock, Anthropic, Gemini, OpenAI)
  • Mem0 — the memory orchestration layer; sits between the agent and ElastiCache, managing the full memory lifecycle from extraction to decay
  • Amazon ElastiCache for Valkey — the managed in-memory store; handles vector embeddings, session state, conversation history, and tool caching



The Performance Numbers

AWS has published measured benchmarks comparing a memory-enabled agent (ElastiCache-backed) versus a stateless agent:
Metric
Without Memory
With ElastiCache Memory
Improvement
Tool calls per request
3
0
✅ Eliminated redundant calls
Token usage
~70,000
~6,300
12× reduction
Response time
9.25 seconds
2 seconds
3× faster
Memory lookup latency
N/A
Sub-millisecond
Valkey in-memory
The 12× token reduction is the headline. Every redundant tool call and context re-establishment that memory eliminates directly reduces your LLM inference costs. At scale, this is the difference between a viable and an unviable agentic application.



Getting Started

Prerequisites

  • AWS account with Amazon Bedrock access (AgentCore Runtime + embedding models)
  • ElastiCache cluster running Valkey 8.2 or later (vector search requires 8.2+)
  • EC2 instance or compute resource in the same VPC as your ElastiCache cluster
  • Python 3.11+

Install dependencies

pip install strands-agents strands-agents-tools strands-agents-builder
pip install mem0ai "mem0ai[vector_stores]"


Configure Mem0 to use ElastiCache as the vector store

from mem0 import Memory

config = {
"vector_store": {
"provider": "redis",
"config": {
"redis_url": "redis://<your-elasticache-endpoint>:6379",
"embedding_model_dims": 1536, # match your embedding model
}
},
"embedder": {
"provider": "aws_bedrock",
"config": {
"model": "amazon.titan-embed-text-v2:0"
}
}
}

memory = Memory.from_config(config)


Add and retrieve memories

# Store a memory after an agent interaction
memory.add(
"User prefers concise technical answers and works in fintech",
user_id="user_123"
)

# Retrieve relevant memories before the next agent response
relevant_memories = memory.search(
query="What do I know about this user's preferences?",
user_id="user_123"
)


Multi-user isolation with TAG filters

When running agents for multiple users, always isolate memories using TAG-filtered vector search:
# TAG-filtered KNN search — isolation + performance in one atomic operation
results = client.execute_command(
"FT.SEARCH", "agent_memory",
f"@user_id:{{{user_id}}}=>[KNN 5 @embedding $query_vec]",
"PARAMS", "2", "query_vec", query_embedding,
"DIALECT", "2"
)




Best Practices

Memory lifecycle management

  • Short-term TTLs: 30 minutes to 24 hours for session context
  • Long-term TTLs: Longer or indefinite for user preferences
  • Memory decay: Use Mem0's built-in decay mechanisms to remove irrelevant memories and prevent bloat over time
  • Deduplication: Before storing a new memory, vector-search for similar existing ones — update rather than duplicate

Vector index configuration

  • FLAT index → under 100,000 memory entries (exact search, highest accuracy)
  • HNSW index → over 100,000 entries (approximate nearest neighbor, better performance at scale)
  • Always match embedding dimensions to your model (e.g., 1536 for Titan Embed v2)

Capacity planning

Each memory entry requires approximately 4–6 KB (embedding dimensions × 4 bytes + metadata). A 1 GB ElastiCache instance stores approximately 170,000–250,000 memory entries depending on embedding size.

Eviction policy

Set
 maxmemory-policy allkeys-lru 
on your cluster to automatically evict least-recently-used entries when memory pressure is reached. Monitor with Amazon CloudWatch — track memory utilization, cache hit rates, and vector search latency.



The Bigger Picture

The agentic AI wave isn't just about better models. It's about stateful infrastructure — systems that remember, learn, and improve across interactions.
ElastiCache for Valkey is uniquely positioned here because it collapses what would otherwise be a multi-service architecture (separate vector DB + session store + cache) into a single, operationally simple, sub-millisecond store. And with native integration into Amazon Bedrock AgentCore, it's already wired into AWS's flagship agentic platform.
If you're building agents today and your memory layer is an afterthought, the performance and cost data above should change that calculus.



Resources



Tags to apply when publishing:
 ElastiCache 
·
 Valkey 
·
 Agentic AI 
·
 Generative AI 
·
 Amazon Bedrock 
·
 Memory 
·
 LLM 
·
 Python 
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article