codex-router

module
v0.2.0 Latest Latest
Warning

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

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

README

codex-router

CI Release Go Report Card 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. The router never edits ~/.codex/config.toml. 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 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.

Status

Early. It was tested live against Codex 0.162.1 on macOS (live test report). 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 the session's private Codex app-server: JSON-RPC 2.0 over a Dialer (WebSocket over the unix socket per research §3.6, with a newline JSON-RPC fallback).
Package appserver is a second client of the session's private Codex app-server: JSON-RPC 2.0 over a Dialer (WebSocket over the unix socket per research §3.6, with a newline JSON-RPC fallback).
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: JSON lines over a 0600 unix socket.
Package control is the router's local control channel: 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.
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`.
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.
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.
tui
Package tui is the router panel: a Bubble Tea program over the control socket with tabs Now, Routes, Policy, Classifier and Usage, modelled on the claude-model-router 1.13.0 /router pane.
Package tui is the router panel: a Bubble Tea program over the control socket with tabs Now, Routes, Policy, Classifier and Usage, modelled on the claude-model-router 1.13.0 /router pane.
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