InferenceAI

Enter at least two characters to search.

Start free

Batch by Token Budget

mediumInference20 min25 XP

Greedily group token counts into batches whose sum does not exceed budget. A single oversized item gets its own batch.

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 WALKTHROUGHpacked

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

INPUT[[3,4,2,5],7]
TASKmake_batches()
EXPECTED OUTPUT[[3,4],[2,5]]

Start with the input values shown in this example.

Example 1 of 2

Input: token_counts = [3,4,2,5], budget = 7

Output: [[3,4],[2,5]]

Explanation: packed

Input: token_counts = [9,1], budget = 5

Output: [[9],[1]]

Explanation: oversized

Hint 1

Flush the current batch before adding an item that would overflow it.

Topics

PythonInference