n4nAI

How context length affects time to first token

Analyze how prompt size drives time to first token in LLM inference, why it's sublinear in practice, and how to benchmark and reduce long-context TTFT.

n4n Team4 min read850 words

Audio narration

Coming soon — every post will get a voice note here.

The biggest lever on context length time to first token is not the model’s raw decode speed but how many input tokens it must process before emitting the first output. In practice, TTFT scales with prefill cost, and that cost is a function of sequence length, batching policy, and whether the provider can reuse a cached prefix. This article breaks down the relationship between context length and time to first token with concrete measurements you can reproduce.

The prefill/decode split

Transformer inference splits into two phases. Prefill consumes the entire input prompt in parallel and builds the key/value (KV) cache. Decode then generates tokens one at a time, attending to the cached prefix.

Time to first token is essentially prefill latency plus scheduling and network overhead. Decode latency affects inter-token gaps, not TTFT. If you send a 2,000-token prompt or a 200,000-token prompt to the same model, the decode phase behaves identically after the first token; the gap you feel is almost entirely prefill.

Prefill compute for a standard transformer layer is:

  • Feed-forward: O(n · d²) where n is sequence length, d is hidden dim.
  • Attention: O(n² · d) for the score matrix, but with flash-attention-style kernels this is memory-bound and heavily parallelized.

Because GPUs process the n positions concurrently, wall-clock prefill grows roughly linearly with n for most production sizes (<128K tokens). The quadratic term shows up as higher slope at extreme lengths, not as a sudden blow-up.

Why context length time to first token is not a single number

Published “TTFT” averages hide three variables:

  1. Prompt shape – A 100K-token prompt of repeated text compresses better in speculative paths than diverse text. Real corpora have varied token distributions.
  2. Batch contention – On a shared endpoint, your request may queue behind a 200K-token prefill from another tenant. TTFT becomes prefill_yours + prefill_theirs / batch_efficiency.
  3. Cache hits – If the provider reused a prefix KV cache, your effective n drops to the uncached suffix.

Thus, context length time to first token for a 50K-token prompt can be lower than for a 10K-token prompt if the latter misses cache and the former hits a warm prefix. Treating TTFT as a function of raw token count alone is misleading.

Measuring it yourself

You cannot trust a dashboard that reports median TTFT across all lengths. Build a harness that pads prompts to specific sizes and streams the first chunk.

A minimal benchmark harness

from openai import OpenAI
import time

client = OpenAI(base_url="https://api.openai.com/v1", api_key="sk-...")

def pad_prompt(n_tokens: int) -> str:
    # ~1.33 words per token is a safe English approximation
    return " ".join(["word"] * int(n_tokens * 1.33))

for n in [1_000, 10_000, 50_000, 100_000]:
    prompt = pad_prompt(n)
    start = time.perf_counter()
    stream = client.chat.completions.create(
        model="gpt-4o",
        messages=[{"role": "user", "content": prompt}],
        stream=True,
    )
    ttft = None
    for chunk in stream:
        if chunk.choices[0].delta.content:
            ttft = time.perf_counter() - start
            break
    print(f"{n} tokens -> TTFT {ttft:.2f}s")

Run this against the same model on the same infrastructure at different times of day. You will see variance of 2–5x on shared pools. For an authoritative number, colocate with dedicated capacity or measure percentiles over hundreds of calls.

The hidden costs: queueing and batching

Modern inference servers use continuous batching. A request with a 120K-token prefix enters the scheduler; the GPU starts its prefill but may interleave smaller requests. Your TTFT includes:

  • Time to acquire a batch slot.
  • Time to compute your prefill chunks (often split to avoid OOM).
  • Time until the scheduler yields the first generated token to the network.

At long contexts, prefill is frequently chunked into max_prefill_per_step blocks (e.g., 4K tokens per step). This protects latency for other tenants but stretches your TTFT linearly with chunk count. If you self-host, tune this; if you use a gateway, understand its fallback behavior when a provider degrades.

Prefix caching and KV reuse

The single most effective way to flatten context length time to first token is to avoid recomputing shared prefixes. System prompts, retrieved document headers, and tool schemas often repeat across calls.

Providers expose cache hints differently. Anthropic uses cache_control blocks; OpenAI uses implicit prompt caching on certain models. An OpenAI-compatible endpoint such as n4n.ai forwards provider cache-control hints, so a request with a cached long system prefix avoids re-prefill on subsequent calls. That turns a 80K-token TTFT from seconds into hundreds of milliseconds.

Example of a cache-aware request body (Anthropic-style, forwarded by compatible gateways):

{
  "model": "claude-3-5-sonnet",
  "messages": [
    {"role": "user", "content": "Long static context...", "cache_control": {"type": "ephemeral"}}
  ]
}

When the hint is honored, only the delta after the cached breakpoint is prefilled.

Tradeoffs of long-context strategies

You have three options for large input:

  • Stuff everything in context. Simplest code. TTFT and cost scale with n. Risk of lost-in-middle degradation on some models.
  • Retrieve and trim (RAG). Lower TTFT, but adds indexing latency and potential misses. Good when only a slice of data is relevant.
  • Chunk and summarize. Pipeline latency increases, but each model call stays small. Complexity rises.

There is no free lunch. If your user waits on TTFT for interactivity, a 200K-token monolith prompt will feel sluggish even on fast hardware. Splitting into a cached prefix plus a dynamic suffix often wins: the prefix is prefilled once, the suffix stays short.

Decisive takeaway

Context length time to first token is governed by prefill, not decode, and prefill is a product of effective token count after cache subtraction, batch contention, and chunking policy. Measure TTFT at your real prompt sizes with a streaming harness, push repeated prefixes into provider cache hints, and treat raw “max context” specs as upper bounds, not latency promises. If your product needs sub-second TTFT at 100K tokens, you must architect for prefix reuse or narrower context—no model alone will save you.

Tagslong-contexttime-to-first-tokenlatencybenchmark

Written by

n4n Team

The team building n4n — a single OpenAI-compatible API in front of 240+ models, with automatic fallback, load balancing and pay-per-token metering.

More from n4n Team →

All long-context latency benchmarks posts →