Intermediate Crewai 3 min read

CrewAI Memory and Knowledge: Agents That Learn and Remember

#crewai #memory #knowledge #rag #vector-store #persistence #python

Why Memory Transforms Agent Quality

Without memory, every crew run starts from zero. Agents can’t learn from past mistakes, remember user preferences, or build on previous work. CrewAI’s memory system lets agents accumulate knowledge across runs, improving with every execution.

CrewAI provides four memory types and a separate Knowledge system — each solving a different problem.

The Four Memory Types

Short-Term Memory

Stored within a single crew run. Agents share context across tasks in the same execution:

from crewai import Crew, Process

crew = Crew(
    agents=[researcher, writer],
    tasks=[research_task, writing_task],
    memory=True,  # enables all memory types
    verbose=True,
)

With short-term memory enabled, when the writer agent runs, it has access to what the researcher discovered — even details not explicitly in the task output.

Long-Term Memory

Persists across runs using a local vector database. Agents remember outcomes from previous executions and use them to improve future decisions:

crew = Crew(
    agents=[researcher, writer, editor],
    tasks=[research_task, writing_task, editing_task],
    memory=True,
    verbose=True,
)

# First run — agents learn what worked
result1 = crew.kickoff(inputs={"topic": "LangChain"})

# Second run — agents use lessons from first run
result2 = crew.kickoff(inputs={"topic": "LlamaIndex"})
# The writer now "remembers" the editorial feedback from the first run

Long-term memory is stored in ~/.local/share/crewai/ by default (configurable).

Entity Memory

Tracks specific entities (people, organizations, products) mentioned across conversations, building a profile over time:

from crewai.memory import EntityMemory

# Entity memory is automatically extracted when memory=True
# Agents learn: "LangChain is a framework created by Harrison Chase"
# and reuse this fact in future runs without re-researching it

User Memory

Stores information about the user/operator for personalization:

from crewai.memory import UserMemory

user_memory = UserMemory()
# Automatically populated when agents learn user preferences

Configuring Memory Storage

By default, CrewAI uses an in-memory embedder. For production, configure a persistent vector store:

from crewai import Crew
from crewai.memory import LongTermMemory, ShortTermMemory, EntityMemory
from crewai.memory.storage.rag_storage import RAGStorage

# Custom storage configuration
short_term = ShortTermMemory(
    storage=RAGStorage(
        embedder_config={
            "provider": "openai",
            "config": {"model": "text-embedding-3-small"},
        },
        type="short_term",
    )
)

long_term = LongTermMemory()  # SQLite by default

crew = Crew(
    agents=[researcher, writer],
    tasks=[research_task, writing_task],
    memory=True,
    short_term_memory=short_term,
    long_term_memory=long_term,
    verbose=True,
)

The Knowledge System

Knowledge is different from memory. Memory is what agents accumulate over time. Knowledge is pre-loaded static information that agents always have access to — your company docs, a product catalog, technical specifications.

Text Knowledge Source

from crewai import Agent, Crew, Process, Task
from crewai.knowledge.source.string_knowledge_source import StringKnowledgeSource

# Define your knowledge base
company_knowledge = StringKnowledgeSource(
    content="""
    Acme Corp Product Catalog:
    - TaskFlow Pro: Project management SaaS, $29/user/month, 14-day free trial
    - DataPulse: Analytics dashboard, $49/month flat, integrates with Google Analytics
    - SupportHub: Customer support ticketing, $19/user/month

    Refund Policy: Full refund within 30 days, no questions asked.
    Support Hours: Mon-Fri 9am-6pm PST. Enterprise: 24/7.
    """,
    metadata={"source": "product_catalog", "version": "2026-Q1"},
)

File Knowledge Sources

from crewai.knowledge.source.pdf_knowledge_source import PDFKnowledgeSource
from crewai.knowledge.source.csv_knowledge_source import CSVKnowledgeSource
from crewai.knowledge.source.text_file_knowledge_source import TextFileKnowledgeSource

# Load from files
pdf_knowledge = PDFKnowledgeSource(file_paths=["docs/manual.pdf", "docs/faq.pdf"])
csv_knowledge = CSVKnowledgeSource(file_paths=["data/products.csv"])
text_knowledge = TextFileKnowledgeSource(file_paths=["docs/policies.txt"])

Attaching Knowledge to Agents and Crews

from crewai import Agent, Crew

# Agent-level knowledge (only this agent can access)
support_agent = Agent(
    role="Customer Support Specialist",
    goal="Answer customer questions accurately using the product catalog.",
    backstory="Expert in Acme Corp products with deep product knowledge.",
    knowledge_sources=[company_knowledge, pdf_knowledge],
    verbose=True,
)

# Crew-level knowledge (all agents can access)
crew = Crew(
    agents=[support_agent, escalation_agent],
    tasks=[handle_query_task, escalate_task],
    knowledge_sources=[company_knowledge],  # shared
    process=Process.sequential,
)

Complete Example: Knowledge-Powered Support Crew

from crewai import Agent, Task, Crew, Process
from crewai.knowledge.source.string_knowledge_source import StringKnowledgeSource
import os

os.environ["OPENAI_API_KEY"] = "sk-your-key"

# Company knowledge base
knowledge = StringKnowledgeSource(
    content="""
    Product: TaskFlow Pro
    Price: $29/user/month
    Features: Task management, Gantt charts, team collaboration, API access
    Limits: Free tier = 5 users, Pro = unlimited users
    Common Issues:
      - Login problems: Clear browser cache, check email for verification link
      - Slow performance: Check internet connection, try incognito mode
      - Missing data: Sync can take up to 5 minutes after changes
    """,
)

support_agent = Agent(
    role="Customer Support Specialist",
    goal="Resolve customer issues quickly and accurately.",
    backstory="You help customers with TaskFlow Pro. Use the knowledge base for accurate answers.",
    knowledge_sources=[knowledge],
    memory=True,
    verbose=True,
)

handle_task = Task(
    description=(
        "Handle this customer query: '{query}'\n"
        "Provide a complete, helpful response using product knowledge. "
        "If you can't resolve it, escalate with a clear summary."
    ),
    expected_output="A helpful response or escalation summary.",
    agent=support_agent,
)

crew = Crew(
    agents=[support_agent],
    tasks=[handle_task],
    memory=True,  # remember past interactions
    verbose=True,
)

# First query
result1 = crew.kickoff(inputs={"query": "I can't log in to my account."})
print("Response 1:", result1.raw)

# Second query — agent may remember context from first interaction
result2 = crew.kickoff(inputs={"query": "How many users can I have on the free plan?"})
print("Response 2:", result2.raw)

Resetting Memory

To start fresh (e.g., for testing):

# Reset all memory for a crew
crew.reset_memories(command_type="all")  # options: all, short, long, entities, knowledge

Or via CLI:

crewai reset-memories --all

Memory Storage Locations

Memory typeDefault storage
Short-termIn-process (lost on restart)
Long-term~/.local/share/crewai/[crew-name]/long_term_memory_storage.db
Entity~/.local/share/crewai/[crew-name]/entities.db
Knowledge~/.local/share/crewai/[crew-name]/knowledge/

Frequently Asked Questions

What is the difference between memory and knowledge in CrewAI?

Memory is dynamic — accumulated during and across crew runs (what agents learned). Knowledge is static — pre-loaded content that doesn’t change (your documentation, product catalog). Use memory for agents to improve over time; use knowledge to give agents specific domain expertise from the start.

Does long-term memory work across different crew instances?

Yes, as long as the crew has the same name and the storage path hasn’t changed. This allows different runs (even on different days) to benefit from accumulated learning.

How much does memory add to API costs?

Memory uses embeddings to store and retrieve information. Each memory insertion calls the embedding API. For text-embedding-3-small, this is extremely cheap (~$0.001 per 1M tokens). The bigger cost is the retrieved memory added to the LLM context — but this is usually small (100–500 tokens).

Can I use external vector databases for memory?

Yes. You can configure the RAGStorage to use Pinecone, Weaviate, or Chroma instead of the default local storage. This is important for multi-server deployments where local storage isn’t shared.

Will memory slow down my crew?

Short-term memory: minimal overhead (in-process). Long-term memory: adds ~200–500ms for the vector search. For most workflows, this is negligible. For latency-critical applications, consider disabling long-term memory and using only short-term.

Next Steps

Related Articles