API reference

Tokun supports three API formats on one metered pipeline: the OpenAI Chat Completions and Responses APIs, and the Anthropic Messages API. Use the one your client already supports. Model IDs, billing, and routing work the same way on all three.

Base URL and authentication

Authenticate every request with a Tokun API key (starts with sk-) from the API keys page. The base URL depends on the API format you call:

API formatBase URLAuth header
OpenAI (Chat Completions, Responses)https://api.tokun.sh/v1Authorization: Bearer sk-…
Anthropic (Messages)https://api.tokun.shx-api-key: sk-… (or Authorization: Bearer)
The Anthropic base URL has no /v1 suffix because the Anthropic SDK adds the path itself. Use https://api.tokun.sh for Messages and https://api.tokun.sh/v1 for the OpenAI endpoints.

If your client can't set an Authorization header, you can put the key in the URL path instead: POST /v1/{sk-key}/chat/completions.

POST /v1/chat/completions

The OpenAI Chat Completions API. Send a standard OpenAI request body with a Tokun model ID. Set "stream": true to stream tokens over SSE. Responses are byte-compatible with the official OpenAI format.

bash
curl https://api.tokun.sh/v1/chat/completions \
  -H "Authorization: Bearer $TOKUN_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "anthropic/claude-opus-4-8",
    "messages": [{"role": "user", "content": "Say hello in one word."}]
  }'

POST /v1/responses

The OpenAI Responses API, with the same authentication and model IDs as Chat Completions. Use it if your SDK targets the Responses API. It is stateless: store is always false, previous_response_id is rejected, and GET /v1/responses/{id} returns 404. Use the Anthropic Messages API if you need signed thinking blocks to round-trip across turns.

POST /v1/messages

The Anthropic Messages API, used by the Anthropic SDKs and Claude Code. Buffered or streaming with the official Anthropic event protocol; signed thinking blocks round-trip in multi-turn tool use. Authenticate with x-api-key: sk-… or Authorization: Bearer.

bash
curl https://api.tokun.sh/v1/messages \
  -H "x-api-key: $TOKUN_API_KEY" \
  -H "anthropic-version: 2023-06-01" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "anthropic/claude-opus-4-8",
    "max_tokens": 64,
    "messages": [{"role": "user", "content": "Say hello in one word."}]
  }'

POST /v1/messages/count_tokens

Returns a conservative token estimate for a Messages request. It is free: no charge and no balance hold.

GET /v1/models

Official model pools

Official model pools appear in the model catalog and use a pool ID such as flash-pool. Choose an available pool from the live catalog; pool names and members are configured by Tokun administrators.

When the pool enables member selection, use pool-id/member-name, replacing the model's lab prefix with the pool ID. The selected member is the only model that can execute; the pool's pricing mode still applies. When selection is disabled, this form returns 400.

bash
# Set TOKUN_API_KEY and TOKUN_POOL_ID from your account and the live catalog.
curl "${TOKUN_BASE_URL:-https://api.tokun.sh/v1}/chat/completions" \
  -H "Authorization: Bearer $TOKUN_API_KEY" \
  -H "Content-Type: application/json" \
  -d "{\"model\":\"$TOKUN_POOL_ID\",\"messages\":[{\"role\":\"user\",\"content\":\"Hello\"}],\"max_tokens\":64}"
bash
# The pool must enable member selection. TOKUN_MEMBER is the member name without its lab prefix.
curl "${TOKUN_BASE_URL:-https://api.tokun.sh/v1}/chat/completions" \
  -H "Authorization: Bearer $TOKUN_API_KEY" \
  -H "Content-Type: application/json" \
  -d "{\"model\":\"$TOKUN_POOL_ID/$TOKUN_MEMBER\",\"messages\":[{\"role\":\"user\",\"content\":\"Hello\"}],\"max_tokens\":64}"

Every pool response echoes the model ID you sent: the pool ID for an automatic request, and pool-id/member-name when you selected a member. The canonical ID of the member that served the request travels in the tokun-resolved-model header, and in executed_public_model on the usage record. Your bill follows the configured pool pricing mode.

A pool stays listed when every member is unavailable. Calling it returns 503; requests never leave the pool to find another model.

Pool api_surfaces is the union of its members' supported APIs. Each request uses a member that supports its API format. Context and output limits are the minimum curated values across members; unknown limits are omitted. Pool count_tokens uses the first supported Anthropic member in routing order and returns its tokenizer count without a charge.

Lists the available model IDs and each model's context window. The response format follows the request: a caller that sends x-api-key or anthropic-version (as Anthropic clients do) gets Anthropic's list format; every other caller gets OpenAI's list format. Listing is free (no charge, no hold).

Errors

StatusMeaningFix
401Missing or invalid API keySend a valid Tokun sk- key.
402Your account balance cannot cover the requestTop up on the Billing page.
403The account has funds, but this key is over its own budget capSend the request with a key that has no budget cap, or create a key with a higher cap.
404/v1 added to the Anthropic base URLUse https://api.tokun.sh (no /v1) for Messages.
400 — unknown modelModel ID not recognizedUse a model or pool ID from the live catalog. An unknown ID does not select another model.
429 / 5xxRate limit or provider errorPassed through from the provider that served the request. Retry with backoff.
402 and 403 are different conditions. 402 is the account balance: the message is insufficient balance. 403 is the per-key budget cap: the message is credential budget exceeded. Tokun checks the balance first, so an empty account returns 402 even when the key is also over its cap. On Messages the same two statuses arrive in the Anthropic envelope, typed billing_error and permission_error.
Individual models use their token rates. Model pools use one of three modes: fixed pool token rates for every member, the serving member's frozen token rates, or a fixed price per request. Token prices are quoted in USD per 1M tokens; per-request prices are quoted in USD per request. Before forwarding, Tokun places a balance hold for the estimated maximum cost. Token-priced requests settle from the serving model's reported usage: an individual model at its published token rates, a model pool according to its pricing mode. Fixed per-request pools charge once after an upstream success response begins, even if usage is missing, the response is partial or the client disconnects. Failures before success release the hold; unsuccessful retries add no per-request charge.