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

Carry context across sessions

Suppose a customer explains a design decision today. How will the agent know about it when the customer returns next month? The answer depends on whether you continue the same session or start a new one.

A session keeps its own conversation history. A new session can copy the same saved agent configuration, but it starts a separate conversation. To give the agent memory across sessions, your app stores information from one conversation and supplies it as context for another.

Step 1 of 11Continue the existing sessionHTTP request from Your app to OpenAI Agents API.
Your app
Sends a follow-up message to an idle session.
HTTP request
OpenAI Agents API
Starts another round of work, or turn, with the session’s history.

Continue the existing session

Request JSON · POST /v1/agents/sessions/sess_a/events

Choose where to keep a decision

When the detail matters What your app does
During the current conversation Send a follow-up to the same session. If the session is idle, the message starts another round of work, or turn, with the existing history.
Across sessions for the same user or customer Store the detail with its owner’s records. Check access before you include it in a new session’s initial input or let the agent call a function in your app to look it up.
As a rule for everyone who uses the agent Update the saved agent’s instructions with the full revised text. New sessions copy that text. Existing sessions keep their original instructions.

Example: Remember a customer’s export requirements

Herringbone defines two function tools for saving and reading integration notes. Its backend handles the function calls and uses the session ID to look up which customer and integration the agent is working on.

  1. Capture the decision. In session sess_a, the agent requests a call to save_integration_context. It passes one required string, note, with the text “Keep the nightly export columns named payment_id and amount. Finance reports look them up by name.”
  2. Store the note. Herringbone checks the user’s write access and saves the note and source session ID with Customer A’s integration records. It then returns the note ID as the function result.
  3. Use the note in a new review. Later, Herringbone reduces the number of payments per page and starts another review. It checks read access and includes the note with the task in a new session, sess_a_next. The agent can now check that the export still fetches every page and preserves both column names. If it needs more context, it can request a call to get_integration_context.

This Python example defines just the save_integration_context tool. Herringbone reads the note directly from its database and sends it with the next review request. That’s how the second session gets the column names.

Carry Customer A’s export requirements into a new session

Here, agents is OpenAI().beta.agents. The agent supplies only the note. Herringbone uses session_owners to look up which customer and integration the session belongs to. The Python SDK calls the function in tool_handlers and returns the note ID with the matching turn and call IDs.

Save the requirement in Customer A’s records
def record_decision(db, session_id):
    def save_integration_context(note):
        owner = session_owners[session_id]
        if "write" not in permissions.get(owner, set()):
            raise PermissionError("Cannot write notes for this integration.")
        if not isinstance(note, str) or not note.strip():
            raise ValueError("Expected a nonempty note.")

        row = db.execute(
            "INSERT INTO notes (customer, integration, source, note) VALUES (?, ?, ?, ?)",
            (*owner, session_id, note),
        )
        db.commit()
        return {"note_id": row.lastrowid}

    with agents.sessions.stream(
        session_id,
        input="""Remember this for future reviews of our nightly export:
Keep both column names, payment_id and amount. Finance looks them up by name.""",
        tool_handlers={
            "save_integration_context": lambda args: save_integration_context(**args)
        },
    ) as stream:
        stream.get_final_result()

Herringbone can keep a short set of notes for each integration in a database or a Markdown file. For a larger collection, a vector store indexes files so the backend can search them by meaning. A query about renaming fields could find the note about column names. The backend limits the search to the customer’s store or filters files by the authorized customer’s ID.

The column names matter only to Customer A. A rule like “Include the name and result of each test you ran” applies to every integration review, so Herringbone adds it to the shared agent instructions.

Ask your coding agent
Define save_integration_context with one required string, note, and get_integration_context with no arguments. Look up the customer and integration by session ID. Check the user's permissions before each read or write. Store each note with its source session ID. Test that a new session for the same integration can retrieve the note and another customer's session cannot. Show how to update the saved agent's instructions for a rule that applies to all customers. Read this lesson for context: https://developers.openai.com/api/training/agents-api/6-carry-context-across-sessions.md