New Tool Radar

AI Agents & Automation

How to Keep an Agent Short-Term Memory to the Current Chat

Keep an agent short-term memory to the current chat so it remembers this conversation without pulling in older ones.

Schematic with three steps: Give each conversation its own session id; Check the agent remembers within the chat; Check it does not remember across chats.

Short-term memory is the conversation you are in right now: the runner retrieves the session history and prepends it to each turn, so the agent remembers what was just said. In the OpenAI Agents SDK a session stores the history for one session id, and the runner adds new items after each run. Keep it scoped to that session: one session id per conversation, and do not reuse an id across conversations, or the agent will answer this chat with facts from the last one. When the chat ends, the session’s job is done.

What you need

  • An agent using SDK sessions for conversation history
  • A session id per conversation, not shared between users
  • Two test chats on different topics

Step 1: Give each conversation its own session id

Create a fresh session id when a conversation starts, and use it only for that conversation. A shared id mixes two chats into one memory.

Step 2: Check the agent remembers within the chat

Ask a question, then ask a follow-up that depends on the first answer, and confirm the agent uses the earlier turn. If it cannot, the session is not being passed to the run.

Step 3: Check it does not remember across chats

Start a second chat on a different topic with a new session id and ask a question that only the first chat could answer. The agent should say it does not know, not answer from the first chat.

Worked example: synthetic session check

The table below is a synthetic example. It shows the expected result of session scoping; no agent was run.

Check Expected Result
Follow-up uses earlier turn Yes Pass
Second chat recalls first chat No Pass
Session id is unique per chat Yes Pass
History is prepended each turn Yes Pass

Expected result (UNTESTED). the agent remembers within each chat and recalls nothing across chats, with a unique session id per conversation. This check was not run live; the table shows the expected result for the synthetic example.

Limits

  • A session stores the full history by default, so long chats grow the context on every turn.
  • An in-memory session is lost when the process ends; use a file-backed session for persistence.
  • A session cannot be combined with server-managed continuation in the same run.
  • Reusing a session id across users leaks one user’s history into another’s chat.

Sources

See all