What is Token Throughput in LLMs?
Token throughput refers to the rate at which a Large Language Model (LLM) processes and generates tokens, typically measured in tokens per second (TPS). In a production environment, throughput is usually divided into two categories: input throughput (how fast the model ingests the prompt) and output throughput (how fast the model generates the response). Because LLM APIs and self-hosted models bill based on token volume and compute time, monitoring token throughput is essential for understanding both the performance and the financial cost of your AI applications.
Why Monitoring Token Throughput Matters
When deploying LLMs to production, moving beyond basic "does it work?" testing is critical. Monitoring token throughput provides several key benefits:
- User Experience: Output throughput directly correlates to perceived latency. If your model generates tokens at 5 TPS instead of 50 TPS, users will experience noticeable lag, especially in streaming chat applications.
- Cost Management: Cloud LLM providers charge by the token. Tracking throughput helps you identify inefficient prompts, overly chatty applications, or users who are consuming disproportionate amounts of resources.
- Capacity Planning: If you are self-hosting an open-source model using vLLM or Hugging Face TGI, throughput metrics tell you when your GPUs are becoming saturated and when you need to scale horizontally.
- Anomaly Detection: A sudden drop in throughput might indicate network issues, rate limiting from the provider, or a degraded node in your inference cluster.
Key Metrics to Track
To get a complete picture of your LLM's performance, you should track a combination of throughput and latency metrics:
- Tokens Per Second (TPS): The total number of completion tokens generated divided by the total generation time.
- Time To First Token (TTFT): The latency between sending the request and receiving the first generated token. This is crucial for streaming interfaces.
- Time Per Output Token (TPOT): The time taken to generate each subsequent token after the first one.
- Prompt vs. Completion Token Counts: Tracking the raw volume of tokens consumed per request, categorized by model and user.
How to Monitor Token Throughput in Production
The most effective way to monitor LLM throughput is to instrument your application code to capture metrics and export them to a time-series database like Prometheus, which can then be visualized in Grafana. Below is a practical example using Python, the OpenAI SDK, and the prometheus_client library.
Instrumenting Your LLM Calls
First, install the required libraries:
pip install openai prometheus_client
Next, create a wrapper around your LLM API calls to capture timing and token usage data. This wrapper will expose a Prometheus metrics endpoint that can be scraped by your monitoring infrastructure.
import time
import openai
from prometheus_client import Counter, Histogram, start_http_server
# Initialize Prometheus metrics
PROMPT_TOKENS = Counter(
'llm_prompt_tokens_total',
'Total prompt tokens processed',
['model', 'tenant_id']
)
COMPLETION_TOKENS = Counter(
'llm_completion_tokens_total',
'Total completion tokens generated',
['model', 'tenant_id']
)
THROUGHPUT_HISTOGRAM = Histogram(
'llm_output_tokens_per_second',
'Output tokens generated per second',
['model'],
buckets=(1, 5, 10, 20, 30, 50, 75, 100, 150, 200)
)
TTFT_HISTOGRAM = Histogram(
'llm_time_to_first_token_seconds',
'Time to first token in seconds',
['model']
)
def generate_llm_response(prompt, model="gpt-3.5-turbo", tenant_id="default"):
start_time = time.time()
first_token_time = None
try:
# Using streaming to capture Time To First Token (TTFT)
response = openai.ChatCompletion.create(
model=model,
messages=[{"role": "user", "content": prompt}],
stream=True
)
full_response = ""
completion_tokens = 0
for chunk in response:
if first_token_time is None:
first_token_time = time.time() - start_time
TTFT_HISTOGRAM.labels(model=model).observe(first_token_time)
if chunk.choices[0].delta.get("content"):
full_response += chunk.choices[0].delta.content
completion_tokens += 1 # Approximate count for streaming
elapsed_time = time.time() - start_time
# Calculate generation time (excluding TTFT)
generation_time = elapsed_time - first_token_time if first_token_time else elapsed_time
# Record throughput
if generation_time > 0 and completion_tokens > 0:
tps = completion_tokens / generation_time
THROUGHPUT_HISTOGRAM.labels(model=model).observe(tps)
# Note: For exact token counts in streaming, you should use the
# usage data from the final chunk if your provider supports it.
# Here we use the approximate count for demonstration.
PROMPT_TOKENS.labels(model=model, tenant_id=tenant_id).inc(10) # Placeholder for actual prompt token count
COMPLETION_TOKENS.labels(model=model, tenant_id=tenant_id).inc(completion_tokens)
return full_response
except Exception as e:
print(f"Error generating response: {e}")
return None
if __name__ == "__main__":
# Start up the server to expose the metrics.
start_http_server(8000)
print("Prometheus metrics server started on port 8000")
# Simulate a request
while True:
generate_llm_response("Explain the concept of token throughput in LLMs.")
time.sleep(5)
Visualizing in Grafana
Once your application is exporting metrics to Prometheus, you can create dashboards in Grafana. Useful PromQL queries for your dashboards include:
- Average Output Throughput:
rate(llm_output_tokens_per_second_sum[5m]) / rate(llm_output_tokens_per_second_count[5m]) - 95th Percentile TTFT:
histogram_quantile(0.95, sum(rate(llm_time_to_first_token_seconds_bucket[5m])) by (le, model)) - Total Tokens Consumed per Minute:
rate(llm_completion_tokens_total[1m]) * 60
Best Practices for Token Throughput Monitoring
- Tag by Model and Tenant: Always include labels for the specific model version (e.g.,
gpt-4-0613vsgpt-3.5-turbo) and the tenant/user ID. This allows you to isolate noisy neighbors and track how different models perform under load. - Use Histograms, Not Averages: Averages hide outliers. Use histograms to track percentiles (p50, p90, p99) for throughput and latency. A p99 drop in throughput will tell you that 1% of your users are having a terrible experience, which an average would mask.
- Set Up Alerts: Configure alerts for when throughput drops below an acceptable threshold or when token consumption spikes unexpectedly, which could indicate a prompt injection attack or an infinite loop in an autonomous agent.
- Monitor Provider Rate Limits: Keep an eye on HTTP 429 Too Many Requests errors. Correlating these errors with your throughput metrics will help you understand if your bottlenecks are internal or imposed by your LLM provider.
Conclusion
Monitoring token throughput is not just a technical necessity; it is a core business requirement for any production LLM application. By instrumenting your code to capture tokens per second, time to first token, and total token consumption, you gain the visibility needed to optimize user experience, control costs, and scale your infrastructure effectively. Implementing a robust observability stack using tools like Prometheus and Grafana ensures that you can proactively detect issues before they impact your users, allowing your AI features to remain fast, reliable, and economically viable.