← Back to DevBytes

How to Monitor Token Throughput in Production LLMs

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:

Key Metrics to Track

To get a complete picture of your LLM's performance, you should track a combination of throughput and latency metrics:

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:

Best Practices for Token Throughput Monitoring

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.

— Ad —

Google AdSense will appear here after approval

← Back to all articles