Sliding Window Attention: How AI Models Read Long Text Without Running Out of Memory

When you chat with an AI model like ChatGPT, Claude or Gemma, it feels like the model "remembers" everything you said. You paste a long document, ask a question about page 3, and it answers. But behind the scenes, remembering everything is expensive. The longer the text, the more work the model has to do and the more memory it needs.
Sliding Window Attention, or SWA for short, is one of the clever tricks model builders use to keep that cost under control. In this article, we will explain what it is, why it exists, and which models use it. No heavy maths, no scary jargon. Just the idea.
The Problem: Reading Everything, Every Time
Imagine you are reading a 500-page novel. Every time you read a new word, someone forces you to flip back and re-check every single word you have read so far. Word 10? Check the 9 words before it. Word 10,000? Check all 9,999 words before it.
That would be exhausting. And it gets worse the further you go.
This is roughly how a normal language model reads text. Each new word looks back at every earlier word to understand its meaning. This "looking back" step is called attention. It works very well, but the cost grows quickly as the text gets longer.
There is also a memory problem. To avoid re-doing the same work again and again, the model saves notes about every word it has already seen. These saved notes are called the KV cache (you can think of it as the model's notebook). Long text means a thick notebook, and a thick notebook needs a lot of computer memory.
What Is Attention? (A Quick Picture)
Before we slide any windows, let us get a simple picture of attention.
Take the sentence: "Priya put the cake in the fridge because it was hot outside."
What does "it" mean? The cake? The fridge? The weather? To figure this out, the model lets the word "it" look at the other words and decide which ones matter most. That is attention: every word asks, "Which earlier words should I pay attention to?"
In normal attention, which we call global attention, every word is allowed to look at every other earlier word in the whole text. Nothing is hidden. That is powerful, but as we saw, it is also costly.
Enter Sliding Window Attention
Sliding Window Attention changes one simple rule:
Instead of looking at all earlier words, each word only looks at a fixed number of recent words.
That fixed number is the window size. If the window is 1,024 words (or more precisely, 1,024 tokens, which are small pieces of words), then each new token only looks back at the last 1,024 tokens. Anything older is outside the window.
As the model moves forward through the text, the window moves with it, like a torch beam sliding along a dark wall. That is where the name comes from.
Think of it like a train window. You see the trees and houses passing right now, plus a little behind. You do not see the station you left an hour ago. You only see what is near.
This is why SWA is called local attention. Normal attention is global (it sees everything), and sliding window attention is local (it sees only what is nearby).
Seeing It in a Picture: The Attention Mask
The easiest way to see the difference is to look at an attention mask. A mask is a small grid that tells the model which tokens each token is allowed to look at.

The picture uses the sentence "The model attends to past tokens". Each row is the token that is currently being read. Each column is a token it might look at. A blue square with a 1 means "allowed to look". A dark square with a 0 means "not allowed".
Left side: regular (causal) attention A language model reads from left to right, so a token can never peek at words that come after it. That is why the upper-right half of the grid is all 0s. But everything before the token, and the token itself, is allowed. Look at the row for "to". It has 1s under "The", "model", "attends" and "to". The yellow arrows show "to" looking back at all of them. By the last row, "tokens" looks at the whole sentence. As the text grows, the blue triangle grows with it.
Right side: sliding window attention The upper-right half is still 0, because the model still cannot see the future. The difference is on the lower-left. In the row for "to", the square under "The" has changed from 1 to 0. With a window of 3, "to" can only look at itself and the two tokens just before it: "model", "attends" and "to". The word "The" has slid out of view.
Now read down the rows. In the last row, "tokens" only sees "to", "past" and "tokens". The blue squares form a narrow diagonal band instead of a full triangle. That band is the sliding window.
What the picture tells us
- Same rule against the future. Both masks block tokens that come later. SWA adds a second limit: tokens that are too far in the past are also blocked.
- The number of blue squares per row stops growing. In regular attention, row 6 has 6 blue squares, and a row at position 10,000 would have 10,000. With a window of 3, no row ever has more than 3. This is exactly where the saving in compute and memory comes from.
- Early tokens look the same in both. The first three rows are identical on both sides, because there are not yet more than 3 tokens to look at. The two masks only start to differ once the text is longer than the window.
In a real model, the window is far bigger than 3 (for example 1,024 tokens, as in Gemma 3), and the text is far longer than 6 tokens. The shape of the mask is the same, just much larger.
But Wait, Doesn't the Model Forget Things?
Good question. If each token only sees the last 1,024 tokens, what happens to an important fact mentioned 20,000 tokens ago?
This is where model builders get smart. They do not make every part of the model local. A language model is built from many layers stacked on top of each other, like floors in a building. Builders mix two kinds of floors:
- Local floors that use sliding window attention (cheap, short view)
- Global floors that use normal attention (expensive, full view)
The local floors handle nearby details, like grammar and the meaning of the current sentence. The occasional global floor makes sure information from far away can still travel across the whole text.
It is like a team in an office. Most people only talk to the colleague sitting next to them, which is fast and easy. But once in a while, there is a full team meeting where everyone shares updates. Nobody gets left out, and the office does not spend all day in meetings.
The Two Knobs: Window Size and Ratio
Sebastian Raschka describes SWA as a tunable knob. In practice, there are two settings that builders play with:
Window Size How many recent tokens each local layer can look at. Bigger windows see more but cost more.
Local-to-Global Ratio How many local layers you use for every one global layer. A ratio of 5:1 means five local layers, then one global layer, then repeat.
Turning these knobs changes how "aggressive" the model is about saving cost. A small window with many local layers saves a lot. A big window with fewer local layers saves less but keeps more direct long-range view.
Real Models That Use It
Here is where it gets interesting. Different model families have made different choices.
Gemma 2 (Google) Used a 1:1 ratio, meaning it switched back and forth between a local layer and a global layer. Each local layer had a window of about 4,000 tokens.
Gemma 3 (Google) Became much more aggressive. It moved to a 5:1 ratio (five local layers for every global layer) and shrank the window from 4,096 to just 1,024 tokens.
OLMo 3 and Arcee Trinity Use a 3:1 ratio, sitting in the middle ground.
Xiaomi MiMo-V2-Flash Uses the same 5:1 ratio as Gemma 3, but with a tiny window of only 128 tokens. That is far smaller than Gemma's 1,024 and a very bold choice.
Other models in the gallery that use SWA include Step 3.5 Flash and Tiny Aya. So this is not a niche idea. It has quietly become a standard tool in modern model design.
Why It Saves Memory
Remember the model's notebook, the KV cache? In a normal global layer, the notebook keeps notes for every token in the whole conversation. A 100,000-token document means 100,000 sets of notes in that layer.
In a sliding window layer with a window of 1,024, the layer only needs notes for the most recent 1,024 tokens. Older notes can be thrown away for that layer, because it will never look at them again.
Now multiply that by many layers. If five out of every six layers are local, most of the model's notebook stays small no matter how long the text gets. Only the few global layers keep the full, thick notebook.
That is the big win. Long documents, long chats and long code files become much cheaper to handle, especially on smaller GPUs or laptops.
Does It Hurt Quality?
You might expect a model that "sees less" to become dumber. Surprisingly, the evidence says the damage is small.
When Google tested Gemma 3's design, they compared the smaller window and the more aggressive 5:1 ratio against less aggressive setups. The quality score (a measure called perplexity, which roughly tells you how well the model predicts the next word) barely changed.
Why? Because most of what a word needs to understand itself is close by. The meaning of "it" in a sentence usually depends on the words in that same sentence or paragraph, not on something 50 pages earlier. And for the rare cases where far-away information matters, the global layers are there to help.
SWA Plays Well With Other Tricks
SWA is not the only memory-saving trick in modern models. Another popular one is called Grouped-Query Attention, or GQA. You do not need the details. Just know this:
Sliding Window Attention Saves memory by keeping notes for fewer tokens (only the recent window).
Grouped-Query Attention Saves memory by making each token's notes smaller (sharing parts of them).
Because they save memory in different ways, you can use both together. Gemma 3, for example, uses SWA and GQA at the same time.
A Tiny Code Picture
If you are curious, here is a very small Python sketch that shows which earlier tokens each token is allowed to see. A "1" means "can look", a "0" means "cannot look".
window = 3
n = 6
for i in range(n):
row = [1 if (i - window < j <= i) else 0 for j in range(n)]
print(row)
Output:
[1, 0, 0, 0, 0, 0]
[1, 1, 0, 0, 0, 0]
[1, 1, 1, 0, 0, 0]
[0, 1, 1, 1, 0, 0]
[0, 0, 1, 1, 1, 0]
[0, 0, 0, 1, 1, 1]
See how the band of 1s slides down and to the right? That band is the window. In global attention, the whole lower-left triangle would be filled with 1s.
Key Terms at a Glance
Token A small piece of text, often a word or part of a word. Models read text token by token.
Attention The step where each token looks at earlier tokens to understand its own meaning.
Global Attention Every token can look at every earlier token. Full view, high cost.
Sliding Window Attention (SWA) Each token only looks at a fixed number of recent tokens. Short view, low cost.
KV Cache The model's notebook of saved notes about earlier tokens, so it does not redo work.
Local-to-Global Ratio How many sliding window layers a model uses for each full attention layer.
The Takeaway
Sliding Window Attention is a simple idea with a big effect: most words only need to look at their neighbours. By letting most layers focus on a small, sliding window and keeping a few layers with a full view, models like Gemma 3, OLMo 3 and Xiaomi MiMo can handle long text with much less memory, while staying almost as smart.
Next time an AI model reads your 100-page PDF without breaking a sweat, there is a good chance a sliding window is quietly doing part of the work.