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.

Open the interactive lesson

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.

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

Try it yourself

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