In-Context Learning › Improving ICL Performance
Language AI HandbookAll chapters In-Context Learning › Improving ICL PerformanceOne section · Sections in this chapter7 / 11 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
Researchers have developed numerous techniques to enhance in-context learning beyond simple few-shot prompting.
Chain-of-Thought Prompting
For reasoning tasks, showing intermediate steps significantly improves performance. Instead of just input-output pairs, demonstrations include the reasoning process:
# Standard few-shot
standard_prompt = """
Q: Roger has 5 tennis balls. He buys 2 more cans of tennis balls. Each can has 3 balls. How many tennis balls does he have now?
A: 11
Q: The cafeteria had 23 apples. If they used 20 to make lunch and bought 6 more, how many do they have?
A:"""
# Chain-of-thought few-shot
cot_prompt = """
Q: Roger has 5 tennis balls. He buys 2 more cans of tennis balls. Each can has 3 balls. How many tennis balls does he have now?
A: Roger started with 5 balls. He bought 2 cans with 3 balls each, so 2 * 3 = 6 new balls. Total: 5 + 6 = 11.
Q: The cafeteria had 23 apples. If they used 20 to make lunch and bought 6 more, how many do they have?
A:"""Standard Few-Shot: ---------------------------------------- Q: Roger has 5 tennis balls. He buys 2 more cans of tennis balls. Each can has 3 balls. How many tennis balls does he have now? A: 11 Q: The cafeteria had 23 apples. If they used 20 to make lunch and bought 6 more, how many do they have? A: Chain-of-Thought Few-Shot: ---------------------------------------- Q: Roger has 5 tennis balls. He buys 2 more cans of tennis balls. Each can has 3 balls. How many tennis balls does he have now? A: Roger started with 5 balls. He bought 2 cans with 3 balls each, so 2 * 3 = 6 new balls. Total: 5 + 6 = 11. Q: The cafeteria had 23 apples. If they used 20 to make lunch and bought 6 more, how many do they have? A:
Chain-of-thought (CoT) prompting guides the model to show its work. The reasoning trace helps the model:
- Break complex problems into steps
- Catch and correct errors mid-reasoning
- Align intermediate computations with the final answer
CoT is particularly effective for arithmetic, logical reasoning, and multi-step problems where the direct answer is hard to compute in a single step.
Self-Consistency
Rather than generating a single answer, self-consistency samples multiple reasoning paths and aggregates them through majority voting. The procedure works as follows:
- Generate different chain-of-thought responses using temperature to introduce diversity
- Extract the final answer from each response
- Return the most common answer (majority vote)
Formally, the self-consistency prediction is:
where:
- : the final predicted answer
- : the number of sampled reasoning paths
- : the answer extracted from the -th reasoning path
- : an indicator function that equals 1 if answer matches , and 0 otherwise
- : selects the answer that maximizes the vote count
This approach uses the intuition that correct reasoning paths, while potentially different in details, converge on the same answer. Incorrect paths are more likely to diverge, creating different wrong answers that split the vote. With enough samples, the correct answer accumulates more votes than any single incorrect answer.


Calibration and Confidence
ICL predictions can be overconfident or poorly calibrated. Several techniques address this:
- Probability calibration: Adjust output probabilities using held-out validation data
- Verbalizing uncertainty: Ask the model to express confidence in its answer
- Null prompts: Compare predictions with and without demonstrations to detect when the model is guessing
Retrieval-Augmented ICL
Instead of using a fixed set of demonstrations, retrieval-augmented approaches dynamically select examples for each test input:
- Embed the test input
- Retrieve similar examples from a large pool
- Use retrieved examples as demonstrations
This combines the benefits of similarity-based selection with the scalability of large example pools. The model effectively has access to thousands of potential demonstrations, selecting the most relevant ones at inference time.
