Lesson 5 of 5 Performance Optimization for GenAI

Profiling Generative AI Requests End to End

This lesson covers profiling the whole path of a generative AI request, API layer, retrieval, model calls, tools and post-processing, and the LLM-specific optimisations that fix the real bottlenecks.

Learning outcomes

After completing this lesson, you will be able to:

  • Profile every step of a request.
  • Apply LLM-specific latency reductions.
  • Make service communication efficient.

API call profiling

API call profiling records, for every step of a request, its duration, and for model calls the prompt tokens, completion tokens, time to first token and model ID. Aggregated by feature, the profile reveals patterns such as a classification step that sends a 6,000-token prompt to produce a one-word label, an agent that makes four model calls where one would do, or a tool that dominates latency. X-Ray or OpenTelemetry traces with annotations provide the per-step timing, and invocation logs provide the token details.

The following diagram compares the latency profile of a slow agent before and after the changes.

Profiling an agent that takes 14 seconds. Query rewriting, retrieval, three sequential planning calls of 2 seconds each and a 700-token final answer dominate. Skipping rewriting for standalone questions, requesting tools in parallel in one turn, a streamed 250-token answer and reused SDK clients bring it under 6 seconds, with the first words in about 2 seconds.
The step timings come from the scenario above.
Select all that apply

What to profile

Exam skills 4.2.6

Choose what API call profiling should record for each model call in a request.

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

LLM-specific latency reduction

Some optimisations apply specifically to language models. Because output is generated one token after another, reducing output length is one of the most effective ways to reduce latency. Merging several small sequential model calls into one, where quality allows, removes repeated overhead, and using smaller models for intermediate steps, such as classification or query rewriting, speeds up multi-step workflows.

Prompt caching reduces the time to process long, repeated prompt prefixes. On self-hosted models, speculative decoding, in which a small draft model proposes several tokens that the large model verifies at once, and quantisation increase generation speed. In RAG applications, vector database query optimisation, through filters, a smaller number of results, tuned indexes and cached query embeddings, reduces the share of latency spent on retrieval.

True or false

LLM latency levers

Exam skills 4.2.6 · 4.2.1

Check three claims about reducing latency in LLM workflows.

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

Efficient service communication

In an application that calls several services for each request, communication overheads add up. Reusing SDK clients and their HTTP connections across Lambda invocations, by creating them outside the handler, avoids repeating connection set-up and TLS handshakes. Keeping the application, vector store and model endpoint in the same Region avoids cross-Region round trips, and VPC endpoints keep traffic on the AWS network.

Design choices matter too. Asynchronous, event-driven calls between services avoid one service blocking while it waits for another, independent calls can be made in parallel rather than one after another, and payloads can carry IDs that refer to documents in storage instead of the large documents themselves. The following table summarises common profiling findings and their fixes.

Profiling findingFix
Large prompt for a trivial stepTrim prompt; smaller model; structured output
Many sequential model callsMerge steps or parallelise independent ones
Long outputs dominate latencyConcise instructions; lower maxTokens
Retrieval dominates latencyFilters, index tuning, smaller k, cached embeddings
Connection set-up on every callReuse SDK clients outside the handler
Cross-Region hopsCo-locate services in one Region
Worked example

An agent that feels slow

An agent takes 14 seconds per answer. Profiling shows: query rewriting 1.5 s, retrieval 1 s, three sequential tool-planning model calls of 2 s each, a 3 s final answer averaging 700 tokens, and new SDK clients created on each Lambda invocation.

Skipping rewriting for standalone questions, letting the model request tools in parallel in one turn, limiting the final answer to 250 tokens with streaming, and reusing clients bring the total to under 6 seconds with the first words in about 2 seconds.

Check yourself

Which property of a response is a major driver of model call latency?

Show answer

Its length: every output token takes time to generate.

Exam Tips

Remember for the exam

  • Profile every step: duration, tokens and TTFT per model call.
  • Output length is a major latency driver.
  • Merge or parallelise sequential model calls.
  • Reuse clients and co-locate services in one Region.