Optimizing LLM Inference Costs: Building a High-Performance Inference Engine on Dedicated AI Chips
Large language model inference has become a persistent, ongoing expense that can quickly exceed one-time training costs. This article examines three cost-reduction paths—efficient architectures, low-precision computing, and hardware-aware software—and takes a deep dive into the third: building a deeply optimized inference engine on dedicated AI accelerator chips. It covers fused megakernels, sparse attention for million-token contexts, FP8 quantization with MoE architectures, a reproducible benchmarking methodology, and practical recommendations for evaluating and deploying inference stacks.
Optimizing LLM Inference Costs: Building a High-Performance Inference Engine on Dedicated AI Chips
Why Inference Cost Is the Core Bottleneck for LLM Adoption
As large language models move from experimentation into production, technical teams face a hard reality: training is a one-time investment, but inference is a continuous expense. For an application processing billions of tokens per day, inference costs can surpass training costs within months. This problem is amplified by the explosion of Agent-style applications, where a single task can easily consume hundreds of thousands or even millions of tokens in ultra-long context.
There are roughly three paths to reducing inference costs. The first is using more efficient model architectures, such as sparsely activated Mixture-of-Experts (MoE) models, which deliver large-model capability with fewer activated parameters. The second is low-precision computing, such as FP8 quantization, which significantly reduces memory footprint and increases throughput while keeping accuracy loss under control. The third is fully exploiting hardware characteristics through deeply customized inference engines. This article focuses on the third path: building an extremely optimized inference engine on dedicated AI accelerator chips.
Technical Background: Co-Designing Chips and Inference Engines
General-purpose GPUs have long been the default choice for LLM inference, but their ecosystem premium and supply volatility have pushed many teams to evaluate alternatives. A custom AI accelerator chip from a cloud provider is one representative example: it uses a systolic array architecture optimized for matrix operations, paired with high-bandwidth on-chip memory, delivering solid performance-per-dollar in inference scenarios.
But hardware is only the foundation—the real performance gap comes from the software stack. There is broad industry consensus that the throughput gap between a general-purpose inference framework and a deeply customized engine on the same chip can reach several times. A representative open-source effort: a community team built an inference runtime for this chip from scratch (released under the permissive Apache 2.0 license). The first model it supported was a 320-billion-parameter MoE model that activates only about 18 billion parameters per inference, achieving a spot inference cost of roughly $0.04 per million tokens in ultra-long-context scenarios. Let’s break down the core techniques behind it.
Key Techniques Explained
3.1 Fused Operators (Megakernels): Fitting an Entire Layer into One Kernel
In traditional inference frameworks, the forward pass of a Transformer layer is decomposed into many independent operators: RMSNorm, QKV projection, attention, output projection, residual connections, FFN, and so on. Each operator boundary involves writing intermediate results back to GPU memory and reading them out again, and in the decoding phase (a memory-bound scenario) this memory traffic often exceeds the cost of computation itself.
The approach taken here is to write fused kernels at layer granularity: the first half of each layer (normalization + attention + projection) is fused into one megakernel, and the second half (MoE routing + expert computation + residual) into another, written directly using the chip vendor’s low-level kernel programming interface. This delivers two direct benefits:
- Reduced kernel launch overhead: CPU scheduling is no longer the bottleneck, which is especially important for high-concurrency, small-batch scenarios;
- Intermediate activations stay on-chip: avoiding large numbers of HBM round trips, which pays off significantly during decoding—the memory-constrained phase where tokens are generated one at a time.
3.2 Sparse Attention: The Key to Supporting Million-Token Contexts
When context length reaches the 1M-token scale, the KV cache memory traffic and computation of standard full attention become unmanageable. Common industry solutions include sparse attention (each query attends only to a filtered set of key KV blocks), sliding windows, and KV cache compression. This implementation uses sparse attention: while keeping model output quality acceptably high, it reduces attention memory traffic by an order of magnitude, keeping decoding speed at a usable level in ultra-long-context scenarios.
For developers, there is an important engineering lesson here: the cost of ultra-long context is not linear—the choice of attention mechanism directly determines whether 1M-token context is “feasible” or “unaffordable.” When evaluating inference frameworks, focus on whether the long-context path implements sparsification or paging optimizations, not just the advertised maximum context length.
3.3 FP8 Quantization Is a Natural Fit for MoE Architectures
FP8 low-precision inference has become a mainstream industry direction. For a 320B-class MoE model, FP8 halves the weight memory footprint, making single-node deployment possible; meanwhile, with only 18B activated parameters, the number of experts actually participating in each forward pass is limited, further reducing compute density. The main engineering challenges in MoE routing are load balancing and All-to-All communication overhead, which can be effectively mitigated in single-node, multi-chip scenarios through sensible expert-parallel partitioning.
Performance and Cost Benchmarking: How to Run a Credible Test
This open-source project used a widely accepted Agent workload replay methodology for its measurements, and the approach is worth learning from:
- Test environment: a single instance equipped with 16 accelerator chips;
- Workload model: 32 concurrent Agent sessions, each maintaining a 1M-token context, running continuously for one hour;
- Measured results: at the default batch size of 12, roughly 29 requests per minute, totaling about 3.57 million tokens, with a decoding p50 speed of about 22 tokens/s;
- Cost calculation: based on the instance’s spot pricing, approximately $0.04 per million tokens.
It’s worth emphasizing that these numbers depend heavily on workload shape—ultra-long context and high-concurrency Agent scenarios happen to amplify the advantages of sparse attention and high-throughput batching. When evaluating, teams should construct replay traffic that mirrors their own business (for example, by recording real session logs and replaying them) rather than relying on benchmark numbers based on synthetic short prompts.
Practical Recommendations for Deployment
- Prefer MoE + FP8 in model selection: assuming capability requirements are met, the inference cost advantage of sparsely activated architectures is structural;
- Evaluate the fusion depth of the inference engine: look at whether operator fusion and KV cache management have been optimized for the target hardware, rather than just checking framework compatibility lists;
- Use real business traffic for cost modeling: adopt “end-to-end cost per million tokens” as the core metric to compare different hardware and engine combinations consistently;
- Standardize interfaces to reduce migration costs: prefer inference services that offer OpenAI-compatible APIs, so the backend can be switched without changes to the business side. A typical deployment flow has four steps: pull the code, build the container image, compile the operator cache, and start the service. The first operator compilation takes considerable time, so it’s advisable to persist compiled artifacts to object storage for reuse during elastic scaling;
- Add observability: integrate logging services and monitoring alerts to continuously track key metrics such as TTFT, TPOT, and batch size distribution, ensuring cost optimization doesn’t come at the expense of tail latency.
Conclusion
LLM inference is entering a stage where the competition is no longer about hardware alone, but about hardware-software co-design. The combination of fused operators, sparse attention, low-precision computing, and MoE architectures has pushed the inference cost of 300B-class models down to levels once thought impossible. For technical teams, rather than chasing any single framework or chip, the better move is to establish a standardized evaluation framework: benchmark with real traffic, compare with unified cost metrics, and maintain portability through standard interfaces. That is how you keep the power of choice in a rapidly evolving inference technology stack.
