Войти

Use CostRouter in apps and coding agents

Choose a path, copy the matching Base URL, then verify the first request in Usage Logs.

Path 1

AI application

For product backends, chatbots, SaaS features, internal tools, and data workflows.

SDK
Base URL
CostRouter
  • OpenAI-compatible Base URL
  • Existing SDK or HTTP client
  • Usage Logs for verification
Path 2

Coding agent

For Codex, Claude Code, Gemini CLI, Cursor, local automations, and custom agent runtimes.

Agent profiles
Codex
Claude
Cursor
  • Dedicated key per tool
  • Custom provider settings
  • Quota, model limits, and expiry
Path 1

Use CostRouter in your own AI application

For most OpenAI-compatible integrations, keep your existing SDK or HTTP client and replace only the Base URL, API key, and model name.

What changes

  • Base URL: https://costrouter.ai/v1
  • Authorization: Bearer YOUR_COSTROUTER_API_KEY
  • Model: copy the exact ID from Models

Fastest working request

curl https://costrouter.ai/v1/chat/completions \
  -H "Authorization: Bearer YOUR_COSTROUTER_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-4o",
    "messages": [
      { "role": "user", "content": "Say hello from CostRouter." }
    ]
  }'
SDK examples

Node.js: OpenAI SDK

import OpenAI from "openai";

const client = new OpenAI({
  apiKey: process.env.COSTROUTER_API_KEY,
  baseURL: "https://costrouter.ai/v1",
});

const response = await client.chat.completions.create({
  model: "gpt-4o",
  messages: [{ role: "user", content: "Say hello from CostRouter." }],
});

console.log(response.choices[0]?.message?.content);

Python: OpenAI SDK

import os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ["COSTROUTER_API_KEY"],
    base_url="https://costrouter.ai/v1",
)

response = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Say hello from CostRouter."}],
)

print(response.choices[0].message.content)
Native provider formats

Anthropic Messages format

curl https://costrouter.ai/v1/messages \
  -H "x-api-key: YOUR_COSTROUTER_API_KEY" \
  -H "anthropic-version: 2023-06-01" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "claude-sonnet-4-5-20250929",
    "max_tokens": 512,
    "messages": [
      { "role": "user", "content": "Say hello from CostRouter." }
    ]
  }'

Gemini Generate Content format

curl "https://costrouter.ai/v1beta/models/gemini-2.5-flash:generateContent?key=YOUR_COSTROUTER_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "contents": [
      { "parts": [{ "text": "Say hello from CostRouter." }] }
    ]
  }'
After the first successful call
  • Open Dashboard > Usage Logs and confirm the request appears with the expected model.
  • Check final cost, token usage, request status, and any refund or routing details.
  • For production, set API key quota, expiry, allowed models, IP allowlist, and group routing rules.
  • Store keys in environment variables or secret managers; never commit them to Git.
Path 2

Start with Agent: connect coding tools to CostRouter

Use this section when a local coding agent, IDE agent, or CLI tool should call models through CostRouter instead of a direct provider account.

Agent tools change quickly. Use the current official settings for each tool as the source of truth, then apply the same CostRouter values: Base URL, API key, and model. If a tool does not expose a custom Base URL for its own built-in model picker, run your custom agent scripts from that tool terminal instead.

Codex

OpenAI-compatible provider

Configure Codex to use a CostRouter OpenAI-compatible provider. Use Responses wiring when your selected model and Codex version support it.

Configure

Install Codex

npm install -g @openai/codex

~/.codex/config.toml

# PowerShell
$env:OPENAI_API_KEY="YOUR_COSTROUTER_API_KEY"

# ~/.codex/config.toml
model_provider = "costrouter"
model = "gpt-5"

[model_providers.costrouter]
name = "CostRouter"
wire_api = "responses"
requires_openai_auth = true
base_url = "https://costrouter.ai/v1"
  • Set OPENAI_API_KEY to your CostRouter API key.
  • Use a model from CostRouter Models that supports the endpoint Codex will call.
  • Restart Codex after changing config or environment variables.
Prefer a guided setup? cc-switch is a popular open-source helper for managing local agent profiles. Download it, select the corresponding agent, then enter your CostRouter Base URL and API key.Download cc-switch

Claude Code

Anthropic-compatible gateway

Claude Code can be pointed at an Anthropic-compatible gateway. Use the CostRouter host and API key, then verify the first request in Usage Logs.

Configure

Install Claude Code

npm install -g @anthropic-ai/claude-code

Environment or settings

# PowerShell
$env:ANTHROPIC_AUTH_TOKEN="YOUR_COSTROUTER_API_KEY"
$env:ANTHROPIC_BASE_URL="https://costrouter.ai"

# Optional ~/.claude/settings.json
{
  "env": {
    "ANTHROPIC_AUTH_TOKEN": "YOUR_COSTROUTER_API_KEY",
    "ANTHROPIC_BASE_URL": "https://costrouter.ai"
  }
}

# Profile helper:
# https://github.com/farion1231/cc-switch/releases
  • Use ANTHROPIC_AUTH_TOKEN as the CostRouter API key.
  • Use ANTHROPIC_BASE_URL as the CostRouter site host, not the /v1 OpenAI relay URL.
  • Run a small prompt, then verify model and cost in Usage Logs.
Prefer a guided setup? cc-switch is a popular open-source helper for managing local agent profiles. Download it, select the corresponding agent, then enter your CostRouter Base URL and API key.Download cc-switch

Gemini CLI

Compatibility check required

Gemini CLI officially uses Gemini authentication variables such as GEMINI_API_KEY and GEMINI_MODEL. Use CostRouter only when your Gemini CLI version and endpoint configuration support the CostRouter Gemini-compatible host you plan to use.

Configure

Install Gemini CLI

npm install -g @google/gemini-cli

Environment

# PowerShell
$env:GEMINI_API_KEY="YOUR_COSTROUTER_API_KEY"
$env:GEMINI_MODEL="gemini-2.5-flash"

# If your Gemini CLI version supports an endpoint/base-url override,
# point it to the CostRouter Gemini-compatible host:
# https://costrouter.ai
  • Prefer a Gemini model from CostRouter Models, such as a gemini-* model ID that is currently available.
  • If your CLI build does not expose a base-url or endpoint override, use the CostRouter API from a script inside the Gemini CLI workspace instead.
  • Do not assume the OpenAI /v1 relay URL works for Gemini-native tools.
Prefer a guided setup? cc-switch is a popular open-source helper for managing local agent profiles. Download it, select the corresponding agent, then enter your CostRouter Base URL and API key.Download cc-switch

Cursor

IDE workflow

Cursor built-in model routing depends on the current Cursor settings. When custom OpenAI-compatible providers are available in your version, use the same Base URL and API key. Otherwise, run CostRouter-backed scripts and agents from the Cursor terminal.

Configure

Install Cursor

winget install Anysphere.Cursor

Project environment

# .env.local for scripts and custom agents run inside Cursor
COSTROUTER_API_KEY=YOUR_COSTROUTER_API_KEY
OPENAI_BASE_URL=https://costrouter.ai/v1
OPENAI_MODEL=gpt-4o

# Then use the OpenAI SDK baseURL/base_url examples above.
  • Use Cursor settings if your version exposes custom provider Base URL and API key fields.
  • For repo agents, store CostRouter values in .env.local and read them from your scripts.
  • Keep AI editor credentials separate from production application keys.
Prefer a guided setup? cc-switch is a popular open-source helper for managing local agent profiles. Download it, select the corresponding agent, then enter your CostRouter Base URL and API key.Download cc-switch
Production handoff checklist
  • One key per app, environment, or agent. Disable keys you no longer use.
  • Quota, expiry, model limits, and IP allowlists are set for shared or CI machines.
  • Usage Logs are reviewed after installation, after model changes, and after deploying production traffic.
  • Your app has retry/backoff handling for 429 and transient upstream failures.
  • Secrets are loaded from environment variables, secret managers, or CI vaults.
Support note

If a tool fails after configuration, capture the model name, Base URL, error code, and request time. Then compare it with Usage Logs. If no log appears, the request usually did not reach CostRouter.

Связаться с нами

Выберите канал, который лучше всего подходит для вашего запроса.

Связаться
How to Use CostRouter with AI Apps, Codex, Claude Code, Gemini CLI and Cursor