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

Inference serving engines (vLLM, TGI)Diagram with these labels, in reading order: Request Handling; Batching Process; Cache Management; Return Output; HTTP/gRPC Request; Continuous Batching; & Scheduling; Paged KV Cache; Management; GPU Execution; of Model Weights; Response to Client.Request HandlingBatching ProcessCache ManagementReturn OutputHTTP/gRPC RequestContinuous Batching& SchedulingPaged KV CacheManagementGPU Executionof Model WeightsResponse to Client
Inference Serving Engine Workflow

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.

  1. Receive the HTTP or gRPC request.
  2. Batch requests dynamically at each decode step.
  3. Manage the KV cache with fixed-size pages.
  4. Run the model weights on the GPU.
  5. 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

SetupFitReason
Self-hosted open weights with concurrent usersvLLM or TGIBuilt to serve many requests at once.
Managed API (OpenAI, Anthropic, Bedrock)No engine neededThe provider already optimizes serving.
Single-user local developmentllama.cpp or OllamaSimpler setup with no concurrency to manage.
Concurrency Continuous batching and efficient cache management are what turn a GPU into throughput under load.

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

Example scenario Inference latency climbs from 150 ms to 500 ms during peak load. The figures are illustrative.
Inference serving engines (vLLM, TGI)Diagram with these labels, in reading order: High Latency Detected; Check GPU Utilization; Check Model Weight Loading; Time; Check API Request Rate; Increase GPU Instances; Optimize Model Loading; Rate Limit API Requests.High Latency DetectedCheck GPU UtilizationCheck Model Weight LoadingTimeCheck API Request RateIncrease GPU InstancesOptimize Model LoadingRate Limit API Requests
Debugging inference latency spikes

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.

  1. Monitor GPU memory usage and see whether it reaches 100% at peak.
  2. Check server logs for memory allocation failures or request timeouts.
  3. Compare the number of concurrent requests with the configured limits.
The call Increase the GPU memory allocation or reduce the model footprint. The tradeoff is higher cost for extra resources, in return for stability under load. Monitor GPU memory usage and error rates after the change.

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.

Back to the knowledge baseRequest an assessment