Demonstration ordering

Also known as: example ordering

On this page 7

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.

MechanismWhat happensTypical symptom
Recency biasLast examples weigh mostAnswers copy the final label
Majority label biasFrequent labels dominateOne class is overpredicted
Format anchoringFinal example sets the styleLength and tone mimic it
Positional attention decayEarly examples receive less attentionFirst examples seem ignored
Label clusteringSame labels grouped togetherPredictions drift toward one group
Mechanisms behind order sensitivity

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.

StrategyHow it worksBest suited for
Random orderShuffle examples onceQuick baselines only
Similarity ascendingMost similar example placed lastDynamic few-shot retrieval
Balanced interleavingAlternate labels evenlyClassification with few labels
Easy to hardSimple cases first, complex laterReasoning and multi-step tasks
Entropy-based selectionPick orders with balanced predictionsSmall models, no labeled dev set
Validation searchTest permutations on held-out dataProduction prompts with stable inputs
Ordering strategies and when to use them

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.

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 choiceQuestion it answersTypical impact
Demonstration orderingIn which sequence do examples appearModerate to large on small models
Demonstration selectionWhich examples are includedUsually the largest effect
Number of shotsHow many examples are usedDiminishing returns after a point
Output format instructionWhat structure answers followLarge on parsing reliability
Prompt ensemblingHow several prompts are combinedReduces variance across orders
Ordering versus neighboring prompt design choices

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.

  1. Fix the example set and the instruction text before testing orders.
  2. Build a held-out evaluation set of at least 50 to 200 realistic inputs.
  3. Evaluate 10 to 30 sampled permutations and record mean accuracy and spread.
  4. Choose the best order only if it beats the mean by more than the run-to-run noise.
  5. 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.

MistakeCauseFix
Grouping examples by labelConvenient authoring orderInterleave labels evenly
Trusting one lucky orderSingle evaluation runCompare several permutations
Reusing order across modelsAssumed transferabilityRe-evaluate per model version
Ignoring the final exampleUnderestimated recency biasMake the last example representative
Overfitting to a tiny dev setToo few test inputsUse a larger held-out set
Ordering mistakes, causes and fixes

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.