learning-it by example

#237 Enterprise architecture and AI agents 5.8 min

Where Should AI Agent State and Memory Live? A Crash Test

I killed my agent in the middle of a write, then reloaded its memory. Three kinds of memory behaved in three different ways.

1. Why does an AI agent that runs for minutes or days need its state kept somewhere other than a list in the process?
2. In the crash test where an agent is killed in the middle of step 6, what did each of the three stores remember?
3. Why did the SQLite store keep all 5 finished steps when the agent was killed in step 6?
4. When two agents each read a shared JSON file and then both wrote it back, how many of the 2 items were kept, and was there an error?
5. In the Python agent-memory demo (process memory, JSON file, SQLite), which kind of store was only discussed and not run, although it is the usual choice when many machines share state?

Practice only: nothing is saved and no score counts towards anything. Each answer comes with the reason.

  1. Read what each store does

    Copy the code folder first. Open stores.py and read the three classes. For each one, write one sentence saying where the memory is kept and what happens to it when the process ends. Then run python compare.py and match each output line to the class that produced it.

    A hint

    MemoryStore keeps a list, JsonStore rewrites a whole file, SqliteStore wraps each step in a transaction.

  2. Crash earlier

    In worker.py, change crash=(step == 6) to crash=(step == 3). Predict what the SQLite line will say, then run python compare.py. When I ran it, the SQLite line said it remembers 2 of the 5 finished steps, and the text still says 5 because that wording is fixed in compare.py. The labels also still say step 6 for the same reason. Then change crash=(step == 3) back to crash=(step == 6) before the next task.

    A hint

    The agent now dies in step 3, so only steps 1 and 2 are finished.

  3. Never crash

    In worker.py, change crash=(step == 6) to crash=False. Predict what each of the three stores remembers, then run python compare.py. When I ran it, the memory store remembered 0, the JSON file remembered 6 and SQLite remembered 6. The printed wording still says the 5 finished steps and a crash in step 6, because it is fixed text.

    A hint

    The memory store lives inside the worker process, which has ended by the time compare.py reads it.

  4. Explain the lost item

    In compare.py, find the lines where agents a and b both call load before either calls write. Write two sentences saying why only one item is kept, using the idea that each agent rewrites the whole file from what it read earlier. Then say which line of the SQLite part makes the second agent wait or fail instead.

    A hint

    Look at seen_a, seen_b and the begin immediate line.

These tasks are for you to try on your own machine. Nothing is checked or saved.

What this does not show

A staged crash and an arranged collision on one laptop; the half-written JSON file is written that way on purpose; an atomic rename was not tested; Redis and server databases were not tested; the stores hold plain text only.

Up next in Enterprise architecture and AI agents

More in Enterprise architecture and AI agents

All Enterprise architecture and AI agents videos

Related videos