CLI: connect Claude Code and Codex from your terminal

The Context Mode CLI signs your terminal in and points Claude Code and Codex at the gateway. Run it with npx, with nothing to install, and remove it again with one command.

Key facts

What it is
The npm package @context-mode/cli, run as npx @context-mode/cli. Seven commands: connect, login, setup, status, doctor, logout and disconnect.
Who it is for
Anyone who connects Claude Code or Codex to Context Mode Gateway, or takes them off it again.
Clients
Claude Code, in ~/.claude/settings.json, and Codex, in ~/.codex/config.toml.
Measured result
Not measured on its own. For the gateway as a whole, see Benchmarks.
Limits
Needs Node 20.12 or later. It edits config files for Claude Code and Codex only. It needs a Claude Code or Codex login on this machine to sign in.

Run it

You do not need to install anything. npx downloads the current version and runs it:

npx @context-mode/cli

This page describes version 0.4.0. To see the commands in your terminal, run npx @context-mode/cli --help.

To keep it installed, install it globally. The command is then context-mode-cli:

npm i -g @context-mode/cli
context-mode-cli status

The old name from version 0.1.0, context-mode-login, still works and runs the same program.

Commands

CommandWhat it does
npx @context-mode/cliSign in if needed, pick your agents, set them up, check them
loginSign this terminal in, in the browser
setupWrite the gateway settings into Claude Code and Codex
statusWho is signed in, and how each agent is set up
doctorCheck the gateway, the device key and each agent's config
logoutSign this terminal out and revoke its device key
disconnectTake Context Mode out of Claude Code and Codex

Each one runs as npx @context-mode/cli <command>. Add --help to any of them to print the list.

No command: connect

With no command, the CLI does the whole setup in one go:

  1. Signs you in, in the browser, when this terminal has no valid device key.
  2. Finds Claude Code and Codex on this machine.
  3. Asks which of them to connect. The ones it found are already ticked. Without a terminal to ask in, it takes the ones it found.
  4. Writes each one's config, with a backup of the old file.
  5. Sends one test request per agent through the gateway, prints one line for each, then the console address and Ready.
npx @context-mode/cli
npx @context-mode/cli --agent codex

Options: --agent claude|codex|all picks the agents without asking. --replace-base-url lets it replace a different base URL or Codex provider. --gateway URL uses another gateway.

login

Signs this terminal in. It opens a sign-in link in your browser and waits until you finish there. Then it stores this terminal's device key. In a terminal it then offers the same agent picker as connect. Otherwise it prints the next command to run.

To sign in it uses the login your agent already has: ANTHROPIC_API_KEY, ANTHROPIC_AUTH_TOKEN, CLAUDE_CODE_OAUTH_TOKEN, your Claude Code login, or else your Codex login. It sends that only to the gateway, and never prints it or writes it anywhere.

npx @context-mode/cli login
npx @context-mode/cli login --label work-laptop

Options: --label NAME names this terminal in the console. The default is the host name. --gateway URL uses another gateway.

setup

Writes the gateway settings, and the device key once you are signed in, into the config of Claude Code, Codex or both. It shows the change and asks before it writes. It makes no network call. See What setup writes.

npx @context-mode/cli setup --dry-run
npx @context-mode/cli setup --agent claude --yes
npx @context-mode/cli setup --check
OptionWhat it does
--agent claude|codex|allWhich agents. The default is the ones installed here. --client is the old name and still works.
--yes, -yWrite without asking. It still never replaces a different base URL or provider.
--dry-runShow the change and write nothing.
--checkWrite nothing. Exit 0 when every chosen agent is set up, 2 when one is not.
--replace-base-urlReplace a different ANTHROPIC_BASE_URL or Codex provider.
--gateway URLUse another gateway.

Exit codes: 0 done or nothing to do, 1 refused or a file could not be written, 2 --check found an agent not set up, 3 a conflict was not confirmed. With two agents, the most serious code wins.

status

Prints the gateway and console addresses, who this terminal is signed in as, the workspace and this device, then one line per agent. It exits 0 when you are signed in and 2 when you are not.

npx @context-mode/cli status
npx @context-mode/cli status --json

Options: --json prints the same facts as JSON for scripts. --gateway URL uses another gateway.

doctor

Checks, one line each: that the gateway answers, that the device key is valid, and that each installed agent is set up. When a check fails, the line names the command that fixes it. It exits 0 when all is well and 1 when something needs a fix.

npx @context-mode/cli doctor

Options: --gateway URL uses another gateway.

logout

Tells the gateway to revoke this terminal's device key, then deletes the key here. The key is deleted here even when the gateway cannot be reached. Your agents keep their config; run disconnect to remove that too.

npx @context-mode/cli logout
npx @context-mode/cli logout --all

Options: --all signs out of every gateway stored on this machine. --json prints the answer as JSON. --gateway URL uses another gateway.

disconnect

Takes Context Mode out of Claude Code and Codex, so they talk to Anthropic and OpenAI directly again. It removes only what setup wrote. Everything else in the files stays. It lists what it will remove and asks first. See What disconnect removes.

npx @context-mode/cli disconnect --dry-run
npx @context-mode/cli disconnect
npx @context-mode/cli disconnect --agent codex --yes --reason "trying it later"
OptionWhat it does
--agent claude|codex|allWhich agents. The default is both.
--dry-runList what it would remove and write nothing.
--yes, -yRemove without asking.
--reason TEXTTell us why you leave. Optional.
--gateway URLAlso treat this gateway's address as ours when removing.

It exits 0 when it removed everything or there was nothing to remove, and 1 when you said no or a file could not be changed.

What setup writes

Claude Code, in ~/.claude/settings.json (or $CLAUDE_CONFIG_DIR/settings.json), under env:

"ANTHROPIC_BASE_URL": "https://gateway.context-mode.com",
"ANTHROPIC_CUSTOM_HEADERS": "x-cm-device-key: cmdk_…",
"ENABLE_TOOL_SEARCH": "true"

Only these keys change. Line endings, indentation and the rest of the file stay as they were. Restart Claude Code afterwards.

Codex, in ~/.codex/config.toml (or $CODEX_HOME/config.toml):

model_provider = "context-mode"

[model_providers.context-mode]
name = "context-mode"
base_url = "https://gateway.context-mode.com"
wire_api = "responses"
requires_openai_auth = true
http_headers = { "x-cm-device-key" = "cmdk_…" }

Comments, spacing and every other table stay as they were. Start a new Codex session afterwards.

What setup and disconnect promise

What disconnect removes

Claude Code, in the env block:

Codex: model_provider = "context-mode", only when it names our provider, and the [model_providers.context-mode] table.

Before it writes, it sends one short message to the gateway so we know you left, with your --reason if you gave one. It does not wait long, and it works offline too. Restart Claude Code, or start a new Codex session, to use the plain agents.

To go back to the exact file you had before, copy a .cm-setup-bak- backup over it. disconnect does not delete the stored device key; run logout for that. If your shell profile also exports ANTHROPIC_BASE_URL for the gateway, disconnect tells you to remove it there.

The device key

Sign-in gives this terminal a device key that starts with cmdk_. It is stored in ~/.context-mode/credentials.json, readable only by you, one per gateway, and it is never printed. Claude Code and Codex send it in the x-cm-device-key header. The gateway knows this terminal by that key, so if you switch Claude or OpenAI accounts, your data stays in one workspace. The gateway removes the header before a request goes to Anthropic or OpenAI.

Gateway and console addresses

Your agents talk to the gateway, https://gateway.context-mode.com. You open the console, console.context-mode.com, to sign in and see your workspace.

Every command picks the gateway in this order: --gateway URL, then $CONTEXT_MODE_GATEWAY, then the hosted gateway. It ignores $ANTHROPIC_BASE_URL in your shell on purpose, so an address set somewhere else is never copied into a config file. A self-hosted gateway serves its own console at the same address.

FAQ

Do I have to install the CLI?

No. npx @context-mode/cli runs it without an install. Install it globally only if you want the short context-mode-cli command.

Does it install a plugin or a background service?

No. It edits one config file per agent, keeps a backup, and stores one device key. Nothing keeps running.

Can I see the change before it writes?

Yes. setup --dry-run and disconnect --dry-run show the change and write nothing.

How do I stop using Context Mode?

Run npx @context-mode/cli disconnect, then logout. Your agents talk to Anthropic and OpenAI directly again.

Does it work in CI or a script?

Yes. Use setup --yes to write without a question, setup --check to test the config, and status --json to read the state. Sign-in still needs a browser once.