Models behind an API
Dive into Deep Learning · §18.7
Models behind an API
protocol and usage · a replaying client · cost · retries · Little’s law · the price of a correct answer · what a service withholds
DeepSeek-V4.1-Flash Needs 382 GB at Four Bits; One Card Has 24 GB
The problem
| DeepSeek-V4.1-Flash |
763 B |
1,526 GB |
763 GB |
382 GB |
8 × 80 GB only |
| Qwen3.8-27B |
27.8 B |
56 GB |
28 GB |
14 GB |
one 24 GB card at 4 bits |
| Qwen3-4B |
4.0 B |
8 GB |
4 GB |
2 GB |
one 24 GB card in bf16 |
Yet one request to the flagship returns in about 1.5 s for under 0.01 cent. The model is a service. What can a client measure, and what is withheld?
Weights only, at one precision throughout: no key-value cache, no activations. The flagship’s release mixes 8 and 4 bits.
By the End of This Deck You Can
Goals
- read a chat-completions request and response, compute the cost of a response from its usage block, and name what the protocol withholds;
- key a replay cache so that a recorded measurement can be repeated;
- choose a retry schedule and compute its expected wait;
- find the capacity and the knee of a server with Little’s law;
- decide whether an accuracy gap is resolved, and price a correct answer.
Idea: a hosted model is a service with a protocol, a price and limits, and the client is the measuring instrument. Running example: DeepSeek-V4.1-Flash and Qwen3.8-27B through a router, Qwen3-4B on one RTX 4090, 100 GSM8K problems.
The Last Instrument of the Chapter Looks at Models from Outside
Where this fits
| open a checkpoint |
files and configuration; the same tensors in the book’s code |
| measure it |
prompts; scoring conventions; error bars and judges |
| inside and outside |
interventions on activations; models behind an API |
- Uses: the few-shot prompt builder, the GSM8K problems and extractor, paired tests.
- Skips: tool calls, open-loop traffic, serving engines.
One Model, Many Providers: Pin One and Use a Dated Id
- Provider: a company that hosts the weights and sells access per token.
- Router: one address and one account in front of many providers. Its listing has a row per provider: prices, supported fields, precision.
- Pin: name one provider and forbid fallbacks.
| DeepSeek-V4.1-Flash-1017, the flagship |
Novita, fp8 |
0.165 |
0.66 |
0.0033 |
| Qwen3.8-27B |
Parasail, fp8 |
0.24 |
2.20 |
0.05 |
| gpt-5-nano, closed |
OpenAI |
0.05 |
0.40 |
0.005 |
Dollars per million tokens, listing of 13 November 2026. The same weights are served at 4 to 16 bits, and an undated alias can be re-pointed.
A Request Carries the Messages and the Sampler’s Parameters
messages |
the conversation: a list of roles and contents |
temperature, top_p |
scale the logits; keep the most probable tokens |
max_tokens, stop |
bound the reply; strings that end it |
seed |
ask for a reproducible draw |
logprobs, top_logprobs |
log-probabilities of generated tokens |
response_format |
output that follows a JSON schema |
stream |
the reply in chunks, as generated |
A field is a request. The provider may honour it, ignore it or reject it, and only a test shows which.
A Response Is Text plus a Served Model, a Provider and a Usage Block
One GSM8K word problem to the flagship, question only:
| text |
a worked solution ending in the correct answer |
finish_reason |
stop: the model ended. length: it reached max_tokens |
usage |
36 prompt tokens, 118 completion tokens |
| served model, provider |
deepseek/deepseek-v4.1-flash-1017, Novita |
| latency, cost |
about 1.5 s, 0.000084 dollars |
The server applies the chat template, so the client never sees tokens: usage is the only record of what was processed.
Hidden Reasoning Spends the Output Budget
Reasoning: text that a model generates for its own use before the reply. Qwen3.8-27B reasons by default. One hard problem, sent twice:
reasoning on, max_tokens=64 |
empty |
length |
64 |
64 |
reasoning off, max_tokens=1024 |
correct answer |
stop |
871 |
0 |
Reasoning tokens are billed as completion tokens and counted against max_tokens. Empty content + length + reasoning = completion is the signature.
A Listed Field Is a Claim; a Detection Rule Tests It
Rule for seed: three sampled requests with seeds 7, 7, 8. Honoured if the first two texts agree and the third differs, ignored if not, rejected on an error status, partial if only part was done.
seed |
ignored |
ignored |
ignored |
honoured |
logprobs, top 5 |
partial |
honoured |
rejected |
ignored |
stop |
honoured |
honoured |
rejected |
ignored |
n=2 |
ignored |
ignored |
ignored |
ignored |
| prompt log-probabilities |
ignored |
ignored |
ignored |
rejected |
All three hosted endpoints list seed; none returned a repeatable draw. Our own server ignores more fields than any provider.
Recorded 13 November 2026; one request field per test. The router refused logprobs and stop for the closed model, whose listing lacks them.
Check: An Empty Reply
Check
A request with max_tokens=64 returns empty content and finish_reason length. Its usage: 125 prompt, 64 completion, 64 reasoning tokens. Prices: 0.24 in, 2.20 out, in dollars per million tokens. What happened, and what did it cost?
- The whole output budget went to reasoning; the answer never began.
- Cost: (125 \times 0.24 + 64 \times 2.20)/10^6 \approx 0.00017 dollars, of which the output is 0.00014.
A Replay Cache Keys on What Changes the Response, Nothing Secret
request 7cc7806f70bb
stream=True 7cc7806f70bb
another API key 7cc7806f70bb
replica=1 d894f66c39ee
seed=1 55eaa444de1b
provider parasail cda240a315d9
Lines 1 to 3 agree: stream and the API key are outside the hash. Lines 4 to 6 each differ.
- In the hash: host, model, messages, sampling and routing fields, and
replica, a client-side number that tells identical requests apart.
- Modes:
replay (a miss is an error), record (send what is missing), live.
Cost Is Arithmetic on Usage and a Dated Price
\textrm{cost} = \frac{(n_\textrm{in} - n_\textrm{cache})\, r_\textrm{in} + n_\textrm{cache}\, r_\textrm{cache} + n_\textrm{out}\, r_\textrm{out}}{10^6}
n: prompt, cached and completion tokens from usage. r: dollars per million tokens. Reasoning tokens are inside n_\textrm{out}.
Novita, USD per million tokens: 0.165 in, 0.66 out, 0.0033 cached in
1,000 prompt and 300 completion tokens: 0.000363 USD
DeepSeek at Thu 02:00 UTC: 0.300 in, 1.200 out
DeepSeek at Thu 12:00 UTC: 0.150 in, 0.600 out
Same model, same weekday: twice the listed price at 02:00 UTC, mid-morning in Beijing. A cost needs the hour.
Whole recording: 5,184 calls, about 0.10 dollars; cost from the listing / cost billed = 1.0000
Jitter Ends a Retry Storm in about 15 s instead of 63 s
64 clients fail together; the server admits 10 requests per second.
over 200 seeds: median (min to max)
no jitter: all done at 63.0 s (63.0 to 63.0), 238 attempts (238 to 238)
full jitter: all done at 14.5 s (8.3 to 31.4), 228 attempts (221 to 239)
full jitter makes fewer attempts on 197 of 200 seeds
- Fixed waits: the clients return in lockstep, ten get through per wave.
- Jitter saves only about ten attempts of 238. What it changes is when the retries arrive.
In a Closed Loop, Throughput Grows with Concurrency up to Capacity
- Little’s law: requests inside L = throughput X × time inside W.
- Closed loop with c requests in flight: L = c, so XW = c.
- Light load: W = W_0, so X = c/W_0. At capacity: X = X_{\max}, so W = c/X_{\max}. The two meet at the knee c^*.
X(c) = \min\left(\frac{c}{W_0}, X_{\max}\right), \qquad c^* = X_{\max} W_0
W_0 = 2 s, X_{\max} = 8/s: knee at 16. At c = 32: still 8/s, and 4 s per request.
XW / c Checks the Client; X(c) and W(c) Show the Server
Fake server: 0.1 s per request, 8 at a time, so X_{\max} = 80/s and c^* = 8.
c X W little sustained formula X
0 1 9.805 0.102 0.998 0.998 9.822
1 2 19.412 0.103 0.997 0.998 19.643
2 4 38.954 0.102 0.998 0.998 39.286
3 8 76.602 0.104 0.992 0.998 78.573
4 16 79.787 0.199 0.994 0.995 80.000
5 32 79.952 0.399 0.998 0.998 80.000
Steady window: from the c-th to the (N-c)-th of N completions. little = XW/c leaves 1 when the window is not steady; sustained = mean in flight / c falls when slots of the loop stand empty.
Check: Where Is the Knee?
Check
A server answers one request in 2.7 s and at most 12 requests per second. Where is its knee? What latency should a client expect with 128 requests in flight?
- c^* = X_{\max} W_0 = 12 \times 2.7 \approx 32 requests in flight.
- W(128) = 128/12 \approx 10.7 s, at the same 12 requests per second.
The Lines Meet just above 32 in Flight; Throughput Rises until 64
pass 1: W_0 2.65 s, X_max 12.4 requests/s, c* 33.0, efficiency below 0.7 from c = 64
pass 2: W_0 2.70 s, X_max 12.4 requests/s, c* 33.5, efficiency below 0.7 from c = 64
knee in both passes: yes
Qwen3-4B, one RTX 4090, 64 tokens per reply, two shuffled passes. Knee rule: efficiency E(c) = X W_0 / c below 0.7 from that level on. At c = 32 the fit is too sharp: 550 tokens/s against a promised 750.
The Server Decodes about 45 Sequences at Once; the Rest Wait
| decoding at once, X(W - \textrm{TTFT}) |
15 |
28 |
46 |
42 |
| median time to first token (TTFT) |
0.15 s |
0.36 s |
0.45 s |
7.8 s |
| mean latency W |
3.1 s |
3.6 s |
5.2 s |
10.4 s |
| tokens per second |
330 |
550 |
800 |
750 |
32 → 64 in flight: over 40% more throughput. 64 → 128: none, only waiting. An admission limit of the server as we started it, with a small cache, not the GPU’s capacity.
The fit’s c^* \approx 33 assumes the unloaded 2.7 s. A sequence in the full batch decodes for 3.7 s: 12.4/\textrm{s} \times 3.7 s = 46.
Hosted flagship: X(32)/X(1) \approx 30 at a flat 1.4 s. One client does not find a large provider’s knee.
Three Points of Accuracy Are Unresolved at 100 Problems
100 GSM8K test problems, four worked examples in the prompt, temperature 0, reasoning off, 8 requests in flight.
| flagship, run twice (replicas 0 and 1) |
0.97 |
0.93 to 1.00 |
| Qwen3.8-27B |
0.94 |
0.89 to 0.98 |
| Qwen3-4B, local |
0.83 |
0.75 to 0.90 |
- Noise floor: the two flagship replicas differ on 2 problems.
- Flagship − Qwen3.8-27B: 0.03, paired interval −0.01 to 0.08. The discordant problems, solved by one arm only, split 4 to 1.
- Minimum detectable effect at 100 problems, 80% power: 0.06.
- Both hosted models − local: resolved (14 and 11 points).
Three hosted replies were cut at the limit of 512 tokens and count as wrong.
The Price of a Correct Answer Differs Four- to Sixfold
| flagship (763 B parameters) |
0.09 |
0.15 |
|
| Qwen3.8-27B |
0.55 |
0.55 |
|
| Qwen3-4B, local |
|
|
1.3 |
- Why: output at 0.66 against 2.20 dollars per million tokens; shorter replies; three quarters of the flagship’s prompt tokens at the cached rate, a discount for a prompt prefix the provider has kept.
- The model with 27 times the parameters is the cheaper one: a sparse mixture of experts, most of its parameters idle on any one token.
- Local: 0.37 GPU-hours per 1,000 correct, 10 to 20 cents at 0.30 to 0.50 dollars an hour. On a full server about 2 to 4 cents (an estimate).
Temperature 0 and a Seed Do Not Make a Service Deterministic
Three prompts, each sent many times with identical settings:
| flagship, hosted |
20 |
5 to 7 |
1 |
| local server, nothing else running |
5 |
1 to 2 |
1 |
| local server, 8 other clients |
5 |
5 |
1 to 3 |
- Float addition is not associative, and kernels pick their summation order by the shape of the batch. A near-tie between two tokens flips.
- Local, alone: later requests reuse the cached prompt of the first. Other operations, other last bits.
The batch is whoever else is being served. A seed fixes the sampler, not the arithmetic.
An API Withholds What This Chapter’s Measurements Need
![]()
Prompts leave the machine, and two documents govern the replies: the license of the weights and the provider’s terms of service. One provider’s terms forbid publishing performance numbers: read both before measuring.
Check: Which Measurements Survive behind a Closed API?
Check
Four measurements: bits per byte of a held-out text (its negative log-likelihood under the model), accuracy with a paired interval, activation patching (overwrite an activation and watch the output), the price of a correct answer.
- Bits per byte: no. It needs log-probabilities of a given text.
- Paired accuracy: yes. Generate, extract, test on paired outcomes.
- Activation patching: no. It needs activations.
- Price of a correct answer: yes. Usage times a dated price, divided by the correct answers.
Wrap-up
Wrap-up
- A response is text plus
usage; cost is usage times a dated price.
- A cache keyed on everything that changes a response replays a recording; the service stays non-deterministic (5 to 7 distinct replies of 20).
- XW = c: the local server decodes about 45 sequences at once and peaks near 800 tokens per second at 64 in flight.
- 0.97 against 0.94 is unresolved; the price of a correct answer differs four- to sixfold.
- The protocol has no field for prompt log-probabilities, activations or weights.
Next: encoders, which return a vector for each text, something no chat endpoint does.