What Is a Context Window? Tokens, Limits and What Fills It
A context window is the maximum number of tokens a model can handle in a single request, counting both what you send in and what it writes back. Everything the model can use for its answer has to fit inside that window. If you want the model to know something, it must be in the window.
Tokens, not characters
Models read tokens, which are chunks of text. A token might be a whole short word, part of a longer word, a punctuation mark or a piece of whitespace. In English, a token is roughly three or four characters on average, but this varies. Code, other languages, JSON and numbers often use more tokens per character than plain English prose. Different model families use different tokenizers, so the same text can have different counts on different models.
Because of this, never estimate with a fixed rule when accuracy matters. Count with the provider's tool. Post 5 shows how to do it with the Claude API.
What counts against the window
Many beginners think only the user's message counts. In reality, the window holds everything below:
- The system prompt.
- The tool definitions (names, descriptions and JSON schemas).
- The whole conversation history, including earlier assistant replies.
- Tool calls and tool results from earlier turns.
- Any documents or retrieved passages you added.
- The model's own output for this call, including any reasoning it produces before answering.
The last point surprises people. The output limit and the input size share the window. If your input already fills almost all of it, there is little room left for the answer.
Reading the limit from the API
Window sizes differ by model and change over time, so do not hard-code a number from a blog post. The Claude API has a Models endpoint that reports the limits for a model. The following script prints them.
import anthropic
client = anthropic.Anthropic()
model = client.models.retrieve("claude-sonnet-5-5")
print("model:", model.id)
print("max input tokens:", model.max_input_tokens)
print("max output tokens:", model.max_tokens)
Run it and you will see the context size and the output cap for that model. Use these values in your own code instead of constants, so an upgrade to a different model does not silently break your budgets.
Bigger windows do not remove the problem
Modern models offer very large windows. That is useful, but it does not mean you should fill them. There are three reasons.
- Cost: you pay for every input token on every call. In an agent loop, the same history is re-sent on each turn, so the cost multiplies.
- Latency: more input usually means a slower first response.
- Quality: as the context grows, a model can become less reliable at finding and using a specific detail. The next post covers this.
A mental model
Think of the window as a desk. A large desk lets you spread out more papers, but a cluttered desk still makes it harder to find the one page you need. Good context engineering keeps on the desk what is needed for the current step and files the rest where it can be fetched.
Checking your own usage
Every response from the Claude API reports how many tokens it used. Print this in development so you build a feel for the numbers.
import anthropic
client = anthropic.Anthropic()
response = client.messages.create(
model="claude-sonnet-5-5",
max_tokens=2000,
system="Answer in one sentence.",
messages=[{"role": "user", "content": "What is a context window?"}],
)
print(response.usage.input_tokens, "input tokens")
print(response.usage.output_tokens, "output tokens")
You should see a small input count (the system prompt plus the question) and an output count for the one-sentence answer. Output tokens may include reasoning the model did before answering, so the number can be larger than the visible reply.
Key points to remember
- The window is shared by input and output.
- Tools, history and tool results count, not just the latest user message.
- Read limits from the API rather than hard-coding them.
- Treat the window as a budget even when it is large.
Comments
No comments yet. Have a question or a tip? Be the first to share it.