Pi now runs through Context Mode: same Pi, with memory and skills

Agents8 min read

Thank you to the Pi team at earendil-works for an open, configurable agent, and thank you to the Pi community. Pi keeps its provider settings in its own models.json, so connecting it needs no plugin and no fork. Today one command points Pi at Context Mode. You keep Pi, your model and your own API key. Every request then gets Saving, Memory, Skills, Instructions and Recall. Install Pi the way its makers do it, then run the Context Mode command.

TL;DR

How to connect Pi to Context Mode

This post answers one question: how do you connect the Pi coding agent to Context Mode? Five steps. The first two are Pi's own. Pick a step to see the exact command and what it prints.

Step
$ npm install -g @earendil-works/pi-coding-agent
# or: curl -fsSL https://pi.dev/install.sh | sh

Pi's own installer, from earendil-works/pi.

$ pi
# in Pi, type /login and add your Anthropic or OpenAI API key
# or: export ANTHROPIC_API_KEY=...
# or: export OPENAI_API_KEY=...

Pi sends your own provider key. Context Mode never writes it.

$ npx @context-mode/cli setup --agent pi
Pi uses your own provider key: an Anthropic or OpenAI API key, stored with Pi's own /login or set as ANTHROPIC_API_KEY / OPENAI_API_KEY. Setup writes no key.
Pi: ~/.pi/agent/models.json
  set providers.anthropic.baseUrl = https://gateway.contextmode.ai
  set providers.anthropic.headers.x-cm-device-key
  set providers.anthropic.headers.x-cm-client
  set providers.anthropic.compat.sendSessionAffinityHeaders
  set providers.openai.baseUrl = https://gateway.contextmode.ai/v1
  set providers.openai.headers.x-cm-device-key
  set providers.openai.headers.x-cm-client
Write these changes? y
Backup of the original: models.json.cm-setup-bak-<timestamp>
Set baseUrl, headers and compat of Pi's anthropic and openai providers: Pi's own documented models.json keys.

It shows each key, asks first, makes a backup, and never prints your key. Your terminal shows the full path of your home folder.

# quit Pi, then
$ pi

Pi reads models.json at start, so a running session keeps the old route.

$ npx @context-mode/cli setup --agent pi --check
Pi: configured (~/.pi/agent/models.json)
$ npx @context-mode/cli doctor

Then ask Pi anything. Your request shows on the console as Pi within a minute.

1. Install Pi its own way

Use the command Pi's makers give in their repository. Pick one:

npm install -g @earendil-works/pi-coding-agent     # or: curl -fsSL https://pi.dev/install.sh | sh

2. Give Pi your own API key

Pi needs your own provider key: an Anthropic or OpenAI API key. Store it with Pi's own /login, or set ANTHROPIC_API_KEY / OPENAI_API_KEY. Context Mode setup never writes this key. Pi sends it, and the gateway passes it on to the provider.

3. Run the Context Mode command

npx @context-mode/cli setup --agent pi

It needs Node 20.12 or later. If this terminal is not signed in yet, run npx @context-mode/cli first: it signs you in and gives this terminal a device key (cmdk_…), stored in ~/.context-mode/credentials.json. The gateway removes the device key before a request goes to the provider.

4. Quit Pi and run it again

Pi reads models.json at start. Quit it, run pi again, and ask it anything. Your request shows on the console as Pi.

5. Check it

npx @context-mode/cli setup --agent pi --check tells you if Pi is set up, and writes nothing. npx @context-mode/cli doctor checks the config, the gateway and the device key. npx @context-mode/cli status shows who is signed in, and each agent's setup.

What setup writes in Pi's models.json

Setup changes only Pi's own ~/.pi/agent/models.json (or $PI_CODING_AGENT_DIR/models.json). Comments in the file stay. These are the keys it owns:

{ "providers": {
  "anthropic": { "baseUrl": "https://gateway.contextmode.ai",
                 "headers": { "x-cm-device-key": "<your device key>", "x-cm-client": "pi" },
                 "compat": { "sendSessionAffinityHeaders": true } },
  "openai":    { "baseUrl": "https://gateway.contextmode.ai/v1",
                 "headers": { "x-cm-device-key": "<your device key>", "x-cm-client": "pi" } }
} }

The Anthropic base URL has no /v1, because Pi adds it. Every other provider, model and key in the file stays. Setup writes no API key, and does not touch auth.json or settings.json.

How setup behaves:

Or connect Pi with your other agents in one command

Do you use more than one coding agent? Run the CLI with no arguments. It signs you in, lists the apps it finds (found ones checked), and sets up the ones you pick. Pi is in that list. It is not part of --agent all, which covers Claude Code and Codex.

$ npx @context-mode/cli
# pick Pi (and any other app) in the list, press Enter
Pi           connected (workspace …)
Console: https://console.contextmode.ai
Ready

Next:
  1. Restart Pi: quit it and run `pi` again (it reads models.json at start).
  2. Ask it anything. Now and then a line that starts with "⚡ context-mode" shows under the answer.
  3. Check it worked: your request shows on https://console.contextmode.ai within a minute.
  Not there, or an error in the agent? Run: npx @context-mode/cli doctor

The CLI's after-setup text still says "⚡ context-mode". The line itself now starts with "⏺ contextmode ·". Each app gets one check request through the gateway before you see Ready. The same picker covers Claude Code, Claude Desktop, Codex, Codex Desktop, T3 Code, OpenCode and Pi. Read one gateway for seven coding agents for the full list.

Does Pi work with a gateway?

Yes. Pi lets you set a baseUrl and headers for each provider in its own models.json (Pi's models.json docs). Context Mode uses only those keys. There is no plugin, no wrapper and no fork. We checked it on Pi 1.4.0, with the real pi binary: a request goes to POST /v1/messages?beta=true (Anthropic) or POST /v1/responses (OpenAI), with the device key and x-cm-client: pi, and the console names it Pi.

On the wire, Pi gets the same work as Claude Code and Codex. A test sends the same captured request as Pi and as Claude Code (Anthropic) or Codex (OpenAI), and the provider gets the same bytes each time. So the context cut and Recall run on Pi the same way they run on the others.

Which login works in Pi with Context Mode?

Setup checks which login Pi uses before it writes. Pick yours.

Your login
providers.anthropic → https://gateway.contextmode.ai
# Claude models in Pi go through Context Mode

Routed. Pi sends your key; setup never writes it.

providers.openai → https://gateway.contextmode.ai/v1
# OpenAI models in Pi go through Context Mode

Routed. Pi sends your key; setup never writes it.

Pi: your Anthropic login (auth.json) is a Claude subscription token. Anthropic does not allow a subscription to be used through another product, so Pi's Anthropic provider is left alone.
To route Claude models too, use an Anthropic API key (Pi's /login, pick API key, or ANTHROPIC_API_KEY) and run setup again.
Anthropic answers a Claude subscription used from another app with "Third-party apps now draw from your extra usage, not your plan limits" (400). That answer comes from Anthropic, not from contextmode. Use an Anthropic API key, or add extra usage at claude.ai/settings/usage.

Not routed. This is Anthropic's rule, not ours. Setup sets up only OpenAI and tells you why.

openai-codex # Pi's ChatGPT login: not touched

Setup does not touch Pi's ChatGPT login. Use an OpenAI API key to route OpenAI models.

What you get in Pi

The gateway does not switch features on or off by app. These run on every Pi request. We measure savings on Claude Code, not on Pi, so this post gives no savings number for Pi. Pick a feature to see what it does.

Feature
$ pi -p "run the tests and fix the failing one"
# the test run prints every result
# the model gets the failing test, its error and the summary line
# the passed results stay out of the context
⏺ contextmode · … one short line on what was kept out

Large tool output (logs, fetched pages, big files) stays out of the model's context. The "⏺ contextmode" line shows in pi -p. Works on every request; not measured on Pi.

Monday, in Pi:
"We use pnpm in this repo, not npm."
Thursday, a new Pi session in the same folder:
"Add the date library."
pnpm add ...

Rules, decisions and preferences come back in later sessions. Stored for Pi, by project folder. Not measured live on Pi.

You, in Pi:
"use release-notes for this sprint"
# the release-notes skill loads for this turn

Import skills once, for example from GitHub. They run from the gateway, so there is nothing to install on each machine. Works on every request; not measured on Pi.

// Console: Settings → Instructions
Keep answers short.
Never touch code or technical terms.

Write your rules once in the console. Every Pi request carries them, on every machine.

You, in Pi, late in a long session:
"what did we decide about the quota check this morning?"
# the agent reads the decision back from the archive

Long history is archived, not lost. The agent reads older work back when it needs it. Works on every request; not measured on Pi.

$ git push origin main
# Claude Code, Codex: blocked by your rule
# Pi: recorded in Activity; blocking not verified

Protect is enforced on Claude Code and Codex. On Pi, the gateway records the call in Activity; blocking is not verified, so do not count on it to stop a command in Pi.

FeatureIn PiMeasured on Pi
SavingYes, every requestNo (measured on Claude Code)
Thinking in CodeYes, every requestNo
MemoryYes, stored for PiNo
SkillsYes, every requestNo
InstructionsYes, every requestNo
Recall and SearchYes, every requestNo
Active ContextYes, every requestNo
ProtectRecorded only; enforcement not verifiedNo
Session resumeNoNot applicable
"⏺ contextmode" lineShows in pi -p; the interactive screen is not verifiedNo

Memory for Pi

A new Pi session does not know what you told an earlier one. With Context Mode, what you told Pi last week comes back: "use pnpm", "we deploy on Fridays only", "no default exports". Memory stores these for Pi, by the folder you run Pi in. It keeps each Pi session apart by the session id Pi sends. Setup turns on sendSessionAffinityHeaders for Pi's Anthropic provider, so Pi sends that id. When a value changes, the old one is closed, not deleted. Read how Memory works.

Say this plainly

How to undo it

npx @context-mode/cli disconnect --agent pi

The file goes back as it was. A backup comes first, and it works offline. Check any time with npx @context-mode/cli status: who is signed in, and each agent's setup.

Start now

Install Pi, add your API key, then run one command. Open the Console to see your workspace.

npm install -g @earendil-works/pi-coding-agent     # or: curl -fsSL https://pi.dev/install.sh | sh
npx @context-mode/cli setup --agent pi

The other apps, each with its makers' own install: Claude Desktop, the ChatGPT desktop app and T3 Code. Context Mode is under the Elastic License 2.0 (ELv2).

Get started free

Your coding agent costs less and remembers your project.

Start free: 1,000 requests, every feature, no card. After you sign in, the console gives you one command to connect your coding agent.

Read the docs