LLM Inference Optimization: A Practical Guide to Speed, Cost, and Version Migration
This guide explains how to optimize large language model inference for production, covering key metrics such as throughput (TPS), time to first token (TTFT), and end-to-end latency. It discusses why inference infrastructure matters, how reasoning-heavy outputs increase token usage and cost, and how to manage them with task tiering, budget limits, and caching. It also outlines a safe version migration process with baselines, canary rollouts, dual-write comparison, and rollback plans, plus cost observability and task-driven model selection.
1. Background: Why a Version Switch Affects Engineering
Once LLM applications reach production, teams often face a familiar situation: a model version is about to be retired, and its replacement offers clear gains in throughput and latency but also changes output style and billing structure. For developers, this is not a simple model swap. It is a systems-level effort involving performance baselines, cost models, prompt compatibility, and traffic migration.
The core tensions can be summarized in three points. First, the older version has higher inference overhead, putting pressure on both providers and callers. Second, the newer version achieves hundreds of tokens per second and sub-second time to first token through significant architectural and inference-infrastructure changes. Third, the newer version tends to “overthink,” consuming many tokens in its reasoning process and offsetting part of the savings from a lower unit price. Each point is discussed below.
2. Define the Metrics First: Throughput, TTFT, and End-to-End Latency
An inference service should not be evaluated only on whether it feels “fast.” It should be broken down into three dimensions:
- Generation throughput (TPS): tokens produced per request per unit of time; this shapes the reading experience for long-form output.
- Time to first token (TTFT): time from request submission to the first visible token; this determines responsiveness in interactive applications such as coding assistants and chat assistants.
- End-to-end latency: total time, equal to TTFT plus generation time; this determines completion time for batch jobs and long-document tasks.
The optimization methods differ. TTFT is mainly affected by queuing, scheduling, and prefill compute. Throughput depends more on decode-phase memory bandwidth and batching strategy. End-to-end latency is the combined result. When evaluating versions, fix the input-length and output-length distributions, and record P50/P95 metrics separately rather than relying on averages.
An illustrative evaluation record structure:
Scenario: code completion / long-document summarization / multi-turn chatInput length distribution: 512 / 2048 / 8192Metrics: TTFT(P50, P95), TPS(P50), total time, input tokens, output tokens, reasoning tokens
3. Architecture and Inference Infrastructure: Why Speed Is Not a Tuning Problem
Reaching hundreds of TPS and sub-second TTFT usually requires several changes on the inference side at once: better attention and decoding path design, higher-concurrency continuous batching, request-priority scheduling, and resource orchestration that separates prefill and decode deployment. These are inference-infrastructure engineering changes, not simple model parameter adjustments.
This also explains a common pattern: an older version from a previous architectural generation struggles to compete on cost-performance even after a price cut, because its output per unit of compute is lower. The takeaway for users is to treat cost per completed task as the core metric, rather than looking only at the price per million tokens.
4. The Double-Edged Sword of Reasoning Output: Capability, Latency, and Cost
Reasoning models generate an internal reasoning process before producing an answer. This improves accuracy on complex reasoning and coding tasks, but it has two side effects: total output tokens increase, and end-to-end latency grows. When generation is fast enough, the second effect is barely noticeable; the first directly raises cost.
The governance approach has three layers:
- Task tiering: route simple extraction, formatting, and classification tasks to a low reasoning-budget configuration; keep a high reasoning budget for complex reasoning and code refactoring.
- Budget constraints: explicitly cap reasoning length on the request side, and truncate and retry with a fallback when the limit is exceeded.
- Result caching: apply semantic caching to highly deterministic requests to avoid repeated reasoning.
Illustrative pseudocode:
def call_model(prompt, task_type):budget = LOW_BUDGET if task_type in SIMPLE_TASKS else HIGH_BUDGETresp = client.chat(prompt, reasoning_budget=budget)if resp.truncated and task_type in SIMPLE_TASKS:resp = client.chat(prompt, reasoning_budget=LOW_BUDGET, fallback=True)cache.put(hash(prompt), resp.answer)return resp.answer
5. Version Migration: From Hard Cutover to Canary and Rollback
Retiring an older version makes migration mandatory, but migration should be observable and rollback-capable:
- Establish a baseline: replay real traffic and record metric and output differences between old and new versions under identical inputs.
- Canary rollout: ramp traffic through 5% → 25% → 50% → 100%, observing error rate, latency, and cost at each stage.
- Dual-write comparison: call both versions in parallel on critical paths and compare structured-output consistency, such as JSON field completeness.
- Rollback plan: keep old-version aliases and feature flags, and roll back by tenant or by endpoint when anomalies occur.
6. Cost Governance: Make Token Usage Observable
Token usage should be instrumented separately for input, output, and reasoning tokens, and correlated with business metrics such as task success rate and human adoption rate. Only by quantifying how much quality improvement the extra tokens buy can you decide whether a high reasoning budget is worth it. General-purpose cloud services such as logging, monitoring and alerting, and object storage can be used for usage archiving and dashboards, producing cost attribution by tenant, endpoint, and task type.
7. Selection Guidance: Let Task Profiles Drive Decisions, Not Version Numbers
Overall, when a new version launches and an older one retires, the practical approach is to first establish a metric baseline with real business traffic, then select models by task complexity: use the high-throughput version for interactive and long-form scenarios, reserve the high-capability version for complex reasoning, and control reasoning overhead with budget constraints and caching. If an organization wants to standardize this evaluation and migration process in its own environment, it can build canary and cost dashboards using general-purpose capabilities such as the container platform, logging service, and monitoring and alerting offered by Baidu AI Cloud, turning version switches from something imposed into a controlled evolution.
