Sub topic 1
What the context window is and why context engineering matters
Prerequisites
This chapter assumes you have called a large language model (LLM) through a chat or API interface. You should know what a prompt and a system prompt are, and have a rough idea of what a token is. You should also be able to read short Python snippets.
The context window
A context window is the maximum amount of text a model can consider in a single call. It is measured in tokens, which are the chunks of text the model reads and writes. Everything the model uses to produce its answer must fit inside this window. That includes your instructions, any documents you supply, the conversation so far, and the answer the model is generating.
The model has no other view of your situation. It does not remember earlier calls unless you include them again. If a fact is not in the context, or in what the model learned during training, the model cannot use it.
This means the contents of the context determine the behavior of the model. Two calls with the same model and different contexts can produce very different results.
Prompt engineering and context engineering
Prompt engineering focuses on wording a single instruction well. Context engineering is wider. It is the practice of choosing, ordering, and maintaining all the information the model sees on each call.
| Aspect | Prompt engineering | Context engineering |
|---|---|---|
| Main question | How should I phrase this instruction? | What information should be present at this step? |
| Scope | One prompt, usually written by hand | Everything assembled for each call |
| Changes over time | Mostly fixed | Rebuilt as a task proceeds |
| Typical tools | Examples, role, format rules | Retrieval, trimming, summaries, memory |
The difference matters most in agents and long conversations. In these, the context grows with every step, and something must decide what stays.
Why it matters
Three limits make this a real engineering problem.
- The window is finite. If you exceed it, the call fails or older content is cut off.
- Cost and speed grow with size. You pay for input tokens, and longer inputs take longer to process.
- More is not always better. Irrelevant, outdated, or conflicting content can lower the quality of answers even when it fits. Later chapters cover these failures in detail.
The goal is the smallest set of information that lets the model do the current step well.
A small example
The function below shows the core idea. It does not send everything it has. It checks a budget and includes only what fits.
def fits(parts, window, output_reserve):
# Rough heuristic: about 4 characters per token for English text.
used = sum(max(1, len(p) // 4) for p in parts)
return used <= window - output_reserve, used
parts = [
"You are a helpful support assistant.",
"Customer asked about refund policy last week.",
"Refund policy: purchases can be returned within 30 days.",
"What is your return window?",
]
ok, used = fits(parts, window=8000, output_reserve=1000)
print(ok, used)Here everything fits, so the call can go ahead. With a long history and many documents, it would not. Then you must decide which parts to drop or shorten. That decision is the subject of the rest of this tutorial.
What you will do next
The following chapters take the context apart and manage each piece. You will count tokens, learn the parts of a context, and select relevant content. You will then handle failures, ordering, retrieval, history, compaction, memory, subagents, and caching. The final chapter shows how to test whether a change helped.
Exercise
A teammate says, "Our model has a very large context window, so we can include the full chat history and every document on every call." Give two reasons this can still be a poor choice.
Show answer
Cost and latency grow with the number of input tokens, so sending everything on every call is slower and more expensive. Quality can also drop, because irrelevant, outdated, or conflicting content can distract the model from what matters for the current step.