← Back to articles Blog

LLM Inference Explained: How Models Generate Text (And What It Costs)

Emmanuel Ekunsumi · 6 min read · 2026-07-12

Training an LLM is a one-time compute event. Inference — running the trained model to generate responses — is the ongoing cost that scales with every user, every query, every token. It's where most of the money goes in production AI systems.

What is LLM inference?

Inference is the process of passing input tokens through a trained model's neural network layers to generate output tokens. Each forward pass through the model produces one token at a time — that's why LLMs generate text sequentially rather than all at once.

The key parameters that drive inference cost:

Why output tokens cost more than input tokens

Most providers charge 3-5x more per output token than input token. The reason: input tokens can be processed in parallel (the whole prompt at once), but output tokens are generated sequentially — each token must be generated before the next one can start. This sequential decoding is more compute-intensive per token.

ProviderModelInput per 1MOutput per 1MOutput/Input ratio
OpenAIgpt-4o$5.00$15.003x
OpenAIgpt-4o-mini$0.15$0.604x
Anthropicclaude-sonnet-4-6$3.00$15.005x
Googlegemini-2.5-flash$0.10$0.404x
Mistralmistral-large-latest$2.00$6.003x

The three layers of inference cost

1. Prefill (input processing)

The model processes all input tokens in parallel. This is fast but memory-intensive — the entire key-value (KV) cache for the input must be held in GPU memory.

2. Decoding (output generation)

The model generates output tokens one at a time, each depending on all previous tokens. This is the slow, expensive phase — and it's why keeping outputs concise matters so much for cost.

3. KV cache

The key-value cache stores intermediate computations so the model doesn't reprocess the full context for every new token. It's the main bottleneck for long-context inference — a 128K context window can consume gigabytes of GPU memory just for the KV cache.

Why inference costs spike unexpectedly

How to reduce inference costs

  1. Compress prompts — remove redundant instructions, filler phrases, and unnecessary context before sending
  2. Cache aggressively — exact match for identical prompts, semantic caching for near-identical ones
  3. Constrain output length — use max_tokens and explicit instructions like "answer in one sentence"
  4. Right-size your model — gpt-4o-mini handles most tasks at 1/30th the cost of gpt-4o
  5. Track per-endpoint usage — find which features consume the most tokens and optimize those first

The average production prompt is 40-70% waste tokens. Inference optimization starts with knowing which tokens are doing work and which aren't.

See exactly what your inference is costing

Tokoscope tracks token usage, waste scores, and cost attribution per endpoint across OpenAI, Anthropic, Gemini, and Mistral.

Get started free →