# Context Window

> The Context Window is the maximum amount of text in tokens that a language model can consider at once.

## What is a context window in an LLM?

The limit on the number of tokens a model can process in one request, including instructions, conversation history, attached files and the answer. Anything outside the window is not seen by the model.

- Topic: Language Models
- Type: concept

## Example

Wrong:

```js
history.push({ role: 'user', content: input });
const body = { model: MODEL, messages: history, maxTokens: 4096 };
```

Right:

```js
history.push({ role: 'user', content: input });
const MAX_OUTPUT = 4096;
while ((await countTokens(history)) + MAX_OUTPUT > CONTEXT_WINDOW) {
  history.shift(); // drop the oldest message
}
const body = { model: MODEL, system: SYSTEM_PROMPT, messages: history, maxTokens: MAX_OUTPUT };
```

Old messages are dropped until input plus reserved output fits the window, and instructions live in the system prompt.

## Common mistakes

- Sending the whole history every turn until requests start failing with a length error.
- Assuming the model uses everything in a huge context equally well.

## Key points

- Input tokens plus the requested output tokens fit inside the context window.
- Long chat history is trimmed or summarized before it reaches the limit.
- Key instructions stay in the system prompt instead of old messages that may be cut.

## Interview questions

### Does the context window include the output?

Yes. Input and generated output share the context window, so a long prompt leaves less room for the answer.

### What happens when I exceed the context window?

The API usually rejects the request with a length error, or some apps silently drop the oldest messages.

### Does a bigger context window replace RAG?

Not always. Huge prompts cost more, run slower and the model may miss details in the middle, so retrieval is often still better.

## Related cards

- [Token](https://wonderium.dev/ai/token.md)
- [Context Compaction](https://wonderium.dev/ai/context-compaction.md)
- [Context Rot](https://wonderium.dev/ai/context-rot.md)
- [Max Tokens](https://wonderium.dev/ai/max-tokens.md)
- [RAG](https://wonderium.dev/ai/rag.md)

---

- Level: [Artificial Intelligence](https://wonderium.dev/ai.md)
- HTML version: https://wonderium.dev/ai/context-window
- Last updated: 2026-11-25
