# LLM API Tokens Explained: How to Top Up Credit and How Token Billing Works
Before you can budget for large language model (LLM) API usage, you need to understand what is actually being metered, why providers require prepaid credit, and which pricing dimensions drive your bill. This guide covers the standard five-step path to obtain API credit, the three configurations you should set up right after funding an account, the input/output and context-window pricing tiers behind most rate tables, and a reusable cost model you can build before you write a single line of production code. It also lists the most common token-billing pitfalls, ordered cost-optimization practices, and how individual developers and enterprise teams should approach budget, quota, key, and audit control.
1. Start with the billing unit: what a token is
Before discussing “buying tokens,” get the technical model right: a token is not a currency and not a transferable virtual asset. It is the smallest unit of measurement a model service uses after splitting input and output text. During inference, the model never processes characters directly. Text is first encoded into a sequence of subword units, and both computation and billing happen at the token level.
Token counts and character counts are not interchangeable. In a mixed-language prompt:
- English text is split into subwords, so a single word often becomes one to three tokens.
- In Chinese, one common character usually maps to roughly one token.
- Punctuation, whitespace, code indentation, and line breaks are all counted too.
Two consequences follow:
- You cannot estimate a bill from character counts. You need exact counts from a tokenizer that matches the model.
- Prompt templates, system instructions, few-shot examples, and prior conversation context all count as input tokens.
- Generated responses count as output tokens, including truncated, unfinished output.
Understanding this is the prerequisite for everything else in cost planning.
2. Why API services use a prepaid credit model
Calling an LLM API is a typical pay-after-consumption online compute service: every request triggers an inference, which consumes real compute and bandwidth. To guarantee capacity and limit abuse, the industry standard is “top up the account → deduct credit → issue the bill,” not “use now, settle later.”
Three direct conclusions follow:
- Account balance is the only source of available quota. Recharging, not applying, determines availability.
- Credit may carry an expiry date, and settlement rules differ between calendar-month and billing-cycle models. Read the pricing notes before you start.
- Deduction happens at the moment of the call. Failed retries and timed-out requests may still be billed, so retry behavior has to be controlled.
3. The standard path to obtaining API credit
Take a general cloud-hosted model service — for example, model services on Baidu AI Cloud, or the equivalent model service category on another cloud platform. The flow is essentially the same everywhere and can be reduced to five steps.
3.1 Complete account and identity verification
Individual users complete individual verification; organizations complete business-entity verification. Business verification typically unlocks higher call quotas and a fuller set of capabilities such as data isolation, audit logs, and invoice issuance.
3.2 Open the model service console
Go to the platform’s model service or API management page. Review the available models, each model’s context window length, per-token pricing, and any free or promotional credit currently in effect.
3.3 Create an API key
Generate credentials (API key / secret) in the key management page. The key is displayed in plain text only once, at creation time. Store it immediately in a secret manager or a local environment variable. Never commit it to a code repository, front-end bundle, or container image.
3.4 Top up the account
Complete payment in the billing or cost center. Enterprise accounts may pay by bank transfer against an invoice. Credit is normally added instantly and recorded as account balance. A single top-up often has a minimum and maximum amount, so large requirements should be split across multiple payments or handled through the provider’s sales channel. Bank transfers need extra time for funds to arrive.
3.5 Verify connectivity
Run one minimal-cost request to confirm the whole chain works:
import os, json, urllib.requestAPI_KEY = os.environ["LLM_API_KEY"] # read from env var; never hardcodeBASE_URL = os.environ.get("LLM_ENDPOINT") # service endpoint from the provider consolepayload = {"model": "<your model identifier>","messages": [{"role": "user", "content": "Reply with two words: connected"}],"max_tokens": 8 # cap output strictly to keep trial-and-error cheap}req = urllib.request.Request(BASE_URL,data=json.dumps(payload).encode("utf-8"),headers={"Content-Type": "application/json","Authorization": f"Bearer {API_KEY}"},method="POST",)with urllib.request.urlopen(req, timeout=30) as resp:print(json.loads(resp.read().decode("utf-8")))
4. Three configurations to finish once credit is available
4.1 Key governance
Split keys by environment (development / staging / production) and bind production keys to a dedicated budget and a call allowlist. Rotate them on a schedule and disable them immediately if exposure is suspected. A key shipped inside a client application is effectively a public key.
4.2 Usage limits and alerts
On the monitoring or usage page, set a monthly budget cap, a daily call ceiling, and a low-balance threshold alert. Alerts should cover two event types: low balance, and a sudden day-over-day spike in consumption. The latter is usually the signal of a runaway loop or abnormal retry behavior, and it surfaces problems earlier than an exhausted balance does.
4.3 Hard constraints on the calling side
Enforce timeouts, retry limits, and backoff in code:
import timedef call_with_guard(send, max_retries=2, base_delay=0.5):for attempt in range(max_retries + 1):try:return send()except Exception as e:if attempt == max_retries:raise# exponential backoff + jitter, avoiding traffic spikes and duplicate deductionstime.sleep(base_delay * (2 ** attempt) + 0.1 * attempt)
5. What actually goes into the price: breaking down the billing dimensions
Rate tables differ between vendors, but the billing dimensions fall into four groups. Understanding the dimensions matters more than memorizing any single number.
5.1 Separate pricing for input and output
Most models price input tokens and output tokens separately, with output typically costing significantly more than input — often two to four times as much. The reason is that autoregressive decoding performs a full forward pass for every token generated, which is more expensive computationally.
5.2 Tiered context-window pricing
The same model is often priced in bands by context length: lower per-token price for short context, higher for long context. Long-document summarization and repository-level code Q&A can silently cross into the expensive band.
5.3 Caching and batch discounts
Some services offer discounted rates for cached repeated prefixes and for asynchronous batch inference jobs. This is the main lever for reducing the cost of repeated system prompts and fixed knowledge-base fragments.
5.4 Time and concurrency dimensions
On top of token-based billing, you may also be charged per concurrent connection or for the duration of a dedicated resource package. Dedicated deployments must be costed separately.
6. How to model cost (a reusable method)
Do not rely on “run it for a few days and look at the bill.” Build a static estimate before integration.
6.1 The core formulas
Cost per call = (input tokens × input price) + (output tokens × output price)Daily cost = cost per call × daily request volume × (1 + retry overhead factor)Monthly cost = daily cost × 30 × (1 + peak/off-peak variance factor)
A conservative starting value for retry overhead factor is 0.1–0.3. Set peak/off-peak variance factor from your actual traffic pattern.
6.2 Count tokens exactly with a tokenizer
def estimate_cost(tok, prompt, expected_output_len, p_in, p_out):"""tok: tokenizer encoding object that matches the modelp_in / p_out: input / output price per 1K tokens(placeholder examples only; the console rate table is authoritative)"""input_tokens = len(tok.encode(prompt))out_tokens = expected_output_lencost = input_tokens / 1000 * p_in + out_tokens / 1000 * p_outreturn input_tokens, out_tokens, round(cost, 6)# Illustrative: replace p_in and p_out with the actual published rates; only the algorithm is shown hereprint(estimate_cost(tok, SYSTEM + user_query, 300, 0.001, 0.002))
6.3 Calibrate with shadow traffic
During a staged rollout, route 1%–5% of real traffic to the new path and compare “tokenizer-estimated tokens” against “tokens actually deducted on the bill.” A deviation above 5% means the prompt template contains dynamic content that was never accounted for, and you should return to template governance.
7. Frequent pitfalls
- Single top-up limit: a large requirement is blocked by the per-transaction ceiling and mistaken for a payment failure.
- Credit expiry: promotional credit expires and calls suddenly fail.
- Repeated system prompt billing: a multi-thousand-token system instruction is prepended to every single turn.
- Unbounded conversation history: without a sliding window, per-call cost multiplies from around turn 20 onward.
max_tokensset too high: a generous output ceiling lets abnormal generations run all the way to the limit.- Log and JSON structured-data overhead: large blocks of raw HTML or JSON pasted straight into the prompt.
- Shared keys across environments: one load test in development drains the production budget.
- Not reading the
usagefield: ignoring token accounting in the response destroys your ability to reconcile spend.
8. Cost-optimization practices
In order of return on effort:
- Tiered model routing: send simple tasks such as intent classification and format conversion to a smaller model, and reserve large-parameter models for complex reasoning.
- Prompt trimming: remove ineffective examples, merge duplicate instructions, and shorten the system prompt.
- Output length constraints: use
max_tokens, thestopparameter, and explicit format requirements to suppress redundant output. - Context governance: sliding window plus summary compression plus retrieval-based recall, instead of resending the full history.
- Prefix cache reuse: place fixed system prompts and knowledge-base fragments at the very front of the prompt to improve cache hit rate.
- Asynchronous batch processing: route latency-tolerant offline work such as bulk labeling and document extraction through batch endpoints.
- Result reuse: build a semantic cache for high-frequency repeated questions so that a cache hit avoids the call entirely.
- Usage dashboard: break token consumption down by business line, endpoint, and environment to identify your cost centers.
9. Individual developers vs. enterprise teams
Individual developers should use any free trial credit offered to new accounts to validate the approach, then fund a small amount to get a production path working. Budget alerts are mandatory. Use separate accounts or separate keys for experimental projects so they can be reset to zero cleanly.
Enterprise teams should go through business verification and procurement, and request higher quotas and dedicated resources. Build four layers of control — budget, quota, keys, and audit — and book token spend under the engineering cost line. For scenarios involving data leaving your environment, prefer services that commit to not persisting data and that provide audit logs, and define data-usage boundaries in the contract. If your organization already runs a unified cloud billing, monitoring, and key-management stack, LLM calls can be folded into that existing cost-governance framework instead of building a parallel one.
10. Key takeaways
Technically, “buying tokens” reduces to three actions: create a key in the model service console, complete a top-up in the billing center, and put usage limits and monitoring in place on the calling side. On pricing, hold on to four dimensions — input/output split, context tiers, cache and batch discounts, and concurrency or dedicated resources — and no rate table will mislead you. On cost, follow the sequence of “estimate with the tokenizer, calibrate with a canary, then roll out fully,” combined with model tiering and context governance; most workloads can cut cost by an order of magnitude this way. What ultimately determines long-term spend is not unit price but the engineering discipline behind prompt design and context management.
