Try Bifrost Enterprise free for 14 days. Request access

How to Route Gemini CLI and Codex Through an AI Gateway

How to Route Gemini CLI and Codex Through an AI Gateway

TL;DR

  • Routing Gemini CLI and Codex CLI through an AI gateway means pointing each tool's base URL at the gateway instead of the provider, so every request runs through one control point for routing, cost tracking, and failover.
  • Gemini CLI routes through Bifrost by setting GOOGLE_GEMINI_BASE_URL=http://localhost:8080/genai; Codex CLI routes through it by setting openai_base_url="<http://localhost:8080/openai/v1>" in ~/.codex/config.toml.
  • Once traffic flows through the gateway, both agents can call any of 1,000+ models with the provider/model-name format, so Codex can run Claude and Gemini CLI can run GPT.
  • Virtual keys enforce per-user budgets, rate limits, and model access, so a coding agent cannot silently spend past its allocation.
  • Bifrost adds 11 microseconds of overhead per request at 5,000 requests per second, so centralizing coding-agent traffic does not add measurable latency.

Gemini CLI and OpenAI's Codex CLI each send their requests straight to a single provider, which leaves platform teams with no shared control over model choice, spend, or failover when that provider returns 429 or 5xx errors. A gateway solves this by sitting between every coding agent and the model providers, so all traffic passes through one place that handles routing, governance, and observability. Bifrost, the open-source AI gateway built in Go by Maxim AI, exposes fully compatible OpenAI, Anthropic, and Gemini endpoints, which means Gemini CLI and Codex CLI can be pointed at it by changing a single base URL. This guide covers how to route both coding agents through the gateway and what that enables for cost control, provider flexibility, and reliability.

What an AI Gateway Does for Coding Agents

An AI gateway is a unified entry point that routes, authenticates, and observes traffic to multiple LLM providers from a single API. For coding agents, it replaces a direct connection to one provider with a shared control plane that every request passes through. That gives a platform team one place to set model access, track spend, and switch providers without touching the agent's own configuration.

Bifrost works as this control plane by exposing OpenAI-, Anthropic-, and Gemini-compatible endpoints. A coding agent that already speaks one of those API shapes needs only its base URL repointed at the gateway. Nothing else in the agent changes, because the drop-in replacement model keeps the request and response formats identical to the provider the agent expects. To understand where a gateway sits in a broader stack, see this overview of what an AI gateway is and how it works.

The practical result is that Gemini CLI and Codex CLI become governed clients rather than isolated tools. Every prompt, completion, and token count flows through the gateway, where routing rules, budgets, and logging apply uniformly across both agents.

Why Route Gemini CLI and Codex CLI Through a Gateway

Routing Gemini CLI and Codex CLI through a gateway centralizes four things that are otherwise fragmented across each developer's machine: model access, cost, reliability, and visibility. Without a gateway, each agent holds its own provider key, bills to its own account, and fails on its own when a provider degrades. A gateway consolidates all of that behind one endpoint.

The specific gains for coding agents are concrete:

  • Provider flexibility: Codex CLI is no longer limited to OpenAI models, and Gemini CLI is no longer limited to Google models. Both can call any supported provider configured in the gateway.
  • Cost control: per-key budgets and rate limits cap what any agent or user can spend, and every request is priced and logged for cost governance.
  • Reliability: automatic retries and provider fallback keep an agent working through rate limits and outages.
  • Observability: all coding-agent traffic is logged in one dashboard, filterable by provider, model, and content.

Teams standardizing this across many agents often start from a broader plan for governing AI coding agents at scale across Claude Code, Cursor, and Codex. The routing steps below are the foundation that governance sits on top of.

How to Route Gemini CLI Through the Gateway

Gemini CLI routes through Bifrost by setting one environment variable, GOOGLE_GEMINI_BASE_URL, to the gateway's GenAI endpoint. Because Bifrost serves a Gemini-compatible API, the CLI treats the gateway as if it were Google's own endpoint, and every request is forwarded through it. Install the Gemini CLI first with npm install -g @google/gemini-cli.

For a Google account using OAuth, point the base URL at the gateway and sign in normally:

export GOOGLE_GEMINI_BASE_URL=http://localhost:8080/genai
gemini

Selecting Login with Google authenticates through the browser, and all traffic then routes through the gateway. For API-key usage, set both the key and the base URL before launching:

export GEMINI_API_KEY=your-api-key   # Gemini API key or Bifrost virtual key
export GOOGLE_GEMINI_BASE_URL=http://localhost:8080/genai
gemini

Enterprise users on Vertex AI add GOOGLE_GENAI_USE_VERTEXAI=true alongside the same base URL. In each case, Bifrost becomes the single hop between Gemini CLI and whichever provider ultimately serves the request. A full multi-provider walkthrough is available in this guide to a Gemini CLI multi-model setup connecting to Claude, GPT, Groq, and 20+ providers.

How to Route Codex CLI Through the Gateway

Codex CLI routes through the gateway by setting its OpenAI base URL to the gateway's /openai/v1 endpoint in ~/.codex/config.toml. Codex CLI prefers OAuth over custom API keys, so run /logout before configuring the gateway, otherwise the CLI ignores the key. Install the Codex CLI with npm install -g @openai/codex.

Export the key that authenticates to the gateway, then set the base URL and default model in the config file:

export OPENAI_API_KEY=<bifrost_virtual_key>
openai_base_url="<http://localhost:8080/openai/v1>"
env_key="OPENAI_API_KEY"
model = "openai/gpt-5.4"

The base URL must end with /v1 for Codex CLI, which is the OpenAI-compatible URL shape the tool expects. Run codex from the same terminal session where the variable was exported so the environment is picked up. For a broader look at putting Codex behind a gateway, see this guide to the best AI gateway for Codex CLI and its governance and cost trade-offs.

Using Any Model From Gemini CLI and Codex CLI

Once traffic runs through the gateway, both agents can call any configured model using the provider/model-name format, not just their native provider. Bifrost translates each agent's API shape to the target provider, so the agent does not need to know which model is behind the request. This is the capability that turns a single-provider CLI into a multi-provider one.

From Codex CLI, switch to a non-OpenAI model with the --model flag or the /model command mid-session:

codex --model anthropic/claude-sonnet-4-5-20250929
codex --model gemini/gemini-2.5-pro

From Gemini CLI, pass any configured model the same way:

gemini -m openai/gpt-5
gemini -m groq/llama-3.3-70b-versatile

One requirement applies to both agents: any non-native model must support tool use, because both CLIs rely on tool calling for file edits and terminal commands. Codex CLI also needs HTTPS mode enabled (rather than its default WebSocket mode) for non-OpenAI models, since WebSocket mode expects the server to hold conversation context. Teams that want the full model matrix can follow this guide to routing Codex CLI to any model, or the walkthrough on running Codex CLI beyond GPT with Claude, Gemini, and Llama.

Governance, Budgets, and Cost Control for Coding Agents

Governance in the gateway is enforced through virtual keys, which are the primary control entity for every request a coding agent makes. A virtual key carries its own access permissions, budget, and rate limits, so a single Gemini CLI or Codex CLI user can be capped independently of everyone else. This is what prevents a coding agent from silently spending past its allocation.

With virtual keys, a platform team can:

  • Set a dollar budget per key with a reset window (1h, 1d, 1w, 1M), enforced through budget and rate limits.
  • Restrict which providers and models a key can reach, so an agent can be locked to approved models only.
  • Throttle token and request volume to stop runaway loops from draining spend.
  • Disable a key instantly when a developer leaves or a project ends.

Because every coding-agent request is priced and attributed to a key, the gateway produces a per-user and per-team spend breakdown that direct provider billing cannot. Teams tracking coding-agent budgets in detail often pair this with the approach in tracking Claude Code costs with an enterprise AI gateway. The same governance model applies identically to Gemini CLI and Codex CLI.

Failover and Load Balancing Across Providers

Failover keeps a coding agent working when its primary provider returns errors, and the gateway handles it without any change to the agent. Bifrost applies two layers: retries within a provider for transient errors, and fallback to the next provider in a chain once retries are exhausted. Each fallback provider gets its own full retry budget.

The retry logic distinguishes failure types. On a 5xx or network error, the Bifrost gateway reuses the same key with exponential backoff and jitter. On a 429 rate-limit or a per-key 401/402/403 failure, it rotates to a different API key from the pool. This means a Codex CLI or Gemini CLI session survives a rate-limit spike or a single-key outage that would otherwise interrupt the developer. The full behavior is documented under retries and fallbacks.

Load balancing complements failover by distributing requests across multiple keys and providers by weight, configured through key management. For a team routing between several model providers, this spreads coding-agent load and reduces the chance any single account hits its ceiling. Reviewing the best AI gateway for routing between OpenAI, Anthropic, and Gemini shows how these routing strategies compose in practice.

Observability for Coding Agent Traffic

Observability is where a gateway repays the setup cost, because it turns scattered coding-agent activity into one auditable stream. Every Gemini CLI and Codex CLI request is logged and viewable in real time at the gateway dashboard, filterable by provider, model, or conversation content. A platform team no longer has to reconstruct usage from separate provider consoles.

Beyond the built-in log stream, the open-source Bifrost gateway emits metrics for production monitoring stacks. Native Prometheus metrics cover request rates, latency, and error counts, and OpenTelemetry tracing exports spans to tools such as Grafana, New Relic, and Honeycomb. This gives coding-agent traffic the same telemetry as any other production service.

The performance cost of this centralization is measurable and small. Published benchmarks show the gateway adds 11 microseconds of overhead per request at 5,000 requests per second, so routing Gemini CLI and Codex CLI through it does not add latency a developer would notice. Deciding which gateway fits a coding-agent workflow is covered in this guide to choosing an AI gateway for coding agents like Claude Code.

Running an Open Source AI Gateway in Production

An open-source gateway gives a team full control over how coding-agent traffic is routed and where it runs, which matters when the data in those prompts is sensitive. Bifrost is fully open source on GitHub, so the routing, governance, and logging layers can be inspected and self-hosted rather than trusted to a black box. This is a deciding factor for many teams evaluating an open source AI gateway for internal tooling.

For regulated and enterprise environments, the same gateway supports deployment inside private infrastructure. The table below summarizes what each capability layer contributes to a coding-agent deployment.

Capability What it does for coding agents Where it is configured
Unified endpoint One base URL for Gemini CLI and Codex CLI Provider configuration
Virtual keys Per-user budgets, rate limits, model access Governance
Fallback chains Zero-downtime provider switching Automatic provider failover
In-VPC deployment Keeps prompt data inside private cloud Enterprise deployment

Enterprises with strict data requirements run the gateway in private cloud infrastructure so no coding-agent prompt leaves their network. Combined with audit logs and role-based access, this extends the routing setup above into a governed platform. A useful reference point is the LLM gateway buyer's guide, which frames these deployment choices against production requirements.

Frequently Asked Questions

How do I route Gemini CLI through an AI gateway?

Set the environment variable GOOGLE_GEMINI_BASE_URL to the gateway's GenAI endpoint (for Bifrost, http://localhost:8080/genai), then run gemini. Because the gateway serves a Gemini-compatible API, the CLI treats it as Google's endpoint and forwards every request through it. Add GEMINI_API_KEY (from Google AI Studio) for API-key access or GOOGLE_GENAI_USE_VERTEXAI=true for Vertex AI.

How do I route Codex CLI through an AI gateway?

Run /logout first, since Codex CLI prefers OAuth over custom keys, then set openai_base_url="<http://localhost:8080/openai/v1>" and env_key="OPENAI_API_KEY" in ~/.codex/config.toml. Export the gateway key as OPENAI_API_KEY in the same terminal session. The base URL must end with /v1 for Codex CLI to resolve requests correctly.

Can Codex CLI use non-OpenAI models through a gateway?

Yes. Once Codex CLI routes through the gateway, pass any configured model with codex --model provider/model-name, for example anthropic/claude-sonnet-4-5-20250929 or gemini/gemini-2.5-pro. Non-OpenAI models require HTTPS mode enabled in config.toml and must support tool use, because Codex CLI relies on tool calling for file and terminal operations.

Does routing through a gateway slow down coding agents?

No measurable slowdown occurs. Bifrost adds 11 microseconds of overhead per request at 5,000 requests per second in sustained benchmarks. The gateway also improves effective reliability through automatic retries and provider fallback, so coding agents stay responsive through rate limits and transient provider outages that would otherwise interrupt a session.

How does the gateway control coding-agent costs?

The gateway attaches a virtual key to each user or agent, and that key carries an independent budget, rate limit, and set of allowed models. Every request is priced and logged against its key, producing a per-user and per-team spend breakdown, and the key stops serving requests once its budget window is exhausted.

Which is the best AI gateway for coding agents?

The best AI gateway for coding agents unifies Gemini CLI, Codex CLI, and other agents behind one endpoint while providing governance, failover, and observability without added latency. Bifrost is built for this: it exposes OpenAI-, Anthropic-, and Gemini-compatible endpoints, routes to 1,000+ models across supported providers, and is fully open source, so teams can self-host and inspect the full path their coding-agent traffic takes.

Start Routing Your Coding Agents Through Bifrost

Routing Gemini CLI and Codex CLI through an AI gateway turns two isolated tools into a governed, multi-provider workflow with shared budgets, failover, and observability, all behind a single base URL. Bifrost delivers this as an open source, enterprise-ready gateway that adds negligible latency while giving platform teams full control over model access and spend. To see how the gateway fits your coding-agent stack, explore the Bifrost resources hub or book a demo with the Bifrost team.