Connect two AI agents with shared memory so they pass context to each other
Set up a researcher and a writer with a single memory store - each agent sees the other's results and accumulates facts between runs
- 1.Open a terminal (Terminal on macOS, Command Prompt on Windows) and install CrewAI with the command below. Wait for the installation to finish.
Python 3.10+ is required. If Python is not installed, download it from python.org. If the installation fails, try adding --user at the end of the command.
CrewAI ↗About this toolPromptpip install crewai
What this prompt doesThis command installs the CrewAI framework for building multi-agent systems. No need to replace anything. - 2.Create a folder for your project and inside it create a file named crew_memory.py. Open the file in any text editor.
You can use VS Code, Sublime, or even Notepad.
- 3.Paste the following code into the file. Replace [YOUR_OPENAI_API_KEY] with your API key and [TOPIC] with the topic the agents will research.
You can get an API key at platform.openai.com. If the service is unavailable in your region, use a local model via Ollama: install Ollama, download a model (ollama pull llama3), and replace the API key line with os.environ['OPENAI_API_BASE'] = 'http://localhost:11434/v1'. Never publish your API key.
CrewAI ↗About this toolPromptimport os from crewai import Agent, Task, Crew, Process os.environ["OPENAI_API_KEY"] = "[YOUR_OPENAI_API_KEY]" researcher = Agent( role="Researcher", goal="Find 3 key facts about [TOPIC]", backstory="You are a thorough researcher who finds and verifies information.", verbose=True ) writer = Agent( role="Writer", goal="Write a clear 200-word summary based on the research", backstory="You are a skilled writer who turns research into readable content.", verbose=True ) research_task = Task( description="Research [TOPIC] and find 3 key facts.", expected_output="A list of 3 key facts about [TOPIC].", agent=researcher ) writing_task = Task( description="Using the research from the previous task, write a 200-word summary.", expected_output="A 200-word summary about [TOPIC].", agent=writer ) crew = Crew( agents=[researcher, writer], tasks=[research_task, writing_task], memory=True, process=Process.sequential, verbose=True ) result = crew.kickoff() print(result)What this prompt doesThe Python script creates two agents (Researcher and Writer) with shared memory. The memory=True parameter enables short-term memory (agents see each other's results), long-term memory (facts persist between runs), and entity memory. Replace [YOUR_OPENAI_API_KEY] with your key and [TOPIC] with your research topic, e.g. 'renewable energy trends 2026'. - 4.In the terminal, navigate to the folder containing the file and run the script.
Command to navigate: cd /path/to/folder. The run will take 1-3 minutes - agents work sequentially.
About this toolPromptpython crew_memory.py
What this prompt doesThis command runs the script. The first agent researches the topic, the second writes a summary based on the first agent's results. Thanks to memory=True, the second agent sees the first agent's output. - 5.After completion, check the memory/ folder - it is created automatically next to the script. It stores accumulated facts.
When you run the script again, the agents will use previously saved knowledge from this folder. To clear the memory, delete the memory/ folder.
CrewAI ↗About this tool
- freeCrewAICrewAI is an open-source framework, free with no restrictions.
- APIOpenAI APIPaid API, pay-per-use. New accounts sometimes receive free credits. Alternative: Ollama (local model, free).
AI services change fast - interfaces and free limits may differ from what's described.