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.

What to profile
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.
LLM latency levers
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 finding | Fix |
|---|---|
| Large prompt for a trivial step | Trim prompt; smaller model; structured output |
| Many sequential model calls | Merge steps or parallelise independent ones |
| Long outputs dominate latency | Concise instructions; lower maxTokens |
| Retrieval dominates latency | Filters, index tuning, smaller k, cached embeddings |
| Connection set-up on every call | Reuse SDK clients outside the handler |
| Cross-Region hops | Co-locate services in one Region |
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.
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.