Concepts / Model Inference Basics

Model Inference Basics

Inference optimization aims to make models faster and cheaper when they produce outputs, and both goals matter to different parties.

  • Programming

Why the Wait Matters

Imagine typing a question into an AI assistant and watching the cursor sit still for several seconds before the first word appears. Most people would call that experience slow, and many would stop using the product. The delay is not a cosmetic problem. It is the visible result of how long a trained model takes to do its work when someone asks it something.

Inference optimization is the set of efforts aimed at shrinking that delay and the cost of producing answers, so that models respond faster and run more cheaply. This article explains why that goal matters, why it has grown more urgent as models have become larger, and how the sequential way many of these models generate output makes it difficult to achieve.

Speed and Cost as Two Goals

Definition

Inference optimization aims to make models faster and cheaper when they produce outputs. Speed and cost are the two goals of this work.

The two goals serve two different audiences. Speed matters to the people using a product, because a faster model gives them a response sooner. Cost matters to the organization running the product, because cheaper inference lowers what it spends to answer each request. The source puts it bluntly: users never say no to faster models, and companies can always benefit from cheaper inference.

Neither goal is ever fully finished. That is why optimizing inference has been important for as long as machine learning engineering has existed. The pressure has grown, however, with the arrival of foundation models. As these models scale up, the cost of each inference rises, and so does latency, which is the time between a request and its response. A much larger model does more work per output, so an optimization effort that was once sufficient now has much more at stake. This is why inference optimization has become an active subfield in both industry and academic research.

Key point

Speed is felt by the person waiting for an answer. Cost is felt by the organization paying to produce each answer. The same optimization work can serve both audiences, but they measure success differently.

Tracing a Token-by-Token Loop

Many foundation models are autoregressive, which means they generate tokens sequentially. A token is a unit of output the model produces one at a time. The model cannot produce the whole answer in a single shot. It produces one token, uses that work to produce the next, and continues until the output is complete.

Trace the loop in the diagram below. The prompt goes in, and the model writes Token 1. To write Token 2, the model needs the work from Token 1, so it cannot start Token 2 early. Token 3 depends on the tokens before it in the same way. The defining feature is ordering: each step depends on the steps before it. The work cannot simply be spread out and run all at once, the way a set of independent calculations might be.

PromptToken 1Complete outputToken 2Token 3pass 1pass 2 builds on token 1pass 3 builds on earlier tokensstop when output is complete
What happens at each step of generation, and why does the model have to wait for one token before producing the next?

Each pass through this loop takes time, and the number of passes equals the number of tokens in the output. This is the reason autoregressive generation is costly. A longer answer means more passes, and each pass adds its own delay.

Adding Up the Per-Token Time

Because the number of passes equals the number of output tokens, the total delay is the per-token time multiplied by the length of the output. If t stands for the time to generate one token and n stands for the number of output tokens, the approximate total response time is given by the formula below.

Formula

The formula shows why length matters so much. Doubling the number of output tokens doubles the total delay, if the per-token time stays the same. A response with many tokens accumulates delay with every step, so even modest per-token times can push the whole response well beyond the benchmark discussed later in this article.

Request senttime 0 msToken 1 doneabout 50 msToken 2 doneabout 100 msToken 3 doneabout 150 msToken 4 doneabout 200 ms+50 ms+50 ms+50 ms+50 ms
How does the time for one token add up across all output tokens to give the total time for a response?

The timeline above uses an illustrative per-token time of 50 ms. It is not a measurement of any particular model. The point is the pattern: each new token adds one more step to the clock, and the clock never resets between tokens.

Worked Estimate for a Long Answer

Worked example

Estimating a 200-token response

Suppose a model generates each token in 50 ms and the answer contains 200 tokens. Estimate the total time before the response is complete, using the approximate relation T approximately equal to n times t.

  1. 1
    Identify the per-token time

    The per-token time t is 50 ms, so each pass through the loop takes 50 ms.

  2. 2
    Identify the output length

    The number of output tokens n is 200, so the loop runs 200 times.

  3. 3
    Multiply

    Compute T approximately equal to 200 times 50 ms, which gives 10,000 ms.

  4. 4
    Convert units

    10,000 ms is equal to 10 seconds.

Result

The response takes roughly 10 seconds to finish under these assumptions. The numbers are illustrative, chosen to show the arithmetic, and do not describe a real model.

Internet Apps Versus AI Applications

Latency is the time an AI application takes to respond. It is one of the main ways a user experiences a system's speed, and it is the figure that inference optimization tries to reduce. The source points to a specific benchmark: a typical internet application is expected to respond within about 100 ms. Users of such applications have learned to expect that speed, and they are notoriously impatient with anything slower.

Single requestMany sequentialtokensAbout 100 ms expectedDelay adds up pertoken
How does a request-response cycle for a typical web app differ in timing from one that streams many sequential tokens from a model?

Getting AI applications down to that roughly 100 ms expectation is a major challenge. The reason connects back to autoregressive generation. A typical web request is a short exchange with a fixed, small amount of work. An AI response that contains many tokens, each produced in sequence, has many steps to pass through before it is finished. The gap between what users expect and what sequential generation naturally delivers is what makes this a central problem for the field, and it is why the area has become an active research topic.

Mistakes Beginners Make

  • Common mistake

    Assuming the model writes the whole answer in one shot

    Why it fails

    Autoregressive models produce one token at a time, and each step depends on the steps before it.

    Do this instead

    Think of the output as a loop with one pass per token.

  • Common mistake

    Treating speed and cost as the same goal

    Why it fails

    Speed mainly matters to the people using a product, while cost matters to the organization running it.

    Do this instead

    Name the audience for each goal when you judge an optimization.

  • Common mistake

    Thinking a short per-token time guarantees a fast response

    Why it fails

    Total delay grows with output length, so many tokens can accumulate delay even when each token is quick.

    Do this instead

    Estimate the total with the per-token time multiplied by the number of output tokens.

Predicting a Response Time

What do you think happens?

A model takes 40 ms to generate each token, and its answer contains 25 tokens. Roughly how long until the full response is complete?

  • About 40 ms

  • About 400 ms

  • About 1 second

  • About 10 seconds

Reveal answer
Answer:

About 1 second (1000 ms)

The loop runs once per token, so the total is 25 times 40 ms, which equals 1000 ms. Multiplying the per-token time by the output length gives the total.

EASY

A model generates each token in 30 ms. How long does a 300-token answer take, according to the approximate relation? Then explain in one sentence why doubling the answer length changes the total.

Hints
  • Multiply the number of output tokens by the per-token time.
  • Doubling the output length doubles the number of passes through the loop.

Summary of Inference Basics

  1. 1
    Inference optimization aims to make models faster and cheaper when they produce outputs. Speed serves users and cost serves the organization running the product.
  2. 2
    Autoregressive models generate tokens sequentially, so each step depends on the steps before it and the work cannot run all at once.
  3. 3
    Total response time is approximately the per-token time multiplied by the number of output tokens, so longer answers take longer.
  4. 4
    A typical internet application is expected to respond in about 100 ms, and making AI applications meet that expectation is a major challenge that drives ongoing research.

At a glance

  • Inference optimization aims to make models faster and cheaper when they produce outputs, and the two goals serve different audiences.
  • Autoregressive models generate tokens one at a time, with each step depending on the earlier steps, so the work cannot simply run in parallel.
  • Total response time is approximately the per-token time multiplied by the number of output tokens, so output length drives delay.
  • A typical internet application is expected to respond in about 100 ms, and bringing AI applications close to that expectation is a central challenge.

More concepts starting with M