Performance

Measure and improve LLM quality, latency, throughput, memory, reliability, and cost across realistic workloads.

Users expect an AI application to be helpful, responsive, and reliable. A model that gives excellent answers but responds too slowly may be unusable. A fast model that frequently produces incorrect answers also fails the product goal.

LLM performance therefore means more than speed. It is the measured balance among quality, latency, throughput, memory, reliability, and total cost for a specific workload.

What Is LLM Performance?

Performance describes how effectively and efficiently the complete AI system serves users. The system includes prompts, retrieval, tools, model inference, network calls, validation, and interface behavior—not only the base model.

  • Quality: Does the output solve the task correctly and safely?
  • Latency: How long does a user wait?
  • Throughput: How much work can the system complete over time?
  • Resource use: How much compute and memory are consumed?
  • Reliability: How often does the request succeed under load?
  • Cost: What is the total expense per useful outcome?

How LLM Inference Uses Time

Prefill

During prefill, the model processes input tokens from instructions, conversation history, retrieved documents, and the current request. Long prompts increase prefill work and occupy more attention-cache memory.

Decode

During decode, the model generates output autoregressively, usually one token step at a time. Earlier attention keys and values are cached so they do not need to be recomputed from scratch at every step.

Output
Request
  -> Queue and network time
  -> Prompt construction and retrieval
  -> Prefill: process input tokens
  -> Decode: generate output tokens
  -> Validation and post-processing
  -> Complete response

Key Performance Metrics

Time to First Token

Time to First Token, or TTFT, measures the delay from starting the request until the first generated token becomes available. It includes queueing, network, preparation, and prefill time depending on the measurement boundary.

Inter-Token Latency and Output Speed

Inter-token latency measures delay between generated tokens. Output speed is often reported as tokens per second. Tokenizers differ, so tokens per second is not a direct reading-speed comparison across all model families.

End-to-End Latency

End-to-end latency covers the complete user-visible request, including retrieval, tools, retries, model generation, validation, and formatting. It is often the most important experience metric.

Throughput and Concurrency

Throughput measures completed requests or processed tokens per unit of time. Concurrency is the number of requests active at once. Higher concurrency can improve hardware utilization but may increase latency and memory pressure.

Tail Latency

Averages hide slow requests. Teams monitor percentiles such as p50, p95, and p99. If p95 latency is eight seconds, 95% of measured requests completed within eight seconds while 5% took longer.

Reliability and Cost

Track success rate, timeouts, provider errors, retry rate, invalid output, fallback usage, and total cost. Cost per successful or approved task is more informative than cost per raw request.

Factors That Affect Performance

Model Architecture and Size

Larger models often require more computation and memory, but size alone does not predict task quality or speed. Architecture, quantization, serving software, and hardware matter.

Prompt and Context Length

Long instructions, repeated history, and excessive retrieved chunks increase prefill latency, token cost, and key-value-cache memory. More context can also distract the model and reduce quality.

Output Length

Long output requires more decode steps, increasing latency and cost. Set useful limits and request concise output when the task does not need a long answer.

Hardware and Runtime

CPU, GPU, accelerator memory, memory bandwidth, network links, kernel support, inference engine, numerical precision, and parallelism all affect performance. Benchmark on the actual deployment environment.

Traffic Shape

Short interactive requests, long document jobs, bursty traffic, and offline batch processing need different optimizations. A benchmark using fixed short prompts may not represent production.

Python Measurement Example

This example records end-to-end durations for a placeholder request function. Use a monotonic high-resolution timer and preserve errors rather than measuring only successful calls.

Python
from statistics import median
from time import perf_counter

def timed_call(request_fn, prompt):
    started = perf_counter()
    result = request_fn(prompt)
    elapsed = perf_counter() - started
    return result, elapsed

def percentile(values, fraction):
    ordered = sorted(values)
    index = round((len(ordered) - 1) * fraction)
    return ordered[index]

# Replace with a real test client and representative prompts.
durations = [0.82, 0.95, 1.10, 1.40, 2.25]

print(f"p50: {median(durations):.2f}s")
print(f"p95: {percentile(durations, 0.95):.2f}s")

For streaming, record request start, first token, final token, input and output token counts, model version, route, cache status, and outcome. Avoid storing sensitive prompt or response text unnecessarily.

Performance Optimization Techniques

Choose the Smallest Reliable Model

Compare models using the same quality and safety evaluation. A smaller model may be faster and cheaper for classification or extraction, while a more capable model handles difficult reasoning or fallback cases.

Reduce Unnecessary Context

Remove duplicated instructions, summarize old conversation safely, retrieve fewer higher-quality chunks, and send only fields needed for the task. Always verify that quality does not regress.

Control Output Length

Use clear response requirements and sensible output limits. Stop sequences or structured schemas can end generation when the desired result is complete.

Stream Output

Streaming displays partial output before completion and improves perceived responsiveness. It may not reduce total computation, and applications must still handle moderation, errors, cancellation, and partial structured data.

Batching

Batching combines work to improve accelerator utilization and throughput. Continuous batching is common in inference servers, but larger batches may increase queueing and individual-request latency.

Caching

Applications can cache identical safe responses, retrieval results, embeddings, or repeated prompt prefixes where supported. Cache keys must include all inputs that affect output, and entries need permission, freshness, privacy, and invalidation rules.

Quantization

Lower-precision weights can reduce memory and improve speed on compatible hardware. Quantization may reduce quality or become slower without optimized kernels, so test the exact runtime.

Prompt and Model Routing

A router can send simple tasks to a fast workflow and difficult tasks to a stronger one. Routing errors affect quality, so measure both router accuracy and end-to-end outcomes.

Parallel Tools and Retrieval

Independent network or retrieval operations can run concurrently when safe, reducing critical-path time. Limit fan-out, apply deadlines, and preserve deterministic authorization and error behavior.

Latency vs Throughput

GoalUseful TechniquesPossible Tradeoff
Lower interactive latencyShorter context, streaming, fast model, less queueingLower hardware utilization or quality
Higher throughputBatching, queues, parallel hardwareLonger wait for an individual request
Lower memoryQuantization, smaller model, cache managementQuality loss or runtime complexity
Lower costRouting, caching, shorter input and outputMore workflow logic and invalidation work

Optimization is a constrained tradeoff, not a single maximum. Define service-level objectives for each workload instead of using one latency target for every request.

Load Testing

  • Replay privacy-safe traffic distributions with varied prompt and output lengths.
  • Increase concurrency gradually and observe the saturation point.
  • Measure queue time, TTFT, decode speed, end-to-end percentiles, and errors.
  • Include retrieval, tools, validation, retries, and fallbacks.
  • Test bursts, provider throttling, slow dependencies, and partial failures.
  • Confirm resource limits, graceful degradation, and recovery.

Monitoring in Production

  • Input and output token distributions.
  • TTFT, end-to-end p50, p95, and p99 latency.
  • Throughput, concurrency, queue depth, and saturation.
  • Success, timeout, retry, fallback, and invalid-output rates.
  • Memory, accelerator utilization, and cache hit rate.
  • Cost by model, route, feature, tenant, or successful task.
  • Quality and safety indicators from ongoing evaluation.

Correlate performance changes with model, prompt, retrieval, runtime, and deployment versions. Protect logs and use traces or request identifiers instead of exposing private content.

Common Performance Mistakes

  • Optimizing latency while ignoring answer quality.
  • Reporting average latency without tail percentiles.
  • Benchmarking one short prompt instead of real traffic.
  • Measuring model time while ignoring queue, tools, and validation.
  • Increasing context because the model supports it, even when it adds no value.
  • Caching personalized output without complete permission-aware keys.
  • Adding retries without limits, backoff, or idempotency.
  • Assuming smaller or quantized always means faster on every runtime.

Best Practices

  • Define quality, safety, latency, throughput, reliability, and cost targets together.
  • Measure a production-like baseline before optimizing.
  • Change one major variable at a time when practical.
  • Use representative workloads and exact deployment hardware.
  • Prefer simple optimizations such as removing unused context first.
  • Validate quality and safety after every performance change.
  • Load test failure modes and plan graceful degradation.
  • Monitor continuously and retain rollback support.