Choose Auto, TokenLab Verified, or Official for each request, with prices shown up front. See what's new

Understanding TokenLab HTTP Headers and Native Protocol Endpoints

·September 19, 2026·5 min read·Updated November 11, 2026·1927 views
#feature#api-formats#developer-experience#agents
Understanding TokenLab HTTP Headers and Native Protocol Endpoints

TokenLab accepts four API formats: OpenAI Chat Completions, OpenAI Responses, Anthropic Messages, and Gemini. You choose the format by the endpoint you call. No request header or response header switches it. Headers carry other things: your credentials, the delivery option, IDs for support and billing, and rate limit information. This guide lists the headers TokenLab documents, says which of them can be missing, and shows a client that reads them correctly.

The Endpoint Decides the Format

Each endpoint keeps the schema of its own API format:

  • Chat Completions (/v1/chat/completions) uses the OpenAI-compatible schema: choices, message.content, and a usage block with prompt_tokens, completion_tokens, and total_tokens.
  • Responses (/v1/responses) uses the OpenAI Responses format, with background responses, tools, and events. Use it only when the model lists openai_responses.
  • Anthropic Messages (/v1/messages) uses the native Anthropic schema, with content blocks, thinking, and usage.output_tokens. When you set up the Anthropic SDK, use https://api.tokenlab.sh as the base URL, without /v1.
  • Gemini (/v1beta/models/{model}:generateContent) accepts native Gemini contents and parts and returns Gemini candidates.

A model does not necessarily accept all four formats. Before you route a request, call Get a Model (GET /v1/models/{model}, public, no key) and read tokenlab.accepted_request_formats. The API formats guide explains when to choose each format.

Request Headers

Header Use
Authorization: Bearer sk-... Your API key for model requests. Management endpoints use a management token (mt-...) in the same header. Anthropic Messages also accepts x-api-key. Gemini also accepts x-goog-api-key or the key query parameter.
Content-Type: application/json Required for POST requests with a JSON body
X-TokenLab-Delivery-Policy auto, verified, or official. It overrides the API key setting for one request. An invalid value returns 400. See the delivery tiers guide.
Idempotency-Key For the requests listed in Retry safely with an idempotency key. A retry with the same key and body does not run or charge the request twice.

Response Headers

Use these headers when they are present. Some are not set on every response.

Header Meaning
X-Request-ID The request identifier. Log it and send it to support.
X-Routing-Time-MS Non-streaming: milliseconds until the upstream response arrived, including queueing and inference. Streaming: always 0. It is not a routing or queue measurement.
X-Task-ID The public task ID of an async job, such as video, music, 3D, or a task-based image. It matches the task id.
X-Billing-Transaction-ID The billing transaction, after final billing. It appears only when billing settled before the response was sent.

For non-streaming OpenAI-compatible responses, the same value is also in the JSON body as billing_transaction_id. Gemini and other native formats may expose it only in the header. A streaming call can settle after the connection closes. When the ID is absent, read it from your usage records later. See the Billing and pricing guide.

Headers on a 429 response

When a request exceeds the limit of its API key, TokenLab returns 429 rate_limit_exceeded with four headers:

Header Meaning
Retry-After Seconds to wait before the next request
X-RateLimit-Limit Requests allowed per minute for this API key
X-RateLimit-Remaining Requests left in the current window
X-RateLimit-Reset Unix time in seconds when the window resets

Use the Retry-After value for retries instead of a fixed backoff. Read the limit from X-RateLimit-Limit instead of assuming a copied table. Repeated failed authentication from one IP address is limited too. After about 20 failures in a minute the API returns 429 with a Retry-After header. Fix the key instead of retrying a 401. The Rate limits guide has the details.

Example: Capture the IDs and Retry on 429

This Python client sends a Chat Completions request, logs the request ID, reads the billing ID from the body or the header, and retries only on 429:

import os
import time

import requests

API_KEY = os.environ["TOKENLAB_API_KEY"]
URL = "https://api.tokenlab.sh/v1/chat/completions"
HEADERS = {
    "Authorization": f"Bearer {API_KEY}",
    "Content-Type": "application/json",
}
PAYLOAD = {
    "model": "gpt-5.6-terra",
    "messages": [{"role": "user", "content": "Summarize system status."}],
}

for attempt in range(3):
    response = requests.post(URL, headers=HEADERS, json=PAYLOAD, timeout=30)
    request_id = response.headers.get("X-Request-ID")  # log it for support

    if response.status_code == 200:
        data = response.json()
        # Present only when billing settled before the response was sent.
        billing_id = data.get("billing_transaction_id") or response.headers.get(
            "X-Billing-Transaction-ID"
        )
        print(f"request_id={request_id} billing_transaction_id={billing_id}")
        print(data["choices"][0]["message"]["content"])
        break

    if response.status_code == 429:
        wait = float(response.headers.get("Retry-After", 2 ** attempt))
        print(
            f"request_id={request_id} rate limited: "
            f"limit={response.headers.get('X-RateLimit-Limit')}/min, "
            f"remaining={response.headers.get('X-RateLimit-Remaining')}, "
            f"resets at Unix time {response.headers.get('X-RateLimit-Reset')}"
        )
        time.sleep(wait)
        continue

    response.raise_for_status()
else:
    raise RuntimeError("Still rate limited after 3 attempts")

Both IDs can be None. The request ID is the main key for support. The billing ID is a convenience that can arrive later through usage records.

What to Log

  • Keep X-Request-ID, the billing transaction ID when present, and X-Task-ID for async jobs, with the status code and latency.
  • Never log Authorization headers, API keys, management tokens, or private signed URLs.
  • For server-side reconciliation, use GET /v1/management/api-keys/{keyId}/usage instead of scraping dashboard pages or estimating from token counts. See Logs and troubleshooting.

Sources

Prepared by TokenLab's AI agents. Facts are checked against the linked primary sources on the date shown.

Related models

Recent model releases

Try the models from this article

Chat, create images, or make video with the same TokenLab balance.

Your first API call

Get your API key
curl https://api.tokenlab.sh/v1/chat/completions \
  -H "Authorization: Bearer $TOKENLAB_API_KEY" -H "Content-Type: application/json" \
  -d '{"model":"gpt-5.6-terra","messages":[{"role":"user","content":"Hello!"}]}'