Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:CrewAIInc CrewAI Memory Subsystem

From Leeroopedia

Overview

Memory classes (short-term, long-term, entity) for accumulating execution history and agent learnings across crew runs provided by the CrewAI framework. This is a Pattern Doc covering the memory subsystem architecture.

Source

Interface

The base Memory class defines the common interface for all memory types:

class Memory(BaseModel):
    storage: Any
    embedder_config: EmbedderConfig | None = None

    def save(self, value: Any, metadata: dict | None = None) -> None:
        """Save a value to memory with optional metadata."""
        ...

    def search(
        self,
        query: str,
        limit: int = 5,
        score_threshold: float = 0.6,
    ) -> list[Any]:
        """Search memory by semantic similarity."""
        ...

Memory Types

Memory Type Storage Backend Scope Key Characteristics
ShortTermMemory ChromaDB (vector) Current session Stores recent interactions; semantic similarity search; cleared between sessions
LongTermMemory SQLite (relational) Cross-session Stores task-level insights; persisted to disk; searchable by task description
EntityMemory ChromaDB (vector) Cross-session Tracks entity relationships and facts; semantic retrieval; persisted across runs

ShortTermMemory

Uses vector-based storage (ChromaDB) to store recent interactions within the current session. Each interaction is embedded and stored with metadata including the agent role, task description, and timestamp. Search returns semantically similar past interactions.

# ShortTermMemory is initialized automatically when memory=True
# Internal usage by the crew execution engine:
short_term_memory.save(
    value="Research findings about AI trends in 2024...",
    metadata={
        "agent": "Senior Research Analyst",
        "task": "Conduct thorough research about AI",
    },
)

# Retrieval during task execution:
relevant_context = short_term_memory.search(
    query="What are the latest AI trends?",
    limit=5,
    score_threshold=0.6,
)

LongTermMemory

Uses SQLite for durable storage of task-level insights that persist across sessions. Unlike ShortTermMemory, LongTermMemory stores structured records about task performance rather than raw interaction content. This enables the crew to learn which approaches work well for different task types over time.

# LongTermMemory stores task performance insights
long_term_memory.save(
    value="Agent performed well when given structured output format",
    metadata={
        "task_description": "Write a comprehensive article",
        "quality_score": 8,
    },
)

# Search by task description for relevant past insights
insights = long_term_memory.search(
    query="Writing a comprehensive article about technology",
    limit=3,
)

EntityMemory

Uses vector-based storage to track relationships and facts about specific entities mentioned across executions. If the crew processes information about companies, people, or technologies, EntityMemory maintains structured knowledge about those entities for future reference.

# EntityMemory tracks entity-specific knowledge
entity_memory.save(
    value="OpenAI released GPT-4o in May 2024 with multimodal capabilities",
    metadata={
        "entity": "OpenAI",
        "entity_type": "organization",
    },
)

# Retrieve known facts about an entity
entity_facts = entity_memory.search(
    query="OpenAI recent releases",
    limit=5,
)

Import

from crewai.memory import ShortTermMemory, LongTermMemory, EntityMemory

Example

Automatic Memory Usage

The simplest way to use the memory subsystem is to set memory=True on the Crew. The crew automatically initializes, populates, and queries all three memory types during execution:

from crewai import Crew, Agent, Task, Process

researcher = Agent(
    role="Senior Research Analyst",
    goal="Uncover cutting-edge developments in {topic}",
    backstory="You are an expert research analyst.",
)

research_task = Task(
    description="Conduct thorough research about {topic}.",
    expected_output="A detailed research report with key findings.",
    agent=researcher,
)

# Memory is enabled automatically with memory=True
crew = Crew(
    agents=[researcher],
    tasks=[research_task],
    process=Process.sequential,
    memory=True,  # Enables ShortTermMemory, LongTermMemory, EntityMemory
)

# First execution: memory starts empty
result_1 = crew.kickoff(inputs={"topic": "AI"})

# Second execution: agents benefit from memories stored during first run
result_2 = crew.kickoff(inputs={"topic": "AI"})
# The researcher now has access to insights from the first execution,
# potentially producing richer and more consistent results.

Manual Memory Search

For advanced use cases, memory stores can be queried directly:

# Access memory stores after crew initialization
if crew.memory:
    # Search short-term memory for recent context
    recent = crew._short_term_memory.search(
        query="AI trends",
        limit=3,
    )

    # Search long-term memory for historical insights
    historical = crew._long_term_memory.search(
        query="research task performance",
        limit=5,
    )

    # Search entity memory for known facts
    entities = crew._entity_memory.search(
        query="OpenAI",
        limit=5,
    )

How Memory Integrates with Training

When used in conjunction with the Crew Train Method, the memory subsystem amplifies the effect of training iterations:

  1. During training: Each iteration stores interactions, insights, and entity knowledge in memory
  2. Across iterations: Later iterations retrieve relevant memories from earlier ones, providing agents with accumulated context
  3. After training: The persisted LongTermMemory and EntityMemory continue to benefit subsequent non-training executions

This creates a compound improvement effect where each execution contributes to a growing knowledge base that improves all future executions.

Principle

Principle:CrewAIInc_CrewAI_Iterative_Improvement

References

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment