# pi-openai-long-context

Raise GPT-5.6+ models from pi's **272K** context window to **872K** with ChatGPT sign-in, or **1.05M** with an OpenAI API key.

It is off until you ask for it, because [it costs more](#what-it-costs).

https://github.com/user-attachments/assets/6844778e-da8f-4892-b9c9-05ed09f00190

## Install

```bash
pi install npm:pi-openai-long-context
```

Restart pi, or run `/reload`.

## Turn it on for one task

```text
/long-context
```

Run it again to turn it off. A `⚠` in the footer means it is on. The command only shows up in the `/` menu when you are on a supported model.

## Turn it on for good

Create/edit `~/.pi/agent/openai-long-context.json` (or the same file in your `PI_CODING_AGENT_DIR`):

```json
{
  "autoEnable": true,
  "autoEnableSubagents": true
}
```

| Flag | Turns long context on for |
| --- | --- |
| `autoEnable` | Your own pi sessions, and foreground children running inside them |
| `autoEnableSubagents` | [pi-subagents](https://github.com/nicobailon/pi-subagents) children in background runner processes |

Use one, the other, or both — they are independent. The split is by process, not by foreground or background: a child gets whichever flag applies to the process it runs in. Only the literal `true` counts; anything else (missing file, `false`, `"true"`, broken JSON) leaves that side off. pi never writes this file for you.

Opted-in sessions turn long context on at startup and again whenever you pick a supported model. `/long-context` still wins for the model you are on, until the next model switch or session.

To opt out, flip the flag to `false` or delete the file. The file is read when a session starts, so a running session keeps auto-arming until you `/reload` it (or start, resume, or fork one).

## It turns itself off

Without the config above, long context never outlives the moment: switching models, a new session, restarting pi, `/reload` — all of it drops you back to 272K, so you cannot leave it on and be billed for it a week later. Nothing is written to your settings.

Compaction is the exception: it keeps the big window, which is the whole point of turning it on. And if turning it *off* would immediately trigger automatic compaction, pi asks whether to compact or keep long context instead.

## What it costs

Past 272K input tokens, OpenAI bills the **whole request** at its long-context rate — see [OpenAI's pricing](https://platform.openai.com/docs/pricing). On a subscription, it burns through your quota faster too. That applies to every subagent child as well, and there can be a lot of those.

## Supported models and providers

Any GPT model from 5.6 onward — `gpt-5.6-*`, `gpt-6-*`, `gpt-6.1-*`, and later versions (including namespaced IDs such as `openai/gpt-5.6-*`) — on the `openai` and `openai-codex` providers. Other providers can be explicitly enabled with `additionalProviders` in `openai-long-context.json`; providers not listed there are left untouched.

To use an equivalent model through another provider, add it to `~/.pi/agent/openai-long-context.json`. A list gives each provider 872K; an object sets the window per provider:

```json
{
  "additionalProviders": { "openrouter": 1050000 }
}
```

This applies to manual toggles, the command menu, and sessions opted in with either auto-enable flag. Entries without a positive whole number are ignored. `openai` and `openai-codex` always use their own limits.

`openai-codex`, and `openai` signed in with ChatGPT, get 872K, the Codex maximum. `openai` with an API key gets 1.05M, the API maximum. If you switch logins mid-session, run `/long-context` off and on to pick up the new limit.

If you set your own context window for a supported model in `models.json`, turning this off restores *your* value, not pi's.

## Subagent details

Which children this reaches, and what to do when it misses one.

Nothing per launch is needed: no flag, no `/long-context`, no `extensionBindings`. An opted-in child raises its window at startup if the extension is loaded and its model is supported.

`autoEnableSubagents` keys off `PI_SUBAGENT_CHILD=1`, which marks the **runner process**, not one child's execution mode. So it also covers nested foreground children (`async: false`) running in that process. A foreground child started directly inside your own pi process has no marker, so it follows `autoEnable` like your session does.

Background children discover installed extensions on their own; foreground children need an explicit path. If a child does not pick this up, add this extension's `index.ts` to the agent's `extensions` or `subagentOnlyExtensions`. Extension-denying policies still apply.

Each child gets its own window. One child turning it off, switching models, or shutting down never touches its siblings.

## License

MIT
