LLM inference costs dominate enterprise AI budgets. A large language model serving thousands of requests per day can cost more than the entire traditional IT infrastructure of a mid-sized company. And at enterprise scale, latency matters — users abandon AI assistants that take 10 seconds to respond.
Optimization isn't about a single technique. It's about systematically reducing cost and latency across the entire inference stack.
The Inference Cost Equation
LLM inference cost is driven by four factors:
- Model size: Larger models require more GPU memory and compute per token
- Request volume: More requests = more compute hours
- Token count: Longer prompts and responses cost more — both in compute and in time
- GPU utilization: Underutilized GPUs waste money. A GPU at 30% utilization costs the same as one at 90%.
Optimization Techniques
1. Model Selection and Right-Sizing
The most impactful optimization is using the right model for the task:
- Task-appropriate sizing: A 7B parameter model for classification, a 70B for complex reasoning. Don't use a 405B model for tasks a 7B handles well.
- Model routing: Route requests to different models based on complexity. Simple questions → small model. Complex analysis → large model.
- Fine-tuned small models: A fine-tuned 7B model often outperforms a general 70B model on specific tasks — at 10x lower cost
- Benchmark on your data: Generic benchmarks don't predict performance on your specific use cases. Test candidate models on your actual tasks.
2. Quantization
- GPTQ / AWQ: Post-training quantization to 4-bit or 8-bit. Reduces memory by 2-4x with minimal quality loss for most tasks.
- GGUF: Quantization format optimized for CPU inference — viable for small models on CPU-only infrastructure
- Quality validation: Always benchmark quantized models against full-precision on your specific tasks. Quality degradation varies by task type.
- Sweet spot: 4-bit quantization (AWQ or GPTQ) offers the best cost/quality tradeoff for most enterprise use cases
3. Inference Engine Optimization
- vLLM: PagedAttention for efficient memory management, continuous batching, tensor parallelism for multi-GPU serving
- TGI (Text Generation Inference): Hugging Face's optimized serving with built-in batching, streaming, and metrics
- TensorRT-LLM: NVIDIA's inference optimizer — significant speedup on NVIDIA GPUs but requires compilation step
- Speculative decoding: Use a small model to draft tokens, large model to verify. 2-3x speedup for generation-heavy workloads.
4. Batching and Scheduling
Batching Strategies
- Continuous batching: Process multiple requests simultaneously, adding new requests as slots become available. vLLM does this automatically.
- Priority queuing: Interactive requests (user-facing) get priority over batch requests (background processing)
- Request coalescing: For identical or similar requests within a time window, serve cached responses
- Off-peak processing: Schedule batch inference jobs during low-traffic periods to maximize GPU utilization
5. Caching
- Semantic caching: Cache responses for semantically similar (not just identical) queries. Tools like GPTCache or custom embedding-based caching.
- Prompt prefix caching: For applications with long system prompts, cache the KV-cache for the prefix to avoid recomputation
- RAG result caching: Cache retrieved context for repeated or similar queries
- Response caching: Full response caching for frequently asked questions
6. Prompt Optimization
- Prompt compression: Reduce prompt length without losing essential context. Shorter prompts = faster + cheaper.
- Structured output: Request JSON or structured responses — they're shorter and more predictable than freeform text
- Max token limits: Set appropriate max_tokens for each use case. Don't allow 4,000 token responses for classification tasks.
Kubernetes Recipes
Practical guide for container orchestration and deployment — hands-on patterns you can use today.
View on Amazon →Infrastructure Optimization
- Auto-scaling: Scale inference replicas based on queue depth and latency targets, not just CPU utilization
- Scale-to-zero: For low-traffic models, scale to zero when idle and accept cold-start latency
- GPU type selection: H100 for large models, L4 for small model inference, T4 for budget-constrained deployments
- Multi-model serving: Serve multiple small models on a single GPU using MIG or model multiplexing
Cost Monitoring
Key Metrics to Track
- Cost per request: Total infrastructure cost / number of requests served
- Cost per token: Separate input and output token costs for accurate budgeting
- GPU utilization: Target >70% average utilization for production serving
- Latency percentiles: p50, p95, p99 latency — not just average. Users feel the p99.
- Cache hit rate: Higher cache hit rates directly reduce cost and latency
- Tokens per second: Throughput metric for capacity planning
Operationalizing ML Models: MLOps for Scalable AI
Turn ML prototypes into robust, scalable systems using real-world tools. In collaboration with Starweaver.
Start on Coursera →
Luca Berton
