1. Two phases
Inference has two phases: prefill processes the prompt in parallel, then decode runs sequentially.
// prefill (parallel) then decode (sequential)Typorithm · medium
Step 1 of 8 · intro card
Inference & Sampling
Inference has two phases — prefill processes the whole prompt in parallel (driving time-to-first-token) and decode generates tokens one by one. Cost scales with input plus output tokens, and a longer context means more KV-cache memory and higher latency.
A request runs in two phases. Prefill ingests the entire prompt at once in parallel and produces the first token; this is what time-to-first-token (TTFT) measures. Decode then generates the remaining tokens sequentially, one per step.
Total latency is roughly TTFT plus the number of output tokens times the per-token decode time. Because decode is sequential, longer responses take proportionally longer regardless of how fast prefill was.
Cost is usually billed on input tokens plus output tokens. A longer context also enlarges the KV cache, consuming more memory and adding latency, which is why prompt length and output length both matter for speed and price.
Prefill is parallel and sets TTFT; decode is sequential.
Total ≈ TTFT + outputTokens × perTokenTime.
Longer context = bigger KV cache, more memory and latency.
// prefill (parallel) then decode (sequential)
const ttft = prefillTime(promptTokens);
const total = ttft + outputTokens * perTokenTime;
// longer context = more memory + latencyConnect each part of the code to the concept before you type.
Inference has two phases: prefill processes the prompt in parallel, then decode runs sequentially.
// prefill (parallel) then decode (sequential)Prefill over the prompt tokens drives time-to-first-token (TTFT).
const ttft = prefillTime(promptTokens);Total time is TTFT plus the sequential decode of each output token.
const total = ttft + outputTokens * perTokenTime;Each output token adds a fixed per-token decode time, so latency grows with output length.
outputTokens * perTokenTimeA longer context means a larger KV cache, more memory, and higher latency.
// longer context = more memory + latencyRead the concept map and follow the code breakdown.
Type the implementation, repair mistakes and work through every recall exercise.
Use the final check without hints; revisit weak concepts in short practice sessions.