Skip to content
intermediate

How LLM Instructions Work: Tokens, Messages, Roles, and Context

You tighten the system prompt. You add the word "always." You move the rule to the top. The model still ignores it, and you have no idea which part of the…

Published 2026-09-11Updated 2026-09-128 min read
Close-up of DeepSeek AI interface on a dark screen highlighting chat functionality.
Close-up of DeepSeek AI interface on a dark screen highlighting chat functionality. Photo by Matheus Bertelli on Pexels.

You tighten the system prompt. You add the word "always." You move the rule to the top. The model still ignores it, and you have no idea which part of the request actually won.

The problem is usually not your wording. It is the mental model behind the wording. Most of us carry a quiet assumption that we are sending a message to a reader who understands roles, priorities, and intent. That assumption is wrong in a specific, useful way. Once you replace it, most instruction failures stop looking mysterious and start looking mechanical.

Here is the invariant this article will prove: the model consumes one serialized, ordered sequence of tokens, and it predicts the next token over all of it. Your message list is an abstraction that gets flattened before the model ever runs. Instructions are not commands. They are context that shifts a probability distribution.

The Model Never Sees Your Messages

A left-to-right flow shows system and user message objects entering a chat template, becoming one serialized sequence with role markers and content, then entering next-token prediction; a side annotation indicates that role behavior is learned rather than enforced permission.
The model does not consume message objects directly: a chat template serializes them into ordered tokens, and the model predicts from that entire sequence.

When you call a chat API, you send a structured list:

[
  {"role": "system", "content": "You are a terse assistant."},
  {"role": "user", "content": "Summarize this contract."}
]

That list is a developer-facing abstraction. It is not what the model consumes.

Between your list and the model sits a chat template: a translation layer that flattens the message objects into one contiguous string using model-specific special tokens and delimiters. A template for one model family might render the above as something close to:

<|im_start|>system
You are a terse assistant.<|im_end|>
<|im_start|>user
Summarize this contract.<|im_end|>
<|im_start|>assistant

The exact tokens differ by model. The structure does not: role markers become tokens in the sequence, and the model reads the whole thing as text.

This is where the base-model versus instruct-model distinction earns its keep. A base model trained on raw text predicts the next token and will happily continue your conversation formatting instead of answering. An instruct model has been fine-tuned on conversation-formatted data, so it learned to treat the role delimiters as a signal to respond rather than continue. The role structure is not a permission system. It is a pattern the model was trained to recognize.

Now the qualification that keeps this model honest. The serialized sequence is what the model reads, but the serialization itself is a learned signal, and the surrounding application can enforce things the model cannot. A framework may reject a malformed tool call before it reaches your handler. A provider may apply its own instruction treatment. Your own code may validate a schema and retry. So the correct statement is not "roles are meaningless." It is: the model's behavior toward roles is learned, not guaranteed, and any guarantee you need has to live outside the model.

Swap models or templates and the same message list can produce different behavior, because the delimiters, ordering conventions, and formatting changed underneath you.

Knowledge check

Check your understanding

Answer this question before you continue.

An application sends the same message list through two different model families and gets different behavior. Which explanation best fits the article's model?
Scenario Interpretation

Focus: Explain how a chat message list becomes model input and why role behavior is learned rather than guaranteed.

Tokens Are the Unit of Everything

Text does not reach the model as text. A tokenizer splits it into sub-word units from a fixed vocabulary. A token might be a whole word, a fragment, a single character, or punctuation. Spaces often attach to the front of the following token, so tokenization is not word-aligned.

The model operates on token IDs. It never sees characters. This matters more than it sounds.

Tokenization boundaries can split a word, an identifier, or a delimiter. If you instruct a model to emit an exact JSON key, an exact format string, or an unusual identifier, the tokenizer may carve that string into pieces that do not match your intuition about "one thing." Exact-match instructions are where tokenization stops being trivia and starts being a bug source.

Token count is also the real currency. It sets cost, and it consumes the context window before generation even begins. A short instruction is not automatically cheap, and a long instruction is not automatically precise. Measure tokens, not characters. If you have ever wondered why a "simple" prompt costs more than expected, the answer is usually in the rendered sequence, not the source text you typed.

Knowledge check

Check your understanding

Answer this question before you continue.

Two instructions have similar character lengths, but one contains an unusual identifier and exact JSON key. What should you inspect to explain unexpected cost or formatting failures?
Comparison Reasoning

Focus: Use tokenization and token count to reason about exact-format reliability and context cost.

Roles, Delimiters, and the Instruction Hierarchy

System, user, and assistant are conventions encoded by special tokens during fine-tuning. They are not hard-coded permissions. What they produce is an instruction hierarchy: a trained tendency to weight system-level content more heavily than user content, and user content more heavily than stray text.

That tendency is real and useful. It is also a bias, not a guarantee. Treating the system prompt as a security boundary is the classic mistake here. It is a strong prior, not an access control list. If your threat model depends on the model refusing something because the system prompt said so, you do not have a threat model. You have a hope.

Two more mechanics deserve attention.

First, assistant turns in the history are model output replayed as context. They shape the next prediction as strongly as user text does. A bad earlier answer does not just sit in the log; it becomes evidence the model builds on.

Second, tool results, retrieved documents, and injected chunks usually arrive as additional roles or delimited blocks. That extends the same mechanism rather than replacing it. A retrieved document is not "knowledge the model has." It is more tokens in the sequence, competing for weight with everything else.

Knowledge check

Check your understanding

Answer this question before you continue.

Which statement correctly applies the article's warning about system instructions?
Misconception Check

Focus: Distinguish a learned instruction hierarchy from an enforceable security boundary.

Ordering and Recency: Why Position Changes Behavior

The model attends across the whole sequence, but training and formatting conventions create position-sensitive weighting. In practice, content near the end of the sequence tends to influence the next token more strongly. This is why late-injected context often overrides earlier instructions, and why appending a constraint at the end frequently beats burying it at the top.

The deeper problem is not position alone. It is competing text. Two instructions that conflict do not merge into a compromise. They compete, and the winner depends on position, specificity, and phrasing. If your system prompt says "be concise" and a late user message says "explain in full detail," you have not given the model a rule. You have given it a fight.

A practical pattern that holds up:

  • Put stable behavioral rules early.
  • Put task-specific and high-salience context late.
  • Do not duplicate conflicting instructions in both places.

Ordering tricks are not enforcement. If a constraint must hold, validate the output. Do not rely on the model's position bias to protect you.

Knowledge check

Check your understanding

Answer this question before you continue.

A system prompt says “be concise,” while a later user request says “explain in full detail.” According to the article, what is the best diagnosis?
Scenario Interpretation

Focus: Predict how ordering and competing instructions can affect the model's next-token behavior.

The Context Window Is a Budget, Not Memory

Everything competes for the same window: system instructions, conversation history, retrieved documents, tool output, and the tokens being generated. It is one finite pool, and every token spends from it.

Longer context is not strictly better. Attention is spread across more tokens, and marginal returns diminish. Irrelevant tokens are not neutral. They consume budget and dilute the signal the model needs. This is the "attention budget" framing, and it is the right one: context is a finite resource with diminishing returns, not free storage.

Keep one distinction sharp. Transient context is what the model sees on this call. Persistent state is what your application stores across turns. They are different systems with different failure modes. Confusing them is how you end up debugging the model when the real bug is in your message assembly.

Once you are programmatically choosing and trimming what enters the window per call, you have crossed from prompt engineering into context assembly. That is the next thing worth learning.

Debugging Instruction Failures with This Model

The mental model becomes useful when it turns into a procedure. When an instruction fails, work the steps in order.

  1. Dump the rendered prompt. Not the message list you wrote. The exact string or token sequence your template produces. Debug the artifact, not your intention.
  2. Locate the competing text. Find every instruction that touches the same behavior and check whether they conflict.
  3. Check position. Is the instruction you care about early, late, or buried under retrieved content?
  4. Check tokenization on anything exact: keys, formats, identifiers, delimiters.
  5. Change one variable at a time. Treat each failure as evidence about what the model actually weighted.

The most common misdiagnosis is blaming model capability when the real cause is a template that silently dropped, reordered, or reformatted a message. I have watched that happen more than once, and it always looks like a model problem until you print the sequence.

What to Do Next

Before you rewrite a single word of your prompt, inspect the rendered sequence. Find the competing text. Check position and tokenization. Most instruction failures are not wording failures. They are context-assembly failures wearing a wording costume.

Your next move: take one prompt that has been misbehaving, dump its fully rendered form, and read it as the model reads it — one ordered sequence, no roles, no privileges, just tokens competing for weight. Then decide what actually needs to change.

Knowledge check

Final check

Finish the article by checking the ideas you just learned.

A model appears to ignore a rule, but the developer has inspected only the original message list. What should be done first?
Question 1 of 2Debugging

Focus: Apply the article's debugging procedure by inspecting the rendered prompt before changing wording.

An application stores conversation history across turns, but sends only a trimmed subset on the next call. Which interpretation matches the article?
Question 2 of 2Comparison Reasoning

Focus: Distinguish transient context from persistent application state and reason about context-window tradeoffs.

References

  1. Messages and Special Tokens · Hugging Facehuggingface.co
  2. LLM Fundamentals | Microsoft Learnlearn.microsoft.com
  3. Effective context engineering for AI agentswww.anthropic.com
  4. Context engineering in agents - Docs by LangChaindocs.langchain.com
8sources checked
8source domains
10searches run

Research updated Sep 11, 2026

Related sites

Build the foundations behind advanced AI systems

Use LearnLLMFast for practical LLM application foundations and LearnPyFast for the Python mechanisms that support implementation work.

LLM tutorialstutorial

LearnLLMFast

Practical LLM tutorials for builders who want to understand prompting, workflows, agents, and AI applications.

LLMAIBuilders
Visit LearnLLMFast
Python tutorialstutorial

LearnPyFast

Beginner-friendly Python tutorials, examples, and learning paths for practical programming foundations.

PythonProgrammingBeginners
Visit LearnPyFast

Keep exploring

Related AI engineering tutorials

Continue with adjacent system layers, implementation patterns, and current AI engineering ideas.