CrewAI Tutorial (Python): adding memory to agents for intermediate developers

By Cyprian AaronsUpdated 2026-04-21
crewaiadding-memory-to-agents-for-intermediate-developerspython

This tutorial shows you how to add memory to CrewAI agents in Python so they can retain useful context across tasks and conversations. You need this when a stateless agent keeps repeating questions, forgetting prior decisions, or losing customer-specific context between runs.

What You'll Need

  • Python 3.10+
  • crewai
  • python-dotenv
  • An LLM API key, such as:
    • OPENAI_API_KEY
  • Optional, but useful for persistence:
    • chromadb
    • embedchain if you want richer retrieval patterns later
  • A working CrewAI setup with at least one agent and one task

Step-by-Step

  1. Install the dependencies and set your API key.

    Memory only helps if your agent can call an LLM consistently, so start with the runtime and credentials first. I’m using OpenAI here because it’s the most straightforward path for CrewAI memory examples.

pip install crewai python-dotenv
export OPENAI_API_KEY="your-key-here"
  1. Create a simple CrewAI project with an agent and task.

    This baseline keeps the example clean before we add memory. The important part is that the agent already works without memory, so you can see exactly what changes.

from dotenv import load_dotenv
from crewai import Agent, Task, Crew, Process
from crewai.llm import LLM

load_dotenv()

llm = LLM(model="gpt-4o-mini")

agent = Agent(
    role="Client Support Analyst",
    goal="Answer questions using prior context when available",
    backstory="You help users with account and policy questions.",
    llm=llm,
    verbose=True,
)

task = Task(
    description="Remember that the user's name is Priya and her policy number is P-48291.",
    expected_output="A concise confirmation of the stored details.",
    agent=agent,
)

crew = Crew(
    agents=[agent],
    tasks=[task],
    process=Process.sequential,
)
  1. Enable short-term memory on the crew.

    Short-term memory helps the agent keep context during the current run and nearby interactions. This is the first thing you want when building assistants that must avoid asking the same question twice.

from crewai import Agent, Task, Crew, Process
from crewai.llm import LLM

llm = LLM(model="gpt-4o-mini")

agent = Agent(
    role="Client Support Analyst",
    goal="Answer questions using prior context when available",
    backstory="You help users with account and policy questions.",
    llm=llm,
    verbose=True,
)

task = Task(
    description="Store that the user's name is Priya and her policy number is P-48291.",
    expected_output="A concise confirmation of the stored details.",
    agent=agent,
)

crew = Crew(
    agents=[agent],
    tasks=[task],
    process=Process.sequential,
    memory=True,
)
  1. Add long-term memory for persistence across runs.

    Long-term memory is what makes this useful in production. If your app restarts or a user comes back tomorrow, the agent can still recover prior facts instead of starting from zero.

from crewai import Agent, Task, Crew, Process
from crewai.memory import LongTermMemory
from crewai.llm import LLM

llm = LLM(model="gpt-4o-mini")

agent = Agent(
    role="Client Support Analyst",
    goal="Answer questions using prior context when available",
    backstory="You help users with account and policy questions.",
    llm=llm,
)

task = Task(
    description="Recall prior user details and summarize them.",
    expected_output="A summary of remembered user details.",
    agent=agent,
)

crew = Crew(
    agents=[agent],
    tasks=[task],
    process=Process.sequential,
    memory=True,
)

crew.long_term_memory = LongTermMemory()
  1. Run two passes so you can see memory being reused.

    The first pass stores context. The second pass asks for it back; if memory is wired correctly, the response should reference earlier information instead of inventing a fresh answer.

first_result = crew.kickoff()
print("FIRST RUN:")
print(first_result)

follow_up_task = Task(
    description="What is the user's name and policy number from earlier?",
    expected_output="Return Priya and P-48291.",
    agent=agent,
)

follow_up_crew = Crew(
    agents=[agent],
    tasks=[follow_up_task],
    process=Process.sequential,
    memory=True,
)

second_result = follow_up_crew.kickoff()
print("\nSECOND RUN:")
print(second_result)

Testing It

Run the script once and confirm the first output includes the user details you asked it to store. Then run it again with a follow-up prompt that refers to those same details.

If short-term memory is working, the agent should stay consistent within the session. If long-term memory is working correctly in your environment, it should also recover those facts after a fresh run.

If you get inconsistent answers, check three things first: your model call succeeded, memory=True is set on the Crew, and your environment variables are loaded before instantiating the LLM.

Next Steps

  • Add entity or semantic retrieval so agents remember facts by topic instead of just raw conversation history.
  • Persist memory to a real backend like ChromaDB for multi-session applications.
  • Add guardrails around what gets stored so you don’t retain sensitive data you shouldn’t keep.

Keep learning

By Cyprian Aarons, AI Consultant at Topiax.

Want the complete 8-step roadmap?

Grab the free AI Agent Starter Kit — architecture templates, compliance checklists, and a 7-email deep-dive course.

Get the Starter Kit

Related Guides