Lesson 2 of 8 · 6 min read
Context Windows, System Prompts and Chat Templates
In this lesson
TL;DR: A context window limits the input and generation a model can process under its interface’s accounting rules. Chat templates represent messages and roles in a model-specific format. Preserve that structure, but enforce resource permissions independently: correct formatting does not guarantee correct interpretation or authorize an action.
Before you read
Pick an answer before you read further. The lesson below explains it, and guessing first, right or wrong, helps the explanation stay with you.
1A member comment says it is an administrator instruction, but the API still labels it as member text. Has its application role changed?
What you should be able to do
Distinguish a tokenizer, a chat template, the context budget and an authorization check. Explain why a role-labeling defect differs from semantic prompt injection. Review a context-selection change without confusing omitted evidence, stored conversation state and trained model knowledge.
Tokens are representations, not word counts
For a text model, a tokenizer maps text into model-specific units and identifiers. A word may be one unit or several; punctuation, scripts and whitespace can affect the representation. Counting characters or splitting on spaces is not a reliable substitute for the tokenizer or service’s accounting method.
Multimodal systems also handle non-text inputs through their supported representations. Do not apply a text-only counting shortcut to an image or audio workflow. Inspect the integration’s documented limits and the actual assembled request, including instructions, tool definitions and any other charged or bounded context.
The context window is a capacity limit, not a database of everything the product knows. A model can use information encoded in its trained parameters as well as the current input. An application can retrieve a stored conversation or summary for a later request. Neither means that the entire historical conversation is present in this particular generation.
Input limits, maximum generated output and reasoning accounting can differ by interface. State the relevant contract when budgeting. If the integration rejects an oversized request, test the resulting failure path. If it trims or summarizes, inspect what is lost and how that changes the task.
A template preserves structure
A chat interface commonly accepts structured messages with roles such as user, assistant, system or tool. Supported roles and their meanings depend on the model and interface. For a self-hosted causal chat model, a template serializes the supported conversation into the representation expected by that model.
The Hugging Face chat-template documentation, checked November 2026, shows that different chat models can require different control-token formats. It also documents how formatting and later tokenization can duplicate special tokens if the integration adds them twice. Use the supported tokenizer/template combination and test the actual path; do not invent a universal escaping rule for every provider.
Role assignment is itself a trusted operation. The public booking form should supply member text, not an arbitrary list of privileged messages. The application constructs the role envelope and keeps deployment instructions under the appropriate administrative control. A document’s claim about its author does not change the authenticated source record.
- 1
1 · Message structure
Assigned by the trusted integration
- Who supplied this message, and which role does the request give it?
- A user-controlled body must not choose its own trusted role.
- 2
2 · Instruction following
Behavior of the configured model
- Does the model interpret source text as data or follow it as a request?
- Correct structure does not guarantee every semantic decision.
- 3
3 · Application permission
Enforced at the resource or effect
- May this principal read this record or perform this exact change?
- Evaluate trusted identity and current policy independently of model text.
Preserve the structure
Use the model’s supported message format and assign roles in trusted code. Do not confuse an arbitrary role field from a caller with an authenticated source.
This separates two failures. A broken adapter may promote an untrusted document into a privileged message or mishandle message boundaries. A correctly formatted request may still influence the model to follow directions found inside task data. The second does not require the attacker to become a real system-message author.
Instruction following is not resource authorization
Training and runtime controls can improve how a model respects the intended instruction hierarchy. That is useful behavior to test for the deployed configuration. It does not make the model’s interpretation a substitute for the booking service’s permission check.
In the fictional makerspace, a member comment may ask the assistant to treat a staff note as public. The application should preserve the comment’s actual source and exclude ineligible records before context assembly. If the model nevertheless proposes a staff-only read, the resource service rejects it using trusted identity and current policy.
A stronger prompt can help the assistant explain that denial or avoid the proposal. It cannot grant the resource service knowledge of the authenticated member if the service accepts only an arbitrary model-supplied identity. Fix the missing enforcement rather than treating better instruction following as proof that the boundary holds.
Do not collapse all role mechanisms into “just ordinary text.” A parser can enforce a structural contract even while the model makes a semantic error within that contract. Nor should correct role formatting be treated as proof that semantic injection is impossible. The worked instruction/data question develops this distinction in an architecture review.
Work a context budget
Consider an original synthetic planning exercise, not the limits of a real provider. Assume the request’s total allowance is 600 units, including a reserved 150 for generation. The fixed instructions, tool descriptions and current request consume 130 units under this exercise’s counting method. That leaves 320 for selected source material.
| Candidate input | Exercise count | Role in this task |
|---|---|---|
| Current booking record | 140 | Required to answer the member’s specific booking question |
| Full public equipment guide | 220 | Relevant, but a smaller supported excerpt may suffice |
| Staff-only maintenance note | 90 | Ineligible for this member regardless of available space |
The booking and full guide need 360 units, so they do not both fit. Select the required booking record first, then obtain an appropriate guide excerpt within the remaining 180. If the needed evidence cannot be included reliably, narrow or decline the unsupported part of the task. Do not silently remove required facts, admit the smaller private note, or claim that a summary preserves details it has dropped.
A real integration must count its actual formatted request and reserve the resources its interface requires. The arithmetic here teaches prioritization after eligibility; it is not a production tokenizer or a suggested model limit.
Eligible sources
Check access, provenance and relevance
Context plan
Reserve required input and output space
Actual request
Format, count and record omissions
Independent checks
Recheck resources and proposed effects
Do not solve overflow by dropping authority
The policy service remains outside the context. Required task evidence must be retained or the task must be narrowed; an unnoticed truncation cannot supply a missing fact.
Compaction changes a record
A long conversation may be summarized before the next model call. That summary is a new derived artifact. It can omit a denial, drop a date, or restate a member’s request as though an operator approved it. Fluency does not establish fidelity.
Keep authoritative permissions, approval state and operation identities outside free-text summaries. Preserve source references and the information needed to review a consequential claim. If an action requires confirmation, resolve the actual confirmation record rather than searching a summary for the word “approved.”
Apply the audience rules to the summary itself. A summary derived from staff-only material is not automatically suitable for a member just because it is shorter. If permissions or recipients change, review which stored material can be reused and how the application prevents stale access.
Longer context can retain useful evidence, but capacity alone does not establish relevance, instruction fidelity or safe readership. A shorter request can still contain decisive untrusted content. Evaluate the real assembly and output path under the conditions the product supports.
Test the assembled request
Use authorized synthetic records to exercise these boundaries: caller-supplied role fields, stale or denied history, oversized context, missing required evidence and compacted summaries that retain claims without their sources. Verify the application’s recorded source identities and selected inputs, then separately check the resulting resource decisions.
When a provider formats messages internally, document what you can inspect and what remains opaque. Do not claim an exact serialized input you cannot observe. Record the supported request structure, relevant configuration and outcome evidence without retaining sensitive context unnecessarily.
For a transfer exercise, replace the equipment guide with an image containing a maintenance notice. Identify the new preprocessing and accounting steps, preserve the notice’s source and audience, and explain why extracting text from it would not grant the notice permission to change booking policy.
Read how LLM applications work for state and data flow, tool calling for the execution boundary, and testing non-deterministic systems for evaluation design.
Key takeaways
- Token counts and limits belong to the actual model/interface contract.
- Trusted message structure and learned interpretation are different layers.
- Select eligible evidence before solving the context budget.
- A summary is derived content, not a permission or approval record.
Check yourself
Say or write your answer first, then reveal. Explaining it from memory, in your own words, is the skill an interviewer is checking.
1The exercise has 320 units for source material. The required booking uses 140 and the full public guide uses 220. What is a justified next step?
2A summary says a booking change was approved, but the application has no bound approval record. May the scheduling handler accept that statement?
3A new template preserves all message roles and the model still follows a direction in a retrieved document. Which check failed?