Memory

CrewAI ships a unified Memory class instead of separate short-term / long-term / entity / external types. On save, an LLM infers scope, categories, and importance. Recall ranks by a composite of semantic similarity, recency, and importance.

Four ways to use it: standalone, with Crews, with Agents, or inside Flows.


Standalone

from crewai import Memory

memory = Memory()
memory.remember("We decided to use PostgreSQL for the user database.")
matches = memory.recall("What database did we choose?")
for m in matches:
    print(f"[{m.score:.2f}] {m.record.content}")

memory.forget(scope="/project/old")
print(memory.tree())

Tune recency_weight, semantic_weight, importance_weight, recency_half_life_days. extract_memories(long_text) splits atomic facts for individual remember calls.

If you omit scope, the LLM hangs the memory on a filesystem-like path (/project/decisions, /agent/researcher, …). recall searches that branch — more precise and faster. You can also remember(..., scope="/research/databases").


With Crews

from crewai import Crew, Process, Memory

crew = Crew(
    agents=[researcher, writer],
    tasks=[research_task, writing_task],
    process=Process.sequential,
    memory=True,
    verbose=True,
)

memory = Memory(recency_weight=0.4, semantic_weight=0.4, importance_weight=0.2)
crew = Crew(
    agents=[researcher, writer],
    tasks=[research_task, writing_task],
    memory=memory,
)

memory=True creates a default Memory() and reuses the crew embedder. Without a custom embedder, memory defaults to OpenAI text-embedding-3-large (you need a working embeddings API). After each task, facts are extracted and stored; before the next task, relevant context is injected. Agents share crew memory unless they have their own.


With Agents (private scope)

from crewai import Agent, Memory

memory = Memory()
researcher = Agent(
    role="Researcher",
    goal="Find and analyze information",
    backstory="Expert researcher with attention to detail",
    memory=memory.scope("/agent/researcher"),
)

The researcher only sees /agent/researcher. A writer with no memory= uses the crew’s shared memory. Useful for private drafts vs public write-up.


With Flows

Every Flow has self.remember(), self.recall(), and self.extract_memories().

from crewai.flow.flow import Flow, listen, start

class ResearchFlow(Flow):
    @start()
    def gather_data(self):
        findings = "PostgreSQL handles 10k concurrent connections."
        self.remember(findings, scope="/research/databases")
        return findings

    @listen(gather_data)
    def write_report(self, findings):
        past = self.recall("database performance benchmarks")
        context = "\n".join(f"- {m.record.content}" for m in past)
        return f"Report:\nNew findings: {findings}\nPrevious context:\n{context}"

Put facts you will retrieve later in Memory. Put this-run orchestration variables (current topic, QA pass/fail) on Flow state. State is workflow variables; Memory is retrievable knowledge.


vs LangChain checkpoints

LangChain memory centers on thread_id + a checkpointer (persisting chat messages). CrewAI Memory centers on cross-task facts and a scope tree. Resume long workflows with Flow @persist or Crew checkpoint — do not replace a state machine with remember alone.

Embedding cost, privacy, and keys are the same as any RAG stack — see RAG. Do not remember secrets or raw PII.


Next steps

评论