Kirchner.io
Back to Essays

Technology

AI and systems /

AI Inference Optimization Under Real Constraints

How to optimize AI inference by measuring latency, throughput, utilization, and cost per successful task across API, cloud, on-premises, and edge deployments.

By Michael Kirchner

reading surface

Technology

words
2,177
sections
9
Opens in a new tab.

Inference Is A Systems Contract

Permalink to Inference Is A Systems Contract

AI inference is the work a trained model performs on a new input. Inference optimization is the work of making that path satisfy a real operating contract: enough quality, within a latency and reliability budget, at an acceptable cost, under the privacy and deployment constraints of the system.

That is a different objective from making one benchmark run as fast as possible. The fastest isolated kernel can sit behind a slow queue. A smaller model can lower the cost of one request while producing enough failures and retries to raise the cost of the task. A large batch can improve accelerator throughput while making an interactive product feel worse. Optimization begins by naming which result has to improve and which results are not allowed to degrade.

Training neural networks changes model parameters. Inference applies the resulting model. The two phases share hardware, tensors, evaluation practices, and numerical tradeoffs, but their workloads are different. Training usually emphasizes sustained computation over known datasets. Production inference has arrivals, queues, variable inputs, cold paths, user-visible deadlines, and failures that propagate into other systems.

The useful unit is therefore not tokens per second in isolation. It is the cost and time required to produce a successful result under a stated quality contract.

Record The Workload Before Optimizing It

Permalink to Record The Workload Before Optimizing It

An inference system cannot be optimized against a representative workload that has never been recorded. Start with a workload profile:

  • model name, version, runtime, and precision;
  • input and output length distributions, not only averages;
  • batch size and concurrent request distribution;
  • modality: text, image, audio, video, embeddings, or a combination;
  • cold, warming, and steady-state behavior;
  • quality tests and task-success criteria;
  • timeout, cancellation, retry, and fallback behavior;
  • privacy, residency, connectivity, and hardware constraints;
  • the baseline configuration being compared.

Shape matters. For a language model, prefill over a long prompt and autoregressive decode have different compute and memory behavior. For vision, image resolution and batch shape can change the bottleneck. For an embedding model, document indexing and interactive query embedding can belong on different machines and different schedules. GPU programming makes this constraint visible: a kernel, memory layout, or batch that is effective for one tensor shape may be poor for another.

Averages hide the tails that often define the user experience. Record distributions and preserve the workload that produced them. A comparison without the prompt set, output bounds, concurrency, warmup policy, model revision, and hardware is a story about a number, not evidence about a system.

Metrics That Do Not Collapse Into One Number

Permalink to Metrics That Do Not Collapse Into One Number

Useful inference measurements describe different parts of the path:

tablescroll for columns
MetricWhat it revealsCommon blind spot
Queue timeWhether demand exceeds available service capacityHidden when timing starts after a worker accepts the request
Time to first result or tokenHow long a user waits before useful output beginsCan improve while total completion time gets worse
Inter-token or incremental latencyThe cadence of streamed generationDoes not describe prompt processing or final quality
End-to-end latencyWhat the caller actually experiencesAn average can hide severe p95 or p99 behavior
ThroughputWork completed per unit of timeCan rise by sacrificing interactive latency
UtilizationHow much of the provisioned hardware is doing useful workHigh utilization can coexist with a long queue
Success rateWhether requests finish within the contractMay omit semantically wrong but technically successful answers
Task qualityWhether the result solves the intended problemOften measured on an unrepresentative evaluation set
Cost per successful taskResources consumed for an acceptable outcomeRequires quality, failures, and retries to be counted

For generative systems, distinguish prompt processing from decode and record time to first token, inter-token latency, and end-to-end completion. The vLLM optimization guide documents a concrete version of this tension: scheduling and chunked prefill can trade among time to first token, inter-token latency, throughput, and GPU utilization. The important lesson is not that one setting is universally best. It is that the service objective has to choose the trade.

Use data visualization to show distributions, concurrency, and quality alongside performance. A single "requests per second" card cannot reveal whether the improvement came from shorter outputs, an easier prompt set, a different precision, or a larger failure rate.

API, Cloud, On-Premises, Or Edge

Permalink to API, Cloud, On-Premises, Or Edge

Placement is part of the inference design.

tablescroll for columns
PlacementStrong reason to use itCost that must remain visible
Hosted model APIFast access to managed models and capacity without operating the serving stackNetwork dependency, provider limits, variable pricing, and a narrower control surface
Managed cloud endpointMore control over model and scaling while retaining managed infrastructurePlatform coupling and the need to understand autoscaling, warm capacity, and quotas
Self-hosted cloud or on-premisesControl over model, runtime, data boundary, hardware, and schedulingCapacity planning, upgrades, observability, security, and incident response
Edge or browserLocal data handling, offline capability, or removal of a network round tripDownload size, device variation, memory pressure, battery use, and limited accelerators
HybridDifferent placement for indexing, interactive inference, retrieval, and fallbackMore boundaries, versions, and failure modes to coordinate

The right choice follows from the contract. Sensitive data may constrain placement before cost is considered. An intermittent network may make local inference more valuable than peak server throughput. A bursty product may prefer managed capacity. A steady, well-understood workload may justify owning more of the serving path.

Do not compare these options using compute price alone. Include engineering labor, idle capacity, network transfer, monitoring, retries, failover, model distribution, and the consequence of missing the deadline.

Optimization is safer when attempted in dependency order.

The cheapest inference call is the one the system does not need. Deduplicate requests, avoid asking a model to recompute deterministic application logic, and stop generation once the task has enough output. Reduce unnecessary context, but preserve the information the quality contract actually needs.

Caching can remove work when identity and privacy are explicit. Cache keys should include every input that changes the answer, including model revision, prompt or template version, tool state, and relevant permissions. A fast cache that crosses a tenant or freshness boundary is a security defect.

2. Match The Model To The Task

Permalink to 2. Match The Model To The Task

Route simple, bounded work to the smallest model that passes the evaluation. Escalate when uncertainty, complexity, or failure justifies it. This is not a blanket claim that smaller is better. It is a claim that model size should be an evaluated decision rather than a prestige setting.

For systems with multiple models, preserve which route was chosen and why. Otherwise a cost or quality change cannot be traced back to the routing policy.

3. Control Arrival, Batching, And Backpressure

Permalink to 3. Control Arrival, Batching, And Backpressure

Concurrency without admission control moves latency into the queue. Define capacity, timeouts, cancellation, and backpressure before maximizing batch size. Continuous batching can keep an accelerator busy, while chunked prefill can interleave prompt work with decode work, but both need workload-specific measurement.

Inference often becomes a memory problem. Quantization, key-value caching, fused kernels, optimized attention implementations, and model compilation can change how much data moves and how often. The current Hugging Face Transformers optimization overview distinguishes memory-footprint reductions from pure speed optimizations and notes that speed optimizations can increase memory use.

Quantization is not merely a smaller file. It changes numerical representation and may change output quality. Compilation can add a warmup cost or recompile for new shapes. Caches consume memory and can create eviction behavior. Parallelism adds communication. Treat each technique as a change to the system contract and rerun the task evaluation.

Profile before rewriting kernels. Measure request parsing, tokenization or preprocessing, host-to-device transfer, queueing, model execution, postprocessing, network transfer, and downstream tool calls. NVIDIA Nsight Systems is one official system-level profiler for CPU, GPU, and runtime activity; TensorRT-LLM, vLLM, and other serving runtimes expose different optimization surfaces.

Provider-specific guidance can still be useful when the provider is part of the system. For example, OpenAI's latency guidance emphasizes model choice and generated-token count as major influences on completion latency. Apply such advice inside a measured workload rather than converting it into a universal rule.

A First-Party Example: Semantic Search On Kirchner.io

Permalink to A First-Party Example: Semantic Search On Kirchner.io

Try the site's semantic search while reading this section. Its request path is a small example of placing inference work across build time, the browser, an API route, and a database.

source blocktext
published site content
  -> build-time embedding and index verification
  -> Neon pgvector search index

reader query
  -> q8 WASM embedding in the browser
  -> 384-dimensional normalized vector
  -> POST /api/search
  -> semantic similarity plus PostgreSQL lexical ranking
  -> ranked site results

The embedding model is Xenova/all-MiniLM-L6-v2. At build time, the site creates normalized 384-dimensional embeddings for the current blog, compendium, art, and page records and verifies that the index matches the published corpus.

For an interactive query, the browser dynamically loads a q8 WebAssembly feature-extraction pipeline, mean-pools and normalizes the result, and posts the numeric vector with the query. The API validates the request and sends the vector to Neon. PostgreSQL calculates cosine-based vector similarity and full-text relevance. The current ordering policy weights the semantic score at 0.82 and the lexical score at 0.18, then applies small intent-aware boosts for navigational requests.

Those weights are an implementation policy, not a discovered constant of search. They should change only against a reviewed query set. A semantic score can retrieve conceptual neighbors while missing an exact identifier. A lexical score can find an exact phrase while missing a useful paraphrase. The combined path keeps both signals visible.

The model library does not run inside the production search API. This keeps native model inference out of that serverless runtime. The trade moves the first interactive embedding to the reader's browser, where device speed, model download, compilation, and memory vary. If browser embedding fails, the search path can fall back instead of making the entire command surface unavailable.

This architecture is evidence of a placement decision, not evidence that the decision is universally faster or cheaper. The site has not published controlled measurements for cold model download, warm query embedding, client memory, database time, total search latency, or retrieval quality. Those measurements would be required before claiming a performance gain.

The case also connects inference to WebAssembly, linear algebra, data storage, and knowledge graphs. The embedding is one representation. The database supplies retrieval. The graph preserves explicit relationships and provenance that vector proximity alone cannot express.

Common failure modes include:

  • reporting average latency while p95 and p99 degrade;
  • maximizing throughput while an interactive queue becomes unusable;
  • comparing different prompt, output, or image-size distributions;
  • measuring a warm process while production repeatedly takes the cold path;
  • quantizing a model without rerunning the task-quality evaluation;
  • caching across privacy, identity, or freshness boundaries;
  • optimizing an isolated kernel while copies and synchronization dominate the application;
  • choosing a smaller model whose retries erase the apparent saving;
  • treating a provider benchmark as a prediction for a different workload;
  • changing several layers at once and losing the ability to attribute the result.

Optimization also creates operational state. Compiled artifacts, quantized weights, prompt caches, key-value caches, routing policies, model revisions, and fallback rules all need versions and invalidation conditions. A system that cannot explain which configuration served a result cannot reliably explain why its performance changed.

A Reproducible Inference Benchmark Record

Permalink to A Reproducible Inference Benchmark Record

Before accepting an optimization, preserve:

  • the task and success criterion;
  • model, revision, runtime, and precision;
  • hardware, driver, accelerator runtime, and region;
  • input and output distributions;
  • concurrency, batching, and queue policy;
  • warmup and cold-start method;
  • timeout, retry, cancellation, and fallback rules;
  • p50, p95, and p99 latency;
  • throughput and utilization;
  • quality results and failure count;
  • cost per successful task;
  • the exact baseline and changed variable.

Review the result as a system change. If throughput rises while task quality or tail latency crosses its limit, the optimization did not satisfy the contract. If cost falls because output was truncated below what the task needs, the metric is incomplete. If a change only works for one shape, record that shape as part of the operating boundary.

The transferable rule is simple: optimize the path that produces an acceptable decision, not the model invocation considered alone.