> ## Documentation Index
> Fetch the complete documentation index at: https://docs.mengram.io/llms.txt
> Use this file to discover all available pages before exploring further.

# OpenAI Agents API

> Memory per end user for products built on the OpenAI Agents API and Agents SDK — one MCP connection per customer, scoped by a header.

## Why a second memory

OpenAI's Agents API (public beta, September 2026) runs agents on the Codex harness with durable sessions, auto-compaction and a sandbox memory. That memory is per **workspace**: summaries and notes in the sandbox, a small `memory_summary.md` injected at run start, keyword search on demand, and consolidation that keeps the newest notes and drops older ones.

A product with returning users needs memory per **user** — what this customer prefers, what was decided with them, what broke last time — that survives longer than the newest notes, and that is the same memory whether the agent runs on OpenAI, on Claude, or in your own code. Mengram is that memory, attached as an MCP server.

## One connection per end user

```python theme={null}
from agents import Agent, Runner
from agents.mcp import MCPServerStreamableHttp

async with MCPServerStreamableHttp(
    name="mengram",
    params={
        "url": "https://mengram.io/mcp",
        "headers": {
            "Authorization": "Bearer om-...",
            "X-Mengram-User": "cust_1042",   # everything on this connection is this user's
        },
        "timeout": 15,
    },
    cache_tools_list=True,
) as memory:
    agent = Agent(
        name="Support",
        instructions="Call context_for before answering; call remember with anything durable.",
        mcp_servers=[memory],
    )
    result = await Runner.run(agent, "hi, it's me again — same car as last time?")
```

Every `remember`, `recall` and `context_for` on that connection belongs to `cust_1042`, isolated from every other user of your product. The model never has to pass a `user_id` itself. There is no cap on the number of end users on any plan.

`X-Mengram-User` accepts letters, digits and `. _ : @ + -`, up to 200 characters; anything else falls back to `default`. When headers are not configurable, send `?user_id=cust_1042` on the URL instead.

## Hosted MCP tool

If OpenAI manages the connection (Responses API / Agents API JSON):

```json theme={null}
{
  "type": "mcp",
  "server_label": "mengram",
  "server_url": "https://mengram.io/mcp?user_id=cust_1042",
  "authorization": "Bearer om-...",
  "allowed_tools": ["context_for", "recall", "remember"],
  "require_approval": "never"
}
```

## Keep the context small

`context_for` takes `max_tokens` (default 1200). Memory is cut in rank order to fit and the pack ends with one line saying what was left out — see [max\_tokens](/api-reference#post-v1search). A support bot that used to replay 4,500 tokens of history per request sends the 600 that matter.

## Full example

[examples/openai-agents-api](https://github.com/alibaizhanov/mengram/tree/main/examples/openai-agents-api) — a support agent for a car-rental company, one command to run.
