Demonstration ordering, also called example ordering, is the sequence in which few-shot examples are placed in a prompt and the practice of choosing that sequence deliberately. In prompting for large language models, the same set of demonstrations can produce markedly different accuracy depending only on their order, so ordering is a real design variable rather than a cosmetic detail.
The effect is strongest in in-context learning with few examples and smaller models, where reported swings range from a few points to more than 30 percentage points on classification tasks. Larger, instruction-tuned models are usually more stable, but order sensitivity rarely disappears entirely.
Why the order of examples changes model output
A language model reads a prompt left to right as one token sequence, so demonstrations are not an unordered set to it. Each example shifts the model’s internal state, and the examples closest to the query tend to have the largest influence on the next prediction.
Three biases explain most of the effect. Recency bias favors labels that appear in the last demonstrations, majority label bias favors the label that appears most often, and common token bias favors answers that are frequent in pretraining data. Ordering interacts with all three, which is why a permutation that clusters one label at the end can skew predictions toward that label.
The table below lists the main mechanisms and how each one appears in practice.
| Mechanism | What happens | Typical symptom |
|---|---|---|
| Recency bias | Last examples weigh most | Answers copy the final label |
| Majority label bias | Frequent labels dominate | One class is overpredicted |
| Format anchoring | Final example sets the style | Length and tone mimic it |
| Positional attention decay | Early examples receive less attention | First examples seem ignored |
| Label clustering | Same labels grouped together | Predictions drift toward one group |
Common ordering strategies
Practitioners use several ordering strategies, from simple heuristics to search over permutations. No single strategy wins on every task, so the choice depends on the model, the label space and the available evaluation data.
A frequent and effective default is to place the most similar example last, next to the query, when demonstrations are retrieved dynamically. Another robust default is balanced interleaving, where labels alternate so that no class dominates the end of the prompt.
| Strategy | How it works | Best suited for |
|---|---|---|
| Random order | Shuffle examples once | Quick baselines only |
| Similarity ascending | Most similar example placed last | Dynamic few-shot retrieval |
| Balanced interleaving | Alternate labels evenly | Classification with few labels |
| Easy to hard | Simple cases first, complex later | Reasoning and multi-step tasks |
| Entropy-based selection | Pick orders with balanced predictions | Small models, no labeled dev set |
| Validation search | Test permutations on held-out data | Production prompts with stable inputs |
Entropy-based selection builds a small probing set from the model itself and prefers orderings whose predicted labels are evenly distributed, which avoids the need for extra labeled data. Validation search is more reliable but grows expensive, because k examples have k factorial orderings, so teams usually sample a few dozen permutations instead of testing all of them.
Demonstration ordering compared with related prompting choices
Ordering is one of several few-shot design decisions, and it is easy to confuse with selection or formatting. Separating them helps to diagnose which change actually improved a prompt.
| Design choice | Question it answers | Typical impact |
|---|---|---|
| Demonstration ordering | In which sequence do examples appear | Moderate to large on small models |
| Demonstration selection | Which examples are included | Usually the largest effect |
| Number of shots | How many examples are used | Diminishing returns after a point |
| Output format instruction | What structure answers follow | Large on parsing reliability |
| Prompt ensembling | How several prompts are combined | Reduces variance across orders |
Prompt ensembling and calibration methods offer an alternative to finding one best order: the system averages predictions over several permutations or corrects label probabilities against a content-free input. Both reduce variance caused by ordering at the cost of extra inference calls.
How to choose an order in practice
A disciplined workflow treats demonstration ordering as a parameter to be measured, not guessed. The steps below work for most production prompts that rely on few-shot examples.
- Fix the example set and the instruction text before testing orders.
- Build a held-out evaluation set of at least 50 to 200 realistic inputs.
- Evaluate 10 to 30 sampled permutations and record mean accuracy and spread.
- Choose the best order only if it beats the mean by more than the run-to-run noise.
- Re-test after any model upgrade, because order effects change between versions.
The spread across permutations is itself useful information. A wide spread signals a fragile prompt, and the better fix is often clearer instructions, more balanced examples or more shots, rather than a single lucky order.
Common mistakes and misconceptions
Most problems with demonstration ordering come from testing too little or from assuming that results transfer between models. The table maps typical mistakes to their causes and fixes.
| Mistake | Cause | Fix |
|---|---|---|
| Grouping examples by label | Convenient authoring order | Interleave labels evenly |
| Trusting one lucky order | Single evaluation run | Compare several permutations |
| Reusing order across models | Assumed transferability | Re-evaluate per model version |
| Ignoring the final example | Underestimated recency bias | Make the last example representative |
| Overfitting to a tiny dev set | Too few test inputs | Use a larger held-out set |
A frequent misconception is that modern models no longer care about order. Large models are indeed less sensitive, but research and field experience still show measurable differences, especially for long prompts, structured outputs and tasks with uneven label distributions.
A non-technical view
When you show an AI model a few examples before asking a question, the order of those examples matters. The model tends to copy the pattern it saw last. It also leans toward the answer it saw most often.
Think of a new employee who gets five sample letters. If the last three are all complaints, the next letter may sound like a complaint too. Mixing the samples well gives a fairer picture.
The main lesson is to test a few orders and keep the one that works reliably, not the one that worked once.
A concrete example
A support team uses a language model to sort incoming emails into four categories: billing, technical, cancellation and other. The prompt holds eight demonstrations, written in the order the team collected them, so the last three examples are all cancellations.
On a test set of 300 real emails, the model labels too many messages as cancellations and reaches only 71 percent accuracy. The team samples 20 permutations of the same eight examples and finds accuracy ranging from 68 to 86 percent, a clear sign of order sensitivity.
The team adopts a balanced interleaved order in which each category appears twice and no label repeats back to back, with a typical billing question in the final slot. Accuracy settles at 84 percent across repeated runs, the cancellation overprediction disappears, and the team adds an ordering re-test to its checklist for every future model upgrade.