Sliding window attention limits each token to attending to only the most recent tokens inside a fixed window, instead of the whole history. The KV cache then stops growing once the layers using sliding window attention have reached their maximum size, which is the window size, so memory stays bounded no matter how long the text gets. The trade-off is real: anything outside the window is no longer attended to, so the model can forget the beginning of a long document. Use it for long streams where recent context matters most.
What you need
- A model whose config uses sliding window attention, for example Mistral or Gemma2
- The window size from the model config
- A test question about the start of a long document
Step 1: Confirm the model uses sliding windows
Check the model config for a sliding window size. If the model has no sliding window layers, its cache grows with the whole context and this technique does not apply.
Step 2: Run the long input and watch the cache
Send the long text and confirm the cache size stops growing at the window size. The model keeps the most recent window of tokens and drops older ones as new tokens arrive.
Step 3: Test whether the start is still answerable
Ask a question whose answer is at the very start of the document. If the model cannot answer it, the window has dropped the evidence, and you need retrieval or a summary of the early part instead.
Worked example: synthetic sliding window check on a long document
The table below is a synthetic example. It shows the expected result of using sliding window attention; no model was run.
| Question about | Position in document | Answered? |
|---|---|---|
| Last section | End | Yes |
| Middle section | Middle | Yes – inside window |
| First section | Start, outside window | No – dropped |
| Fix | Summary of start kept | Yes after fix |
Expected result (UNTESTED). questions about recent sections are answered, the question about the start fails, and keeping a summary of the start fixes it. This check was not run live; the table shows the expected result for the synthetic example.
Limits
- Sliding window attention is a model architecture choice, not a setting you can turn on for any model.
- For models using sliding window attention, the cache will never be larger than the window on those layers, even if the maximum length is larger.
- A static cache with sliding window layers is only usable while the number of processed tokens is smaller than the window for assisted decoding rollbacks.
- If the whole document matters, retrieval or chunking is a better fit than a sliding window alone.
