Skip to content

The Brain upgrade: Memory and Knowledge

By default, Large Language Models are stateless—they don’t remember what you said five minutes ago once the context window closes. For an agent to be truly effective, it needs context. CrewAI provides two major systems to handle this: Memory (for recalling interactions) and Knowledge (for grounding answers in data).

CrewAI implements a sophisticated memory system capable of storing and retrieving information across different execution runs. It is categorized into three types:

  • Short-Term Memory: Uses RAG (Retrieval-Augmented Generation) to store the immediate context of the current execution flow. It helps agents keep track of what just happened.
  • Long-Term Memory: Stored in a local database (like SQLite/Chroma). This allows agents to learn from past executions. If an agent learned a user’s preference yesterday, it can recall it today.
  • Entity Memory: Focuses on remembering specific details about entities (people, places, concepts) to maintain consistency.

Enabling memory is simple. You just toggle the flag in your Crew configuration:

my_crew = Crew(
agents=[...],
tasks=[...],
memory=True, # This enables Short, Long, and Entity memory
verbose=True
)

While Memory is about interactions, Knowledge is about information ownership. You often need agents to answer questions based on your specific business data—PDFs, Spreadsheets, or internal documentation—rather than the LLM’s general training data.

CrewAI simplifies RAG (Retrieval-Augmented Generation) by treating data sources as “Knowledge” that can be attached to agents. The system handles the complex work of chunking text, creating embeddings, and performing vector searches.

You can import various source loaders from crewai.knowledge.source. Common sources include text strings, text files, PDF files, and Excel/CSV sheets.

from crewai import Agent, Crew, Task
from crewai.knowledge.source.string_knowledge_source import StringKnowledgeSource
from crewai.knowledge.source.text_file_knowledge_source import TextFileKnowledgeSource
# 1. Create Knowledge Sources
# A simple string source
content_source = StringKnowledgeSource(
content="CrewAI was created in 2024 to simplify multi-agent orchestration.",
)
# A file source (local PDF, TXT, etc.)
file_source = TextFileKnowledgeSource(
file_paths=["specs.txt", "user_manual.pdf"]
)
# 2. Assign Knowledge to an Agent
support_agent = Agent(
role="Support Specialist",
goal="Answer questions based strictly on provided manuals",
backstory="You are the first line of support.",
knowledge_sources=[content_source, file_source] # The agent now 'knows' this data
)
# 3. Create a task requiring that knowledge
task = Task(
description="When was CrewAI created according to your knowledge?",
expected_output="The creation date.",
agent=support_agent
)

By explicitly defining knowledge sources, you ensure that your agents provide accurate, domain-specific answers while reducing hallucinations.