A dedicated inference server such as vLLM, HuggingFace TGI, TensorRT-LLM or SGLang hosts model weights and turns HTTP or gRPC calls into efficient GPU work, usually behind an OpenAI-compatible API.
Continuous batching and the paged KV cache
Two techniques account for most of what these engines gain. Continuous batching accepts new requests at every decode step instead of waiting to fill a static batch. When one request finishes, the GPU takes another immediately, so it stays busy.
The paged KV cache, as in vLLM's PagedAttention, manages attention memory in fixed-size pages, the way an operating system manages virtual memory. Storing each sequence's cache in one contiguous block causes fragmentation. Pages avoid it, so more concurrent sequences fit on one GPU, and a finished request returns its pages for reuse.
- Receive the HTTP or gRPC request.
- Batch requests dynamically at each decode step.
- Manage the KV cache with fixed-size pages.
- Run the model weights on the GPU.
- Return the response to the client.
The nearest familiar system is a connection-pooling server such as a tuned Postgres instance or an nginx worker pool, sharing a scarce resource among many clients. The analogy stops at allocation. A connection is a fixed-size resource, while KV caches grow token by token, so scheduling can change at every decode step.
Why model.generate() behind Flask wastes the GPU
The simple route is calling model.generate() inside a Flask server. It serves requests one at a time and leaves the GPU idle between them. Throughput falls under load and cost per token rises, because nothing batches requests or manages memory.
A serving engine batches continuously and manages memory dynamically. The vLLM authors reported up to 24 times the throughput of HuggingFace Transformers and up to 3.5 times that of TGI in their 2023 tests (LLaMA-7B on an A10G, LLaMA-13B on an A100). Gains depend on workload and model.
Self-hosted engine, managed API or local runner
| Setup | Fit | Reason |
|---|---|---|
| Self-hosted open weights with concurrent users | vLLM or TGI | Built to serve many requests at once. |
| Managed API (OpenAI, Anthropic, Bedrock) | No engine needed | The provider already optimizes serving. |
| Single-user local development | llama.cpp or Ollama | Simpler setup with no concurrency to manage. |
The result is higher tokens per second for each dollar and more predictable latency under heavy concurrency, without writing your own batching or memory code.
Example scenario: latency rises from 150 ms to 500 ms at peak
A likely cause is resource exhaustion: GPU memory is full, or concurrent requests exceed what the server can handle. A large model under heavy load may fail to allocate enough resources, which surfaces as internal errors.
- Monitor GPU memory usage and see whether it reaches 100% at peak.
- Check server logs for memory allocation failures or request timeouts.
- Compare the number of concurrent requests with the configured limits.
Primary source. vLLM: Easy, Fast, and Cheap LLM Serving with PagedAttention, vLLM project blog, by Woosuk Kwon, Zhuohan Li, Siyuan Zhuang, Ying Sheng, Lianmin Zheng, Cody Yu, Joey Gonzalez, Hao Zhang and Ion Stoica, 20 June 2023.