skip to content

What should a model gateway normalise when providers report finish reasons and token usage differently, and what does that mapping lose?

level: middleimportance: should knowfreq 35%

answer

  1. callers branch on both fields
  2. truncation must never look complete
  3. cached tokens: inside or beside input?
  4. unknown is not zero
  5. keep the raw provider values

basics

~20 s

Map finish reasons onto a small gateway enum and usage onto fields with defined inclusion rules, keep the raw provider values beside them, and mark missing usage as unknown; distinctions a provider never reports, and cross-provider token comparability, are lost.

solid answer

~40 s

Callers branch on both fields, so the gateway defines a small finish enum (complete, output limit, tool call, stop sequence, filtered, other) and maps every provider value onto it. The fatal bug is mapping an output-limit stop to complete, which hides truncated answers; an unmapped new value goes to `other` with the raw value kept and an alert raised. For usage, the gateway's fields must state what includes what, because some providers count cached input tokens inside the input total and others report them beside it, and reasoning tokens are folded into output by some and separate in others. An aborted stream with no usage is recorded as unknown, never zero. What is lost: distinctions a provider never reports, detail like safety categories unless kept raw, and comparability, since each provider's tokenizer differs.

go deeper

for a junior

Recall the common finish reasons and the usage counters, and why callers branch on them.

for a middle

Explain the gateway enum, why output-limit must never map to complete, and how cached and reasoning tokens are counted differently across providers.

for a senior

Show a usage shape with explicit inclusion rules, a merged end-of-stream usage event, unknown-not-zero for aborted calls, and raw values kept for audit.

for a principal

Weigh how small the gateway enum should be against callers wanting provider detail, and who owns remapping when a provider changes what it reports.

## Two fields every caller branches on After every model call, application code reads two things: **why generation stopped** (the finish reason) and **how many tokens it used**. Retry logic, continuation, tool loops and spend tracking all branch on them. A gateway that translates one request shape across several providers has to translate these two fields as well, and providers name their values differently, split them differently and deliver them at different moments in a stream. ## Finish reasons A gateway defines a small closed set of its own and maps every provider's value onto it. The names below are illustrative: | gateway value | meaning | what a caller does | |---|---|---| | `complete` | the model ended its turn | use the answer | | `output_limit` | hit the output-token limit | treat as truncated: continue, or raise the limit | | `tool_call` | stopped to request tools | run them and send results back | | `stop_sequence` | matched a caller's stop string | use the text up to it | | `filtered` | the provider's safety system withheld output | show a fallback; do not blindly retry | | `other` | a value the gateway does not map | log the raw value and alert | What the mapping loses: - **Distinctions the provider never makes.** Some providers do not separate a stop-sequence match from a natural end of turn. The gateway cannot invent that distinction, unless it emulated stop sequences itself and so knows. - **Detail beyond the enum**: which stop string matched, safety categories, a refusal's text. Keep these in an extra field. - **New values.** Providers add finish values over time. An unmapped value must land in `other` with the raw value attached and an alert; it must never default to `complete`. The single worst bug is mapping an **output-limit stop to `complete`**. Every truncated answer then looks finished, and callers ship half a summary or a cut-off JSON document. ## Token usage Usage is a set of counters: input, output, **cached input read**, **cached input written**, and **reasoning tokens** spent before the visible answer. The traps: - **Inclusion differs.** Some providers report cached input tokens as part of the input total; others report them as separate counts alongside it. Adding all counters for both providers double-counts the first; copying both into the same field names undercounts the second. The gateway's shape must state inclusion explicitly, for example that `input_total` equals uncached plus cache-read plus cache-write, and convert each provider into it. - **Reasoning tokens** are folded into the output count by some providers, reported separately by others, and not exposed at all by some. - **Timing in streams.** Some providers send input usage when the stream opens and output usage at the end; others send usage only in a final chunk, and some only when the request asks for it. The gateway asks where it must and emits one merged usage event at the end. - **Missing usage.** A stream aborted before the final event may carry no usage at all. Record it as **unknown** (optionally with a flagged estimate), never as zero, or every aborted call looks free. - **Tokens are not a common unit.** Each provider's tokenizer differs, so the same text yields different counts. A normalised field puts counts in one place, but they remain each provider's own tokens. ## Keep the raw values Normalisation is lossy by design, so the response should carry the **raw provider finish value and usage block** beside the normalised fields. The raw values are the only way to audit a mapping, debug a disagreement with an invoice, or remap history when the table was wrong. ```json { "finish": {"reason": "output_limit", "raw": "provider-specific-value"}, "usage": { "input_total": 1800, "input_cache_read": 1200, "input_cache_write": 0, "output_total": 512, "output_reasoning": 300, "status": "reported", "raw": {} } } ``` (An illustrative shape: `input_total` includes both cache fields, and `output_total` includes `output_reasoning`.) ## Where the gateway's job ends Metering spend from these counts, attributing it to tenants and alerting on burn are a telemetry subject. Enforcing a spend cap from them belongs to the gateway's budget logic. The translation's job is narrower: a finish reason and a usage shape whose meanings are defined, whose source is preserved, and whose gaps are marked rather than filled with zeros.

  • A provider adds a new finish value overnight. What should the gateway do with it?
    Map it to `other`, attach the raw value, and raise an alert or a counter on unmapped values so someone updates the table. It must not fall back to the normal-completion value, because a new value often means something callers must handle, such as a pause or a refusal. Once mapped, the raw values kept on old responses let you reclassify history.
  • Why record an aborted stream's usage as unknown rather than estimating it silently?
    The provider may still bill the tokens it generated, so zero is wrong, and a silent estimate is indistinguishable from a reported count. A flagged status lets downstream consumers decide: reconcile later against the provider's invoice, use a labelled estimate, or exclude the call. Hiding the gap makes every later disagreement impossible to explain.

  • Token counts from two providers are directly comparable once the field names match.
  • A stream that ends without usage can be recorded as zero tokens.
  • Every provider counts cached input tokens inside its input total.
  • Unmapped finish values can default to normal completion to keep clients simple.
  • The raw provider finish value can be thrown away once it is mapped.