AI Engineer Roadmap All lessons Interactive Quiz GitHub

Sliding Window Chunking

Beginner · 6 min read · Updated · By Saurabh Pathak

In short: Sliding window chunking makes fixed-size windows of text that move forward by a smaller step, so every border region appears inside at least one full window.

Play the interactive lesson: Sliding window chunkingRead on GitHub
On this page
  1. Explain it like I'm new
  2. Watch it
  3. How it works
  4. Code example
  5. When to use it / when not to
  6. Common mistakes
  7. Recap
  8. Interview questions
  9. Go deeper

Level: Beginner · Reading time: 6 min · Phase: 1 — RAG Foundations

Before you start: Chunking and Recursive Chunking. You should know that a long document is cut into smaller pieces (chunks) before search.

Explain it like I'm new

Imagine reading a long scroll through a cardboard frame that shows 10 lines. You read, then slide the frame down by 5 lines, and read again. Every line is seen at least once, and lines near the edge of one view are in the middle of the next. That frame is the window. How far you slide it is the stride (or step).

Words to know:

This fixes a real problem with simple cutting. If an important sentence lands on the border between two chunks, each chunk gets only half. With a sliding window, some window contains the whole sentence.

Sliding windows are fixed-size chunking with overlap, usually counted in words or tokens instead of characters.

Watch it

Sliding window chunking: Fixed windows that move by less than their size, so borders overlap.
Sliding window chunking: Fixed windows that move by less than their size, so borders overlap.

It plays on a loop. Read the caption in the box, one step at a time. Want to go at your own pace? Step through it with Previous and Next.

How it works

  1. Split the text into units (words or tokens).
  2. Take units 0 … window−1 as chunk 1.
  3. Move the start forward by stride and take the next window.
  4. Stop when a window reaches the end of the text.
words:   w0 w1 w2 w3 w4 w5 w6 w7 w8 w9
window=4, stride=2
chunk 1: [w0 w1 w2 w3]
chunk 2:       [w2 w3 w4 w5]
chunk 3:             [w4 w5 w6 w7]
chunk 4:                   [w6 w7 w8 w9]

Worked example: 1,000 tokens, window 200, stride 150 (overlap 50). Windows start at 0, 150, 300, 450, 600, 750 and 900. Window 750 ends at token 950, so one more window (900) is needed to reach the end. That is 7 chunks. With stride 200 (no overlap) windows start at 0, 200, 400, 600, 800: 5 chunks. Here, overlap costs 2 extra chunks (40% more).

Worked example

Take the 10 words w0 … w9 from the diagram above with window 4 and stride 2. The overlap is 4 − 2 = 2 words.

  1. Chunk 1 starts at word 0 and holds w0 w1 w2 w3.
  2. Move forward by the stride (2). Chunk 2 starts at word 2 and holds w2 w3 w4 w5.
  3. Chunk 3 starts at word 4: w4 w5 w6 w7. Chunk 4 starts at word 6: w6 w7 w8 w9.
  4. Chunk 4 reaches the last word, so we stop. That is 4 chunks.

Now say the key fact is the pair w3 w4. A plain cut into w0-w3 and w4-w7 splits that pair across two chunks. Chunk 2 above holds both words together, so a search can find the whole fact.

Code example

From code/rag_chunkers.py:

def sliding_window_chunks(text, window=50, stride=25):
    words = text.split()
    chunks = []
    for start in range(0, len(words), stride):
        chunks.append(" ".join(words[start:start + window]))
        if start + window >= len(words):
            break
    return chunks

text = " ".join(str(i) for i in range(10))
print(sliding_window_chunks(text, window=4, stride=2))
# ['0 1 2 3', '2 3 4 5', '4 5 6 7', '6 7 8 9']

In real projects most splitters express this as chunk_size + chunk_overlap. Token-based (pip install langchain-text-splitters tiktoken):

from langchain_text_splitters import TokenTextSplitter

splitter = TokenTextSplitter(chunk_size=200, chunk_overlap=50)   # stride = 150 tokens
chunks = splitter.split_text(long_text)

When to use it / when not to

OverlapProsCons
0%Fewest chunksBorder facts get split
10–20%Common balanceSome duplication
50%Very safeAbout 2× chunks, many near-duplicates

Common mistakes

Recap

If you remember only three things:

Interview questions

1. How do window size and stride relate to overlap? <details><summary>Answer</summary> Overlap equals window minus stride. Stride equal to window means no overlap; stride smaller than window means neighbours share text. </details>

2. Why use overlap at all? <details><summary>Answer</summary> So that information near a chunk border appears whole in at least one chunk, which improves recall for facts that would otherwise be split. </details>

3. What is the downside of a large overlap? <details><summary>Answer</summary> More chunks to embed and store, higher cost, and duplicate results that crowd out other relevant chunks in the top-k. </details>

Go deeper

Prev: Parent Child Chunking · Next: Metadata · Back to phase: Phase 1