Published · Updated · AI-generated, automated fact-check against live catalog · 中文版
How usage-based LLM billing works: tokens, ledgers and 402s
TL;DR: Usage-based LLM billing charges you per token used for input and output, with costs deducted in real-time from a prepaid credit balance. It offers pay-as-you-go pricing without monthly commitments. When your balance hits zero, the API returns a clear HTTP 402 Payment Required error.
How does usage-based LLM billing work?
Most cloud LLM APIs, including TokShop, charge you per token rather than per request. A token is a chunk of text — roughly 0.75 words for English — and every prompt you send and every response you receive is broken into tokens. The API counts them, multiplies by a per-model price, and subtracts the cost from your prepaid balance.
This usage-based pricing model (also called pay-as-you-go) means you only pay for what you actually use. There is no monthly commitment, no seat license, and no surprise overage if you run one heavy batch job. But it also means you need to understand how tokens are counted, how your balance is tracked, and what happens when it runs out.
Tokens: the unit of consumption
Every LLM call has two token streams: input (the prompt, system message, conversation history) and output (the generated response). APIs report both counts in the response metadata.
For example, a typical response from TokShop’s OpenAI-compatible endpoint includes:
{
"usage": {
"prompt_tokens": 142,
"completion_tokens": 83,
"total_tokens": 225
}
}
Input and output tokens are priced differently because generating tokens (output) is computationally heavier than processing them (input). On TokShop, for instance, DeepSeek V3.2 costs $0.42 per million input tokens and $0.63 per million output tokens — a 1.5x multiplier. Other models vary: GLM 4.6 charges $0.90 input and $3.30 output (3.7x), while Qwen3 Coder charges $2.25 input and $11.25 output (5x).
You can see the full price list at TokShop pricing. The key takeaway: output tokens dominate your bill, especially on coding or reasoning models.
The ledger: prepaid credits and real-time deduction
Usage-based billing requires a balance. TokShop uses a prepaid USD credit system: you add funds to your account, and every API call deducts the exact cost in real time. There is no monthly invoice or credit card charge per call — just a running ledger.
Here is how a typical call translates to cost:
- You send a prompt. The API processes it, counts input tokens, and begins generating output.
- When generation completes, the API computes:
cost = (input_tokens * input_price_per_token) + (output_tokens * output_price_per_token) - That cost is subtracted from your ledger immediately.
For example, a 1000-input / 500-output call to DeepSeek V3.2 costs:(1000 * 0.42/1e6) + (500 * 0.63/1e6) = $0.00042 + $0.000315 = $0.000735
That is less than one tenth of a cent. You would need about 1,360 such calls to spend $1.00.
Your ledger is visible in the TokShop dashboard alongside a log of every call with its token counts and exact USD cost. This transparency helps you debug cost spikes and estimate future usage.
The 402: what happens when your balance hits zero
When your prepaid credits run out, the API returns an HTTP 402 Payment Required error. This is not a bug — it is the system enforcing your spending limit.
A typical 402 response body looks like:
{
"error": {
"message": "Insufficient balance. Please add credits in your dashboard.",
"type": "insufficient_balance",
"code": 402
}
}
Your application should handle this gracefully. A simple Python approach:
import openai
client = openai.OpenAI(base_url="https://tokshop.xyz/v1", api_key="sk-tok-...")
try:
response = client.chat.completions.create(
model="deepseek-v3.2",
messages=[{"role": "user", "content": "Hello"}]
)
print(response.choices[0].message.content)
except openai.APIStatusError as e:
if e.status_code == 402:
print("Balance exhausted. Please top up at https://tokshop.xyz/register")
else:
raise
The 402 is a clean signal: it tells you exactly why the request failed, without ambiguity. You can set up a monitoring script that checks your balance periodically via the dashboard API, or simply catch the 402 and alert your team.
How to estimate and control costs
Because pricing is per-token and per-model, you can estimate costs before building. A few practical tips:
- Use shorter system prompts. Every token in your system message is charged as input on every call.
- Set
max_tokenslimits. Without one, the model may generate very long responses, especially on creative or reasoning tasks. - Choose cheaper models for simple tasks. DeepSeek V3.2 at $0.42/$0.63 is suitable for summarisation and chat. Reserve Qwen3 Coder ($2.25/$11.25) for code generation where accuracy matters more.
- Log token usage. Store
usage.total_tokensfrom every response. Multiply by your model’s blended price (weighted average of input and output rates) to track spend over time.
For a rough estimate, assume a typical conversation averages 500 input tokens and 200 output tokens per turn. With DeepSeek V3.2, that is about $0.000336 per turn — roughly 3,000 turns per dollar.
Why usage-based billing matters for developers
Traditional SaaS billing often charges per seat or per month, regardless of actual use. For LLM APIs, where usage can spike during development and drop in production, usage-based pricing aligns cost with value. You pay more when your app is busy and less when it is idle.
The trade-off is that you must monitor your balance and handle the 402 gracefully. But with prepaid credits and transparent logging, the system is predictable. You can also set up alerts at thresholds (e.g., “warn me when balance drops below $10”) using the dashboard.
For full documentation on managing keys and checking usage, see TokShop docs.
Summary
Usage-based LLM billing works by counting tokens, applying per-model prices, and deducting from a prepaid ledger. The 402 error is your safety net — a clear signal that funds are exhausted. By understanding token economics and handling the 402 in your code, you can build cost-aware applications that never surprise you with an unexpected bill.
The same principles apply across most LLM providers, but the specific prices, model choices, and ledger mechanics vary. Check the pricing page for exact rates on the models you use, and always test with a small balance first.
FAQ
What happens when my TokShop balance runs out?
When your prepaid credits are exhausted, the API returns an HTTP 402 Payment Required error with a clear message prompting you to add more credits via your dashboard.
Why are output tokens more expensive than input tokens?
Output tokens are priced higher because generating text (output) is computationally more intensive for the model than processing it (input), with output prices typically being 1.5x to 5x higher depending on the model.
How can I estimate and control my LLM API costs?
You can control costs by using shorter system prompts, setting max_tokens limits on responses, choosing cheaper models for simpler tasks, and logging token usage to track spending over time.
All models discussed are live on our OpenAI-compatible API with transparent per-token pricing. See pricing and get a key →