Memory

CrewAI · Advanced · 12 min

Memory lets crews improve over repeated runs and keep track of facts without you passing everything by hand.

Memory stores useful information from runs so agents can recall it later, in the same run or the next one. Set memory=True on a crew to enable it with defaults.

CrewAI 1.x uses one unified `Memory` class. It replaced the older separate short-term, long-term and entity memories that older tutorials describe.

How it works:

  • When something is saved, an LLM decides where it belongs, how important it is and which categories apply.
  • Saved text is turned into embeddings and stored locally, by default with LanceDB (a small embedded vector database) under ./.crewai/memory.
  • Before a task, relevant memories are recalled and added to the prompt. Ranking combines similarity, recency and importance.

You can also use it directly:

memory = Memory()
memory.remember("We chose PostgreSQL for user data.", scope="/project/alpha")
matches = memory.recall("Which database did we pick?", limit=3)

Scopes are paths like folders (/project/alpha, /agent/writer). memory.scope("/agent/writer") gives a view limited to one branch, and forget(scope=...) deletes a branch.

Practical notes:

  • The default embedding model is from OpenAI and needs OPENAI_API_KEY, even if agents use another provider. Pass embedder={"provider": "ollama", ...} or another provider to change it.
  • Memory adds model calls and prompt text, so measure cost.
  • Clear it with crewai reset-memories -m (or -a for everything) when tests need a clean start.
  • Memory may hold user data: decide what you store and for how long.

Example

from crewai import Agent, Crew, Memory, Task

memory = Memory(embedder={"provider": "ollama",            # local embeddings
                          "config": {"model_name": "mxbai-embed-large"}})
memory.remember("Customer ACME prefers invoices in EUR.", scope="/customers/acme")

for m in memory.recall("What currency does ACME want?", limit=2):
    print(f"[{m.score:.2f}] {m.record.content}")
# [0.87] Customer ACME prefers invoices in EUR.

agent = Agent(role="Billing assistant", goal="Draft correct invoices",
              backstory="You follow customer preferences.")
task = Task(description="Draft an invoice note for ACME.", expected_output="2 sentences.",
            agent=agent)
crew = Crew(agents=[agent], tasks=[task], memory=memory)   # or memory=True
print(crew.kickoff().raw)

Remember

  • memory=True enables the unified Memory with defaults
  • remember, recall and forget work on hierarchical scopes
  • Default embeddings are OpenAI's; set embedder to change, reset with crewai reset-memories

Watch out for

  • Enabling memory in a crew that otherwise uses only a non-OpenAI provider and getting an API-key error from the default embedder.

Check yourself

  1. Why might memory=True fail on a machine with only an Anthropic key configured?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: docs.crewai.com/en/concepts/memory