How do you give an AI agent memory (ReAct, version 6, memory.json)?
Version 5 ended
with a promise. The follow-up question "Is it available in Hong Kong
yet?" failed because the agent had no memory of the first task, and
the proper fix was to give it a memory of its own. Version 6 does exactly
that, with one small file: memory.json. (JSON is plain text
that stores data as labelled values, such as
{"question": "...", "answer": "..."}, so a program can read
one field at a time.)
The script
This is ReAct06.py. The ReAct loop from
version 4 is
unchanged; everything new is about memory. Three small functions read and
write a JSON file:
MEMORY_FILE = "memory.json" # long-term memory lives in this file
def load_memory():
"""Return the list of saved facts (empty if there is no file yet)."""
if not os.path.exists(MEMORY_FILE):
return []
with open(MEMORY_FILE, encoding="utf-8") as f:
return json.load(f)
def save_memory(question, answer):
"""Add one question + answer (with today's date) to memory.json."""
memory = load_memory()
memory.append({"date": today, "question": question, "answer": answer})
with open(MEMORY_FILE, "w", encoding="utf-8") as f:
json.dump(memory, f, indent=2, ensure_ascii=False)
def memory_text():
"""Turn the saved facts into text we can put in the prompt."""
memory = load_memory()
if not memory:
return "(empty)"
return "\n".join(f"- [{m['date']}] Q: {m['question']} A: {m['answer']}"
for m in memory)
The prompt inside perform_task gains one new section:
prompt = f"""Task: {task}
Today is {today}.
Memory (what you learned before, with the date you learned it):
{memory_text()}
You can use ONE tool: search[query] searches the web (Tavily) and returns the top results.
If your memory already answers the task, you may finish without searching.
Work in steps. Each step has a Thought, an Action, and an Observation."""
And when the agent finishes a task, the answer is saved before the
function returns. The script also wipes memory.json at the
start of each run, so every run begins with a clean slate:
if action.startswith("finish"):
found = re.search(r"finish\[(.*)\]", action, re.S)
answer = found.group(1) if found else action
print("=== Final Answer ===")
print(answer)
save_memory(task, answer) # long-term memory: keep it for next time
return answer
# Start each run with an EMPTY long-term memory (overwrites any old memory.json)
with open(MEMORY_FILE, "w", encoding="utf-8") as f:
json.dump([], f)
perform_task("When did Mark launch Muse")
perform_task("Is it available in Hong Kong yet?")
The one thing to see in the code
This script has two kinds of memory, and it is worth telling them apart:
-
Short-term memory is the growing
promptstring inside one task. It holds the Thoughts, Actions and Observations of the current question, and it disappears when the function returns. -
Long-term memory is
memory.json. It keeps finished answers after a task ends, so the next task — or a future run — can read them back.
Nothing here is magic. "Memory" is still just text: answers are appended to a file, and that file is pasted into the next prompt. The model itself still remembers nothing between calls.
One real run
--- Task 1: "When did Mark launch Muse" ---
=== Memory loaded into the prompt ===
(empty)
...three rounds of searching...
=== Final Answer ===
Muse (Meta's personal AI agent) launched in early September 2026; Mark Zuckerberg's Sept 20, 2026 post said it had "been out for a little over a week," pointing to a launch around early September 2026.
--- Task 2: "Is it available in Hong Kong yet?" ---
=== Memory loaded into the prompt ===
- [2026-10-09] Q: When did Mark launch Muse A: Muse (Meta's personal AI agent) launched in early September 2026; Mark Zuckerberg's Sept 20, 2026 post said it had "been out for a little over a week," pointing to a launch around early September 2026.
=== Round 1 - Thought ===
Thought 1: The task asks whether something is available in Hong Kong yet, but the memory only tells me about Muse's launch timing... I need to search for current information about Muse availability in Hong Kong.
=== Round 1 - Action chosen by the model ===
search[Muse Meta personal AI agent available in Hong Kong]
...launched September 8; 2.5 million downloads in two weeks...
=== Round 2 - Action chosen by the model ===
search[Muse Meta AI agent Hong Kong availability rollout regions]
...available in the United States and Canada only; no launch announced for Hong Kong, Taiwan, Singapore or any Asia-Pacific market...
=== Final Answer ===
No, Muse is not available in Hong Kong yet. As of October 2026, Meta Muse is only available in the United States (and possibly Canada), and no launch has been announced for Hong Kong or any other Asia-Pacific market.
Compare this with version 5. The same two calls, the same loop, the same model — but this time task 2 opens by reading the memory of task 1. The word "it" is no longer a mystery: the first line of the prompt says exactly what was asked and answered before. The agent resolves the reference for free, then spends its two searches on the real question and answers it correctly: not yet, and no date announced.
And here is memory.json after a real run — the whole of
the agent's long-term memory, readable in one glance:
[
{
"date": "2026-11-09",
"question": "When did Mark launch Muse",
"answer": "September 8, 2026"
},
{
"date": "2026-11-09",
"question": "Is it available in Hong Kong yet?",
"answer": "No — as of 2026-11-09, Muse does not appear to be available in Hong Kong yet. It launched on September 8, 2026 and was reported as US-only; Meta AI becoming available in Hong Kong on October 29, 2026 is separate from Muse."
}
]
Two entries, each stamped with the date it was learned. Notice the second answer: the agent even kept the two Hong Kong facts apart — Meta AI (the chatbot) arriving in Hong Kong is not the same thing as Muse (the agent) being available there.
Running it on your own computer
Download the demo program
(Folder-AI-ReAct06.zip). The zip holds three files:
ReAct06.py (the full script), pyproject.toml and
README.md.
- Open the folder in Visual Studio Code.
-
Create a file named
.envin the folder with two lines:
Keys: DeepSeek console and Tavily (both have free starting credit).DEEPSEEK_API_KEY=your-deepseek-key TAVILY_API_KEY=your-tavily-key - In the Terminal, run:
After the run, openuv sync uv run ReAct06.pymemory.jsonin the folder — you will see both answers saved there, dated.
Why this experiment matters
-
Memory is a design choice, not a feature of the model.
The model forgot everything between calls in version 5 and remembers in
version 6 — same model. The difference is fifteen lines of Python
for memory and one JSON file,
memory.json. - Saved answers make follow-up questions work. "It", "that one" and "the second option" are safe now, because the context travels in the prompt instead of being lost.
-
Memory you can inspect is better than memory you cannot.
memory.jsonis a plain text file: open it, read it, edit it, delete it. Knowing exactly what your agent remembers is the first step to trusting it.
Further reading
- ReAct: Synergizing Reasoning and Acting in Language Models — the paper that named the pattern this series builds.
- Tavily documentation — search options, free tier and pricing.
- DeepSeek API documentation — models, keys and pricing.