Nexus supports the OpenAI protocol natively, allowing you to use it as a proxy for OpenAI Codex CLI. This enables you to leverage Nexus's features like rate limiting, observability, and multi-provider routing while using Codex.
- Nexus v0.5.1 or later
- OpenAI Codex CLI installed (github.com/openai/codex)
- API keys for your preferred providers (OpenAI, Anthropic, etc.)
Update your nexus.toml
to enable the OpenAI protocol and configure your providers:
[llm]
enabled = true
# Enable the OpenAI protocol endpoint
[llm.protocols.openai]
enabled = true
path = "/llm/openai" # This is the default path
# Configure your providers
[llm.providers.openai]
type = "openai"
api_key = "{{ env.OPENAI_API_KEY }}"
[llm.providers.anthropic]
type = "anthropic"
api_key = "{{ env.ANTHROPIC_API_KEY }}"
# Configure the models you want to use
[llm.providers.openai.models."gpt-4-turbo-preview"]
[llm.providers.openai.models."gpt-4"]
[llm.providers.anthropic.models."claude-3-5-sonnet-20241022"]
[llm.providers.anthropic.models."claude-3-5-haiku-latest"]
Set your API keys:
export OPENAI_API_KEY="sk-..."
export ANTHROPIC_API_KEY="sk-ant-api03-..."
nexus --config nexus.toml
By default, Nexus will listen on http://localhost:6000
.
Configure Codex CLI to use Nexus by editing ~/.codex/config.toml
:
Important: Nexus serves its OpenAI-compatible endpoint at
/llm/openai/
; Codex expects the/v1
suffix. Make sure thebase_url
ends with/v1
.
[model_providers.nexus]
name = "Nexus AI router"
base_url = "http://127.0.0.1:6000/llm/openai/v1"
wire_api = "chat"
query_params = {}
base_url
must point to the Nexus OpenAI-compatible endpoint and include the/v1
suffix (adjust host/port if Nexus runs elsewhere)wire_api
should be set to"chat"
for chat completionsquery_params
can stay empty, but the table must exist to satisfy Codex's schema
Start Codex with a Nexus-managed model:
codex -c model="openai/gpt-4" -c model_provider=nexus
You can use any provider/model pair that you have configured in Nexus:
# Use OpenAI models
codex -c model="openai/gpt-4-turbo-preview" -c model_provider=nexus
# Use Anthropic models through OpenAI-compatible interface
codex -c model="anthropic/claude-3-5-haiku-latest" -c model_provider=nexus
# Use other configured models
codex -c model="groq/llama-3.1-70b-versatile" -c model_provider=nexus
If you're running Nexus in Docker:
services:
nexus:
image: ghcr.io/grafbase/nexus:latest
ports:
- "6000:6000"
volumes:
- ./nexus.toml:/etc/nexus.toml
environment:
- OPENAI_API_KEY=${OPENAI_API_KEY}
- ANTHROPIC_API_KEY=${ANTHROPIC_API_KEY}
Then configure Codex to use the containerized Nexus:
[model_providers.nexus]
name = "Nexus AI router"
base_url = "http://localhost:6000/llm/openai/v1"
wire_api = "chat"
query_params = {}
Using Nexus with OpenAI Codex provides:
- Unified Gateway: Route all AI requests through a single endpoint
- Multi-Provider Support: Easily switch between OpenAI, Anthropic, and other providers
- Rate Limiting: Control token consumption per user and model
- Observability: Built-in OpenTelemetry metrics, traces, and logs
- Cost Control: Monitor and limit token usage across all providers
- Model Management: Configure and manage models centrally
- Security: Add authentication, CORS, and CSRF protection
The OpenAI protocol implementation in Nexus:
- Fully supports OpenAI's chat completions format
- Handles streaming responses
- Supports function calling (tools)
If Codex can't connect to Nexus:
-
Verify Nexus is running:
curl http://localhost:6000/health
-
Check the OpenAI protocol is enabled:
curl http://localhost:6000/llm/openai/v1/models
-
Verify your Codex configuration:
cat ~/.codex/config.toml | grep -A 4 model_providers.nexus
If you get a "model not found" error:
- Ensure the model is configured in
nexus.toml
- Use the correct format:
provider/model-name
- List available models:
curl http://localhost:6000/llm/openai/v1/models | jq .
If you get authentication errors:
-
Verify your API keys are set correctly:
echo $OPENAI_API_KEY echo $ANTHROPIC_API_KEY
-
Check Nexus logs for more details:
nexus --log debug
If Codex fails to use the Nexus provider:
- Ensure the
model_providers.nexus
section exists in~/.codex/config.toml
- Verify the
base_url
ends with/v1
- Check that
wire_api
is set to"chat"
- Ensure
query_params
table exists (even if empty)
# Start a Codex session with GPT-4
codex -c model="openai/gpt-4" -c model_provider=nexus
# Execute a single command
codex exec -c model="anthropic/claude-3-5-sonnet-20241022" -c model_provider=nexus "Write a Python function to calculate fibonacci"
Compare responses from different models:
# Try with GPT-4
codex exec -c model="openai/gpt-4" -c model_provider=nexus "Explain quantum computing"
# Try with Claude
codex exec -c model="anthropic/claude-3-5-sonnet-20241022" -c model_provider=nexus "Explain quantum computing"
# Try with Llama
codex exec -c model="groq/llama-3.1-70b-versatile" -c model_provider=nexus "Explain quantum computing"
- Configure Rate Limiting to control usage
- Set up Telemetry for observability
- Explore Header Rules for advanced routing
- Learn about Token Forwarding to let users provide their own API keys
- Add MCP Servers to give Codex access to tools