Remembering the conversation
Run the agent twice and the second run knows nothing about the first. A session fixes that in one argument.
Everything so far has been a single question. The model is handed the instructions and the question, and when the run ends, all of it is gone.
One object, passed to both runs
session = SQLiteSession("asha")
await Runner.run(agent, "Hi, my name is Asha", session=session)
second = await Runner.run(agent, "What is my name?", session=session)await Runner.run(agent, "Hi, my name is Asha", session=session)
second = await Runner.run(agent, "What is my name?", session=session)
print("answer:", second.final_output)
print("remembered:", len(await session.get_items()), "items")The string you pass is the name of the conversation. The same name means carry on; a different one starts fresh with nothing remembered. That is how one program serves a thousand people without mixing them up.
What it is actually doing
Before each run it loads the stored items and puts them in front of your question. After each run it appends what happened. There is no magic and no summarising, which is exactly why it is worth understanding before you rely on it.
Four items after two turns: your two questions and the two answers. That number is the thing that grows, and lesson 20 is about what to do when it does.
Where it is stored
| What you write | Where it goes |
|---|---|
SQLiteSession("asha") | memory, gone when the program exits |
SQLiteSession("asha", "chat.db") | a file on disk, survives a restart |
| a session of your own | anywhere, by writing four methods |
- Change the session name on the second run and watch the memory disappear.
- Add a third turn and count the items.
- Print
await session.get_items()and read what is actually stored.
This is what real progress feels like.