codex-router

module
v0.3.1 Latest Latest
Warning

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

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

README

codex-router

CI Release Go Reference Go version

The right model for every turn of a Codex session. codex-router picks the model and reasoning effort for each turn of the Codex CLI. Reading code and small edits go to a light model, and a fix that keeps failing goes to the strongest one. The choice shows in Codex's own footer.

Left: Codex. The same test failed twice with an edit in between, so the router moved the turn from gpt-6-sol · medium to gpt-6.1-sol · high, and the footer follows. Right: the optional panel shows why. Sample data.

Why

Codex runs a whole session on the model you picked. With the strongest model, every directory listing and one-line rename spends your plan's usage, so it runs out sooner. With a light model, the hard bug stalls. Switching with /model by hand means watching every turn.

codex-router does the switching for you:

  • Per turn, automatically. A small classifier reads your prompt and recent dialogue and rates how hard the turn is. A policy switches only when the evidence repeats, and asks for more when a switch would lose the prompt cache.
  • Visible where you work. The Codex footer shows the routed model in any terminal. An optional panel shows why, lets you pin a model or pause, and estimates what routing saved.
  • You stay in charge. A /model pick holds your model. On any router error the request goes out unchanged. If the router cannot start, you get plain codex.
  • Your setup is left alone. codex-router never edits ~/.codex/config.toml. Only the optional codex-router setup does, in marked blocks with a backup, and --undo takes them out. Your ChatGPT login goes only to chatgpt.com. It is never logged and never sent to the classifier.

It is for people who use the Codex CLI with a ChatGPT sign-in and run long sessions that mix easy and hard work.

How it works

flowchart LR
  codex["Codex<br/>(normal TUI)"]:::codex ---->|"request"| proxy["codex-router<br/>local proxy"]:::core
  proxy <-->|"prompt, dialogue"| cls["classifier<br/>Jev or Ollama"]:::ext
  proxy ---->|"model + effort<br/>swapped"| up["chatgpt.com"]:::ext
  proxy -.->|"footer"| codex

  classDef core fill:#ecfdf5,stroke:#059669,color:#064e3b
  classDef codex fill:#f1f5f9,stroke:#64748b,color:#0f172a
  classDef ext fill:#eef2ff,stroke:#6366f1,color:#1e1b4b

codex-router starts a private Codex app-server and runs the normal Codex TUI against it. Codex sends its model requests to a proxy on 127.0.0.1. On each turn, the proxy asks the classifier, then changes only model and reasoning.effort and forwards everything else byte for byte. It also writes the choice back into the thread settings, so the footer repaints. Compaction, subagents and anything it cannot parse pass through untouched. See architecture for the details.

Quick start

You need the Codex CLI on PATH, signed in with ChatGPT, on macOS or Linux.

# macOS on Apple silicon; other builds and `go install` are in the user guide
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

export TYPESAFE_API_KEY=...   # key for the default classifier (Jev), or set envFile; or use a local Ollama model
cd your-repo
codex-router                  # use it like `codex`; arguments pass through

In a second pane, run codex-router tui for the panel. The user guide has the other install options, configuration and troubleshooting.

Many sessions, one panel

  • Run as many sessions as you like, in any repo or worktree and under any Codex home (CODEX_HOME). codex-router tui shows them all; --session locks it to one (picture).
  • codex-router setup --home ~/.codex-work routes every plain codex session of that home, through a small service that launchd or systemd keeps running. Add the Codex plugin for the transcript line and the router skill.
  • codex-router setup --undo puts config.toml back and removes the services. See Route a whole home.

Status

Early. It was tested live against Codex 0.162.1 on macOS: one session (live test report), then five sessions in two homes, with and without setup (stage 1, stage 2). On Linux, the systemd units are covered by unit tests only. The footer sync uses Codex's experimental --remote mode and thread/settings/update, so a Codex update can break it. Routing keeps working when sync fails.

Docs

License

MIT

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.
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