Teaching Algorithmic Reasoning via In-context Learning

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

TL;DR

This paper introduces Algorithmic Prompting, a novel in-context learning approach that enables Large Language Models (LLMs) to achieve robust out-of-distribution generalization on complex algorithmic tasks. By decomposing algorithms into highly detailed, unambiguous natural language instructions and intermediate steps, the authors demonstrate massive error reductions—up to 10x—on tasks like long addition, multiplication, and parity without any model fine-tuning.

What Problem Does This Paper Address?

While LLMs excel at many natural language tasks, they notoriously struggle with precise, multi-step algorithmic reasoning—such as performing multi-digit arithmetic or computing the parity of a long binary list.

Existing prompting techniques like Few-Shot or Chain-of-Thought (CoT) help to an extent, but they fail dramatically when tested on out-of-distribution (OOD) inputs, such as evaluating numbers much longer than those shown in the prompt examples. The core issue is ambiguity: when a prompt only shows a high-level scratchpad or a few examples, the model can extrapolate multiple incorrect rules that happen to fit the short training instances but fail on longer ones. This paper tackles how to explicitly teach unambiguous, input-independent algorithms to LLMs entirely in-context.

Methods

The authors propose Algorithmic Prompting, which drastically increases the granularity of the rationales provided in few-shot prompts. It explicitly details every single state transition, variable update, and mathematical rule (e.g., explicitly showing how a carry digit is computed and tracked from right to left).

The paper systematically explores this approach across four progressive learning stages:

  1. Teaching an Algorithm as a Skill: Providing a detailed, unambiguous description of a single algorithm execution on running examples.
  2. Skill Accumulation: Teaching multiple distinct algorithms simultaneously within a single prompt (e.g., handling both addition and subtraction with positive/negative numbers).
  3. Skill Composition: Learning a complex skill by building upon simpler, previously taught ones (e.g., converting multiplication into multi-number addition).
  4. Using Skills as Tools: Integrating the learned algorithm as a subroutine to solve broader math word problems via a dialogue-like multi-model interaction.
Figure 1: The four learning stages investigated in this work (from left to right): (i) Teaching an algorithm as a skill (Section 3) (ii) Skill Accumulation, i.e., teaching multiple skills simultaneously (Section 4) (iii) Skill Composition, i.e. the ability to learn a complex skill through building upon simpler ones (Section 5) (iv) Using Skills as Tools to solve problems (Section 6). We teach these algorithms in-context using our proposed algorithmic prompting approach, which does not involve any further training of the underlying model.

To bypass context window limitations and avoid mixing different types of reasoning, the authors implement a dialogue-based tool-use strategy. One model handles the informal natural language reasoning of a word problem, and when it encounters a calculation, it outputs a special token to call a second model specialized in the precise arithmetic algorithm.

Datasets

The approach was evaluated using the OpenAI code-davinci-002 (Codex) model on several benchmarks:

  • Synthetic Algorithmic Tasks: Multi-digit addition (up to 19 digits), subtraction (up to 14 digits), multiplication, and binary list parity (up to 30 elements).
  • GSM8k (Pure-Addition Subset): A filtered subset of 108 high-quality math word problems from the GSM8k dataset whose solutions only require addition steps.
  • GSM8k-Hard: A new dataset created by the authors consisting of 50 problems from the pure-addition subset but modified with significantly larger numerical values (averaging 7.2 digits) to test OOD scaling.

Key Insights and Ideas

  • LLMs Are Truly Learning In-Context: To prove the model wasn't just relying on pre-trained shortcuts, the authors introduced systematic errors into the prompt's algorithm. The model's accuracy dropped to near 0%, confirming that it strictly follows the exact (and in this case, flawed) algorithm prescribed in the context.
  • The Interference Phenomenon: Mixing informal natural language reasoning (which relies on pre-training intuition) with rigid algorithmic execution (which requires strict instruction-following) within the same prompt causes severe performance degradation.
  • Modularity Alleviates Interference: Separating these two distinct cognitive tasks into a multi-model "dialogue" (where one model acts as the reasoning engine and the other as an algorithmic tool) successfully mitigates interference and bypasses context length limits.
  • Positive Transfer in Skill Accumulation: Teaching similar algorithms simultaneously (like addition and subtraction) yields a synergistic effect, improving performance compared to teaching them in isolation with fewer examples.

Key Results

Algorithmic Prompting achieved near-perfect OOD length generalization and significantly outperformed standard prompting baselines:

  • Error Reductions: Compared to the best available baselines, the proposed method reduced errors by approximately 10x for long parity, 9x for addition, 5x for multiplication, and 2x for subtraction.
  • Arithmetic Scaling: For two-number addition, the model maintained near-perfect accuracy up to 19 digits, whereas standard Chain-of-Thought accuracy degraded rapidly after 5 digits.
  • GSM8k-Hard Performance: Utilizing the algorithmic prompt as an external tool via the dialogue strategy doubled the overall accuracy on hard math word problems (55.8% vs. 27.4% for standard CoT).

Limitations and Future Work

  • Context Length Constraints: Algorithmic prompts generate long execution traces. For very complex or long problems, the token count can exceed the model's maximum context window.
  • Trace Efficiency: The current method requires a large number of generation steps, which increases inference latency and cost.
  • Future Directions: The authors point toward exploring architectural enhancements like recurrence or external memory to handle longer contexts, as well as extending this framework to teach more abstract "soft algorithms," such as simplifying complex mathematical equations.

Summary updated

BibTeX
@inproceedings{zhou2023teaching,
  author = {Hattie Zhou and Azade Nova and Hugo Larochelle and Aaron Courville 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