NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Teaching Algorithmic Reasoning via In-context Learning

Hattie Zhou, Azade Nova, Aaron Courville, Hugo Larochelle, Behnam Neyshabur, Hanie Sedghi

TL;DR

While large language models (LLMs) notoriously fail at out-of-distribution algorithmic tasks like multi-digit arithmetic, this paper introduces Algorithmic Prompting—a prompting strategy providing detailed, unambiguous step-by-step algorithmic execution directly in-context. Without any weight fine-tuning, this framework teaches LLMs single skills, skill accumulation, skill composition, and tool use, achieving drastic error reductions (up to 10×) over standard chain-of-thought baselines.

What Problem Does This Paper Address?

Standard prompting techniques—such as Few-Shot learning, Scratchpad, and Chain-of-Thought (CoT)—have vastly improved multi-step reasoning in LLMs. However, they routinely fail at systematic, out-of-distribution (OOD) generalization for algorithmic tasks (e.g., adding numbers with far more digits than seen in the prompt, or calculating long binary parity).

The core issue is ambiguity: sparse reasoning steps leave multiple valid-looking hypotheses open to the model (such as ambiguous rules for carry calculation), causing the LLM to revert to pre-trained heuristics rather than systematically executing an underlying algorithm.

Methods

The authors propose Algorithmic Prompting (AP), which formulates problem solving into strictly specified algorithmic stages taught entirely via in-context learning:

  • Disambiguated Execution: In running examples, AP includes natural language instructions, explicit state variables, and detailed calculations (e.g., computing intermediate carries via explicit operations like $c = (z - (z \pmod{10})) / 10$).
  • Four-Stage Learning Framework:
  • Teaching an Algorithm as a Skill: Structuring a single algorithm (e.g., column addition, binary parity) unambiguously with tracking of processed list states.
  • Skill Accumulation: Teaching multiple distinct algorithms in one prompt (e.g., combined addition and signed subtraction) so the model chooses the correct execution path.
  • Skill Composition: Building complex algorithms on top of simpler sub-routines (e.g., multi-number addition, or converting multiplication into repeated addition).
  • Using Skills as Tools: Integrating algorithmic subroutines into broader, informal natural language reasoning tasks (e.g., math word problems) or querying specialized algorithmic models via dialogue-like interactions.

All experiments are conducted using OpenAI’s code-davinci-002 (Codex) evaluated at temperature 0.

Datasets

  • Synthetic Arithmetic Benchmarks:
  • Two-number addition (up to 19 digits)
  • Addition/subtraction with positive and negative integers (up to 14 digits)
  • Memorized/grouped multiplication (up to 7 digits)
  • Multi-number addition and multiplication-as-addition
  • Binary Parity: Binary lists evaluated up to length 30 (comparing against settings from Anil et al., 2022).
  • GSM8k (Addition-only subset): 108 math word problems from the GSM8k benchmark requiring solely addition.
  • GSM8k-Hard: A newly introduced benchmark modifying 50 problems from the GSM8k addition subset with significantly larger numerical values (average answer length of 7.2 digits vs. 2.4 digits in the original subset).

Key Insights and Ideas

  • Format Consistency is Secondary to Rule Specificity: Contrary to prior beliefs that rigid token formats drive scratchpad success, ablations show that explicit mathematical operations (like explicit modulo/carry calculations) matter far more than strict formatting.
  • Models Truly Follow the Algorithm: Unlike standard few-shot or CoT setups where corrupted prompt labels often have minimal impact, introducing systematic errors into the algorithmic prompt drops performance to near 0%. The LLM faithfully follows the prompt instructions rather than just pattern-matching surface tokens.
  • Interference Between Intuition and Rigid Algorithms: When mixing free-form mathematical reasoning with rigid algorithmic steps in the same context, the model suffers from interference—its informal reasoning accuracy degrades. Separating these roles via a dialogue-like, multi-model tool-calling setup largely alleviates this issue while bypassing model context window limits.

Key Results

Algorithmic Prompting consistently achieves near-perfect length generalization where baselines collapse:

Task (Eval Length) Algorithmic Prompt Accuracy Best Available Baseline Accuracy Approximate Error Reduction
Addition (19 digits) 90.5% 9.5% (Few-shot) ~9.5×
Subtraction (14 digits) 65.6% 16.7% (Few-shot) ~2.4×
Multiplication (7 digits) 79.7% 5.5% (Chain-of-thought) ~4.7×
Parity (20 elements) 95.0% 50.0% (Random chance) ~10.0×

On GSM8k-Hard, invoking an algorithmic tool-use prompt doubled overall problem accuracy (55.8% vs. 27.4% for standard Chain-of-Thought), with addition calculation accuracy rising from 61.9% to 98.4%.

Limitations and Future Work

  • Context Window Bottlenecks: Because algorithmic prompts require extremely verbose intermediate traces, complex multi-step problems rapidly hit context length ceilings (requiring multi-pass or multi-model dialogue strategies).
  • Error Cascades on Long Sequences: In two-number addition, error analysis reveals that errors cluster around the very earliest steps, where list tracking and state copying overhead are highest.
  • Scaling to Diverse Operations: Handcrafting unambiguous algorithmic prompts covering every edge case (e.g., negative signs and borrow mechanics) requires non-trivial manual effort. Developing automated prompt generators or selective retrieval mechanisms to mitigate skill interference remains an open research challenge.

Summary updated

BibTeX
@inproceedings{zhou2023teaching,
  author = {Hattie Zhou and Azade Nova and Aaron Courville and Hugo Larochelle and Behnam Neyshabur and Hanie Sedghi},
  title = {{Teaching Algorithmic Reasoning via In-context Learning}},
  booktitle = {International Conference on Learning Representations},
  year = {2023},
  url = {https://openreview.net/forum?id=6dlC7E1H_9},
}
Figure