Knaph

Module 3Episode 310 min

The Context Window

The whole of module 3 as one video. Jump to any chapter, or read the lesson it comes from.

Transcript

Intro

Ten messages into a chat, Leila mentions her name. The assistant says: yes, Leila, as you said earlier. But open a new chat, and it has no idea who she is. Where did the memory go? And where was it in the first place? Today, what a model can see, how much of it there is room for, and what happens when it fills up.

Does the model remember?

Leila says, so the model does have a memory. It remembered my name. Omid asks, how many people are talking to the same model right now? Millions. It's the same model for all of them. The same numbers. If your name were stored inside it, everyone would get it. The numbers inside the model don't change when you chat. They were fixed at the end of training. So where is her name? In the app. The app keeps the conversation. Every time you send a message, the app sends the model the whole conversation so far. Your newest message at the end. The model reads all of it, writes one reply, and forgets everything. So the chat remembers, and the model doesn't. A new chat starts blank, because the app doesn't send the old one.

One request at a time

Each time, the app sends a request. One package of text with everything the model needs. The first request holds one message. The second holds that, the reply, and a new message. The third holds all of it, and one more. A program that keeps nothing from one request to the next is called stateless. Language models are stateless. That's a sensible design. One model can serve millions of people at once. And nothing of yours stays inside it. And when a chat app remembers your job or your city, weeks later? The app saves short notes about you, and quietly puts them into each new request. The model still remembers nothing.

Why wrong turns keep coming back

There's a catch. Mistakes in the history keep working against you. Say the assistant misunderstood you, and wrote three replies about the wrong report. You correct it. But those three wrong replies are still in the history. They go into every later request, and keep pulling it back. Better to start a new chat, with a short summary of what you really want.

Using a chat well

Three habits. One. When a chat goes wrong, start again with a clean summary. Two. In a new chat, repeat what matters. The model knows only what is in the request. Three. Where your app allows it, edit an earlier message instead of piling on corrections.

What the model can see

Everything in one request is called the context. For an agent, it holds much more than your messages. Instructions from the app. Descriptions of the tools it can ask for. Notes and rules that you wrote for it. The conversation so far. And everything it has read or run. Files, command output, web pages. All of it is text, so all of it is tokens. The model reads all of it, every request, and nothing else. If a fact isn't in the context, the model knows only what it learned in training. Nothing about your files, or this morning.

How big is the context window?

Can the context keep growing? No. A model can take in only so many tokens at once. That limit is the context window. The request, and the reply it writes, must fit inside it. In late twenty twenty six, Claude's current models had about a million tokens. Some smaller or older models, two hundred thousand. For a chat, that rarely fills. But an agent reads twenty files. One is three thousand lines long. It runs the tests five times, four hundred lines each. Picture the window as a bar that fills from the top. Some is taken before you type. The app's instructions, the tool descriptions, your notes. Then the conversation grows. And what the agent reads and runs grows fastest. One big file takes a slice in one go. When it's nearly full, older parts must be shortened or removed. In Claude Code, you can see this bar. Type slash context. And the reply is written into the same window, after everything else. A full window leaves little room to answer, or to think.

Using the window well

Four habits. One. Give the agent what it needs, not everything you have. Two. Watch out for huge outputs. Ask for a summary, or a search, instead. Three. In a long session, say anything important again. Four. When a session feels slow or forgetful, check how full the window is.

The system prompt

Leila uses Claude in two places. The app on her phone, and the help chat on a shop's website. Same model. On her phone, it chats about anything. On the shop's site, it talks only about orders and returns. Is the shop using a special model? Usually not. The model only sees the request. So the difference must be in the request. Something she never typed, and never sees. Instructions. That hidden first message is called the system prompt. The app puts it at the start of every request. It says who the assistant is. You are the customer assistant for a shoe shop. Its rules, what to help with, and what to refuse. How to answer. Useful facts, like today's date. And its tools. That's why the same model can be a shop assistant, a language tutor, or a coding agent. Same model. Different first message. An agent's system prompt is long. Often thousands of tokens. Claude Code's tells the model to read a file before changing it, to keep changes small, to check the result, and to be careful with commands that delete or send things. And because it's in every request, it takes part of the window before you type a word.

Who the model listens to

Models are trained to give the system prompt the most weight. Then your messages. And less weight to text that merely arrives, like a web page the agent has read. But careful. This order comes from training, not from a lock. A clever message, or text hidden in a document, can sometimes talk a model around its system prompt. You can usually add to the start of the context yourself. Chat apps call it custom instructions. Agents have instruction files that load at the start of every session. Text that comes first, every time.

Using instructions well

Four habits. One. Put what's true every time in custom instructions. Your role, your style, your language. Two. Keep them short. They're in every request, so every line costs tokens. Three. When an app won't do something, it's often the system prompt, not the model. Four. Don't treat the system prompt as a security wall. Real limits come from the program around the model.

When the window fills up

Leila's agent has worked on her report for two hours. It started sharp. Now it's slower. It has redone something she already approved. And a message says, the conversation was compacted. Two different things have gone wrong. Both come from the context window. When the window is nearly full, the app has to make room. It could drop the oldest messages. But they often hold the most important thing. Your original request. So most agents use compaction. The model writes a summary of the conversation, and the summary replaces the long history. In Claude Code, it happens on its own. Or you can type slash compact, and say what to keep. So is nothing lost? What does a summary of two hours look like? A page, maybe. Two hours had every file, every command, and little things like, use the blue from last year's report. A summary keeps the gist, and loses details. Like the approval of her chart.

Why a crowded window works worse

But it was also slower and vaguer before it compacted. That's the second problem. A two thousand twenty three study, called Lost in the Middle, put one useful fact in the middle of a long pile of documents. Models found it less often. Newer models are much better. But people who use agents still see it. A long, cluttered context tends to make answers worse. Some call it context rot. And a long session is cluttered. Old files, long outputs, failed attempts.

Keeping the context clean

So keep in the window what the task needs, and little else. One task per session. In Claude Code, slash clear starts an empty conversation. Compact on purpose, with a note of what must survive. Keep plans and decisions in a file. A file survives compaction, and a new session. Search instead of reading every file whole. And hand side tasks to a helper agent, with its own window.

Using long sessions well

Four habits. One. Start a new session for a new task. Two. Compact before the window decides for you, and say what to keep. Three. Put decisions and plans in a file. Four. If an agent forgets something it clearly knew, check for a full window first.

Recap

Let's put it together. The model remembers nothing. The app sends everything, every time. That is the context, and it has to fit in the window. The system prompt comes first, and shapes it all. A full window gets compacted. So keep it clean. Next time, tools and the loop. Exactly how an agent asks for an action, and how the program around it answers. The lessons and quizzes for this course are free on knaph dot com. See you in the next episode.