Contrastive examples in prompt engineering is the technique of providing the language model with both positive (correct) and negative (incorrect) demonstrations — showing not just what good output looks like, but also what bad output looks like and why, enabling the model to learn sharper decision boundaries for the target task.
Why Contrastive Examples Work
- Standard few-shot prompting shows only positive examples — the model sees what to do, but not what to avoid.
- Contrastive examples add negative demonstrations — "here is a wrong answer and why it's wrong" — helping the model understand the boundaries between correct and incorrect responses.
- This is especially valuable for tasks with subtle distinctions where the model might otherwise confuse similar categories or make common errors.
Contrastive Example Format
Good example:
Input: "The battery lasts all day"
Label: Positive
Why: Describes a desirable product feature.
Bad example:
Input: "The battery lasts all day"
Label: Negative
Why WRONG: Despite mentioning "lasts," this is a
positive statement about battery life, not negative.
When to Use Contrastive Examples
- Fine-Grained Classification: Distinguishing between closely related categories — e.g., sarcasm vs. genuine praise, factual claims vs. opinions.
- Error Correction: When the model consistently makes a specific type of mistake — show the mistake explicitly and explain why it's wrong.
- Boundary Cases: Tasks with ambiguous edge cases — contrastive pairs on either side of the decision boundary help the model calibrate.
- Style Requirements: Show both the desired writing style AND common style mistakes to avoid.
Contrastive Prompting Strategies
- Paired Examples: For each positive example, provide a closely matched negative example — same topic or structure, but different correct label.
- Near-Miss Examples: Show examples that are almost correct but wrong in a specific way — teaches the model what subtle features matter.
- Error Annotation: Include an explanation of WHY the negative example is wrong — the reasoning helps the model internalize the distinction.
- Before/After Pairs: Show a bad output and its corrected version — teaches the model what transformations to apply.
Benefits
- Accuracy: Contrastive examples can improve classification accuracy by 5–15% on difficult tasks compared to positive-only few-shot prompting.
- Reduced Ambiguity: Explicitly showing the boundary between categories reduces misclassification of edge cases.
- Error Awareness: The model learns to actively avoid common mistakes rather than just mimicking correct patterns.
Practical Tips
- Don't use too many negative examples — a ratio of 1 negative per 2–3 positive examples works well.
- Make negative examples realistic — they should represent actual mistakes the model might make, not obviously wrong cases.
- Always explain WHY the negative example is wrong — unexplained negatives can confuse the model.
Contrastive examples are a high-impact prompt engineering technique — by teaching the model what to avoid alongside what to produce, they create sharper, more discriminating few-shot learners.
Explore 500+ Semiconductor & AI Topics
From EUV lithography to CUDA optimization — search the full knowledge base or chat with our AI assistant.