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

# Context Fold for Every Agent

> How the gateway folds long Anthropic conversations for every governed agent without an x-mnemom-conversation-id header, how the owner opts out on the alignment card, and what changes for the agent.

The gateway's context fold replaces the older turns of a long conversation with a
gateway-written digest, so the session keeps fitting the model's context window.
The fold applies to every governed agent on the Anthropic route. Your client does
not need to send an `x-mnemom-conversation-id` header, and the agent does not need
to be launched with `mnemom agent`.

> [!NOTE]
>
> Availability follows the gateway cell rollout. The fold for agents that send no
> conversation header is controlled by a cell setting that is off until the cell
> turns it on. Check with your Mnemom contact, or watch the [changelog](https://docs.mnemom.ai/changelog.md),
> for when it reaches your cell.

## How the gateway identifies the conversation

The fold needs to know which requests belong to one conversation. The gateway uses
only signals that are already in the request, in this order:

1. **The header.** If the request carries `x-mnemom-conversation-id`, that id is used, as before.
2. **The session id of the client.** Claude Code and the Agent SDK send a session id, either in the
   `x-claude-code-session-id` header or inside `metadata.user_id` of the request body. The gateway
   uses it as the conversation.

The gateway does not match conversations by their message content for these agents: two end users
of one agent can send the same opening messages, and matching on content could join them into one
conversation.

A request with no header and no session id is not folded. It is forwarded unchanged.

The response tells you which signal the gateway used: the `X-Mnemom-Lineage-Source` header carries
the source name (`header`, `sdk_session` and so on), never the value itself, and is absent when no
conversation was identified. See [Response Headers](https://docs.mnemom.ai/api-reference/headers.md#the-canonical-set).

Compaction and auxiliary requests are never folded this way. These are the requests Claude Code
marks with `x-claude-code-request-class`, compaction prompts and side queries. The fold does not
rewrite the history a `/compact` summary is written from.

## On by default, with an owner opt-out

Once the rollout reaches your cell, the fold is **on by default** for every governed agent. The agent
owner can turn it off for that agent with `context_fold` on the agent's
[alignment card](https://docs.mnemom.ai/specifications/alignment-card-schema.md#context_fold):

```yaml theme={null}
context_fold:
  enabled: false
```

| Card value | Result |
| - | - |
| `context_fold` absent | Fold enabled |
| `context_fold: { enabled: true }` | Fold enabled |
| `context_fold: { enabled: false }` | Fold off, including for requests that send `x-mnemom-conversation-id` |
| `context_fold` present but malformed (`enabled` is not a boolean, or `context_fold` is not an object) | Fold off. The gateway never reads a value the owner wrote as consent. |

The opt-out wins over the cell setting. It is a card field, so it composes across scopes: if the
platform, an organization, a team or the agent sets `enabled: false`, the composed card is
`enabled: false`. An org or team template can switch the fold off for every agent under it, and an
agent's own `enabled: true` cannot turn it back on. See
[Card Composition](https://docs.mnemom.ai/concepts/card-composition.md).

The opt-out also applies when the client sends `x-mnemom-conversation-id`: an opted-out agent's
requests are not folded with or without the header. The header still identifies the conversation for
the features that use it, such as implicit goal mode. A fold choice made in the session configuration (`x-mnemom-session-config`, or
`--context-profile` and `--no-context` from `mnemom agent`) can turn the fold off for a session, and a
session that turns it off does no conversation-identification work. It cannot turn the fold back on
for an agent whose owner opted out. Until the rollout reaches your cell, the gateway does not read the
opt-out, and a request with the header folds as before. See
[Context folding](https://docs.mnemom.ai/gateway/agent.md#context-folding) for the profiles.

If the gateway cannot read the card when it checks the opt-out, it treats the agent as opted out for
that request, so the request is forwarded unfolded. The gateway caches the opt-out result for 60 seconds
per agent.

## What changes for the agent

* Older turns are replaced, in the request the gateway sends to the model, by a digest the gateway
  writes. The system prompt and tools are not folded, and the latest messages stay word for word.
  What a fold keeps is described under [What folding keeps](https://docs.mnemom.ai/gateway/agent.md#what-folding-keeps).
* Your client keeps its full transcript. The fold changes what the gateway forwards to the
  model, not what your client holds or shows you.
* Tool results and earlier work can drop out of the digest, so restate anything the agent needs that
  is not in recent turns.

## Limits

* **Anthropic route only.** The fold applies to `/v1/messages` on the Anthropic route. OpenAI and
  Gemini requests are not folded.
* **Fold only.** Identifying a conversation without a header does not turn on the other
  conversation-level features. Implicit goal mode and sealed contracts still need the conversation
  header or a `mnemom agent` session. See [Implicit goal mode](https://docs.mnemom.ai/gateway/implicit-goal-mode.md).
* **Large requests.** A request body over 32 MiB, or a transcript over 10,000 messages, is forwarded unchanged.
  The response says so in `x-mnemom-context`: `skipped=body_too_large;bytes=<n>`, or
  `skipped=too_many_items;bytes=<n>;items=<n>`.
* **Billing.** The model calls the fold makes are billed as `gateway.context.fold` when fold billing is enabled. See
  [Pricing](https://docs.mnemom.ai/pricing/overview.md).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.