AI Agent Memory Using Redis (Full Working Example – 2026)

AI Agent Memory Using Redis (Full Working Example – 2026)

Cloud Edventures

Cloud Edventures

about 1 month ago12 min

AI Agent Memory Using Redis (Full Working Example – 2026)

Most AI agents fail because they forget everything.

If you want your AI agent to remember previous conversations, tasks, or context, you need a memory layer.

Redis is one of the simplest and fastest ways to add persistent memory to an AI agent.

This guide gives you a full working example.


Why Use Redis for AI Agent Memory?

  • In-memory performance (very fast)
  • Simple key-value storage
  • Supports TTL (auto-expiring memory)
  • Easy to integrate with Python

Redis works well for short-term memory, session memory, and lightweight context storage.


Architecture Overview

  • AI Agent (Python)
  • Redis Server
  • Optional: Vector database for long-term memory

In this tutorial, we implement short-term conversational memory.


Step 1: Install Redis

Run locally using Docker:

docker run -d -p 6379:6379 --name redis redis

Or install Redis directly on your system.


Step 2: Install Python Dependencies

pip install redis openai

Step 3: Basic Redis Memory Implementation

import redis
import json

# Connect to Redis
r = redis.Redis(host='localhost', port=6379, decode_responses=True)

def save_memory(session_id, message):
key = f"memory:{session_id}"

existing = r.get(key)
if existing:
history = json.loads(existing)
else:
history = []

history.append(message)

r.set(key, json.dumps(history), ex=3600)  # 1 hour TTL

def get_memory(session_id):
key = f"memory:{session_id}"
data = r.get(key)
if data:
return json.loads(data)
return []

This stores conversation history per session.


Step 4: Integrate With AI Agent

def agent_response(session_id, user_input):
memory = get_memory(session_id)

context = "\n".join(memory)

prompt = f"""
Conversation history:
{context}

User: {user_input}
Assistant:
"""

# Call your LLM here
response = call_llm(prompt)

save_memory(session_id, f"User: {user_input}")
save_memory(session_id, f"Assistant: {response}")

return response

Now your agent remembers prior conversation context.


Optional: Storing Structured Memory

Instead of raw text, you can store structured memory:

memory_entry = {
"role": "user",
"content": user_input,
"timestamp": time.time()
}

This allows better filtering and retrieval.


Memory Expiry (TTL)

Redis allows automatic expiration:

  • Short-term memory → 1 hour TTL
  • Session memory → 24 hours
  • No expiry → persistent memory

This prevents uncontrolled memory growth.


Production Setup (AWS)

For production AI agents:

  • Use AWS ElastiCache (Redis)
  • Deploy agent via ECS or Fargate
  • Use environment variable for REDIS_URL
  • Restrict access via security groups

Never expose Redis publicly.


Common Mistakes

  • Not limiting memory size
  • Storing full conversation forever
  • No session separation
  • Blocking Redis on main thread

Keep memory bounded and structured.


Redis vs Vector Database

  • Redis → short-term memory, fast access
  • Vector DB → semantic long-term retrieval

Advanced agents combine both.


Final Thoughts

Adding Redis memory transforms a stateless AI agent into a contextual system.

This is one of the simplest upgrades that dramatically improves agent usefulness.

Production AI agents require memory, logging, monitoring, and scaling — not just prompts.

What did you think of this article?

42 people reacted to this article

Share this article

Cloud Edventures

Written by Cloud Edventures

View All Articles

Previous

No more articles

Next

No more articles