For the complete documentation index, see llms.txt. Markdown versions of documentation pages are available by appending .md to the page URL.
Primary navigation

Reuse memory and history across sessions

Give your agents persistent memory and access to past sessions.

While your agents work, it’s useful for them to store and recall discoveries from past sessions (memory) and reference the messages in previous sessions (history). The Agents API provides first-class support for both memory and history. This guide will demonstrate how to use both to improve your agents’ performance and accuracy.

You can enable memory and history by configuring one or both of the memory and history tools for an agent. You can do this either when you create an agent, or when you create session (by overriding agent configuration settings for its tools).

Tool What the store contains What the agent can do
memory Editable text notes, each with a path and a revision Search, read, create, and update notes.
history Conversations from the sessions attached to the store Search past conversations and read the original messages.

Your application can manage notes directly, list sessions in a history store, and inspect what each tool retrieved.

If you’re new to the OpenAI API, start with the Agents API quickstart.

Quickstart

To use memory and history in your application, create a memory and history store using the REST API. You’ll need the generated UUIDs to attach these stores to agents and individual sessions.

Store type
history_store_id=$(curl -fsS https://api.openai.com/v1/agents/history_stores \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -H 'OpenAI-Beta: agents=v1' \
  --json '{}' | jq -er '.id')

Use the two IDs in tools when you create an agent.

curl -fsS https://api.openai.com/v1/agents \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -H 'OpenAI-Beta: agents=v1' \
  --json @- <<JSON
{
  "model": "gpt-6.2-astra",
  "tools": [
    { "type": "history", "store_id": "$history_store_id" },
    { "type": "memory", "store_id": "$memory_store_id" }
  ]
}
JSON

Pass the returned agent id as agent_id when you create a session. The session inherits both stores by default.

Attach stores to a session

In addition to configuring memory and history stores when you create an agent, you can also override them when you create a new session.

When you send POST /v1/agents/sessions, add the history tool to agent.tools. Set its store_id to the history store ID you just saved. This session records the traveler’s booking code so the agent can look it up later.

curl -sS --fail-with-body --no-buffer https://api.openai.com/v1/agents/sessions \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -H "OpenAI-Beta: agents=v1" \
  -H "Content-Type: application/json" \
  --data-binary @- <<JSON
{
  "agent": {
    "model": "gpt-6.2-astra",
    "tools": [
      { "type": "history", "store_id": "$history_store_id" },
      { "type": "programmatic_tool_calling", "enabled": false }
    ]
  },
  "environment": { "type": "none" },
  "stream": true,
  "input": "My Paris booking code is ZX721."
}
JSON

Save session.id from the agent.session.created event to identify this conversation in search results. Wait for agent.session.turn.completed before searching for the booking code in another session.

Reuse memory with a new history store

You can configure each tool independently.

  • Use an existing store. Set store_id to the ID of a store of that type in the same project.
  • Create a new store. Include the tool and omit store_id. The API creates a store when it creates the session.
  • Leave out a tool. Omit memory or history when the agent doesn’t need access to that source.

For this example, add a note about the user’s preferred units to your memory store. Pass memory_store_id as the memory tool’s store_id. Omit store_id on the history tool to start a separate history.

curl -sS --fail-with-body --no-buffer https://api.openai.com/v1/agents/sessions \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -H "OpenAI-Beta: agents=v1" \
  -H "Content-Type: application/json" \
  --data-binary @- <<JSON
{
  "agent": {
    "model": "gpt-6.2-astra",
    "tools": [
      { "type": "memory", "store_id": "$memory_store_id" },
      { "type": "history" },
      { "type": "programmatic_tool_calling", "enabled": false }
    ]
  },
  "environment": { "type": "none" },
  "stream": true,
  "input": "Which units should I use for measurements? Check my saved preferences."
}
JSON

When you receive agent.session.created, find the history entry in session.agent.tools. Save its store_id to reuse the new history store. Keep history_store_id set to the store with the booking message for the later example.

{ "type": "history", "store_id": "histstore_example" }

Memory tool responses can also include the deprecated alias memory_store_id. Use store_id in requests.

If you override a saved agent’s tools, include every tool you want to keep. Providing agent.tools replaces the saved list.

These sessions run without a sandbox and disable Programmatic Tool Calling so the agent calls the tools directly.

Manage memory notes

A memory store holds notes that both the agent and your application can edit. The agent can find notes by path or content. Your application uses the note’s id to retrieve, update, or delete it.

Set memory instructions

By default, the memory tool prompts the agent to consult relevant notes and save or update useful information. Set instructions on the tool in agent.tools to tell the agent which details to save and when to update them.

{
  "type": "memory",
  "store_id": "memstore_example",
  "instructions": "Save writing preferences only when the user asks you to remember them. Update the existing note when a preference changes."
}

Custom instructions replace the default memory guidance for this session. Include them again in any new session that needs the same guidance.

Create a note

To create a note, set memory_store_id to the ID of the store that should hold it. The path identifies the note within the store. The content field holds its text.

curl -sS --fail-with-body \
  "https://api.openai.com/v1/agents/memory_stores/$memory_store_id/notes" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -H "OpenAI-Beta: agents=v1" \
  -H "Content-Type: application/json" \
  -d '{"path": "style.md", "content": "Use metric units."}'

Save the returned id as note_id to read, edit, or delete the note.

List and read notes

If you know the path, list the notes and find the matching path in data. The list returns only metadata. While has_more is true, pass last_id as the after query parameter to get the next page.

curl -sS --fail-with-body \
  "https://api.openai.com/v1/agents/memory_stores/$memory_store_id/notes" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -H "OpenAI-Beta: agents=v1"
{
  "data": [{ "id": "note_example", "path": "style.md", "revision": 1 }],
  "last_id": "note_example",
  "has_more": false
}

Set note_id to the matching entry’s id and retrieve the note to read its text and current version number (revision).

curl -sS --fail-with-body \
  "https://api.openai.com/v1/agents/memory_stores/$memory_store_id/notes/$note_id" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -H "OpenAI-Beta: agents=v1"
{
  "id": "note_example",
  "path": "style.md",
  "content": "Use metric units.",
  "revision": 1
}

Update a note

When you update a note, use the revision from the note you just read. For the response above, set revision=1. Passing that value as expected_revision prevents the edit from overwriting a concurrent change.

curl -sS --fail-with-body \
  "https://api.openai.com/v1/agents/memory_stores/$memory_store_id/notes/$note_id" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -H "OpenAI-Beta: agents=v1" \
  -H "Content-Type: application/json" \
  --data-binary @- <<JSON
{
  "content": "Use metric units and Celsius.",
  "expected_revision": $revision
}
JSON

A successful edit returns the updated note. Use its new revision for the next edit or deletion.

If the API returns 409 or 503, read the note again and compare the stored content and revision with your intended change before retrying. An edit can take effect before the API returns 503.

Delete a note

Set revision to the latest value returned by the API. To delete the note, pass expected_revision as a query parameter.

curl -sS --fail-with-body -X DELETE \
  "https://api.openai.com/v1/agents/memory_stores/$memory_store_id/notes/$note_id?expected_revision=$revision" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -H "OpenAI-Beta: agents=v1"

Browse session history

Attach the history tool when you create each session whose messages you want the agent to search. Reuse the same history store ID to make those sessions searchable together. To limit a search to one conversation, include its session ID in input and ask the agent to use the session_id filter.

To browse the history in your application, set history_store_id to the ID on the history tool and list the sessions.

curl -sS --fail-with-body \
  "https://api.openai.com/v1/agents/history_stores/$history_store_id/sessions" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -H "OpenAI-Beta: agents=v1"

The list returns full session objects. While has_more is true, pass next as the page query parameter. Use a session’s id to retrieve its messages and tool calls.

Inspect what each tool retrieved

A single turn can use both tools. This example uses an application-written note and a message from an earlier session. Set memory_store_id and history_store_id to the IDs of the stores that hold those inputs.

Populate both stores with sample data

Write a note directly to the memory store.

curl -sS --fail-with-body \
  "https://api.openai.com/v1/agents/memory_stores/$memory_store_id/notes" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -H "OpenAI-Beta: agents=v1" \
  -H "Content-Type: application/json" \
  -d '{"path": "preferences.md", "content": "Prefers aisle seats."}'

Set history_store_id to the ID of the store with the booking message. If you skipped the earlier example, send the booking message in a session with history enabled.

Attach both stores to a new session.

curl -sS --fail-with-body --no-buffer https://api.openai.com/v1/agents/sessions \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -H "OpenAI-Beta: agents=v1" \
  -H "Content-Type: application/json" \
  --data-binary @- <<JSON
{
  "agent": {
    "model": "gpt-6.2-astra",
    "tools": [
      {
        "type": "memory",
        "store_id": "$memory_store_id",
        "instructions": "Read the user's saved seat preference. Leave the notes unchanged."
      },
      { "type": "history", "store_id": "$history_store_id" },
      { "type": "programmatic_tool_calling", "enabled": false }
    ]
  },
  "environment": { "type": "none" },
  "stream": true,
  "input": "Use my saved seat preference to pick 12A (window) or 12C (aisle). Find my Paris booking code in session history and include it in your answer."
}
JSON

Set session_id to the new session’s id from agent.session.created. After the turn completes, list the session’s items.

curl -sS --fail-with-body \
  "https://api.openai.com/v1/agents/sessions/$session_id/items?order=asc&limit=100" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -H "OpenAI-Beta: agents=v1"

Match each tool call (function_call) to its result (function_call_output) by call_id. Look for results with status set to completed. In this example, output[0].text contains each result as JSON.

Memory search result

The notes.search result groups matches by note path. Each pair in matches gives the line number and the text on that line.

{
  "results": [
    {
      "path": "preferences.md",
      "matches": [[1, "Prefers aisle seats."]],
      "match_count": 1
    }
  ],
  "returned_files": 1
}

History search result

The history.search result includes a hits array of message snippets. Results can include the current session. Check ref.session_id to identify the source conversation and ref.item_id to find the original message. This excerpt shows one hit from the earlier session.

{
  "ref": { "session_id": "sess_earlier", "item_id": "msg_example" },
  "snippet": "My Paris booking code is ZX721."
}

Final response

The agent used the note to choose between the options in input and quoted the code from the earlier message. The final agent.session.turn.output_text.done event contained this text.

{
  "text": "Pick **12C (aisle)** to match your saved preference. Your Paris booking code is **ZX721**."
}

Manage stores

For both lists, pass next as the page query parameter while has_more is true, even if data is empty.

To rename a store or change its metadata, send name, metadata, or both. If you send metadata, include every key you want to keep.

curl -sS --fail-with-body \
  "https://api.openai.com/v1/agents/history_stores/$history_store_id" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -H "OpenAI-Beta: agents=v1" \
  -H "Content-Type: application/json" \
  -d '{"metadata": {"team": "support", "region": "eu"}}'

Delete a history store

To delete a history store, set history_store_id to the store’s ID. The original sessions remain available by session ID. To remove their saved work, delete the sessions.

curl -sS --fail-with-body -X DELETE \
  "https://api.openai.com/v1/agents/history_stores/$history_store_id" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -H "OpenAI-Beta: agents=v1"