Module 1 · Core principles

How an LLM processes a prompt

7 min read

A prompt is not read the way a human reads an instruction. It is split into tokens, formatted by a template, and then the model predicts, one token after another, the most likely continuation given everything before it. Understanding this mechanism explains most prompt engineering rules: why order matters, why examples are so powerful, why a model "forgets" an instruction buried in the middle of a long text, and why numbers are a problem. This lesson opens the hood.

The course lab

Every technique in the course is measured in Quill, the companion project, on tasks with verifiable answers: extracting fields from purchase orders, solving arithmetic problems, classifying contract clauses, finding a piece of information in a long document, resisting a prompt injection. Two engines:

  • Claude, through the Anthropic API: the target of the production examples (module quill/claude.py, tested offline with a fake client);
  • a small local model, Qwen2.5-1.5B-Instruct (Apache 2.0 license), on which every technique is measured at no API cost, on a consumer GPU.

A small model is an excellent test bench: it forgives nothing. What a large model understands despite an approximate prompt, a small model misses — and the effect of each technique becomes visible. The orders of magnitude change with a model like Claude; the principles remain.

The course data is in French, like the customers of the fictional Nimbus store; the prompts shown are therefore in French, with their meaning given in the text.

Step 1: splitting into tokens

The model does not see letters but tokens, pieces of words from a fixed vocabulary. With the Qwen2.5 tokenizer:

TextTokensSplit
Bonjour1Bonjour
échéance (due date)3é ché ance
pénalités (penalties)4p én al ités
BC-32816BC - 3 2 8 1
12 450,00 €10one token per digit, plus the space, the comma and the symbol
2026-03-1510one token per digit and per dash

Three practical consequences:

  • Cost and length are counted in tokens, not words: a French text often counts more tokens than its English translation, and each model family has its own tokenizer (Claude Sonnet 5 and Claude Haiku 5.5, for example, count about 30% more tokens than their predecessors for the same text).
  • Numbers are fragmented: this tokenizer splits each digit separately. The model does not "see" 12,450 as a quantity, but as a sequence of symbols; adding two amounts means manipulating digit sequences. This is one of the reasons calculations are handed to code (lesson 2.3).
  • Identifiers (references, codes, numbers) are cut into arbitrary pieces: copying them exactly is a task, not a given.

Step 2: the conversation template

The API receives a list of messages (system, user, assistant). The model receives a single text, formatted by its chat template. Here is what Qwen2.5 actually sees for a Quill extraction prompt:

<|im_start|>system
Tu es un assistant comptable. Tu extrais les informations clés des bons de commande pour qu'un programme les enregistre : la sortie doit être exacte et exploitable telle quelle.<|im_end|>
<|im_start|>user
<bon_de_commande>
Bonjour à l'équipe achats,
Veuillez trouver ci-dessous notre bon de commande n° BC-7219 auprès de Inox et Compagnie.
Le montant total hors taxes s'élève à 79200 francs suisses, soit 15 840,00 francs suisses de TVA.
…
</bon_de_commande>

Réponds uniquement par un objet JSON avec exactement ces clés : …<|im_end|>
<|im_start|>assistant

(The system message sets an accountant-assistant role; the user message contains the purchase order between tags, then the output specification.) 335 tokens in total. Roles are only text tags the model recognizes because it was trained with them. With Claude, the template is applied server-side and is not visible, but the principle is the same: everything you send — system prompt, tools, messages — becomes a single sequence the model reads from start to end.

def render(tokenizer, messages: list[dict], tools: list[dict] | None = None) -> str:
    """Le texte exact envoyé au modèle (chat template), terminé par l'ouverture du tour de l'assistant."""
    return tokenizer.apply_chat_template(messages, tools=tools, tokenize=False, add_generation_prompt=True)

Step 3: prediction, token by token

The model computes, for every possible token, a probability of being the next one. It picks one, appends it to the sequence, and starts again. Three ideas follow:

  • Everything before conditions what comes next. An example answer in JSON makes JSON more likely; a sentence starting with "Here is the extracted information" makes likely… some text, then a code block. The prompt does not "give an order": it creates a context in which some continuations become likely.
  • The model never goes back. An error at the start of the answer conditions everything else. This is why asking it to reason before concluding improves answers (lesson 2.1): the conclusion is then generated after the reasoning, and can rely on it.
  • The choice of token can be deterministic or random. With greedy decoding (temperature 0), the most likely one is always taken: the output is reproducible. With a positive temperature, it is drawn according to the probabilities: the output varies. Quill uses greedy decoding to compare two prompts. On recent Claude models, sampling parameters can no longer be set (Claude Opus 5.5 rejects temperature): variability is part of the system, which is one more reason to evaluate on dozens of examples, never on a single one.

Step 4: attention and the context window

To predict each token, the model "looks at" all previous tokens through the attention mechanism, within the limit of its context window (32,768 tokens for Qwen2.5-1.5B; up to one million for current Claude models). This attention is not uniform: depending on models and tasks, information placed at the start, end or middle of a long context is not retrieved with the same reliability. Module 3 measures this effect and derives placement rules from it.

What this changes when writing a prompt

MechanismResulting ruleLesson
The prompt creates a context of probabilitiesShowing (examples) is stronger than asking1.2, 2.2
Everything becomes a single sequenceClearly delimit instructions and data1.3, 4.4
Prediction without going backReason before concluding; check afterwards2.1, 2.4
Fragmented tokens and digitsSpecify formats; hand calculations to code1.4, 2.3
Uneven attention over a long contextPlace documents and question with care3.2
Non-deterministic outputEvaluate on a set of cases, not one example5.2

Key takeaways

  • The model sees tokens, not words: a five-digit number can be ten tokens.
  • Messages (system, user, tools) become a single text, formatted by a template.
  • The model predicts one token after another, without going back: context creates probabilities, it does not give orders.
  • Decoding can be deterministic or sampled; on recent Claude models you no longer set it: evaluate on dozens of cases.
  • Attention over a long context is not uniform: where information sits matters.

Practice

  1. With quill.local.render, display the exact text of one of your prompts as Qwen2.5 sees it, then count its tokens with local.count_tokens.
  2. Count the tokens of the same paragraph in French and in English: what is the gap?
  3. With the Claude API, use client.messages.count_tokens on your real system prompt and compute its cost per million requests.

Resources