{"slug": "why-does-the-first-llm-token-take-longer", "title": "Why Does the First LLM Token Take Longer?", "summary": "A developer published a walkthrough explaining why long-context chats take longer to produce their first output token, attributing the delay to prefill work that scales with prompt length while decode proceeds one token at a time. The post provides illustrative timings showing a long chat starting 200 ms later than a fresh one, and offers a Python helper that measures time to first text and average inter-chunk gap from streaming events, warning that timers stopping at the first event can mask the difference.", "body_md": "Open a fresh chat and type `Finish this sentence: A triangle has…`. Now ask the same thing at the end of a long conversation.\n\nBoth replies might say `three sides.` But the second can take longer to start. The sentence you typed stayed the same. The prompt the app sent to the model got longer.\n\nIn [the first post](https://dev.to/int8ai/what-happens-after-you-hit-send-an-illustrated-llm-walkthrough-4mgb), we followed the loop that chooses a token, appends it, and repeats. This time, let’s see what happens before that first choice.\n\nThink of joining a conversation halfway through. If someone asks “What about that one?”, you need to hear some of what came before. A chat app gives the model that context by including earlier turns in the prompt.\n\n```\nFresh: [Instructions] [Latest message]\nLong:  [Instructions] [History] [Latest message]\n```\n\nProcessing the prompt is called **prefill**. Since the prompt tokens are already available, the GPU can work on many positions in parallel. A longer prompt still creates more work. It has more token positions to process, and with standard attention, later positions can compare against more earlier ones.\n\nPrefill produces the scores used to choose the **first output token**. Then **decode** generates what follows. To score the token after `\" three\"`, the model first needs to know whether it picked `\" three\"` or `\" four\"`. That dependency gives ordinary generation its one-token-at-a-time rhythm.\n\nImagine two requests to the same model. We’ll keep setup and delivery time fixed and change only prefill. These timings are illustrative.\n\n| Before the first text chunk | Fresh chat | Long chat | \n|---|---|---|\n| Setup, queueing, and tokenization | 40 ms | 40 ms | \n| Prefill | 60 ms | 260 ms | \n| First-token selection and delivery | 20 ms | 20 ms | \n| **Time to first text** | **120 ms** | **320 ms** | \n\nNow suppose both replies stream the same three text chunks.\n\n``` php\nFresh: 120 ms -> 145 ms -> 170 ms\nLong:  320 ms -> 345 ms -> 370 ms\n```\n\nThe long chat starts **200 ms later**. Once text appears, both replies advance at 25 ms per chunk in this example. In production, a longer context can also affect decode because each new token may attend to more earlier positions.\n\n**Time to first token (TTFT)** measures the wait for the first output token. **Inter-token latency (ITL)** measures the gap between consecutive output tokens. Check where your dashboard starts the clock. A client-side measurement can include network time as well as time spent on the server. [NVIDIA inference metrics](https://docs.nvidia.com/nim/benchmarking/llm/1.0.0/metrics)\n\nNow imagine both requests receive an event with no text at **40 ms**. A timer that stops at the first event reports 40 ms for both requests and misses the entire 200 ms difference.\n\nIf you only have text chunks, measure **time to first text** and **inter-chunk latency**. A chunk can contain multiple tokens. Here, each event is a pair of elapsed milliseconds and text. An empty string represents an event with no text.\n\n``` python\ndef stream_times(events):\n    text_times = [ms for ms, text in events if text]\n    if not text_times:\n        raise ValueError(\"No text chunks arrived\")\n\n    first = text_times[0]\n    last = text_times[-1]\n    gaps = len(text_times) - 1\n    return {\n        \"first_text_ms\": first,\n        \"avg_chunk_gap_ms\": (last - first) / gaps if gaps else None,\n        \"last_text_ms\": last,\n    }\n\nfresh = [(40, \"\"), (120, \" three\"), (145, \" sides\"), (170, \".\")]\nlong = [(40, \"\"), (320, \" three\"), (345, \" sides\"), (370, \".\")]\n\nassert fresh[0][0] == long[0][0] == 40\nassert stream_times(fresh) == {\n    \"first_text_ms\": 120, \"avg_chunk_gap_ms\": 25.0, \"last_text_ms\": 170,\n}\nassert stream_times(long) == {\n    \"first_text_ms\": 320, \"avg_chunk_gap_ms\": 25.0, \"last_text_ms\": 370,\n}\n\nprint(\"Fresh chat\", stream_times(fresh))\nprint(\"Long chat \", stream_times(long))\n```\n\n**Try it.** Change the last two timestamps in `long` to `365` and `410`. Update the expected values in the second assertion and run it again. First text still arrives at 320 ms. The average gap rises from 25 ms to 45 ms. A dashboard that tracks only first text will miss that slowdown.\n\nSome serving engines can shorten prefill by reusing work when a request starts with the same sequence of tokens as an earlier request. That’s **prefix caching**. In the next post, we’ll look at what the **KV cache** actually stores and how decode uses it. [vLLM prefix caching](https://docs.vllm.ai/en/stable/features/automatic_prefix_caching/)\n\nWhen you’ve seen a slow streamed reply, was the delay before the first text or between chunks?\n\n*Adapted from [Prefill: Process the Prompt and Build the Cache](https://int8ai.com/llm-foundations/prefill-process-the-prompt), which includes a causal attention diagram, a worked GPU compute estimate, and a KV cache sizing exercise. AI assisted with drafting this adaptation.*", "url": "https://wpnews.pro/news/why-does-the-first-llm-token-take-longer", "canonical_source": "https://dev.to/int8ai/why-does-the-first-llm-token-take-longer-3fpj", "published_at": "2026-11-27 10:37:32+00:00", "updated_at": "2026-11-27 10:46:02.352670+00:00", "lang": "en", "topics": ["large-language-models", "ai-infrastructure", "mlops", "ai-tools"], "entities": ["NVIDIA"], "also_reported_by": [], "takeaway": "Measure time to first text and inter-chunk latency from non-empty streaming chunks rather than the first event, since an empty event at 40 ms reported identical TTFT for both fresh and long chats while the real gap was 200 ms.", "recommendation": {"advice": "When tracking LLM latency, filter streaming events to non-empty text chunks and compute time to first text plus average chunk gap, since timers that stop at the first event can hide prefill delays.", "playbook": "production-ops", "playbook_url": "https://wpnews.pro/playbooks/production-ops"}, "alternates": {"html": "https://wpnews.pro/news/why-does-the-first-llm-token-take-longer", "markdown": "https://wpnews.pro/news/why-does-the-first-llm-token-take-longer.md", "text": "https://wpnews.pro/news/why-does-the-first-llm-token-take-longer.txt", "jsonld": "https://wpnews.pro/news/why-does-the-first-llm-token-take-longer.jsonld"}}