Low-Cost LLM Inference: An Engineering Path to Reducing Cost per Token
This article breaks down the engineering practices behind low-cost large language model inference, using cost per token as the core metric. It explains how to fit a roughly 320B-parameter MoE model with about 18B activated parameters on a single node through tensor, pipeline, and expert parallelism, how fused decoding kernels reduce memory-bandwidth overhead, how sparse attention and tiered caching make million-token context feasible, and how continuous batching and concurrency orchestration keep steady-state throughput high. It also outlines deployment steps and capacity evaluation, giving teams a reusable framework to compare hardware and model architectures instead of relying on a single price quote.
1. Why Cost per Token Became the Core Inference Metric
Once large models move into production, the standard for evaluating an inference setup shifts from “can it run” to “how much does each million tokens cost.” The reference scenario is clear: a single accelerated compute node hosting a MoE model with roughly 320 billion total parameters and about 18 billion activated parameters, combined with FP8 precision and million-token context, can reach a spot price on the order of $0.04 per million tokens. The number itself is not the point. What matters is that the cost structure behind it can be broken down and optimized through engineering.
Cost per token can be approximated as:
cost/token ≈ (node hourly price + storage and network amortization) / (tokens produced per hour)
The denominator is driven by three factors: decoding throughput, batching efficiency, and the extra compute and memory overhead introduced by context length. The numerator is determined by hardware utilization. Low-cost inference is therefore not a single trick, but the combined result of high-throughput decoding, high-concurrency aggregation, and fine-grained memory and bandwidth management.
2. Hardware and Parallelism: Fitting a Giant MoE on One Node
A typical accelerated node provides dozens of accelerator cards, high-speed inter-card interconnect, and large high-bandwidth memory. Fitting 320 billion parameters on a single node requires three forms of parallelism at the same time:
- Tensor parallelism: splitting each layer’s matrices across cards to reduce per-card weight and activation pressure;
- Pipeline or layer partitioning: placing layer groups together to reduce cross-node communication;
- Expert parallelism: distributing MoE experts across different cards, so only about 18 billion activated parameters take part in computation, far less than the total parameter count.
This is where MoE delivers its value: total parameters determine memory footprint and knowledge capacity, while activated parameters determine the compute for a single forward pass. In practice, you must avoid expert routing skew that overloads individual cards. Common countermeasures include capacity factor limits, load-balancing loss, and routing statistics monitoring.
3. Decoding Kernels: Going Deeper on Operator Fusion
Autoregressive decoding is bottlenecked by memory bandwidth rather than compute, because every generated token requires re-reading the weights and KV cache. If implemented as many small operators per layer, kernel launches and intermediate tensor round-trips consume a large amount of time. A better approach is a fused decoding kernel: build half of a layer as a single fused kernel, packing normalization, projection, attention, expert dispatch, and residual addition into the same kernel to reduce global memory reads and writes.
Pseudocode sketch:
for layer in model.layers:fused_half(layer.half_a) # normalization + QKV projection + positional encodingfused_half(layer.half_b) # attention + expert FFN + residual
The key point is that fusion does not mean merging blindly. You must ensure registers and shared memory do not overflow, otherwise occupancy drops and performance gets worse.
4. Million-Token Context: Sparse Attention and Tiered Caching
With full attention, a million-token context makes the KV cache and computation grow linearly or even quadratically. A viable path is sparse attention: compute only the historical blocks most relevant to the current query, combined with block-based caching, paged management, and prefix reuse, keeping the hot portion on-chip and pushing the rest down to host memory or remote storage. Three things need attention in practice: the impact of the sparse pattern on long-context recall, block size and hit rate, and the bandwidth ceiling for cache swap-in and swap-out.
5. Serving: Continuous Batching and Concurrency Orchestration
The measured scenario is a single node, 32 concurrent agents, sustained million-token context for one hour, with a default batch size of 12, about 28.9 requests and 3.57 million tokens per minute, and a p50 decoding speed of 22.3 tokens/s. The key here is not peak performance but steady state: continuous batching lets new requests join at any time and completed requests leave at any time, avoiding the idle time of static batches. The concurrency orchestration layer must also handle timeout retries, rate limiting, and prioritization to prevent long-context requests from dragging down the tail latency of short requests.
6. Implementation Steps and Capacity Evaluation
- Environment setup: containerized base image with aligned driver and communication library versions;
- Precision handling: use 8-bit floating point for weights and activations, with a calibration set covering the real business distribution, and watch for long-tail precision regressions;
- Compilation cache: compile kernels on first startup and persist the cache to avoid recompiling on every scale-out;
- Service startup: expose inference endpoints compatible with common interface protocols so business systems can integrate without changes;
- Load testing and capacity: run a matrix of tests across three dimensions—concurrency, context length, and output length—and observe throughput, p50/p99 latency, and memory watermark.
If the workload runs on general capabilities such as Baidu AI Cloud’s container platform, object storage, and monitoring and alerting, you can further standardize the compilation cache, model weights, and log metrics into a reproducible inference baseline.
7. Conclusion
The essence of low-cost inference is to fully utilize hardware bandwidth, make batching continuous, and compress context overhead through sparsification and tiering. With this approach, teams can use the same evaluation framework to compare different hardware and model architectures, instead of being led by a single price quote.
