InferenceAI

Enter at least two characters to search.

Start free

Decode Step Capacity

mediumInference20 min25 XP

Each active request is [id, cached_tokens]; generating its next token requires one more KV position. Implement fit_decode_step(active, max_total_tokens). Return IDs accepted in input order while the sum of (cached_tokens + 1) stays within the total capacity. Skip requests that do not fit and continue. All counts are nonnegative. This simplified model treats each selected request as generating exactly one token.

Explore the underlying concept with illustrative inputs. This experiment is separate from the problem’s test cases.

INTERACTIVE ENGINEERING · TEACHING MODEL

Continuous batching

Compare fixed batches with a scheduler that refills slots as requests finish.

Fixed batches · 9 ticks total

R1
Active
R2
Active
R3
Queued
R4
Queued
R5
Queued

Continuous batching · 6 ticks total

R1
Active
R2
Active
R3
Queued
R4
Queued
R5
Queued

All five requests arrive at tick 0. Two slots each decode one token per tick. Request lengths: 2, 4, 1, 3, 2. Bars show scheduled work; the vertical cursor marks the current tick.

Change the experiment

Algorithm sketch
for each decoding_tick:
    decode_one_token(active_requests)
    remove_finished_requests()
    fill_free_slots(queue)
Try it yourself

Increase the last request length. Compare the two completion times and locate idle slots in fixed batching.

Tick 0. Fixed batches completed 0 requests; continuous batching completed 0.

Refilling free slots can improve utilization. This idealized model excludes prefill, memory limits and shared GPU contention.

Playback pauses in hidden tabs and offscreen. With reduced motion enabled, use the timeline or Next step.

Open the full explainer and practice links

Examples

EXAMPLE WALKTHROUGHskip long sequence

A guided view of the published example, not a trace of your code.

INPUT[[["a",3],["b",8],["c",1]],7]
TASKfit_decode_step()
EXPECTED OUTPUT["a","c"]

Start with the input values shown in this example.

Example 1 of 2

Input: active = [["a",3],["b",8],["c",1]], max_total_tokens = 7

Output: ["a","c"]

Explanation: skip long sequence

Input: active = [["a",0],["b",0]], max_total_tokens = 2

Output: ["a","b"]

Explanation: first decode

Hint 1

A selected sequence costs its cached length plus one.

Topics

PythonDecode SchedulingKV Cache