Skip to content
derpx06Notes on systems, models & learning
1. LLM Foundations · lesson 5 of 68 · 4 min · January 9, 2026

The Context Window & Budgeting

A technical architectural guide to understanding statelessness, attention mechanisms, and context management in Large Language Models.

To understand the Context Window, one must first understand the fundamental state of a Large Language Model: it is stateless.

Unlike a human brain, which retains a continuous stream of consciousness and long-term memory, an LLM resets after every interaction. It does not "remember" what you said five seconds ago. To maintain a conversation, the application must re-send the entire dialogue history (User Input + Model Output) back to the model with every new request.

The Context Window is the maximum capacity of this re-sent data. It defines the model's "working memory"—the rigid boundary of text it can perceive, analyze, and reference at any single moment during inference. If information falls outside this window, it is effectively deleted; to the model, it ceases to exist.


If context is so critical, why do models have limits (e.g., 8k, 32k, or 128k)? The limitation is physical and mathematical, rooted in the Self-Attention Mechanism of the Transformer architecture.

For an LLM to "understand" a sentence, it must calculate the relationship between every unit of data and every other unit in the window. This process has Quadratic Complexity (O(n2)O(n^2)).

  • If you double the length of the context window, the computational power required does not double; it quadruples.
  • This creates a massive bottleneck in hardware memory (VRAM) and processing speed (Latency).

A larger context window does not automatically equal better understanding. Research has identified a performance degradation known as the "Lost in the Middle" phenomenon.

When models are presented with a massive block of text, their retrieval accuracy follows a U-Shaped Curve:

  1. Primacy Bias (High Accuracy): Information at the very start of the window (typically System Instructions) is retained well.
  2. Recency Bias (High Accuracy): Information at the very end of the window (the user's most recent question) is retained well.
  3. The Trough (Low Accuracy): Critical details buried in the middle of a long context block are frequently overlooked or "forgotten" during generation.
Lost in the Middle Phenomenon
Lost in the Middle Phenomenon

As we move toward building agents that need to operate for days or weeks, we can't simply stuff everything into the window. We need optimization strategies.

Human language is full of "fluff" (connecting words, articles). In Context Engineering, we strip this away to increase density using structured state.

The Human Way (38 Tokens)

"The user explicitly stated that they are interested in buying a house in New York, specifically in Brooklyn, and their budget is around 1 million dollars. They also mentioned they have a dog."

The Engineered Way (16 Tokens)

compressed_state.json
User: {
"Goal": "Buy House",
"Loc": "Brooklyn/NY",
"Budget": "1M",
"Pets": "Dog"
}

Effective agents separate memory into two piles.

Memory TypePurposeRetention Rule
Semantic MemoryPermanent Facts (User Name, Preferences)Never delete. Store in 'Fact Bank'.
Episodic MemoryRecent Events (Greetings, Jokes)Delete often. Keep last 3-5 turns.

Don't re-transmit the entire state every turn. Send only the diff. If the user updates their age, just send Update: {Age: 26}. The model applies this patch to its internal state, saving massive amounts of tokens compared to re-sending the whole user profile.


When an application requires access to a dataset larger than the context window (e.g., a company's entire internal documentation), the architecture shifts to Retrieval Augmented Generation (RAG).

RAG decouples "Knowledge" from "Processing."

  1. Storage: The vast dataset is indexed in an external Vector Database.
  2. Retrieval: When a user asks a question, the system conceptually "searches" the database for the specific paragraphs relevant to that question.
  3. Injection: Only those specific paragraphs are injected into the Context Window.

This allows a model with a small window to appear as though it has access to infinite knowledge, by dynamically swapping the contents of its "working memory" for every specific query.


While the Context Window defines what the model sees, Hyperparameters define how it processes that information.

  • Definition: Controls the randomness of the output.
  • Low (0.1): The model becomes deterministic, always picking the highest probability token. Use for fact retrieval and code.
  • High (0.8+): The model increases the weights of lower-probability tokens. Use for creative writing and brainstorming.
  • Definition: A smarter alternative to Temperature. It limits the token pool to the top PP% of probability mass.
  • Low P (0.1): Only considers the top 10% most likely words. Extremely confident and factual.
  • High P (0.9): Considers the top 90% of plausible words. More diverse.

Tip: Alter Temperature OR Top P, but rarely both.

  • Definition: Punishes a token based on how many times it has already appeared.
  • Effect: The higher the penalty, the less likely the model is to repeat the same word verbatim. Use this to reduce repetitive loops.
  • Definition: A specific string (e.g., "User:", "\n\n") that forces the model to halt generation immediately.
  • Use Case: Essential for preventing the model from hallucinating a continuation of a conversation (e.g., generating the User's reply for them).