AIDiscount优汇智
Home / Blog

Use Claude Code, Codex CLI and OpenCode with a Third-Party API

Configuration for running terminal coding agents through a third-party API: environment variables for Claude Code, config.toml for Codex CLI and opencode.json for OpenCode.

2026-11-25 · 3 min read · AIDiscount team

Claude CodeCodex CLIOpenCodeCoding tools

Terminal coding agents read their API address and key from environment variables or a config file. Pointing them at another provider takes a few lines. This guide covers three popular agents. Each one uses a different API format, and AIDiscount accepts all three with the same key.

ToolAPI formatBase URL
Claude CodeAnthropic Messageshttps://aidiscount.ai
Codex CLIOpenAI Responseshttps://aidiscount.ai/v1
OpenCodeOpenAI Responses or Chat Completionshttps://aidiscount.ai/v1

Create a key on the API Keys page after you sign up. Get model IDs from the model marketplace.

Claude Code

Set two environment variables before you run claude:

# macOS / Linux
export ANTHROPIC_BASE_URL="https://aidiscount.ai"
export ANTHROPIC_AUTH_TOKEN="sk-..."

# Windows PowerShell
$env:ANTHROPIC_BASE_URL="https://aidiscount.ai"
$env:ANTHROPIC_AUTH_TOKEN="sk-..."

Do not add /v1 to the base URL. Claude Code adds /v1/messages itself.

To make the setting permanent, add the export lines to ~/.bashrc or ~/.zshrc. Alternatively, put them in the env block of ~/.claude/settings.json:

{
  "env": {
    "ANTHROPIC_BASE_URL": "https://aidiscount.ai",
    "ANTHROPIC_AUTH_TOKEN": "sk-..."
  }
}

If you have previously logged in to Claude Code with an account, run /logout first so it uses the token from the environment.

Codex CLI

Add a provider to ~/.codex/config.toml:

model_provider = "aidiscount"
model = "YOUR_MODEL_ID"

[model_providers.aidiscount]
name = "AIDiscount"
base_url = "https://aidiscount.ai/v1"
env_key = "AIDISCOUNT_API_KEY"
wire_api = "responses"

Then set the key and start Codex:

export AIDISCOUNT_API_KEY="sk-..."
codex

With models that Codex does not know, it prints Model metadata for ... not found. Defaulting to fallback metadata. Requests still work. The warning means Codex does not know the model's context size. Tell it, so it compacts long sessions at the right point. For a model with a 200K-token context:

model_context_window = 200000
model_auto_compact_token_limit = 160000

Put these two lines at the top of config.toml. Use the context size shown on the model's page in the marketplace.

OpenCode

Add a provider to opencode.json (in your project, or in ~/.config/opencode/ for all projects):

{
  "$schema": "https://opencode.ai/config.json",
  "provider": {
    "aidiscount": {
      "npm": "@ai-sdk/openai",
      "name": "AIDiscount",
      "options": {
        "baseURL": "https://aidiscount.ai/v1",
        "apiKey": "{env:AIDISCOUNT_API_KEY}"
      },
      "models": {
        "YOUR_MODEL_ID": { "name": "YOUR_MODEL_ID" }
      }
    }
  }
}

The @ai-sdk/openai package uses the Responses API. If a model gives errors there, change the package to @ai-sdk/openai-compatible, which uses Chat Completions. The same key works for both.

Check that it works

  1. Ask the agent something small, such as "What files are in this folder?"
  2. Open the Usage page in your AIDiscount dashboard. The request should appear with the model name and the amount charged.

If nothing shows up, the agent is still using its previous provider. Check that the environment variables are set in the same terminal where you started the agent.

Choosing a model for agents

Agents make many tool calls in a row and resend the conversation each time, so they use a lot of input tokens. A strong model makes fewer mistakes and needs fewer steps. A cheaper model costs less per step. A practical approach is to use a strong model for planning and hard bugs, and a cheaper one for routine edits. Prices for some lower-cost options:

ModelInput / 1M tokensOutput / 1M tokensvs. official price
DeepSeek V4.1 Flash
deepseek-v4.1-flash
$0.0857$0.342942.9% off
GLM-5.3
glm-5.3
$0.6857$2.4045.5% off
Kimi K2.7 Code
kimi-k2.7-code
$0.5571$2.31441.4% off
DeepSeek V4 Pro
deepseek-v4-pro
$0.3857$1.157141.6% off

Prices are read from the live model marketplace when this page is generated and may change. The marketplace always shows the current rate.

For more on keeping agent costs down, see How to cut AI API costs.

Try it with AIDiscount. One API key works with OpenAI- and Anthropic-compatible tools. Pay as you go, no monthly fee.
Create a free accountRead the API docs

Related guides

Languages

English · 简体中文 · 繁體中文 · 日本語 · 한국어 · Español · Français · Deutsch · Português · Русский