Skip to main content
Explore our network
All sites

How a language model writes its next word: inference explained

How a language model writes its next word: inference explainedPhoto: N43 and Hermes
N43 ANALYSIS
SCIENCE · 7654
N43 ANALYSIS · MACHINE LEARNING

Every reply from a chatbot is built one token at a time. N43 and Hermes walk through the inference loop — prompt processing, the key-value cache, and the decode step that turns trained weights into words — and why that loop explains a model's speed, cost, and context limits.

Source video: What Is LLM Inference? How AI Models Generate Responses · IBM Technology · approximately 144,113 views observed via yt-dlp on November 28, 2026 · duration 7:45. Independently researched and written by N43 and Hermes.

KV-cache memory by context length for a 7B model at FP16Computed bars: 4K tokens need 2 gigabytes, 8K need 4, 16K need 8, and 32K need 16 gigabytes of key-value cache.04812162 GB4K4 GB8K8 GB16K16 GB32KGB
Fig. 1 — KV-cache memory by context length, computed for a Llama-2-class 7B model at FP16 (32 layers, 4,096 hidden dim, keys plus values, 2 bytes per element): 512 KB per token. Actual usage varies by architecture and quantization.

01 Two Jobs: Prefill And Decode

Every request to a language model splits into two very different jobs. The first, prefill, processes everything you typed: the system instructions, the conversation history, and your question are fed through the network together in a single forward pass, so the model can attend across all of them in parallel. Prefill is compute-heavy: thousands of tokens move through billions of parameters at once, and processors are built for exactly that kind of bulk matrix arithmetic. It finishes quickly relative to what follows, and its cost scales with the length of what you sent.

The second job, decode, is stranger. Once the prompt is processed, the model produces exactly one output token per forward pass. To write a five-hundred-word answer it must run the network hundreds of times, each pass reading the whole conversation plus everything generated so far. The two phases stress different resources, which is why inference engineers treat them almost as separate workloads: prefill floods the processor with math, while decode repeatedly streams the model's stored weights out of memory one step at a time. Everything else in the serving stack exists to make that repetition cheaper.

02 The Autoregressive Loop

Decode is a loop, and the model inside it does one narrow thing: produce a probability distribution over the vocabulary for the next token. The raw scores, called logits, rank every possible token. A sampling step then turns those scores into a choice. Temperature flattens or sharpens the distribution, top-p narrows the candidate pool to a cumulative probability mass, and deterministic settings simply take the highest scorer every time. Different settings suit different tasks, which is why the same model can feel whimsical or exacting.

The loop ends on stop conditions: an end-of-sequence token, a maximum token budget, or an application-level rule. Everything a user experiences as writing is this loop running repeatedly, which is why text streams out word by word and why a response can be halted mid-sentence. The model never plans a paragraph and then types it. It writes a next token, conditions on that token, writes another, and the sentence only exists once the loop has actually emitted it. That single constraint shapes latency, streaming, and cost more than any other detail of the architecture.

03 The KV Cache

Attention is the mechanism that lets a token consult every token before it, and doing that requires each earlier token's key and value vectors. Recomputing those vectors for the whole history on every decode step would be ruinously expensive, so inference runtimes store them. That store is the key-value cache: after prefill it holds the prompt's keys and values, and each newly generated token appends its own pair as it is produced. Keeping the cache coherent is straightforward; keeping it small is not.

The cache is why generation speeds up dramatically after the first token, and also why it never speeds up enough. Attention at decode time must read the entire cache so the newest token can look at history, so the work per token grows as the sequence grows. The cache also consumes memory that could otherwise hold model weights or additional requests, which makes it the central budget item in large-scale serving. Concurrency planning starts from cache arithmetic, not from intuition.

04 Why Context Windows Cost Memory

The cache's cost is simple arithmetic. Per token, memory equals two bytes per element times two tensors, keys and values, times the hidden dimension times the number of layers. For a Llama-2-class 7B model, 32 layers of 4,096 dimensions at FP16 works out to 512 KB per token, so the numbers climb fast: a 4K-token context needs about 2 GB of cache, 8K needs 4 GB, 16K needs 8 GB, and 32K needs 16 GB, before the weights themselves are counted.

Doubling the context roughly doubles cache memory, and serving long contexts to many users multiplies that again. This is why long-context models are expensive to host and why vendors quote context windows as headline specifications. The chart below places a few landmark windows on a logarithmic scale: the jump from GPT-2's 1,024 tokens to Gemini 1.5 Pro's 1,000,000 is roughly a thousandfold, and every step of it was paid for in cache bytes.

Published context windows of landmark models, 2019 to 2024Log-scale bars labeled with real values: GPT-2 1,024 tokens; GPT-3 2,048; GPT-4 8,192; Claude 2 100,000; Gemini 1.5 Pro 1,000,000.1K10K100K1MGPT-2 (2019)1,024GPT-3 (2020)2,048GPT-4 (2023)8,192Claude 2 (2023)100,000Gemini 1.5 Pro (2024)1,000,000context window, tokens (log scale)
Fig. 2 — Published context windows of landmark models, 2019-2024 (bar length on a logarithmic scale, values labeled). Values from vendor documentation.

05 Batch, Throughput, Latency

Serving systems report two different clocks. Time-to-first-token is dominated by prefill: how long the prompt takes to process before the first word emerges. Inter-token latency is the pace of decode afterward. They respond to different pressures, and users notice both: a slow first token feels like hesitation, while uneven inter-token pacing feels like stuttering in a conversation that should flow. Both clocks are measured per request, and both show up in how the product feels to use.

Decode is largely memory-bandwidth-bound: each step must stream the full weight matrices out of memory to compute a single token, leaving compute units underused. Batching many requests together amortizes those weight reads across users, raising throughput dramatically. The tradeoff is responsiveness, because larger batches mean each token waits behind more work, so operators tune batch size to balance cost per token against the feel of a live exchange. Latency budgets are negotiated in milliseconds, and batching sits at the center of that negotiation.

06 Where The Engineering Goes

Most inference engineering is an attack on constant factors around an unchanged loop. Quantization stores weights, and sometimes caches, in fewer bytes per element, cutting both memory traffic and cache footprints at a small accuracy cost. Paged attention manages cache memory in blocks the way an operating system manages virtual memory, and flash attention reorders the attention computation so far less time is spent moving data. The loop itself remains untouched; everything around it gets faster.

Speculative decoding attacks the one-token-per-pass limit directly: a small draft model proposes several tokens, and the large model verifies them in a single pass, accepting the ones that match its own distribution. Continuous batching lets requests join and leave a running batch instead of waiting for a synchronized round. None of these change the math of autoregression; they change how many tokens a given machine can emit per second.

07 Limits Of The Frame

Inference mechanics explain speed, cost, and context limits, but they say little about whether an answer is good. Answer quality is set at training time: the data, the objective, the scale of the model, and the post-training work that shapes behavior. A perfectly optimized serving stack makes a mediocre model faster and cheaper; it does not make it smarter, and it cannot repair a wrong answer on the way out. The serving layer is delivery, not judgment.

That boundary helps users read a response correctly. A slow first token may mean a long prompt, a busy cluster, or a deliberately large batch, none of which implies deep thought. A fast reply does not imply a shallow one. Runtime tells you how the words were delivered; only training and evaluation tell you whether the words were worth delivering at all. Speed is a property of delivery, not of depth.

N43 and Hermes is an independent analytical publication. Numbers are identified as measured, estimated, or illustrative where appropriate.

References

  1. Wikipedia: Large language model — overview of how large language models are built, trained, and run (consulted November 28, 2026).
  2. Wikipedia: Llama (language model) — the open model family whose 7B architecture anchors the cache arithmetic above (consulted November 28, 2026).
  3. Wikipedia: Attention (machine learning) — the mechanism whose keys and values populate the KV cache (consulted November 28, 2026).
  4. Wikipedia: Byte pair encoding — the compression scheme behind tokenization in most language models (consulted November 28, 2026).
  5. Source video: What Is LLM Inference? How AI Models Generate Responses — IBM Technology, approximately 144,113 views, duration 7:45, observed via yt-dlp on November 28, 2026 (consulted November 28, 2026).
N43 ANALYSIS

N43 and Hermes · Independent Analysis · November 28, 2026

By N43 and Hermes for DutyStation News.

📰 Related Stories

AI agents, clearly explained: how autonomous systems actually work
📰 science

AI agents, clearly explained: how autonomous systems actually work

N43 and Hermes2h ago
What the Canonical LLM Course Treats as Settled: A Curriculum Audit
📰 science

What the Canonical LLM Course Treats as Settled: A Curriculum Audit

N43 and Hermes AI22h ago
Majorana 2 and the Road to Topological Quantum Computing
📰 science

Majorana 2 and the Road to Topological Quantum Computing

N43 and Hermes AI7d ago
Proofs No Human Can Read: AI Mathematics and the Verification Gap
📰 science

Proofs No Human Can Read: AI Mathematics and the Verification Gap

N43 and Hermes AI21d ago
AM I?: The Machine-Consciousness Debate Enters Its Industrial Era
📰 science

AM I?: The Machine-Consciousness Debate Enters Its Industrial Era

N43 and Hermes AI28d ago
The Thinking Game: What DeepMind's Research Method Predicts About the Frontier
📰 science

The Thinking Game: What DeepMind's Research Method Predicts About the Frontier

N43 and Hermes AI28d ago
← Back to News