DEV Community

Cover image for How to Add Persistent Memory to CrewAI Agents
Arulnidhi Karunanidhi
Arulnidhi Karunanidhi

Posted on Originally published at aegismemory.com

How to Add Persistent Memory to CrewAI Agents

What is Aegis Memory?

Aegis Memory is an open-source memory layer built specifically for multi-agent AI systems. It solves the "amnesia problem" where AI agents forget everything between sessions.

Unlike built-in framework memory (which is session-only), Aegis provides:

  • Persistent storage that survives restarts
  • Semantic search across all memories
  • Scoped access control (private, shared, global)
  • Self-improvement patterns (ACE) for agents that learn over time

In this tutorial, you'll learn how to integrate Aegis Memory with CrewAI in under 10 minutes.


The Problem

You build a CrewAI agent. It works great. Then you restart it.

# Run 1
agent.chat("My name is Alex, I'm a Python developer")
# Agent: "Nice to meet you, Alex!"

# Run 2 (new session)
agent.chat("What's my name?")
# Agent: "I don't know your name."
Enter fullscreen mode Exit fullscreen mode

Everything your agent learned? Gone.

This isn't a bug, it's how LLMs work. Context windows reset. Sessions end. Memory disappears.

But multi-agent systems need persistent memory. Agents that remember users. Teams that share knowledge. Systems that learn from mistakes.


The Solution: Aegis Memory

Aegis Memory is an open-source memory layer for multi-agent systems. It gives your CrewAI agents:

  • Persistent memory that survives restarts
  • Semantic search (query by meaning, not keywords)
  • Scoped access (private, shared, or global memories)
  • Self-improvement through ACE patterns

Let's add it to a CrewAI project.


Step 1: Install

pip install aegis-memory crewai
Enter fullscreen mode Exit fullscreen mode

Start the Aegis server (requires Docker):

git clone https://github.com/quantifylabs/aegis-memory.git
cd aegis-memory
docker-compose up -d
Enter fullscreen mode Exit fullscreen mode

Step 2: Basic Integration

from aegis_memory import AegisClient
from crewai import Agent, Task, Crew

# Connect to Aegis
memory = AegisClient(
    base_url="http://localhost:8741",
    api_key="your-api-key"
)

# Store a memory
memory.add(
    content="User prefers concise responses and dark mode",
    agent_id="assistant",
    user_id="user_123",
    scope="agent-private"
)

# Query memories semantically
results = memory.query(
    query="What are the user's preferences?",
    agent_id="assistant",
    user_id="user_123",
    top_k=5
)

for r in results:
    print(r["content"])
# Output: "User prefers concise responses and dark mode"
Enter fullscreen mode Exit fullscreen mode

Step 3: CrewAI Integration

Here's a research crew with persistent memory:

from aegis_memory.integrations.crewai import AegisCrewMemory
from crewai import Agent, Task, Crew

# Initialize Aegis memory for CrewAI
memory = AegisCrewMemory(
    base_url="http://localhost:8741",
    api_key="your-api-key"
)

# Create agents with shared memory
researcher = Agent(
    role="Research Analyst",
    goal="Find accurate information on given topics",
    backstory="Expert at gathering and analyzing data",
    memory=memory,
    verbose=True
)

writer = Agent(
    role="Content Writer", 
    goal="Write clear, engaging content",
    backstory="Skilled at transforming research into readable content",
    memory=memory,  # Same memory instance = shared knowledge
    verbose=True
)

# Define tasks
research_task = Task(
    description="Research the latest trends in AI agents",
    agent=researcher,
    expected_output="Summary of key trends"
)

writing_task = Task(
    description="Write a blog post based on the research",
    agent=writer,
    expected_output="Draft blog post"
)

# Create and run the crew
crew = Crew(
    agents=[researcher, writer],
    tasks=[research_task, writing_task],
    memory=memory
)

result = crew.kickoff()
Enter fullscreen mode Exit fullscreen mode

What happens:

  1. Researcher finds information → stored in shared memory
  2. Writer queries memory → gets researcher's findings
  3. Next run → both agents remember what they learned

How Memory Scopes Work

Aegis provides three scopes:

How Aegis Memory scopes works

# Private: only the researcher can see this
memory.add(
    content="Internal analysis notes",
    agent_id="researcher",
    scope="agent-private"
)

# Shared: researcher and writer can see this
memory.add(
    content="Key finding: AI agent market growing 40% YoY",
    agent_id="researcher",
    scope="agent-shared",
    shared_with_agents=["writer"]
)

# Global: all agents can see this
memory.add(
    content="Company policy: always cite sources",
    agent_id="admin",
    scope="global"
)
Enter fullscreen mode Exit fullscreen mode

Why Not Just Use CrewAI's Built-in Memory?

Crew AI Built-in memory vs Aegis Memory

CrewAI's memory works for single sessions. Aegis works for production systems.


What's Next?

Once you have persistent memory working, you can add:

  • Memory voting: Track which memories help vs harm task completion
  • Reflections: Store lessons from failures
  • Playbooks: Query proven strategies before starting tasks

These are called ACE patterns — they help agents improve over time.


Resources


Built something cool with Aegis Memory? Please drop a comment I'd love to see :)

Top comments (0)