Streaming Token Delays and TTFT Optimization in ADK
What time-to-first-token actually measures in an agent turn, where the delay comes from, and a concrete measurement and optimization playbook for ADK streaming — prompt size, model tier, region, thinking budgets, and transport.
Published on • November 27, 2026
AI Assistant

Users do not experience your agent’s latency as an average. They experience the moment before anything appears on screen. Two seconds of silence feels broken; two seconds of streaming text feels fast. That first moment has a name — time to first token (TTFT) — and in agent systems it is the single most controllable perception lever you have.
TTFT is also misleading: it looks like a model metric, but in an ADK turn it is a stack of latencies stacked on top of each other, most of which belong to you. This post breaks the stack apart, shows how to measure it honestly, and walks through the optimizations in the order that actually pays.
What TTFT is — and what it is not
TTFT is the wall-clock time from “request leaves my process” to “first content token arrives back”. It is not:
- Not generation speed. Once tokens flow, throughput (tokens/sec) takes over. A model can have excellent TTFT and mediocre throughput, or the reverse.
- Not end-to-end latency. For an agent with tools, the user-visible completion includes tool execution, a second model call, and possibly more.
- Not a model constant. The same model gives you different TTFTs depending on prompt size, region, queue depth, and whether it decided to think first.
That last point matters most: on reasoning models, thinking happens before the first visible token, so a generous thinking budget reads as TTFT even though the model is working productively. Treat “no tokens for 1.5 s” as ambiguous — it may be queueing, or it may be deliberation.
The latency stack in an ADK turn
your code → transport → API queue → prompt processing → (thinking) → first token
tool overhead ↑ ↑ ↑ ↑
serialization connection setup prompt size thinking budget
Rough ordering for a typical web-facing agent:
- Client-side serialization and queueing. Python event-loop blocking, JSON encoding of a big history, an HTTP layer that buffers.
- Connection setup. TLS handshake on a cold connection is real money on a first turn.
- Prompt processing. Prefill scales with total input tokens — system instruction + tool declarations + history + RAG chunks.
- Queueing and region distance. Round trips to a distant region, or a busy endpoint, add queue time before any model work starts.
- Thinking. Optional reasoning before output.
- First streamed chunk arriving at your process, then being forwarded to the browser.
Only step 4–5 belong to the provider. Everything above is yours.
Measuring it honestly
You cannot optimize what you have not instrumented. Add a timestamp at the start of the run and a timestamp at the first partial event:
import time
async def run_with_ttft(runner, session_id, user_id, message):
started = time.perf_counter()
first_token_at = None
last_token_at = None
text = ''
async for event in runner.run_async(
session_id=session_id, user_id=user_id, new_message=message
):
if event.content and event.content.parts:
chunk = ''.join(p.text or '' for p in event.content.parts)
if chunk:
now = time.perf_counter()
if first_token_at is None:
first_token_at = now
last_token_at = now
text += chunk
return {
'ttft_s': None if first_token_at is None else round(first_token_at - started, 3),
'total_s': None if last_token_at is None else round(last_token_at - started, 3),
'tokens_out': len(text) // 4,
}
Three disciplines make the number trustworthy:
- Report percentiles, not means. TTFT distributions have long tails; publish p50 and p95.
- Separate the first turn from later turns. Turn one pays connection setup and no cache hits; turn five has warm connections and a populated prefix.
- Tag the segments. Record
ttft_model(first model event) separately fromttft_ui(first byte your client renders), because the gap between them is your transport.
The optimization playbook, in order of payoff
1. Make sure you are streaming at all
If your API layer buffers the response until the model finishes, TTFT becomes total latency and nothing else you do matters. ADK emits partial events during generation; forward them immediately and flush your HTTP/SSE layer per chunk. Fixing buffering is usually a 5× improvement, not a 5% one.
2. Cut prompt size before the turn
Prefill is proportional to input tokens, so the fastest token is the one you didn’t send:
- Trim the system instruction’s dead weight — repeated persona padding, stale safety boilerplate.
- Prune history: drop tool payloads from turns older than the window where they matter.
- Inject RAG context just in time rather than accumulating it across turns.
- Strip re-serialized JSON blobs after a tool has already been summarised.
See context compaction strategies for the machinery behind this.
3. Pick the right model tier
A Flash-class model on a routing task is not a compromise, it is the correct engineering choice. Reserve the Pro-tier model for turns that need depth. If your agent uses one model for everything, your TTFT is set by your hardest turn.
4. Be deliberate about thinking
Reasoning budgets trade TTFT for quality. Give deep planning turns a budget and give simple turns none — see thinking models and reasoning budgets. If your product surfaces a “thinking…” indicator, the trade becomes a UX decision rather than an invisible stall.
5. Close the distance
Deploy the runtime in the same region as the model endpoint. A Cloud Run service in europe-west4 calling a US-only endpoint pays a transatlantic round trip on every turn, including the streaming chunks. Keep your session store in the same region too — session read/write sits in front of the first token.
6. Keep connections warm
- Reuse a single HTTP client across requests instead of constructing one per turn.
- Size serverless instances so you are not paying cold-start penalties on the interactive path.
- Warm the endpoint with a tiny canary request after deploy or scale-up rather than letting the first real user eat the cold start. This is the same discipline described in cold start management.
7. Reduce post-token overhead
If TTFT looks fine but perceived latency doesn’t improve, the problem moved: chunk serialization, per-chunk database writes, or a front end re-rendering the whole transcript on every delta. Batch UI updates on requestAnimationFrame and persist at completion, not per token.
A worked improvement
A support agent with these numbers:
| Step | Before | After |
|---|---|---|
| Response buffering in API layer | 2.4 s | 0.0 s (flush per chunk) |
| System instruction + history | 11,200 tokens | 4,100 tokens (pruned) |
| Model tier for triage turns | Pro-tier | Flash-tier |
| Thinking budget on triage | 1,024 | 0 |
| p50 TTFT | 3.1 s | 0.6 s |
None of these required a new model or a clever algorithm. Three were deletions.
Takeaways
- TTFT is a stack: your transport, your prompt size, your model tier, region distance, and thinking — then the model.
- Measure p50/p95, split first turn from steady state, and separate model TTFT from UI TTFT.
- The biggest wins are structural: actually streaming, flushing per chunk, and cutting prompt size.
- Treat thinking budgets and model tiers as TTFT inputs, not just quality inputs.
- Colocate with the endpoint, keep connections warm, and only then micro-optimize.