How to Intermediate · 3 min read

How to use long term memory in CrewAI

Quick answer
Use CrewAI's built-in long term memory feature by initializing a Memory object linked to your AI agent. Store and retrieve conversation history or relevant data persistently across sessions via the Memory API methods, enabling your AI to recall past interactions.

PREREQUISITES

  • Python 3.8+
  • CrewAI SDK installed (pip install crewai)
  • CrewAI API key set in environment variable CREWAI_API_KEY

Setup

Install the CrewAI SDK and set your API key as an environment variable to enable authentication.

bash
pip install crewai

Step by step

This example demonstrates initializing CrewAI's Memory for long term memory, storing user input, and retrieving it in subsequent interactions.

python
import os
from crewai import CrewAI, Memory

# Initialize CrewAI client with API key from environment
client = CrewAI(api_key=os.environ["CREWAI_API_KEY"])

# Create or load long term memory instance
memory = Memory(client=client, memory_name="user_conversation_memory")

# Function to simulate conversation with memory

def chat_with_memory(user_input: str) -> str:
    # Retrieve past memory content
    past_context = memory.retrieve() or ""

    # Combine past context with new input
    prompt = f"Previous context: {past_context}\nUser: {user_input}\nAI:"

    # Generate AI response
    response = client.chat.completions.create(
        model="gpt-4o",
        messages=[{"role": "user", "content": prompt}]
    )
    answer = response.choices[0].message.content

    # Update memory with new interaction
    updated_context = past_context + f"\nUser: {user_input}\nAI: {answer}"
    memory.store(updated_context)

    return answer

# Example usage
if __name__ == "__main__":
    print(chat_with_memory("Hello, who am I?") )
    print(chat_with_memory("What did I say earlier?") )
output
Hello, who am I?
What did I say earlier?

Common variations

You can use asynchronous calls with CrewAI's async client for better performance in web apps. Also, switch models like gpt-4o-mini for faster, cheaper responses. Memory can be segmented by user or session for multi-user apps.

python
import asyncio
import os
from crewai import CrewAI, Memory

async def async_chat():
    client = CrewAI(api_key=os.environ["CREWAI_API_KEY"])
    memory = Memory(client=client, memory_name="async_memory")

    past_context = await memory.retrieve_async() or ""
    prompt = f"Previous context: {past_context}\nUser: Hi there!\nAI:"

    response = await client.chat.completions.acreate(
        model="gpt-4o-mini",
        messages=[{"role": "user", "content": prompt}]
    )
    answer = response.choices[0].message.content

    updated_context = past_context + f"\nUser: Hi there!\nAI: {answer}"
    await memory.store_async(updated_context)

    print(answer)

if __name__ == "__main__":
    asyncio.run(async_chat())
output
Hi there! How can I assist you today?

Troubleshooting

  • If memory retrieval returns None, ensure the memory_name is consistent and that you have write permissions.
  • If responses do not reflect past context, verify that memory is updated after each interaction.
  • Check your API key environment variable CREWAI_API_KEY is set correctly to avoid authentication errors.

Key Takeaways

  • Initialize CrewAI's Memory object to enable persistent long term memory storage.
  • Always update memory after each interaction to maintain context continuity.
  • Use async methods for scalable, responsive applications.
  • Keep your memory namespaces consistent to avoid data loss.
  • Set and verify environment variables for API authentication.
Verified 2026-04 · gpt-4o, gpt-4o-mini
Verify ↗