Skip to content

Docs / Cost and usage

Cost and usage

Look up the exact cost of any request with GET /v1/generation, read the request id from the response headers, and export usage as CSV.

Get an API key

You pay per token from a prepaid balance. To see what a request cost:

  1. Read the request id from the x-yunshu-request-id response header.
  2. Look it up with GET /v1/generation?id=<request id>.

The cost is not in the completion response. The usage block in the response counts tokens; the price is in the lookup.

Response headers

Every response from /v1/chat/completions, /v1/messages and /v1/responses carries:

Header Value
x-yunshu-request-id The request id. Also on error responses
x-yunshu-request-ref The same id
x-yunshu-model The model that answered (differs from model when a fallback served)
x-yunshu-cache hit (served from cache, not charged) or miss

On a stream, the headers arrive before the first event. Browser apps can read all of them; they are exposed to cross-origin scripts.

Look up one request

curl "https://api.yunshurouter.com/v1/generation?id=$REQUEST_ID" \
  -H "Authorization: Bearer $YUNSHU_API_KEY"
import httpx
from openai import OpenAI

BASE = "https://api.yunshurouter.com/v1"
KEY = "tq-..."

client = OpenAI(base_url=BASE, api_key=KEY)
raw = client.chat.completions.with_raw_response.create(
    model="deepseek-v4-pro",
    messages=[{"role": "user", "content": "Hi"}],
)
request_id = raw.headers["x-yunshu-request-id"]

generation = httpx.get(
    f"{BASE}/generation",
    params={"id": request_id},
    headers={"Authorization": f"Bearer {KEY}"},
).json()["data"]
print(generation["cost"]["amount"], generation["cost"]["currency"])
import OpenAI from "openai";

const BASE = "https://api.yunshurouter.com/v1";
const KEY = process.env.YUNSHU_API_KEY!;
const client = new OpenAI({ baseURL: BASE, apiKey: KEY });

const { response } = await client.chat.completions
  .create({ model: "deepseek-v4-pro", messages: [{ role: "user", content: "Hi" }] })
  .withResponse();
const requestId = response.headers.get("x-yunshu-request-id");

const res = await fetch(`${BASE}/generation?id=${requestId}`, {
  headers: { Authorization: `Bearer ${KEY}` },
});
const { data } = await res.json();
console.log(data.cost.amount, data.cost.currency);

From the Anthropic SDK, read the header with client.messages.with_raw_response.create(...) and call the same URL.

Response

{
  "data": {
    "id": "...",
    "model": "deepseek-v4-pro",
    "created_at": "2026-11-03T08:15:02.114000+00:00",
    "streamed": false,
    "cache_hit": false,
    "status": 200,
    "error": null,
    "latency_ms": 1840.2,
    "ttft_ms": null,
    "tokens": {
      "input": 1540,
      "cached_input": 0,
      "output": 212,
      "reasoning": 0,
      "saved": null
    },
    "cost": {
      "currency": "USD",
      "amount": "0.000731",
      "micros": 731
    },
    "api_key_id": "..."
  }
}
Field Meaning
id The request id
model The model that answered
created_at When the request was made (UTC, ISO 8601)
streamed Whether it was a stream
cache_hit Served from our cache. Cost is 0
status The HTTP status of the request
error Our error code if it failed, otherwise null
latency_ms Total time, in milliseconds
ttft_ms Time to first token for a stream; null otherwise
tokens.input Input tokens billed
tokens.cached_input Input tokens billed at the cached input price
tokens.output Output tokens billed, reasoning included
tokens.reasoning Of the output, how many were reasoning
tokens.saved The token saver’s estimate of tokens saved; null when it did not run
cost.currency Your balance currency: USD, CNY or SGD
cost.amount The amount charged, as a decimal string
cost.micros The same amount in millionths of a currency unit (an integer)
api_key_id The key that made the request

Notes on lookups

  • You can look up any request made by any key in your organisation. Requests from other organisations answer 404 not_found.
  • A stream is recorded when it ends.
  • Each lookup counts as one request against the key’s rate limit. Do not poll it in a tight loop.
  • id must be 4 to 64 characters.

What is charged

  • Input, cached input and output tokens, each at the model’s price for that line, in your currency.
  • Requests served from our cache: not charged.
  • Failed requests, including streams that fail part-way: not charged.
  • A reply with no usage report is billed on the text actually returned, never on max_tokens.

Before a request is sent, its highest possible price is held from your balance, so requests running at the same time can never spend more than you have. The hold is released when the request finishes and only the real cost is taken.

Usage in the console

The console’s Usage page shows spend and requests by day, model and key, with per-request detail.

CSV export

On the Usage page, choose Export CSV. The file opens in Excel as UTF-8, and cells that would start a formula are escaped. Columns:

Column Meaning
request_id The request id
created_at When it was made (UTC)
offer_id The model that answered
status_code HTTP status
input_tokens, cached_read_tokens, output_tokens, reasoning_tokens Token counts
price The amount charged
currency Your balance currency
latency_ms, ttft_ms Timing
streamed, cache_hit true or false
error_type Error code if the request failed

Monthly invoices

Each month’s invoice is built from the same records and reconciles to your opening and closing balance. Find them under Billing in the console.