100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Context Engineering
30 minadvanced

Anatomy of a Context Window

A context window is not a metaphor for “what the model remembers.” It is a literal, finite sequence of tokens that the model's self-attention mechanism operates over in a single forward pass. Every token in that sequence — whether it came from your system prompt, a tool schema, a message a user typed three turns ago, a document your retrieval step pulled in, or a result a tool just returned — sits in the same flat sequence and is visible to every other token through attention. There is no separate channel for “instructions” versus “conversation”; it is all just tokens, in order.

That flat sequence is assembled fresh on every single API call. The model does not retain state between turns — there is no persistent memory sitting on a GPU between your first message and your fifth. Instead, the serving stack concatenates the system prompt, the tool definitions, the full message history so far, any retrieved documents, and the results of any tools that were called, then runs a forward pass over the whole thing again. Turn five re-sends and re-processes everything from turn one. This is why the window's size is not a soft guideline — it is the hard ceiling on how much of a conversation, codebase, or tool catalog can be “in play” at once.

Most people building on top of LLM APIs think of “context” as the text they typed into a chat box, and treat everything else as free. It isn't. Tool definitions, retrieved documents, and accumulated history are frequently the largest consumers of the window, and they grow or shrink without the user typing a single extra word. Understanding the anatomy of the window — what's in it, who put it there, and how big each piece actually is — is the prerequisite for every context engineering technique that follows in this course: budgeting, compaction, retrieval grounding, and position-aware ordering all assume you can already answer “where did my tokens go?”

Analogy🏏Cricket
🏏 Think of it like cricket: A T20 innings has exactly 120 deliveries and not one more — the umpire doesn't hand you ball 121 because the top order batted beautifully. Everything a batting side does happens inside that fixed count: the openers surviving Jasprit Bumrah's new-ball spell, the middle order rotating strike through the middle overs, and finally a finisher like Hardik Pandya swinging for the boundary in the last five overs. The 120 balls are the stadium's hard limit, exactly like a model's context window is a hard limit on tokens — nobody negotiates for one more delivery any more than an API call gets tokens past the window ceiling. Here's the detail coaches obsess over: the balls spent building the innings and the balls spent finishing it come out of the *same* 120, not two separate pools. If the openers grind out sixty dot balls surviving the powerplay, only sixty deliveries remain for whoever has to finish the chase — no extra overs materialize because the top order was cautious. That is exactly how a context window works: the tokens spent on system prompt, tool schemas, and conversation history, and the tokens reserved for the model's own answer, are drawn from one shared envelope. Spend too much on the setup, and the finisher — the model's generated response — is the one left short of balls.
Lesson 2 of 35
0% complete