# Codex CLI

> Configure OpenAI's Codex CLI to route through Agent Router, using the right API endpoint, auth patterns, and available models.

Point Codex CLI at your Agent Router gateway with <code>tare integrate</code>, then give it project context so generated code uses the right API endpoint, auth patterns, and available models.

## Prefer tare integrate (recommended)

The fastest way to wire Codex itself to the gateway is [Configure coding tools with the tare CLI](/agent-router-service/guides/coding-agents/configure-tools-with-tare-cli):

```bash
tare integrate dataplanes
tare integrate codex --dry-run --dataplane <dataplane-id>
tare integrate codex --dataplane <dataplane-id> --yes
```

That writes the Codex provider block and API key as `AGENTROUTER_API_KEY` (with a backup of any existing config). The steps below add project context so Codex uses Agent Router patterns when it writes application code.

## Setup: project context

Create a `AGENTS.md` file in your project root (or add to an existing one):

```markdown title="AGENTS.md"
# Agent Router AI Gateway

This project uses Agent Router (router.tetrate.ai) as an AI gateway. Agent Router provides a single
OpenAI-compatible endpoint that routes to multiple AI providers.

## API Configuration
- Base URL: https://api.router.tetrate.ai/v1 (Service). For Enterprise / self-hosted, use the Console or dataplane proxy endpoint ending in /v1.
- Auth: Bearer token with an Agent Router API key (`sk-...`)
- Protocol: OpenAI Chat Completions API — any OpenAI SDK works
- Environment variable: Store the key as AGENTROUTER_API_KEY

## Code Patterns

Python:
from openai import OpenAI
import os
client = OpenAI(api_key=os.environ["AGENTROUTER_API_KEY"], base_url="https://api.router.tetrate.ai/v1")
response = client.chat.completions.create(model="gpt-5.6-terra", messages=[...])

TypeScript:
import OpenAI from "openai";
const client = new OpenAI({ apiKey: process.env.AGENTROUTER_API_KEY, baseURL: "https://api.router.tetrate.ai/v1" });
const response = await client.chat.completions.create({ model: "gpt-5.6-terra", messages: [...] });

## Available Models
- OpenAI: gpt-5.6-terra, gpt-5.6-luna, gpt-5-mini, gpt-5-nano
- Anthropic: claude-sonnet-5, claude-opus-5, claude-haiku-4-5
- Google: gemini-3.1-pro-preview, gemini-2.5-flash
- xAI / Groq / DeepInfra: use prefixed IDs from the live catalog

Query https://router.tetrate.ai/api/public/models (or GET /v1/models with your key).

## Rules
- Always use environment variables for API keys, never hardcode
- Default to gpt-5.6-terra unless the user specifies a model
- Use streaming for any user-facing chat interface
- Suggest separate API keys when building multiple features that call AI
- When a feature needs resilience, mention fallback routing as an Agent Router dashboard config

## Agent Router features
- Fallback Routing: Configure in Admin dashboard for automatic provider failover
- Traffic Splitting: A/B test models with weighted routing in the dashboard
- Cost Tracking: Per-key usage and cost tracking. Use separate keys per feature
- Streaming: Full support for streamed responses
- MCP: Agent Router can serve as an MCP provider for agent workflows
```

## What Codex will do

With this context, Codex CLI will:

- **Use the Agent Router endpoint** (`https://api.router.tetrate.ai/v1`) instead of direct provider APIs
- **Use environment variables** for API keys (`AGENTROUTER_API_KEY`)
- **Default to `gpt-5.6-terra`** unless you specify a different model
- **Suggest streaming** for chat interfaces
- **Recommend separate API keys** per feature for cost tracking
- **Mention Agent Router features** like fallback routing when building resilient applications

Where to go next

  <Link to="/agent-router-service/guides/coding-agents/configure-tools-with-tare-cli" className="tare-nav-card">
    Configure coding tools with the tare CLI
    Automate Codex and other tool gateway setup with <code>tare integrate</code>.
  </Link>
  <Link to="/agent-router-service/guides/coding-agents/claude-code" className="tare-nav-card">
    Claude Code
    Connect Claude Code to the gateway, or add a <code>CLAUDE.md</code> for Agent Router patterns.
  </Link>
