Get started
Python, JavaScript and developer tools
Connect official SDKs, custom base URLs, Codex and Claude Code to Token4AI Cloud.
Updated
Python SDK
Install openai and explicitly pass the Token4AI key and base_url. TOKEN4AI_MODEL must come from the current key’s catalog. This example uses a bounded timeout and one SDK retry as a starting point; adjust to the workload without adding unbounded outer retries.
python -m pip install openaiimport os
from openai import OpenAI
client = OpenAI(
api_key=os.environ["TOKEN4AI_API_KEY"],
base_url="https://api.token4ai.cloud/v1",
timeout=60.0,
max_retries=1,
)
reply = client.chat.completions.create(
model=os.environ["TOKEN4AI_MODEL"],
messages=[{"role": "user", "content": "Explain API authentication."}],
)
print(reply.choices[0].message.content)JavaScript / TypeScript
Install openai in a Node.js backend. JavaScript uses baseURL while Python uses base_url. The following stream example prints text deltas; production applications must also handle tools, terminal state and errors.
npm install openaiimport OpenAI from "openai";
const client = new OpenAI({
apiKey: process.env.TOKEN4AI_API_KEY,
baseURL: "https://api.token4ai.cloud/v1",
timeout: 60_000,
maxRetries: 1,
});
const stream = await client.chat.completions.create({
model: process.env.TOKEN4AI_MODEL,
messages: [{ role: "user", content: "Explain streaming." }],
stream: true,
});
for await (const chunk of stream) {
process.stdout.write(chunk.choices[0]?.delta?.content ?? "");
}Codex
Prefer token4ai agent setup --agent codex: it reuses or creates an API key, merges ~/.codex/config.toml and auth.json, and backs up before writing. For a manual setup, merge the config.toml below and put the API key in OPENAI_API_KEY inside ~/.codex/auth.json. Back up existing configuration and select a Responses-capable model in your group. HTTP requests retry up to 4 times and SSE interruptions retry up to 5 times; the current turn ends after retries are exhausted.
model_provider = "token4aicloud"
model = "YOUR_MODEL_ID"
[model_providers.token4aicloud]
name = "Token4AI Cloud"
base_url = "https://api.token4ai.cloud/v1"
wire_api = "responses"
requires_openai_auth = true
request_max_retries = 4
stream_max_retries = 5
stream_idle_timeout_ms = 300000{
"OPENAI_API_KEY": "YOUR_API_KEY"
}Claude Code and Anthropic SDK
Claude Code uses Anthropic-native streaming requests. Merge the four env keys below into the user-level ~/.claude/settings.json and keep existing hooks, permissions, theme, plugins, and other env keys, so every project uses the same Token4AI settings. ANTHROPIC_BASE_URL must be https://api.token4ai.cloud without a trailing /v1. Set ANTHROPIC_MODEL explicitly so client aliases cannot select a model outside the billing group.
- Use ANTHROPIC_API_KEY only. Do not set ANTHROPIC_AUTH_TOKEN at the same time. Remove stale ANTHROPIC_* overrides from the current shell and ~/.zshrc, ~/.bashrc, or the PowerShell Profile, then restart the terminal.
- After a manual merge on macOS or Linux, run chmod 600 ~/.claude/settings.json so only your user can read or write it. token4ai agent setup --agent claude-code merges the configuration, writes the key and tightens permissions automatically.
- Use /status inside Claude Code to inspect the effective Base URL and model, then run the minimal request below. The provider path is fully connected only when the real response reports usage.
- An upstream 400 means the provider rejected the request. A following provider_circuit_open 503 is a gateway circuit-breaker short circuit and does not mean the provider returned 503. Keep the fg- request id when troubleshooting.
- The Polaris provider connection test is a simple non-streaming probe and does not replace a real Claude Code streaming request. Vega client configuration and CC Switch imports contain credentials and should only be shared with trusted local tools.
{
"env": {
"ANTHROPIC_BASE_URL": "https://api.token4ai.cloud",
"ANTHROPIC_API_KEY": "YOUR_API_KEY",
"ANTHROPIC_MODEL": "claude-opus-5",
"CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC": "1"
}
}claude --version
claude --model claude-opus-5 -p "Reply with exactly OK" --output-format jsonOfficial references and compatibility
Official SDK references explain client behavior; the Token4AI documentation and catalog define this gateway’s integration scope. Do not assume every provider product, administration endpoint or built-in tool is exposed here.