Tokun

Tokun Docs

Tokun is a metered, OpenAI-compatible LLM gateway. Point any OpenAI client at the base URL below, pass a Tokun model id, and Tokun meters your usage and routes the request to an eligible upstream. Today (V0) that is the direct/official provider; reseller and discount providers roll in over time, so the same model gets cheaper.

Quick Start

Fastest path — hand this one line to your coding agent (Claude Code, Codex, Cursor, …) and it will read the full machine-readable guide at /llms.txt and wire Tokun up for you. You only supply your key.
text
Read https://tokun.sh/llms.txt and integrate Tokun into this project.

Prefer to do it by hand? It is two changes — the base URL and the API key.

1. Create an API key

Sign in to the Tokun console, open API Keys, and click Create key. The full secret (sk-…) is shown exactly once — copy it immediately; only a short prefix is stored and displayed afterward. Keys are billed against your prepaid balance, so treat them like passwords.

Add a balance first under Billing (top-ups run $5–$1,000 via Stripe). A request with no balance returns 402 Payment Required.

2. Base URL & auth

Value
Base URLhttps://api.tokun.sh/v1
Auth headerAuthorization: Bearer sk-...
FormatOpenAI Chat Completions
EndpointPOST /v1/chat/completions (streaming via "stream": true)

3. First call

curl:

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."}]
  }'

OpenAI SDK (Python):

python
from openai import OpenAI

client = OpenAI(
    base_url="https://api.tokun.sh/v1",
    api_key="sk-...",  # your Tokun key
)

resp = client.chat.completions.create(
    model="anthropic/claude-opus-4-8",  # or "openai/gpt-5.5"
    messages=[{"role": "user", "content": "Say hello in one word."}],
)
print(resp.choices[0].message.content)

OpenAI SDK (Node):

node
import OpenAI from "openai";

const client = new OpenAI({
  baseURL: "https://api.tokun.sh/v1",
  apiKey: process.env.TOKUN_API_KEY, // your sk-... key
});

const resp = await client.chat.completions.create({
  model: "anthropic/claude-opus-4-8", // or "openai/gpt-5.5"
  messages: [{ role: "user", content: "Say hello in one word." }],
});
console.log(resp.choices[0].message.content);

4. Models

Pass a Tokun model id in the model field. Ids follow the lab/model convention — the lab that made the model, then the model name. Tokun resolves the id to an upstream and routes the request; you never pass the upstream id directly.

model idServed byPrice (input / output per 1M tokens)
anthropic/claude-opus-4-8Anthropic Claude Opus 4.8$5.00 / $25.00
anthropic/claude-opus-4-7Anthropic Claude Opus 4.7$5.00 / $25.00
anthropic/claude-sonnet-5Anthropic Claude Sonnet 5$2.00 / $10.00
anthropic/claude-sonnet-4-6Anthropic Claude Sonnet 4.6$3.00 / $15.00
anthropic/claude-haiku-4-5Anthropic Claude Haiku 4.5$1.00 / $5.00
anthropic/claude-fable-5Anthropic Claude Fable 5$10.00 / $50.00
openai/gpt-5.5OpenAI GPT-5.5$5.00 / $30.00
openai/gpt-5.4OpenAI GPT-5.4$2.50 / $15.00
openai/gpt-5.4-miniOpenAI GPT-5.4 mini$0.75 / $4.50
openai/gpt-5.4-nanoOpenAI GPT-5.4 nano$0.20 / $1.25
openai/gpt-5.3-codexOpenAI GPT-5.3 Codex$1.75 / $14.00
gemini/gemini-3.1-pro-previewGoogle Gemini 3.1 Pro (preview)$2.00 / $12.00
gemini/gemini-3.1-flash-lite-previewGoogle Gemini 3.1 Flash Lite (preview)$0.25 / $1.50
xai/grok-4.5xAI Grok 4.5$2.00 / $6.00
xai/grok-4.3xAI Grok 4.3$1.25 / $2.50
deepseek/deepseek-v4-proDeepSeek V4 Pro$0.435 / $0.87
deepseek/deepseek-v4-flashDeepSeek V4 Flash$0.14 / $0.28
moonshot/kimi-k2.7-codeMoonshot Kimi K2.7 Code$0.95 / $4.00
moonshot/kimi-k2.6Moonshot Kimi K2.6$0.60 / $3.00
glm/glm-5.2Z.ai GLM-5.2$1.40 / $4.40
glm/glm-4.6Z.ai GLM-4.6$0.60 / $2.20
minimax/MiniMax-M3MiniMax M3$0.30 / $1.20
qwen/qwen3-maxQwen3 Max$1.20 / $6.00
qwen/qwen3.7-plusQwen3.7 Plus$0.40 / $1.60

Model ids are accepted flexibly so Claude Code and Codex work out of the box: you can send the lab/model id above or the bare model name on its own (claude-haiku-4-5, gpt-5.4-mini) — both resolve to the same offering. A trailing context-window selector ([1m]), a provider/region prefix (anthropic/, us.anthropic.), and a trailing date stamp are all tolerated and ignored. anthropic/claude-fable-5 is listed but not yet enabled (coming soon). An id that does not match a served model is rejected with unknown model — Tokun never silently substitutes a different model, so unsupported ids like gpt-5.5-codex or gpt-5.2 return an error rather than billing you for the wrong model.

5. Integration recipes

Any agent runner that exposes an OpenAI base URL works. Set OPENAI_BASE_URL to the Tokun gateway and OPENAI_API_KEY to your sk-… key.

Prefer a step-by-step guide with a copy-for-agent block? See Use Tokun with Claude Code (more per-tool guides coming).

Codex — set the OpenAI env vars and choose a Tokun model:

bash
export OPENAI_BASE_URL="https://api.tokun.sh/v1"
export OPENAI_API_KEY="sk-..."
# Then point Codex at a Tokun model id:
codex --model openai/gpt-5.5   # or: anthropic/claude-opus-4-8

OpenCLAW — point its OpenAI provider at the gateway (env or config):

bash
OPENAI_BASE_URL=https://api.tokun.sh/v1
OPENAI_API_KEY=sk-...
OPENAI_MODEL=openai/gpt-5.5

Hermes — same OpenAI-compatible base URL + key; set the model to openai/gpt-5.5 or anthropic/claude-opus-4-8:

bash
OPENAI_BASE_URL=https://api.tokun.sh/v1
OPENAI_API_KEY=sk-...
# model: anthropic/claude-opus-4-8

Anthropic SDK / Claude Code

Tokun also serves the Anthropic Messages API (POST /v1/messages), so Anthropic SDK clients — including Claude Code — work by changing the base URL and key. Auth is the Anthropic-native x-api-key header (Authorization: Bearer also works); pass your sk-… key. Note the base URL has no /v1 suffix — the Anthropic SDK adds the path itself.

Claude Code — point it at Tokun and pin a Tokun model id:

bash
export ANTHROPIC_BASE_URL="https://api.tokun.sh"
export ANTHROPIC_API_KEY="sk-..."
export ANTHROPIC_MODEL="anthropic/claude-opus-4-8"
claude

Anthropic SDK (Python):

python
import anthropic

client = anthropic.Anthropic(
    base_url="https://api.tokun.sh",
    api_key="sk-...",  # your Tokun key
)

msg = client.messages.create(
    model="anthropic/claude-opus-4-8",  # or "openai/gpt-5.5"
    max_tokens=1024,
    messages=[{"role": "user", "content": "Say hello in one word."}],
)
print(msg.content[0].text)

Use Tokun model ids (anthropic/claude-opus-4-8, openai/gpt-5.5) — protocol and model are independent, so the Anthropic SDK can call GPT through this surface too. The bare Anthropic-native id (claude-opus-4-8, claude-haiku-4-5) is normalized to the same offering, so Claude Code's default and /model picker work without pinning a Tokun id. An id with no served match returns a clear invalid_request_error (never a silent substitution).

  • Supported: text conversations (buffered + streaming), system prompts (string or text blocks), client tools (tools / tool_use / tool_result), tool_choice, stop_sequences, temperature / top_p, POST /v1/messages/count_tokens (free, returns a conservative estimate). Prompt caching (cache_control) is honored and billed at cache rates on native Anthropic-dialect routes, and accepted-and-ignored on OpenAI chat-bridge routes.
  • Extended thinking: supported. thinking: {type:"enabled", budget_tokens} (validated like the official API: budget ≥ 1024 and < max_tokens) and the output_config.effort dial both work; responses carry signed thinking blocks before the text (streaming emits thinking_delta / signature_delta events), and replayed thinking blocks round-trip upstream on multi-turn tool use. Extended thinking is forwarded verbatim across all channels — automatic, official, reseller, and discount, including a credential explicitly pinned to the discount channel; nothing is stripped at runtime (TXE-670 reversed the earlier pinned-discount degrade).
  • Not supported — rejected with a clear invalid_request_error: image / document blocks, server tools other than Anthropic web search (e.g. code execution), mcp_servers, structured outputs via the deprecated top-level output_format (use output_config.format, which IS supported). (Anthropic web search is the exception — natively supported and billed on Anthropic-dialect routes.) max_tokens is required, as in the official API.
  • Errors use the Anthropic envelope (authentication_error, invalid_request_error, billing_error on 402, …) with the same statuses as the OpenAI surface.

FAQ

What is Tokun, in one line?

A single OpenAI-compatible endpoint that meters your usage and routes the model you asked for to an eligible upstream — across three kinds of providers, so the same model can be served more cheaply over time.

What are the three channels Tokun routes across?

Every model can be served from one of three channel types. They serve the same model; they differ in who you are ultimately buying capacity from.

  • Direct / official — the model owner or a first-party cloud. For anthropic/claude-opus-4-8 that is Anthropic directly; for cloud-hosted models it is a first-party cloud such as Google Vertex, Azure OpenAI, or AWS Bedrock. This is the canonical source.
  • Resellers — aggregators that resell official capacity, e.g. OpenRouter or GMI. They sit between you and the official provider and typically price at or slightly above official.
  • Discount providers — independent hosts that serve the same model below reseller/official pricing. These are the cheapest channel and are how Tokun lowers your bill.

Today (V0) both anthropic/claude-opus-4-8 and openai/gpt-5.5 route to their direct/official providers (Anthropic and OpenAI). Reseller and discount channels are part of the routing design and roll in over time; your usage records show who actually served each request.

Is it really the same model?

Yes. A Tokun model id maps to one logical model (e.g. anthropic/claude-opus-4-8 → Claude Opus 4.8). Whichever channel serves it, the weights are the same model — the difference is the billing surface and price, not the model.

How does pricing and billing work?

Prepaid and per-token. You top up a balance (USD, $5–$1,000 per top-up via Stripe), and each request draws down that balance by metered consumption: input tokens × the model's input rate + output tokens × the model's output rate, times any account multiplier. Prices are listed per 1M tokens (see the Models table).

Mechanically, Tokun places a balance hold for the request's estimated maximum cost before forwarding, then settles the exact charge from the upstream's reported token usage. If a request fails before the upstream responds, the hold is released and you are not charged.

Is my data / prompt private?

Your prompts and completions pass through Tokun in transit to whichever upstream provider fulfills the request, and are processed transiently to operate and meter the Service. Tokun logs request metadata (timestamps, model, token counts, latency, status, which key) for billing and security — not the prompt/completion bodies as a product feature. The upstream provider that serves a request handles that content under its own terms and privacy policy. See the Privacy Policy.

Why is it cheaper?

Because Tokun can route the same model to a lower-cost channel (a discount provider or reseller) instead of always paying official list price, while still settling your bill at the Tokun list price for that model. You get one endpoint and one balance instead of negotiating with each provider yourself.

Are there rate limits?

There is no fixed requests-per-second quota in the beta. The effective limits are economic: your prepaid balance and any per-key budget cap. A request is rejected with 402 when your balance is insufficient, or 403 when a key's budget is exhausted. Upstream providers may apply their own limits, surfaced as 429. This is a beta service with no uptime SLA.

What does "served by" / served_vendor mean?

It is the upstream provider that actually served a given request — the authoritative value reported by the routing layer (e.g. anthropic or openai). Tokun records it as the served_vendor usage dimension so you can see which channel/provider fulfilled each call. When a fanned-out request's upstream can't be determined, it is recorded as unattributed rather than left blank.

How do I pick or filter a channel?

You pick the model (anthropic/claude-opus-4-8 / openai/gpt-5.5); Tokun picks the channel that serves it. In V0 that is the direct/official provider; as reseller and discount channels roll in, Tokun picks among them to lower cost. There is no per-request channel selector in the OpenAI request body today. The served_vendor on your usage records shows which channel was used after the fact. Explicit channel preference/filtering is a routing-policy feature, not a request parameter.

Can I use the Anthropic API format / Claude Code?

Yes. The gateway serves the Anthropic Messages API at POST /v1/messages (plus POST /v1/messages/count_tokens), so Anthropic SDK clients and Claude Code work: set ANTHROPIC_BASE_URL=https://api.tokun.sh and ANTHROPIC_API_KEY=sk-…, and pin a Tokun model id. See the Anthropic SDK / Claude Code section for the supported feature set (extended thinking is supported, with signed thinking blocks round-tripping on multi-turn tool use; image blocks are rejected).

Which endpoints exist?

POST /v1/chat/completions (buffered or streaming), POST /v1/responses (OpenAI Responses API, stateless), and POST /v1/messages (Anthropic Messages API) are the inference endpoints — all three run the same metering pipeline. POST /v1/messages/count_tokens returns a free token estimate. GET /v1/models lists the available model ids in the OpenAI format (same Authorization: Bearer key; listing is free). There is no /v1/embeddings surface today. The gateway also exposes /healthz and /readyz for liveness only.