Skip to main content
Chunked Prefill is a technique that splits long prompts into smaller chunks during the prefill phase, significantly reducing peak memory usage and preventing Out-Of-Memory (OOM) errors in long-context serving.

Overview

Introduced by Sarathi-Serve, Chunked Prefill is enabled by default in Mini-SGLang. This feature addresses the memory challenges of processing very long input sequences by breaking them into manageable pieces.
Chunked Prefill is particularly important for long-context models where a single prefill batch could consume excessive GPU memory, starving the decode phase and reducing overall throughput.

The Problem: Memory Spikes

During the prefill phase, the system must:
  1. Compute attention over the entire input sequence
  2. Store KV cache for all input tokens
  3. Store intermediate activations for the forward pass
For long sequences (e.g., 32K or 128K tokens), this can cause:
  • Memory spikes that exceed GPU capacity
  • OOM errors that crash the serving system
  • Starvation of decode requests as memory is monopolized by prefill

Traditional Approach

Chunked Prefill Approach

How It Works

Prefill Manager

The PrefillManager handles chunking logic in /home/daytona/workspace/source/python/minisgl/scheduler/prefill.py:116:

Chunked Request Class

Chunked requests are marked with a special class to prevent premature completion:
This ensures chunked prefill requests:
  • Are not sampled for output tokens
  • Stay in prefill queue until all chunks are processed
  • Don’t enter decode phase prematurely

Scheduling Logic

The scheduler processes chunks across multiple iterations:
Chunked requests are prioritized in the pending list to ensure all chunks of a request are processed before moving to new requests. This prevents head-of-line blocking.

Configuration

Max Prefill Length

Control the chunk size with --max-prefill-length:
The max-prefill-length is also accessible via SchedulerConfig.max_extend_tokens.
The token_budget in the prefill adder is set to max_extend_tokens and shared across all requests in a batch. This ensures the total number of tokens processed in a single iteration stays within limits.

Choosing the Right Value

  • Too large: May cause OOM on long sequences
  • Too small: Excessive overhead from many small batches
  • Recommended: Start with default (8192) and adjust based on:
    • Available GPU memory
    • Typical sequence lengths
    • Model size and architecture
Setting --max-prefill-length to a very small value (e.g., 128) is not recommended as it may significantly degrade performance due to excessive chunking overhead. Values between 4096-16384 work well for most use cases.

Benefits

Prevents OOM Errors

By limiting the maximum tokens processed in a single forward pass, chunked prefill prevents memory spikes that could crash the server. Without chunked prefill:
With chunked prefill:

Enables Long-Context Serving

Models with large context windows (32K, 128K, or even 1M tokens) can be served effectively:

Better Resource Utilization

By controlling prefill memory usage, more memory remains available for:
  • Decode requests: Maintains responsiveness
  • KV cache: Supports more concurrent requests
  • Batch processing: Higher throughput

Memory Estimation

The prefill adder estimates memory requirements before processing:
The reserved_size accounts for in-flight decode requests to prevent starvation:

Integration with Radix Cache

Chunked Prefill works seamlessly with Radix Cache:
  1. First chunk: Matches prefix in cache, only processes uncached portion
  2. Subsequent chunks: Continue from cached state
  3. After completion: Full sequence is cached for future reuse
This means even partially processed requests can benefit from and contribute to the cache.

Performance Considerations

Trade-offs

Benchmarking

For performance testing without chunked prefill:
Compare throughput and latency with different --max-prefill-length values to find the optimal setting for your workload.

Example: Processing a Long Document

Each chunk is processed in a separate forward pass, keeping memory usage stable.

Radix Cache

Learn how KV cache reuse works with chunked prefill

Architecture

Understand the scheduler’s role in chunked prefill

Overlap Scheduling

See how chunking interacts with scheduling optimizations