Troubleshooting LLM Inference Latency and Long-Connection Interruptions

    This guide provides a systematic approach to troubleshooting Large Language Model (LLM) inference latency and long-connection interruptions. It explains how to standardize performance metrics such as Time to First Token (TTFT) and Tokens Per Second (TPS), and explores common root causes including prefill overhead, KV cache constraints, and batching schedules to help developers optimize the end-to-end request chain.

    1. Symptoms of the Problem

    When using Large Language Models (LLMs), developers often encounter three main types of issues. The first is a delayed response start, where users must wait a long time after sending a request to see the first token. The second is slow generation speed, where the text output rate noticeably decreases. The third is mid-inference connection drops, where clients receive error codes during long inference tasks, causing all progress to be lost.

    These three issues seem independent but actually share the same request chain. A request must pass sequentially through the client, network egress, load balancer, gateway, inference service, and scheduling queue. Performance bottlenecks or timeout misconfigurations in any of these links will manifest to the user as one of the aforementioned issues. Therefore, troubleshooting cannot focus solely on the model itself; the entire chain must be broken down and verified segment by segment.

    2. Standardize Measurement Metrics

    Before discussing fast or slow performance, we need to clarify the metrics. Two commonly used metrics are:

    • Time to First Token (TTFT): The time from sending a request to receiving the first output token. It mainly reflects queuing time and input processing (prefill) time.
    • Tokens Per Second (TPS): The number of tokens output per second during the generation phase. It reflects the throughput capacity of the decode phase.

    End-to-end latency can be approximately broken down as:

    End-to-end latency ≈ TTFT + Output token count / TPS

    This formula shows that two requests with identical total latency might have completely different characteristics: one might have a slow TTFT but fast TPS, while the other has a fast TTFT but slow TPS. The optimization directions for these two scenarios are entirely different, and failing to distinguish between them prevents proper root cause analysis.

    Below is a sample code snippet for measuring these metrics from a streaming output:

    1. import time
    2. def measure_stream(stream_iter):
    3. """Measure TTFT, TPS, and total latency of a streaming output. stream_iter is an iterator that yields text chunks."""
    4. start = time.monotonic()
    5. first_token_time = None
    6. chunk_count = 0
    7. for chunk in stream_iter:
    8. if first_token_time is None:
    9. first_token_time = time.monotonic()
    10. chunk_count += 1 # Approximate count by data chunks here; for exact counting, use the corresponding model's tokenizer
    11. end = time.monotonic()
    12. if first_token_time is None:
    13. return {"ttft_s": None, "tps": None, "total_s": end - start}
    14. gen_time = end - first_token_time
    15. tps = chunk_count / gen_time if gen_time > 0 else None
    16. return {"ttft_s": first_token_time - start, "tps": tps, "total_s": end - start}

    This code is a reference implementation. In actual use, the counting method should be adjusted based on the API response format.

    3. Common Causes of Inference Slowdown

    3.1 Input Length and Prefill Overhead

    The prefill phase requires a forward pass over all input tokens, and the computation scales with input length. With long contexts, extensive conversation history, or large attached documents, TTFT will noticeably increase. The computation of standard attention implementations correlates with the square of the sequence length, so longer inputs lead to more prominent TTFT growth.

    3.2 Generation Length and KV Cache

    During the decoding phase, every generated token requires reading the existing KV cache. The cache grows with the context, consuming GPU memory and limiting the number of concurrent requests. When memory is constrained, the server might reduce concurrency or swap out the cache, causing TPS to drop.

    3.3 Queuing and Batching Schedules

    Inference services typically batch multiple requests together to improve hardware utilization. Continuous batching allows new requests to join an existing batch during decoding gaps, reducing queuing time. However, during peak hours, request backlogs will directly increase TTFT. Different scheduling strategies also perform differently and should be observed under actual load.

    3.4 Resource Contention and Multi-tenancy

    If multiple models run on the same hardware