Latency and Throughput Tracking
Complete the full lesson to earn 25 points
Work through each section, then tap “Mark as Complete” on the last one.
Lesson: Latency and Throughput Tracking in GenAI Systems
Introduction: Why Observability Matters for Generative AI
In the world of traditional software, monitoring often focuses on uptime, error rates, and CPU utilization. However, Generative AI (GenAI) introduces a new paradigm where the "output" is non-deterministic and the computational cost per request is significantly higher than a standard database query or API call. When we talk about Latency and Throughput in the context of Large Language Models (LLMs), we are not just talking about system performance; we are talking about user experience and operational cost.
Latency in GenAI refers to the time it takes for a model to generate a response, which is often perceived by users as "typing speed" or "waiting time." Because LLMs are inherently sequential—generating one token at a time—latency can range from a few hundred milliseconds to several minutes depending on the model size and the length of the output. Throughput, on the other hand, measures how many requests or tokens your system can handle simultaneously. If your latency is high, your throughput drops, and your infrastructure costs skyrocket.
Understanding these metrics is vital because GenAI systems are often the most expensive components of a modern tech stack. Without proper observability, you might find yourself with a system that works in testing but fails under the load of real-world traffic, leading to frustrated users and unsustainable cloud bills. This lesson will guide you through the technical foundations of tracking these metrics, implementing instrumentation, and optimizing your LLM-based applications.
Defining the Core Metrics: Latency vs. Throughput
To effectively observe a GenAI system, we must break down metrics into granular components. Unlike a simple web server where a request is processed and returned, a GenAI request involves multiple stages: prompt processing, inference, and token streaming.
Latency Components
- Time to First Token (TTFT): This is arguably the most important metric for user-facing applications. It measures how long the user waits before the first character of the response appears on their screen. A low TTFT makes the system feel responsive, even if the total response takes a long time.
- Inter-token Latency: This measures the time delay between generated tokens. If this is high, the text will appear to "stutter" or lag, which provides a poor reading experience for the end user.
- Total Generation Time: The end-to-end duration from sending the prompt to receiving the final end-of-sequence token. This is the primary metric for cost estimation and long-running background tasks.
Throughput Components
- Requests Per Second (RPS): The number of incoming prompts your system receives.
- Tokens Per Second (TPS): This is the gold standard for LLM performance. It measures the total volume of text generated across all active sessions. High TPS indicates efficient use of GPU resources.
- Concurrent Sessions: The number of users or agents actively interacting with the model at any given moment.
Callout: Latency vs. Throughput in LLM Context While latency is a measure of individual speed, throughput is a measure of aggregate capacity. In LLM serving, these two are inversely related. As you push more requests into the model (increasing throughput), the system eventually reaches a saturation point where the GPU can no longer process tokens at the same speed, causing latency to spike exponentially. Finding the "sweet spot" where you maintain acceptable latency while maximizing throughput is the main challenge of LLM operations.
Implementing Observability: Instrumentation Strategies
Observability is not just about logging; it is about structured telemetry. You need to capture metadata alongside every request to ensure you can debug performance bottlenecks later.
Step 1: Capturing Telemetry
To track latency accurately, you must wrap your LLM calls in a timer. Do not rely on server-side timestamps alone, as these often miss the network transit time between your client and your inference service.
Step 2: Code Implementation Example
Below is a conceptual example using Python. This structure can be adapted to any framework, such as LangChain, LlamaIndex, or raw OpenAI API calls.
import time
import uuid
def track_llm_call(model_id, prompt, llm_function):
request_id = str(uuid.uuid4())
start_time = time.perf_counter()
# We use a list to store tokens if we are streaming
tokens = []
try:
# Assuming a streaming response generator
response_stream = llm_function(prompt)
first_token_time = None
for chunk in response_stream:
if first_token_time is None:
first_token_time = time.perf_counter()
tokens.append(chunk)
end_time = time.perf_counter()
# Calculate metrics
total_duration = end_time - start_time
ttft = first_token_time - start_time
token_count = len(tokens)
tps = token_count / total_duration
# Here you would push these metrics to a monitoring system
# like Prometheus, Datadog, or an observability platform
log_metrics(request_id, ttft, total_duration, tps, token_count)
return "".join(tokens)
except Exception as e:
log_error(request_id, e)
raise e
Note: Always use
time.perf_counter()instead oftime.time()for measuring latency.time.time()is subject to system clock updates (like NTP adjustments), which can result in negative durations or inaccurate measurements.perf_counter()is designed specifically for measuring intervals.
Analyzing Performance Bottlenecks
Once you have the data, you need to know where to look. Performance degradation in GenAI usually stems from one of three areas: the model architecture, the infrastructure, or the prompt design.
The Impact of Prompt Length
The number of tokens in your prompt directly affects latency. Because LLMs use a transformer architecture, the attention mechanism scales with the length of the input. A very long prompt (like a large context window) will increase the TTFT significantly, as the model must "read" and process all those tokens before generating the first word of the response.
Infrastructure Constraints
If your throughput is low, check your GPU memory utilization. If your KV cache (the memory used to store the attention keys and values for previous tokens) is full, the system will have to swap memory to disk, causing latency to skyrocket. This is often the primary cause of sudden performance drops in production.
Best Practices for Optimization
- Batching: If you are running your own inference server (using tools like vLLM or TGI), enable continuous batching. This allows the model to process multiple requests simultaneously, significantly increasing throughput without sacrificing much latency.
- Quantization: Reducing the precision of the model weights (e.g., from FP16 to INT8 or INT4) can reduce the memory footprint and speed up generation.
- Caching: Use semantic caching for repeated prompts. If a user asks a common question, retrieve the answer from a database instead of calling the model. This results in near-zero latency for those queries.
Common Pitfalls and How to Avoid Them
Even with robust instrumentation, developers often fall into traps that skew their data or degrade performance.
1. Ignoring Network Latency
Many developers measure the time the LLM takes to generate tokens but forget to measure the time it takes to send the request over the network. If your users are globally distributed, the round-trip time (RTT) can be significant. Always measure latency from the "edge"—the point closest to the user.
2. Over-Logging
Logging every single token or every single internal state transition can create a massive amount of data, increasing your storage costs and potentially slowing down your application. Log only the summary metrics (TTFT, total time, token counts) and keep raw request/response logs sampled at a lower rate.
3. Static Thresholds for Alerts
Alerting when latency exceeds 500ms might be appropriate for a chat interface, but it might be completely wrong for a complex document summarization task. Use percentile-based alerting (e.g., "Alert if the 95th percentile of TTFT exceeds 2 seconds") rather than hard-coded averages.
Warning: Be cautious with averages. In GenAI, the distribution of latencies is rarely a bell curve. It is often "long-tailed," meaning a few very slow requests can skew your average. Always monitor the P95 and P99 (the 95th and 99th percentiles) to understand the experience of your "unluckiest" users.
Comparison of Monitoring Tools
When choosing a platform to observe your GenAI system, consider the following categories.
| Feature | Standard APM (Datadog/NewRelic) | Specialized LLM Observability (LangSmith/Arize/Helicone) |
|---|---|---|
| Token Tracking | Limited or manual | Native integration |
| Prompt/Response Logging | Difficult to store | Built-in UI for prompt debugging |
| Cost Estimation | Manual calculation | Automatic per-request cost tracking |
| Model Versioning | No | Built-in tracking of model variants |
Standard Application Performance Monitoring (APM) tools are excellent for infrastructure metrics, but they lack the semantic understanding of LLM interactions. If you are building a production-grade GenAI application, you will likely need a combination of both: infrastructure monitoring for the servers and specialized tools for the model interactions.
Advanced Strategy: The "Golden Signals" for GenAI
In the Site Reliability Engineering (SRE) world, the "Four Golden Signals" are Latency, Traffic, Errors, and Saturation. For GenAI, we should adapt these specifically:
- Latency: Track TTFT and Inter-token latency as separate metrics.
- Traffic: Measure both Request Throughput (RPS) and Token Throughput (TPS).
- Errors: Distinguish between System Errors (API timeouts, 500s) and Content Errors (hallucinations, refusal to answer, safety filter triggers).
- Saturation: Monitor GPU memory usage (VRAM) and KV Cache utilization.
By tracking these four signals, you can proactively identify when your system is nearing its capacity and scale your infrastructure before your users experience a degradation in service.
Step-by-Step: Setting Up a Monitoring Dashboard
To build an effective dashboard for your GenAI observability, follow these steps:
Phase 1: Data Collection
- Instrument your code: Add the wrapper function mentioned earlier to your LLM interaction layer.
- Export metrics: Use a library like
prometheus_clientor a logging library to send these metrics to a centralized aggregator. - Add context: Tag your metrics with
model_version,user_region, andprompt_template_id. This allows you to filter your latency data to see if a specific version of your prompt is slower than others.
Phase 2: Visualization
- Create a heatmap: A heatmap of latency distribution is better than a line chart. It shows you the density of requests at different latency levels.
- Build a token-count dashboard: Plot
Tokens GeneratedvsTime Takenover time. This helps you identify if the model is slowing down as the conversation history grows. - Cost tracking: Multiply the
total_tokens(input + output) by the model's cost per token to visualize your daily spend in real-time.
Phase 3: Alerting
- Set P99 alerts: Create an alert for when your P99 latency exceeds a threshold that impacts user experience.
- Error rate spikes: Set an alert for a sudden increase in 4xx or 5xx errors from the LLM provider, which often indicates an outage or a rate-limiting issue.
- Cost anomalies: Set an alert for any sudden, unexplained spike in token usage, which could indicate a recursive prompt loop or a malicious attack.
Addressing Common Questions (FAQ)
Q: Does streaming increase latency? A: Technically, streaming does not reduce the total time to generate the full response, but it drastically improves the perceived latency. By showing the user the first token as soon as it is generated, you reduce the TTFT, which is the most important metric for user satisfaction.
Q: Why is my throughput lower than expected? A: This is usually due to "sequential processing." If your application waits for the full response from the LLM before sending the next prompt, you are not utilizing the hardware effectively. Ensure your architecture allows for asynchronous requests and, if possible, use batching.
Q: How do I handle rate limits from providers like OpenAI?
A: Rate limits are a form of "external throughput constraint." Your observability dashboard should track your Rate Limit Remaining headers. If you see this number dropping, your system should implement an exponential backoff strategy to prevent getting banned or hitting hard limits.
Best Practices Checklist for Production
- Always log the prompt metadata: You cannot debug latency if you don't know what input caused it.
- Implement circuit breakers: If the LLM provider is down or responding extremely slowly, have a fallback mechanism (e.g., a simpler, faster model or a cached response).
- Monitor the "Cost-per-Query": Always link performance to cost. Sometimes, a faster model is actually cheaper because it reduces the idle time of your compute resources.
- Standardize your units: Use milliseconds for time and "tokens" for volume. Do not mix units across different parts of your system.
- Automated Regression Testing: Every time you update your prompt or model, run a benchmark script to measure the impact on latency and throughput. If the new prompt increases latency by more than 10%, investigate why.
Conclusion: Key Takeaways for GenAI Observability
- Latency is Multi-Dimensional: You cannot just measure "total time." You must track Time to First Token (TTFT) and Inter-token latency to understand the user experience.
- Throughput is the Efficiency Metric: High throughput, measured in Tokens Per Second (TPS), is the key to keeping operational costs down while scaling to more users.
- Instrument at the Edge: Always measure latency from the client's perspective to account for network transit time, which is often a hidden bottleneck in distributed GenAI applications.
- Use Percentiles, Not Averages: Because latency distributions in GenAI are often skewed, always monitor the P95 and P99 to understand the experience of your most affected users.
- Context is King: Always tag your metrics with metadata like model version, user region, and prompt ID. This allows you to perform root-cause analysis when performance degrades.
- Integrate Cost with Performance: Performance is not just about speed; it is about cost-efficiency. Track the cost per token alongside your latency metrics to ensure your system remains economically viable as it scales.
- Proactive Alerting: Build alerts based on P99 latency thresholds and cost anomalies to catch problems before they impact your entire user base or your budget.
By mastering these concepts, you shift your role from a developer who "just uses an API" to an engineer who builds robust, scalable, and cost-effective GenAI systems. Observability is the foundation upon which reliable AI is built; without it, you are essentially flying blind in a highly volatile and expensive environment. Start by implementing basic instrumentation today, and iteratively refine your monitoring stack as your application grows in complexity.
Enjoying the courses?
Everything stays free. Pro shows fewer ads, doubles your daily points limit so you progress twice as fast, and lets you read each lesson on one page.
- ✓ Fewer advertisements
- ✓ 2× daily points limit
- ✓ Distraction-free lessons