Serving reasoning models, agents and RL rollouts
Reasoning models write thousands of tokens per answer, agents loop through tools over a growing context, and RL training generates millions of samples. What each does to KV memory, scheduling, caching and cost, with a runnable model: KV growth during decode, holding versus offloading versus recomputing during tool calls, and why synchronous rollout batches leave most GPU slots idle.
By Sandip Maity, Applied AI Engineer
Three workloads now dominate new inference traffic, and none of them looks like the chat requests most serving guides assume. Reasoning models think for thousands of tokens before answering. Agents loop: call the model, run a tool, append the result, call again. Reinforcement learning generates huge batches of samples to train on. This lesson works out what each does to memory, scheduling and cost, and what serving stacks do about it.
You need Python 3 and numpy.
#Reasoning traffic is decode traffic
Compare three request shapes for a 70B-class model with grouped-query attention (320 KB of KV cache per token) on a GPU with 40 GB left for the cache:
import numpy as np
# 70B-class GQA model on one 80 GB GPU slot: KV bytes per token and the KV budget
KV_PER_TOKEN = 80 * 8 * 128 * 2 * 2 # layers x kv heads x dim x (K,V) x BF16 = 327,680 bytes
KV_BUDGET = 40e9 # bytes left for KV after weights and activations
WORKLOADS = { # (prompt tokens, output tokens)
"chat": (2000, 300),
"RAG": (8000, 400),
"reasoning": (500, 8000),
}
print("1. KV memory over a request's life")
STEP_S, PREFILL_TOK_S = 0.025, 10000 # 25 ms per decode step, 10,000 prompt tokens/s
print(f"{'workload':10} {'peak KV':>9} {'avg KV':>9} {'fit at peak':>12} {'seconds on GPU':>15}")
for name, (p, o) in WORKLOADS.items():
peak = (p + o) * KV_PER_TOKEN
avg = (p + o / 2) * KV_PER_TOKEN # cache grows linearly while decoding
secs = p / PREFILL_TOK_S + o * STEP_S
print(f"{name:10} {peak/1e9:8.2f}G {avg/1e9:8.2f}G {int(KV_BUDGET // peak):>12} {secs:>15.1f}")1. KV memory over a request's life
workload peak KV avg KV fit at peak seconds on GPU
chat 0.75G 0.70G 53 7.7
RAG 2.75G 2.69G 14 10.8
reasoning 2.79G 1.47G 14 200.1The reasoning request has the shortest prompt and the largest bill. It holds a slot for about 200 seconds, 26 times longer than chat, and its cache starts at 0.16 GB and ends at 2.79 GB.
Time
0 s
Chats in the same slot
0
Reasoning KV
0.00 GB
Two requests arrive
A chat request with a 2,000-token prompt and 300 output tokens, and a reasoning request with a 500-token prompt that will think for 8,000 tokens. Same 70B model, 25 ms per decode step.
KV cache at 320 KB per token (70B-class GQA model), 25 ms per decode step.
What follows for serving:
- Decode speed is the product. Time per output token (TPOT) decides how long users wait for the answer, so the levers that cut decode cost matter most: quantisation, speculative decoding and a smaller KV cache (attention optimisations).
- Memory is a moving target. A chat batch is sized mostly by its prompts. A reasoning batch grows by one token per sequence per step, for thousands of steps. Admit too many and the engine must preempt halfway through, throwing away or swapping out minutes of work. Good schedulers admit on expected final length, keep headroom, and prefer swapping long sequences to CPU over recomputing them.
- Thinking budgets are a cost control. Most reasoning APIs let callers cap thinking tokens or choose an effort level. On your own deployment, cap the output length per tier and measure the quality change on your evals (test-time scaling). A budget turns an unbounded request into a plannable one.
- Speculative decoding helps more than usual. Reasoning text repeats itself (restating the problem, rechecking steps), so draft acceptance is high. Prompt lookup and suffix-based drafting, which copy spans already in the context, work especially well for agents that echo tool output.
- Measure the right latency. Time to first token says little if the first 5,000 tokens are hidden thinking. Track time to first answer token and total time, and stream a summary of the reasoning if users watch.
#Parallel sampling shares the prompt
Self-consistency and best-of-n send the same prompt several times (self-consistency). Engines with prefix caching store the prompt's cache once and fork it: each sample gets its own new blocks for its own tokens, and the shared prefix is counted once. Send them as one request with samples (or back to back with prefix caching on) rather than as unrelated requests spread across replicas, or every replica computes the same prefill.
Pro content
Keep reading with Inference Academy Pro
Unlock the rest of this lesson, including 2 animated diagrams, plus every advanced lesson, hands-on lab, the larger playground models and completion certificates.
Still to come in this lesson
- Agents: a growing prefix with pauses
- RL rollouts: throughput with a long tail
- A checklist by workload
- What you can now do
- Common mistakes
- Exercise
- Going deeper
You will be able to answer
- Why does a reasoning request with a 500-token prompt cost so much more than a chat request with a 2,000-token prompt?
- Why is admission control harder for reasoning traffic?
- An agent with 30,000 tokens of context calls a tool that takes 4 seconds. What does the lesson's model suggest?
- Why do synchronous RL rollout batches waste GPU time?
- Why must the inference engine's log-probabilities match the trainer's in RL?