Zenaique

Describe why structured output mode quietly raises the per call token bill

Short answer·Medium·4.0 · 0·~3 min·Asked atHclOpenAIZepto
Attempt it

Switching from free text completions to strict JSON or structured output mode usually raises the realized cost per useful answer by 10-40%, even when the underlying information returned is identical. Name the three mechanisms responsible and explain how each adds tokens or wall clock time.

Free · 2 AI evals / day
TL;DR

Structured-output mode adds 10-40% to output cost via three axes: constrained-decoding mask hurts speculation acceptance, JSON syntax adds envelope tokens, and parse failures cause retries.

Memory aid
Sign in to see the mnemonic that makes this stick.
Easy to grasp

Imagine asking someone for an answer in two ways. First, just tell me. Second, fill out this fixed form with labeled boxes (name, age, address). The second response always takes more words because of the labels, the brackets, and the formal structure, even when the underlying information is the same. On top of that, the person has to slow down while writing to make sure every label is spelled right and every bracket matches; rules force corrections. And occasionally they hand back a form that has a typo and you ask them to redo it, doubling the work for that one. That is what JSON mode does to an LLM: more words for the envelope, slower per word because of the rules, and the occasional do-over.

Concept explanation~2 min read

Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.

Structured output mode is one of the most useful features in modern LLM serving, and one of the least understood from a cost perspective. The API charges the same per-token rate whether you use JSON mode or free text, so casual reading of price sheets suggests structured output is free. Production observation tells a different story: realized cost per useful answer climbs 10-40% on the same model, the same prompt, the same underlying information.

The three mechanisms responsible are independent and stack. Constrained decoding interacts badly with speculation. The JSON envelope is real billed output. Parse-validation failures double the cost on the unhappy path. Each axis admits its own mitigation, and a strong team measures and tunes them separately.

This question tests whether you can decompose the realized cost into its components and reason about which mitigation pays off for your workload. The deep dive walks each axis, the operational consequences, and the schema design choices that minimize total cost.

Constrained decoding and its effect on speculation

Structured output mode runs every sampling step through a grammar or finite-state machine that encodes the schema. At each step the FSM computes which tokens are legal at this position and zeros out the logits of illegal tokens. The model then samples normally from the masked distribution. This guarantees the output is syntactically valid by construction (every brace closes, every key is correctly placed, every value type matches).

The masking itself is cheap. A vector operation over the vocabulary, microseconds per step, invisible against the matmul. But its interaction with speculative decoding is not cheap.

Speculation acceptance under masking

Speculative decoding works because a small draft model produces tokens whose distribution closely matches the target. The verifier accepts candidates that lie within the target's distribution. Apply a schema mask to the target but not to the draft, and the draft proposes plenty of schema-illegal candidates that the target now assigns zero probability. Acceptance rate drops sharply, often from 0.7 baseline to 0.4-0.5 under heavy schema masking.

Lower acceptance means fewer tokens generated per round, which means more rounds per output, which means more HBM sweeps per token. The per-step bandwidth cost is unchanged, but the realized tokens per second falls. Effective speedup from a 2x baseline speculator can collapse to 1.2-1.3x under JSON mode.

Grammar-aware speculation

The fix is to apply the schema mask to the draft model too. Production stacks like llguidance support grammar-aware draft sampling. Acceptance rate recovers because draft and target now share the same support. But this requires coordinated grammar evaluation across both models, which is operational complexity many stacks have not yet implemented.

The envelope tax
Parse-validation failures and retry cost
Combined cost shape and operational priorities
Sign in to unlock the full deep dive.

Situations where this technique stops working.

Sign in to see when this approach fails.

2–4 min · Everything important, quickly.

Sign in to see the quick scan of the deep dive.

Real products, models, and research that use this idea.

  • Outlines and llguidance are the production-grade grammar-aware sampling libraries used by vLLM, TGI, and Together AI to enforce JSON Schema during decoding.
  • OpenAI Structured Outputs (introduced 2024) uses a constrained sampler and documents that strict schema mode can be slightly slower per request than free-text completion.
Sign in to see more production examples.

What an interviewer would ask next. Try answering before peeking at the approach.

QHow does grammar-aware speculative decoding restore some of the lost acceptance rate?
A

A grammar-aware speculative decoder applies the same schema mask to the draft model's logits, so the draft only proposes schema-valid candidates. Acceptance rate recovers because the draft and target distributions now overlap on the schema-allowed support. Production stacks like llguidance support this; it requires draft-target coordination beyond what naive speculation provides.

2 more follow-ups an interviewer would ask next. Sign in to reveal them.

Red flags & common mistakes

The phrases that signal junior thinking. Click to expand.

Most common mistake

Believing JSON mode is free because the model already produces structured-looking text. Constrained decoding actively masks tokens, lowering speculative-decoding acceptance, and the schema envelope tokens are real billed output.

Sign in to see all red flags and common mistakes.

60 second bullets to scan on the way to the call.

  • Why constrained-decoding masking lowers speculative-decoding acceptance

  • How JSON envelope tokens contribute to output cost

Sign in to unlock the revision sheet.

Primary sources. Browse if you want the original framing.

Similar questions

Same topic, related formats. Practice these next.

4 curated
Next question
What is the KV cache in transformer inference?
Flashcard·Easy