DEV Community

Int8ai
Int8ai

Posted on Originally published at int8ai.com

Why Does the First LLM Token Take Longer?

Open a fresh chat and type Finish this sentence: A triangle has…. Now ask the same thing at the end of a long conversation.

Both 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.

In the first post, we followed the loop that chooses a token, appends it, and repeats. This time, let’s see what happens before that first choice.

Same message, different amount of work

Think 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.

Fresh: [Instructions] [Latest message]
Long:  [Instructions] [History] [Latest message]
Enter fullscreen mode Exit fullscreen mode

Processing 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.

Prefill 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.

Where the extra 200 ms goes

Imagine two requests to the same model. We’ll keep setup and delivery time fixed and change only prefill. These timings are illustrative.

Before the first text chunk Fresh chat Long chat
Setup, queueing, and tokenization 40 ms 40 ms
Prefill 60 ms 260 ms
First-token selection and delivery 20 ms 20 ms
Time to first text 120 ms 320 ms

Now suppose both replies stream the same three text chunks.

Fresh: 120 ms -> 145 ms -> 170 ms
Long:  320 ms -> 345 ms -> 370 ms
Enter fullscreen mode Exit fullscreen mode

The 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.

The first event can arrive before the first text

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

Now 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.

If 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.

def stream_times(events):
    text_times = [ms for ms, text in events if text]
    if not text_times:
        raise ValueError("No text chunks arrived")

    first = text_times[0]
    last = text_times[-1]
    gaps = len(text_times) - 1
    return {
        "first_text_ms": first,
        "avg_chunk_gap_ms": (last - first) / gaps if gaps else None,
        "last_text_ms": last,
    }


fresh = [(40, ""), (120, " three"), (145, " sides"), (170, ".")]
long = [(40, ""), (320, " three"), (345, " sides"), (370, ".")]

assert fresh[0][0] == long[0][0] == 40
assert stream_times(fresh) == {
    "first_text_ms": 120, "avg_chunk_gap_ms": 25.0, "last_text_ms": 170,
}
assert stream_times(long) == {
    "first_text_ms": 320, "avg_chunk_gap_ms": 25.0, "last_text_ms": 370,
}

print("Fresh chat", stream_times(fresh))
print("Long chat ", stream_times(long))
Enter fullscreen mode Exit fullscreen mode

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.

Some 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

When you’ve seen a slow streamed reply, was the delay before the first text or between chunks?

Adapted from Prefill: Process the Prompt and Build the Cache, which includes a causal attention diagram, a worked GPU compute estimate, and a KV cache sizing exercise. AI assisted with drafting this adaptation.

Top comments (0)