Context Windows, Tokens, and Cost
What fits in an agent's working memory, how tokens translate to dollars, and how to keep long-running agents sharp and affordable.
An agent that's brilliant for the first twenty minutes can become sluggish, forgetful, and expensive by the second hour. It starts ignoring instructions you gave at the beginning, re-reads files it already read, and each step costs more than the last. Nothing broke. The agent simply filled up its working memory.
That working memory is the context window, and it's measured in tokens, the same tokens that determine your bill. Understanding how it fills, what it costs, and how to keep it clean is what separates agents that run reliably for hours from agents that fall apart. In this lesson you'll see exactly what takes up space in an agent's context, how to estimate what a run will cost, how to measure real usage in code, and the handful of habits that keep long-running agents both sharp and affordable.
What the context window is
The context window is the maximum amount of text, measured in tokens, that a model can consider in a single request. Think of it as the model's desk: everything it needs for the current step must fit on the desk, because, as you learned in LLMs in Plain English, the model has no other memory.
Modern frontier models have very large windows, enough to hold several long books' worth of text. Exact sizes vary by model and change over time, so check your provider's documentation rather than memorizing numbers. Large as they are, agents fill them faster than you'd expect.
Everything counts
On every turn, the model's context includes all of the following, and every token of it counts toward the window:
| What's in the context | Where it comes from | Typical size |
|---|---|---|
| System prompt | The agent's built-in instructions | Small to medium |
| Tool definitions | Names, descriptions, and schemas of every available tool | Grows with each tool or MCP server you add |
| Project instructions | Files like CLAUDE.md or AGENTS.md | Small, if you keep them tight |
| Conversation history | Your messages and the agent's replies | Grows every turn |
| Tool results | File contents, command output, search results, web pages | Often the largest part by far |
| Reasoning | The model's thinking on the current turn | Varies with task difficulty and effort |
| The output | What the model writes this turn | Also counts against the limit |
The tool results row is the surprise for most people. When an agent reads a 2,000-line file, the entire file goes into the context and, unless the harness later trims or summarizes it, stays there for the rest of the session. A verbose test run, a long log file, or a web page full of navigation menus can each consume a large share of the window in one step.