OpenAI Agents SDKopenai-agents 0.22 · Python 3.10+
1
Curious builder0 XP earned · 300 to level 2
0 daysFinish a lesson to begin
Badge collection0 of 6 unlocked
25 small wins to finish your pathNext lesson

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

python
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)
Example
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 writeWhere 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 ownanywhere, by writing four methods
One argument from real
The in-memory one is for learning and for tests. Give it a filename and the same code keeps a real conversation across restarts, which is the smallest possible step from a demo to something usable.
Try it yourself
  • 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.