High-Concurrency AI Inference: Engineering Practices for Low Latency and Cost Optimization
This article turns the common tension between fast and cheap LLM inference into a repeatable engineering problem. It defines the metrics that matter (TTFT, per-request TPS, cluster throughput, P50/P95/P99 latency, and cost per request), builds a cost model that exposes output tokens and prefix cache reuse as the main levers, and walks through call-chain practices such as streaming, continuous batching, traffic separation, structured output, semantic caching, and elastic autoscaling. It closes with a vendor evaluation checklist and common pitfalls, helping teams design capacity and budgets around measured workloads rather than list prices or generic benchmarks.
## 1. Why Speed and Cost Must Be Discussed Together
When teams connect large model capabilities to real business flows, two things become clear quickly: users have almost no tolerance for waiting, and the bill tends to grow faster than call volume. A newer class of lightweight inference models has recently pushed both metrics forward at once—input pricing at the million-token level brought down to around $1.5, output pricing to around $9, output throughput of roughly 289 tokens per second, and time to first token of about 65 ms. Compared with flagship models of the same generation (around $5 input and $25 output) and the previous generation of general-purpose models, this creates a several-fold cost gap and roughly a 4x latency gap.
This article does not evaluate any specific product. Instead, it abstracts the phenomenon into a general engineering question: at the same model capability tier, how do you turn “low latency plus low unit price” into a reproducible system capability through metric definitions, capacity planning, and call-chain optimization?
2. Define the Metrics First, or Optimization Has No Starting Point
Many performance discussions contradict each other because the underlying definitions are inconsistent. It helps to fix the following in every load-test report:
- Time to first token (TTFT): the time from sending a request to receiving the first output token. It drives perceived speed and has the largest impact on typewriter-style streaming, voice playback, and IDE completion.
- Output throughput (TPS per request): tokens generated per second for a single request, which determines how long long-form tasks take.
- Cluster throughput (tokens/s): the combined system-level capacity across all concurrent requests, which determines how many users can be served per unit time.
- P50 / P95 / P99 latency: averages hide the long tail, so capacity planning must be based on P95 or even P99.
- Cost per request: determined by input tokens, output tokens, and cache hit rate together—not by the price list alone.
A common misconception is to look only at unit price. In practice, output tokens usually cost several times more than input tokens (for example, the 1.5 versus 9 ratio), so controlling output length has far more leverage on the bill than compressing the prompt.
3. Cost Modeling: Break the Bill into Optimizable Variables
Let the input unit price be p_in, the output unit price be p_out, and a single request use n_in input and n_out output tokens:
cost_per_request = n_in * p_in / 1e6 + n_out * p_out / 1e6monthly_cost = QPS * 86400 * 30 * cost_per_request
Three direct conclusions follow:
- The output side is the main cost battlefield. Cutting
n_outfrom 800 to 400 is roughly equivalent to halving the entire bill. - The focus on the input side is prefix reuse: if stable prefixes such as system prompts, tool definitions, and long documents can hit the cache, repeated calls become significantly cheaper.
- Lower unit price and higher throughput multiply. If unit price drops to one third and throughput improves 4x, the number of requests servable under the same budget can theoretically increase by an order of magnitude—this is where lightweight models create value in cost-sensitive scenarios.
4. Architecture Practices: Putting Low Latency into the Call Chain
4.1 Streaming Output and First-Screen Placeholders
Enable streaming responses by default, and have the frontend render a skeleton and placeholders first to further reduce perceived TTFT. For voice scenarios, split playback at sentence boundaries instead of waiting for the full generation to finish.
4.2 The Trade-off Between Batching and Scheduling
Static batching improves cluster throughput but raises single-request latency; continuous batching strikes a better balance between the two. A practical recommendation: route interactive traffic through a low-latency queue and offline batch jobs through a throughput queue, physically isolated so long jobs cannot block short requests.
# Pseudocode: route by task type, interactive traffic firstdef route(req):if req.kind == "chat_stream":return low_latency_pool.submit(req, timeout_ms=800)return throughput_pool.submit(req, priority="low")
4.3 Prompts and Structured Output
- Put stable prefixes first and volatile content last to improve prefix cache hit rates;
- Replace vague instructions like “please keep it short” with structured output constraints (JSON Schema validation plus field trimming);
- Set a hard
max_tokenslimit and loop detection to prevent abnormal long outputs from blowing up cost.
4.4 Semantic Caching and Result Reuse
Build a vector-retrieval cache for high-frequency similar questions, and return historical results directly for requests above the hit threshold. For deterministic subtasks (classification, extraction, format conversion), prefer a small model or rules as the fallback, forming a layered routing pattern of “large model as backstop, small model as the workhorse.”
4.5 Elastic Scaling and Observability
Autoscale based on queue length and P95 latency rather than CPU alone. Feed TTFT, TPS, cache hit rate, token consumption, and failure rate into a unified logging service and monitoring alerts, forming a cost dashboard that can be split by tenant and by API.
5. Selection Checklist: An Actionable Evaluation Table
| Dimension | Key Question | Recommended Action |
|---|---|---|
| Latency | Does P95 TTFT meet the interaction threshold | Load tests must include long-context and tool-calling scenarios |
| Throughput | Does P99 degrade at peak QPS | Apply stepped load to find the inflection point, not a single point |
| Cost | What share is output, and what is the cache hit rate | Measure the real n_out distribution before discussing unit price |
| Capability | Does accuracy on target tasks meet the bar | Build a business evaluation set instead of relying on generic leaderboards |
| Stability | Are rate limiting, retries, and degradation complete | Design exponential backoff and multi-model fallback |
6. Common Pitfalls
- Leaderboard-only thinking: generic benchmark scores are not linearly correlated with business accuracy, so always build your own evaluation set.
- Unit-price-only thinking: ignoring output length and cache hit rate can make the actual bill deviate from expectations by several times.
- Ignoring the long tail: reporting only averages and planning capacity on averages guarantees a collapse at peak.
- One-shot migration: roll out gradually by traffic percentage, and keep a rollback switch and dual-run comparison.
7. Conclusion
Combining low TTFT with high throughput and low unit price essentially turns “inference” from a scarce resource into programmable infrastructure. For developers, the real competitive edge is not chasing the parameters of any single model generation, but building an engineering system where metrics are definable, cost is decomposable, performance is load-testable, and capacity is elastic. If an organization already uses relevant Baidu AI Cloud capabilities (for example, a container platform to host inference services, object storage for models and datasets, and logging plus monitoring alerts to build a cost dashboard), the methods above can be mapped directly into an implementation checklist and deliver stable, predictable intelligent capacity within a controlled budget.
