Skip to content
47% off: the complete course for $79 (reg. $149)Order now →
Agent Builder
Module 1Full course9 min read

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 contextWhere it comes fromTypical size
System promptThe agent's built-in instructionsSmall to medium
Tool definitionsNames, descriptions, and schemas of every available toolGrows with each tool or MCP server you add
Project instructionsFiles like CLAUDE.md or AGENTS.mdSmall, if you keep them tight
Conversation historyYour messages and the agent's repliesGrows every turn
Tool resultsFile contents, command output, search results, web pagesOften the largest part by far
ReasoningThe model's thinking on the current turnVaries with task difficulty and effort
The outputWhat the model writes this turnAlso 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.