codex-router

module
v0.11.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 28, 2026 License: MIT

README

codex-router

CI Release Go Reference Go version

Automatic model and reasoning effort selection for Codex CLI.

A Codex session can start with a small edit and end with a stubborn bug. Keeping the strongest model on for everything uses more of your plan. Switching by hand means watching the model picker while you work.

codex-router picks a route for each turn. It uses lighter models for simple work and moves up when the work needs it. Codex's own footer shows the choice.

Codex beside the router panel. Two failed checks with an edit between them trigger a move to high effort. The image is labeled sample data.

The panel is optional. This illustration uses sample data, including its savings figures.

What you get

  • Less model picking. A classifier scores the prompt and recent dialogue. The router uses that score, cache state and tool results to decide whether to switch.
  • A reason for each choice. The panel shows the route and why it changed. One panel covers multiple sessions and Codex homes.
  • Cost or quality, your call. A routing mode, cost, balanced or quality, chooses cheaper or stronger model/effort pairs. The default ladder uses Luna, Terra and Sol. Set it per profile, per repository or per session.
  • Manual control. Use /model to keep your own choice. Pin the next turn or pause routing from the panel.
  • A way back. Routing errors forward the request unchanged. Launcher startup errors fall back to plain Codex. The wrapper does not edit your Codex config.

How it works

A classifier is a small model that rates the work. It does not answer your coding request. Hosted options include Jev, from Typesafe, and Cloudflare's Clef. Ollama runs the classifier locally.

flowchart LR
  codex["Codex CLI"] --> router["codex-router on your machine"]
  router <-->|"clipped prompt and dialogue"| classifier["Hosted or local classifier"]
  router -->|"model and effort"| openai["OpenAI ChatGPT backend"]
  router -.->|"route in footer"| codex

The router starts a private Codex app-server and a local proxy. It changes the model and effort in supported requests. Compaction, subagents and requests it cannot parse pass through unchanged. Architecture details.

Try it

You need macOS or Linux, Codex CLI on PATH, and a ChatGPT sign-in. API-key sign-in is not supported.

Recommended: install the latest stable release with Homebrew (macOS or Linux):

brew install alexei-led/tap/codex-router

See Install for Homebrew trust setup and Update or uninstall for upgrades. The formula follows stable releases; no version pin is needed.

Or download the latest binary directly on an Apple silicon Mac:

curl -fsSL -o codex-router \
  https://github.com/alexei-led/codex-router/releases/latest/download/codex-router-darwin-arm64
sudo install -m 0755 codex-router /usr/local/bin/codex-router
codex-router version

Linux, Intel Mac and Go installation.

Choose where classification runs before you start. Your coding requests still go to OpenAI in both cases. Codex credentials are never sent to the classifier.

Hosted classifier: Jev is the default. Get a Typesafe API key, then run:

export TYPESAFE_API_KEY=your-key
cd your-repo
codex-router

Jev receives the clipped prompt and recent dialogue. If the key is missing, automatic classification is off. Pins and the context guard still work.

Local classifier: install and start Ollama, then run:

ollama pull qwen3.5:9b
mkdir -p ~/.config/codex-router
# Use a separate file so this example does not replace an existing config.
printf '%s\n' '{"classifier":"ollama"}' > ~/.config/codex-router/ollama.json
cd your-repo
codex-router --config ~/.config/codex-router/ollama.json

The default Ollama endpoint keeps classifier text on your machine. See classifier setup and privacy for other options.

Run codex-router tui in a second terminal to see decisions and controls. Arguments pass through, for example codex-router resume --last.

Limits

Early software. Live checks covered Codex 0.162.1 and 0.163.0 on macOS, including model and effort changes in the bottom status line. Linux service files have unit tests, but no recorded live run. Footer sync uses experimental Codex interfaces, so updates can break it.

Switching models can lose prompt-cache reuse. The policy accounts for this, but routing does not guarantee lower usage or better answers. The panel's dollar figures are list-price estimates, not ChatGPT plan billing.

The local proxy carries your ChatGPT credentials. If its process is killed, close the affected Codex session before restarting. Close a home's sessions before undoing setup. Recovery and security limits.

Documentation

  • User guide: install, choose a classifier, use the panel, update and troubleshoot.
  • Configuration: fields, defaults and what the classifier receives.
  • Architecture: request flow, routing policy and security boundaries.
  • Evaluation: small benchmark results and their limits.
  • Contributing: build, tests and release process.

pi-model-router routes models in the Pi coding agent. claude-router routes Claude Code sessions and was built with Claude Mods. They started the same experiment: let the task influence the model choice. Each has its own integration and limits.

MIT license.

Directories

Path Synopsis
cmd
codex-router command
Command codex-router routes Codex CLI requests to a model and reasoning effort per step.
Command codex-router routes Codex CLI requests to a model and reasoning effort per step.
internal
appserver
Package appserver is a second client of a Codex app-server: the session's private one in stage 1, or a home's shared daemon in stage 2.
Package appserver is a second client of a Codex app-server: the session's private one in stage 1, or a home's shared daemon in stage 2.
brain
Package brain is the stage 2 router of one Codex home (plan decision M6).
Package brain is the stage 2 router of one Codex home (plan decision M6).
catalog
Package catalog reads the Codex model catalog (GET {base_url}/models, or $CODEX_HOME/models_cache.json read-only) and answers two questions: which models share a compatibility group, and how large a model's usable context window is.
Package catalog reads the Codex model catalog (GET {base_url}/models, or $CODEX_HOME/models_cache.json read-only) and answers two questions: which models share a compatibility group, and how large a model's usable context window is.
classifier
Package classifier asks an external model which tier and activity a prompt needs.
Package classifier asks an external model which tier and activity a prompt needs.
config
Package config holds the router configuration: tiers, activities, routes, policy, classifiers, and the JSON file under the XDG config dir they load from.
Package config holds the router configuration: tiers, activities, routes, policy, classifiers, and the JSON file under the XDG config dir they load from.
control
Package control is the router's local control channel, protocol v2: JSON lines over a 0600 unix socket.
Package control is the router's local control channel, protocol v2: JSON lines over a 0600 unix socket.
cost
Package cost keeps the prompt-cache ledger per thread and model and prices a switch in uncached tokens, with USD only when a model has list prices.
Package cost keeps the prompt-cache ledger per thread and model and prices a switch in uncached tokens, with USD only when a model has list prices.
facts
Package facts derives what the policy needs from one Responses request: the prompt and recent dialogue for the classifier, a repeated tool-failure signature, the activity the tools show, whether a verification passed, and a context size estimate.
Package facts derives what the policy needs from one Responses request: the prompt and recent dialogue for the classifier, a repeated tool-failure signature, the activity the tools show, whether a verification passed, and a context size estimate.
home
Package home resolves the Codex home a router serves ($CODEX_HOME, else ~/.codex) into a stable identity: a short ID hashed from the canonical path and a display name.
Package home resolves the Codex home a router serves ($CODEX_HOME, else ~/.codex) into a stable identity: a short ID hashed from the canonical path and a display name.
hook
Package hook implements `codex-router hook`, the optional Codex command hook (UserPromptSubmit, Stop) that prints a systemMessage when the route changed since the previous turn.
Package hook implements `codex-router hook`, the optional Codex command hook (UserPromptSubmit, Stop) that prints a systemMessage when the route changed since the previous turn.
launcher
Package launcher runs one routed Codex session: it makes the runtime dir, starts and probes the proxy, starts the private app-server with -c overrides only, connects the router client, wires the engine to the app-server and control socket, then runs `codex --remote`.
Package launcher runs one routed Codex session: it makes the runtime dir, starts and probes the proxy, starts the private app-server with -c overrides only, connects the router client, wires the engine to the app-server and control socket, then runs `codex --remote`.
link
Package link is the edge-brain socket of stage 2 (plan decision M7).
Package link is the edge-brain socket of stage 2 (plan decision M7).
listen
Package listen takes a listening socket from the service manager (plan decision M8): systemd socket activation on Linux, launchd Sockets on macOS.
Package listen takes a listening socket from the service manager (plan decision M8): systemd socket activation on Linux, launchd Sockets on macOS.
modestore
Package modestore keeps each session's routing mode override: one small file per Codex thread under <state>/codex-router/modes/<home id>/.
Package modestore keeps each session's routing mode override: one small file per Codex thread under <state>/codex-router/modes/<home id>/.
policy
Package policy decides the tier for a step: escalation on a repeated tool failure, holds, votes with hysteresis, cost-gated upgrade and downgrade bars, and the context-fit tier search.
Package policy decides the tier for a step: escalation on a repeated tool failure, holds, votes with hysteresis, cost-gated upgrade and downgrade bars, and the context-fit tier search.
proxy
Package proxy is the loopback Responses proxy: POST /v1/responses and GET /v1/models go to the fixed ChatGPT backend with no redirects; SSE streams through unchanged except for the stripped model headers.
Package proxy is the loopback Responses proxy: POST /v1/responses and GET /v1/models go to the fixed ChatGPT backend with no redirects; SSE streams through unchanged except for the stripped model headers.
registry
Package registry is the shared list of running routers: $XDG_STATE_HOME/codex-router/registry (else ~/.local/state/...), one <endpoint_id>.json file per control endpoint.
Package registry is the shared list of running routers: $XDG_STATE_HOME/codex-router/registry (else ~/.local/state/...), one <endpoint_id>.json file per control endpoint.
responses
Package responses models the Codex Responses request as the proxy sees it: the body fields the router reads or rewrites (model, reasoning.effort, input items), the turn metadata header, the request kind, and the usage reported in the response.completed SSE event.
Package responses models the Codex Responses request as the proxy sees it: the body fields the router reads or rewrites (model, reasoning.effort, input items), the turn metadata header, the request kind, and the usage reported in the response.completed SSE event.
route
Package route is the router engine: it keys state by thread, turn and request fingerprint, asks the classifier on a turn's first step and in background reviews, runs the policy, checks the compatibility group and context fit, pins a decision for retries, and publishes decisions and snapshots.
Package route is the router engine: it keys state by thread, turn and request fingerprint, asks the classifier on a turn's first step and in background reviews, runs the policy, checks the compatibility group and context fit, pins a decision for retries, and publishes decisions and snapshots.
service
Package service installs, loads and checks the stage 2 service units of one Codex home: launchd agents on macOS, systemd user units on Linux.
Package service installs, loads and checks the stage 2 service units of one Codex home: launchd agents on macOS, systemd user units on Linux.
sessions
Package sessions finds the running sessions of every router in the registry, or of one control socket, and picks one: by id or label for the panel, by thread id or working dir for a shell command.
Package sessions finds the running sessions of every router in the registry, or of one control socket, and picks one: by id or label for the panel, by thread id or working dir for a shell command.
setup
Package setup routes one Codex home through a stage 2 edge and brain (codex-router setup), and undoes it.
Package setup routes one Codex home through a stage 2 edge and brain (codex-router setup), and undoes it.
statedir
Package statedir is codex-router's state dir, $XDG_STATE_HOME/codex-router (else ~/.local/state/codex-router): where it is, the privacy rule for its dirs, the layout of a home's stage 2 dir in it, and atomic file writes.
Package statedir is codex-router's state dir, $XDG_STATE_HOME/codex-router (else ~/.local/state/codex-router): where it is, the privacy rule for its dirs, the layout of a home's stage 2 dir in it, and atomic file writes.
tui
Package tui is the router panel: a Bubble Tea program over the control sockets of every registry endpoint, with a Sessions tab and per-session tabs Now, Routes, Policy, Classifier and Usage.
Package tui is the router panel: a Bubble Tea program over the control sockets of every registry endpoint, with a Sessions tab and per-session tabs Now, Routes, Policy, Classifier and Usage.
scripts
render-docs command
Command render-docs draws the pictures in docs/images from the real TUI renderer and a sample snapshot.
Command render-docs draws the pictures in docs/images from the real TUI renderer and a sample snapshot.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL