Generative AI & Agentic AILLM fundamentals and prompt design

Tokens and context windows: budget a real document task

PK
Pankit Kumar
Sr. Data Scientist at Parexel (a Goldman Sachs–backed company) · 20 September 2026 · 2 min read
Technically reviewed by Ishaan Sharma
In this article (5 sections)

A context window is shared space. System instructions, conversation history, retrieved evidence, tool schemas and generated output all consume it. Sending a document that fits by itself can still overflow once the application adds everything else.

Reserve the budget before selecting evidence

The offline foundations lab uses whitespace-delimited teaching units to make the arithmetic inspectable.

python
from foundation_cases import context_budget_case

result = context_budget_case()
assert result["context_limit"] == 1000
assert result["evidence_budget"] == 500
assert result["selected_sections"] == ["contract", "exceptions"]
assert result["provider_tokenizer_executed"] is False

From a 1,000-unit limit, the fixture reserves 120 for system instructions, 80 for the user request, 220 for output and 80 as margin. That leaves 500 for evidence. The 180-unit contract and 210-unit exception section fit; adding the 260-unit appendix would exceed the budget.

These are not provider token counts. Production code must use the tokenizer and context rules supported by the selected model/version.

Budget for the whole serialized request

Measure the exact messages and tool definitions the API receives. Reserve output explicitly; a request that fills the input window can leave no room for the answer. Include a margin for escaping, wrappers or provider-specific formatting. Log the model/version, tokenizer/version and counted components with the run.

When evidence exceeds the budget, do not truncate blindly. Rank sections against the question, preserve headings and source identifiers, and split work where independent subproblems exist. Summaries can reduce size, but they become derived evidence that needs provenance and checks for lost constraints.

Test important boundaries

Create cases just below, at and above the limit. Include long tables, non-English text, code, repeated metadata and a large tool schema. Verify that the application rejects or reduces an oversized request before an expensive call. Measure answer quality as context grows; fitting within a limit does not prove the model uses every part reliably.

The Generative & Agentic AI course connects context budgeting to RAG ingestion, evaluations and cost control. A deliverable should include its budget calculation and truncation policy.

Exercise

Choose a real public document and three questions. Count the complete serialized requests with your selected model’s supported tokenizer, allocate output/margin, and compare section selection with naive first-*n* truncation on a fixed evidence set.

Continue learning

This article is part of the LLM fundamentals and prompt design sequence. Use the neighbouring tasks when you need the prerequisite or the next application.

Reference: OpenAI tokenizer tool.

PK
Pankit Kumar
Lead Instructor, NeuraPath Academy

Pankit Kumar has 10 years in Data Science & AI, building and shipping production systems in regulated pharma and clinical environments. He is a freelance trainer at Boston Institute of Analytics, AnalytixLabs and Scaler, and has taught this material to thousands of working professionals.

This article is part of our Generative & Agentic AI programme — 3 months. Add practical GenAI, retrieval and agent-building skills to your existing toolkit.

Explore Generative & Agentic AI
Counselling is free · no obligation

Not sure which programme fits?

Tell us your background and we will map it to the right entry point — including saying so when a cheaper programme is the better fit. A counsellor replies within one working day.