Home › Agentic AI with Bunty › Lesson 6
Memory: write it down, read it next time
Agentic AI with Bunty, lesson 6 of 6. simple English notes, free.
The idea
Last time Bunty planned Mummy's birthday party. A week later Raj said: "Bunty, order the cake." Bunty asked: "Sir, which cake?"
Raj had told him last time: Mummy loves chocolate cake. But Bunty forgot, because he had no memory. Every task started from zero.
So Raj gave Bunty a diary. Now Bunty writes down the important facts, and reads the diary before the next task.
- Write: "Mummy likes chocolate cake. Balloons can be ordered online."
- Next time, before starting, Bunty reads the diary.
- Result: "Sir, chocolate cake ordered! And the balloons online too."
The code: Bunty's diary in Python
An agent's memory can be as simple as a file. Bunty reads diary.json before the task and writes new facts into it. Save this as bunty_memory.py:
import json, os
DIARY = "diary.json"
def read_diary():
# Before any task, Bunty reads his diary
if os.path.exists(DIARY):
with open(DIARY) as f:
return json.load(f)
return {} # first day: the diary is empty
def write_diary(diary, key, value):
# Bunty writes an important fact down
diary[key] = value
with open(DIARY, "w") as f:
json.dump(diary, f)
def order_cake():
diary = read_diary()
if "mummy_cake" in diary:
print("Sir,", diary["mummy_cake"], "cake ordered!")
else:
print("Sir, which cake?")
write_diary(diary, "mummy_cake", "chocolate")
print("(Bunty writes: Mummy likes chocolate cake)")
order_cake()
Run it two times: python bunty_memory.py
--- first run (empty diary)
Sir, which cake?
(Bunty writes: Mummy likes chocolate cake)
--- second run (diary is read first)
Sir, chocolate cake ordered!
The program stopped after the first run, but diary.json stayed on disk: {"mummy_cake": "chocolate"}. That file is Bunty's memory. Real AI agents do the same thing with bigger "diaries" (files or databases), and they read them before every task.
Try it: change the code
- Add a second fact:
write_diary(diary, "balloons", "order online"), then print it on the next run. - Delete
diary.jsonand run again. What does Bunty forget?
Try it yourself
- Think of a helper (an app, a friend, an assistant). What one thing should it remember about you?
- Write 3 lines for Bunty's diary about a party you plan every year.
- What should Bunty NOT write in the diary? (Think: passwords, private things.)
Remember
An agent's memory: write the important things down, and read them next time. Next: Part 7, Bunty's team.
→ Bunty's team: Planner, Worker, Checker
← Planning: big goal, small steps
All lessons: Agentic AI with Bunty