Lesson 1 of 5 Performance Optimization for GenAI

Reducing Time to First Token and Total Latency

This lesson covers the main latency techniques for generative AI applications, whose latency users perceive in two parts, how long until the first words appear and how long until the answer is complete, each with different causes and remedies.

Learning outcomes

After completing this lesson, you will be able to:

  • Measure time to first token, output speed and total latency.
  • Reduce latency through model and serving choices.
  • Apply architectural techniques such as pre-computation and streaming.

Measuring latency correctly

Latency in generative AI applications has three components, and improving the wrong one wastes effort. Time to first token (TTFT) is the delay before the first token of the response arrives; it depends on queueing, on the time to process the input, which grows with prompt length, and on network hops. Output speed, measured in tokens per second, depends on the model and the infrastructure serving it. Total latency is roughly TTFT plus the number of output tokens divided by the output speed.

For chat interfaces with streaming, TTFT determines how responsive the application feels; for forms and back-end processing, total latency matters. Performance benchmarking measures all three with representative prompts at realistic percentiles, such as p50, p90 and p99, before and after every change, because averages hide the slowest requests, which are the ones users complain about.

The following diagram shows the parts of one request's latency.

The components of latency. Time to first token covers queueing, input processing, which grows with prompt length, and network hops. Output generation then takes the number of output tokens divided by the output speed. Total latency is roughly the sum; streaming chat feels fast when time to first token is short.
The proportions are schematic.
Fill in the number

Breaking down latency

Exam skills 4.2.1

Use the latency formula from the lesson: total latency ≈ TTFT + output tokens ÷ output speed.

Log in to try this activity Interactive activities are free with an account.

Model and infrastructure choices

The choice of model and serving option has the largest effect on latency. Smaller models generate tokens faster and process prompts more quickly. Latency-optimized inference in Amazon Bedrock, enabled for supported models and Regions by setting performanceConfig to optimized, runs requests on infrastructure tuned for lower latency at a higher price, which suits time-sensitive applications such as real-time agents and voice assistants.

Other options address specific causes. The Priority service tier gives requests preferential processing during busy periods, which reduces queueing delays. Prompt caching reduces TTFT when long prompt prefixes, such as instructions or reference documents, repeat across requests. Deploying the application in a Region close to its users reduces network latency. The following example enables latency-optimized inference.

r = brt.converse(
    modelId="us.amazon.nova-pro-v1:0",
    messages=messages,
    performanceConfig={"latency": "optimized"},   # supported models and Regions only
)
True or false

Latency options

Exam skills 4.2.1

Check three claims about model and infrastructure choices for latency.

Log in to try this activity Interactive activities are free with an account.

Architectural techniques

Pre-computation generates answers to predictable requests in advance (product summaries, daily briefings, FAQ answers) with batch inference and serves them from storage instantly. Parallel requests run independent sub-tasks concurrently, for example retrieving from two knowledge bases at once or analysing sections of a document in a Step Functions Parallel or Map state, so latency equals the slowest branch. Streaming cuts perceived latency to TTFT. Shorter outputs reduce total time because output tokens are generated sequentially. Avoiding unnecessary sequential calls, such as a separate rewriting step for simple queries, also helps. The following table summarises the trade-offs.

TechniqueImprovesTrade-off
Smaller modelTTFT and output speedPossible quality loss
Latency-optimized inferenceSpeed on supported modelsHigher price; limited availability
Priority tierConsistency under loadPremium price
Pre-computationInstant answers for predictable queriesStorage; staleness
Parallel callsTotal time of multi-part tasksConcurrency and quota use
StreamingPerceived latencyClient complexity
Shorter outputsTotal time and costLess detail

The following diagram compares when a voice assistant starts speaking before and after changes to cut its latency.

Getting a voice assistant to speak within one second. It used to wait four seconds for a full response from a large model. Streaming tokens into text-to-speech, latency-optimized inference or a smaller model, short answers and a cached system prompt bring the time to first audio under one second.
The timings come from the scenario above.
Match the pairs

Latency techniques

Exam skills 4.2.1

Match each latency problem with the technique that addresses it.

Log in to try this activity Interactive activities are free with an account.
Worked example

A voice assistant

A voice assistant must start speaking within one second. It currently waits for a full response from a large model, which takes four seconds.

Streaming the response into text-to-speech as tokens arrive, enabling latency-optimized inference for a supported model (or choosing a smaller model), keeping answers short for voice, and caching the long system prompt bring the time to first audio under one second. Benchmarks at p90 confirm the target.

Check yourself

Which latency measures should be tracked, and at which percentiles?

Show answer

Time to first token, output speed and total latency, at p90 and p99.

Exam Tips

Remember for the exam

  • Measure TTFT, output speed and total latency at p90 and p99.
  • Time-sensitive apps on supported models: latency-optimized inference.
  • Predictable queries: pre-compute with batch inference.
  • Independent sub-tasks: run in parallel; stream to cut perceived latency.