News / Learn / LLM fundamentals
What is a context window?
You'll be able to explain what a context window is and plan your product's prompts to fit inside it.
In plain English
A context window is the maximum amount of text a language model can read and use at one time. It holds everything the model sees for one request: your instructions, the conversation so far, any documents you add, and the reply it is writing. The window is measured in tokens, not words.
Why it matters
Every request you send to a model must fit inside its context window. If it does not fit, the request fails or parts of your input get dropped. Knowing the limit shapes how you build chat history, document search, and file uploads.
How it works
The model does not remember anything between requests on its own. Each call starts fresh, so your code must send the instructions and any needed history again each time. Everything sent in that call counts against the window.
The window covers both input and output. If your prompt uses most of the space, the model has less room to write a reply. Leave headroom for the answer you expect, especially for long outputs like code or reports.
A bigger window does not mean the model uses every part equally well. Information in the middle of a very long input is often noticed less than material near the start or end. Put the most important content where it is easy to find, and test your prompts with real inputs.
Counting tokens before you send a request helps you avoid surprises. Most providers offer a way to count tokens, and many libraries include a tokenizer. Trim, summarize, or retrieve only the relevant parts when the input grows.
A concrete example
A simple check that trims old chat messages so the total stays under a budget. The token count here is a rough estimate of four characters per token, so keep a safety margin.
def estimate_tokens(text: str) -> int:
# Rough rule of thumb for English text. Use your provider's tokenizer for exact counts.
return len(text) // 4
def trim_history(messages: list[dict], budget: int) -> list[dict]:
# Keep the most recent messages that fit inside the token budget.
kept = []
total = 0
for msg in reversed(messages):
cost = estimate_tokens(msg["content"])
if total + cost > budget:
break
kept.append(msg)
total += cost
return list(reversed(kept))A short sketch to show the idea. Check the official docs of the tool you use.
What goes wrong
- Assuming a large window means no limits Count tokens for your typical and worst-case inputs. Set a budget that leaves room for the reply.
- Forgetting that output uses the same window Reserve tokens for the answer. Set a maximum output length in your request.
- Sending full chat history every time Trim or summarize older turns. Keep the system prompt and the latest messages.
- Pasting whole documents without need Retrieve only the sections relevant to the question and send those.
Going deeper
Learn about tokenization, how providers bill by token, retrieval-augmented generation, and conversation summarization. These techniques help you fit the right information into a limited window.
FAQ
Is the context window the same as memory?
No. The window is what the model can see in one request. Memory across sessions must be built by your application, for example by storing history and sending the relevant parts again.
What happens if my input is too long?
The provider usually returns an error. Some tools silently truncate input, which can drop important content. Check the response and handle the error in your code.
Do words and tokens match?
Not exactly. A token is often a word piece, and common words may be one token while rare words take several. Code, non-English text, and symbols often use more tokens per character.
Should I always use the largest window available?
Not always. Longer inputs cost more time and money, and relevant content can get lost in noise. Send what the task needs.
Written with AI by CapyLaunch. Spotted a mistake? Email hello@capylaunch.com.