Source-linked AI summary
Teaching Algorithmic Reasoning via In-context Learning
Hattie Zhou, Azade Nova, Hugo Larochelle, Aaron Courville, Behnam Neyshabur, Hanie Sedghi
TL;DR
LLMs still struggle with algorithmic reasoning despite progress from rationale-based prompting, especially on simple tasks such as parity. The paper introduces Algorithmic Prompting and studies four stages of algorithm learning through in-context learning, achieving substantial error reductions over existing baselines. It also identifies interference when learned skills are used as tools and points to selective retrieval or attention as a future direction.
Problem
LLMs remain far from solving simple algorithmic reasoning tasks such as parity, despite rationale-based prompting improving some multi-step reasoning problems.
Source: §1 · p. 2Method
Algorithmic Prompting teaches algorithms through detailed execution examples, explicit explanations and natural-language instructions, while studying skill learning, accumulation, composition and tool use in-context.
Source: §1 · pp. 2–3Results
Approximately 10x, 9x, 5x and 2x error reductions over the best available baselines are achieved for long parity, addition, multiplication and subtraction, respectively.
Source: §1 · p. 2Takeaways & Limitations
The results suggest that longer contexts with more thorough solution examples may improve reasoning performance through in-context teaching.
Source: §7 · p. 12Tool-use experiments identify interference, motivating future work on retrieving or selectively attending to instructions for a particular problem.
Source: §7 · p. 12
Abstract
from arXiv · showhide
Large language models (LLMs) have shown increasing in-context learning capabilities through scaling up model and data size. Despite this progress, LLMs are still unable to solve algorithmic reasoning problems. While providing a rationale with the final answer has led to further improvements in multi-step reasoning problems, Anil et al. 2022 showed that even simple algorithmic reasoning tasks such as parity are far from solved. In this work, we identify and study four key stages for successfully teaching algorithmic reasoning to LLMs: (1) formulating algorithms as skills, (2) teaching multiple skills simultaneously (skill accumulation), (3) teaching how to combine skills (skill composition) and (4) teaching how to use skills as tools. We show that it is possible to teach algorithmic reasoning to LLMs via in-context learning, which we refer to as algorithmic prompting. We evaluate our approach on a variety of arithmetic and quantitative reasoning tasks, and demonstrate significant boosts in performance over existing prompting techniques. In particular, for long parity, addition, multiplication and subtraction, we achieve an error reduction of approximately 10x, 9x, 5x and 2x respectively compared to the best available baselines.
1 Introduction
The paper studies how to teach algorithmic reasoning to LLMs through in-context learning, addressing persistent difficulty with complex and out-of-distribution reasoning. It proposes Algorithmic Prompting and investigates four stages of skill learning, reporting substantial error reductions over existing prompting techniques.
- Algorithmic reasoning requires parsing, decomposing and recombining problems, and applying rules, transformations, processes and axioms.Learning the underlying algorithm can support out-of-distribution generalization because algorithms are input-independent when executed properly.Source: §1 · p. 1
- The work investigates four stages: teaching algorithms as skills, accumulating multiple skills, composing skills, and using skills as tools.These stages organize the study of algorithmic reasoning through in-context learning.Source: Figure 1 · p. 2, §1 · p. 2
- LLMs remain far from solving simple algorithmic tasks such as parity despite rationale-based prompting improving some multi-step reasoning problems.Prior work found that rationale-based prompting can generalize to longer instances but does not solve these algorithmic tasks reliably.Source: §1 · pp. 1–2
- Algorithmic Prompting provides detailed algorithm execution on running examples plus explicit explanations and natural-language instructions to remove ambiguity.The approach is applied through in-context learning without further training of the underlying model.Source: §1 · p. 2, Figure 1 · p. 2
- Approximately 10x, 9x, 5x and 2x error reductions over the best available baselines are achieved for long parity, addition, multiplication and subtraction, respectively.These results support significant gains from algorithmic prompting on several algorithmic reasoning tasks.Source: §1 · p. 2
- The paper studies simultaneous learning of multiple algorithms and their composition for solving more complex tasks.It also explores algorithm use in math word problems, where complex calculations can improve while general reasoning decreases because of interference.Source: §1 · pp. 2–3
2 Algorithmic prompting
Algorithmic Prompting is designed to constrain how models interpret examples by combining detailed intermediate computations with explicit algorithmic explanations. The section defines its baselines and evaluation setting, emphasizing length-based out-of-distribution generalization as evidence of algorithm learning.
2 Algorithmic prompting
- Scratchpad exposes intermediate computations, while chain-of-thought provides explanations and a reusable thinking pattern for multi-step reasoning.The paper hypothesizes that more specific and applicable thinking patterns can increase adherence to those patterns during problem solving.Source: §2 · p. 3
- A single addition example can leave carry rules ambiguous, so the model may infer spurious patterns instead of the general rule.For example, 182 + 376 does not by itself reliably establish when a digit sum generates a carry.Source: §2 · p. 3
- Algorithmic Prompting drastically increases rationale detail and specifies algorithm steps to constrain interpretation and support systematic generalization.It combines scratchpad-style intermediate computations with chain-of-thought-style explanations for each step.Source: §2 · p. 3
2.1 Experimental setup
- The few-shot baseline supplies question-answer pairs, whereas chain-of-thought adds rationales generated using “let’s think step by step.”These baselines are compared with algorithmic prompts in the experiments.Source: §2.1 · p. 3
- Evaluation distinguishes in-distribution questions whose answer lengths occur in the prompt from out-of-distribution questions with longer answers.Length generalization is used because it provides an indication of whether the underlying algorithm has been learned.Source: §2.1 · pp. 3–4
- Table 1 characterizes prompting strategies by their use of natural-language explanation and explicit intermediate computations.Algorithmic prompting includes both qualities, unlike strategies that provide only input-target pairs or instructions without running examples.Source: Table 1 · p. 4
- Experiments use Codex code-davinci-002 with an 8000-token context window, uniformly sampled task examples, temperature 0, and default remaining hyperparameters.The paper reports one sample for each result.Source: §2.1 · p. 4
3 Teaching algorithms as skills
Algorithmic prompting teaches arithmetic procedures through explicit, unambiguous algorithmic steps and running examples, producing strong out-of-distribution generalization across several tasks.
3.1 Two-number addition
- Near-perfect accuracy and length generalization on addition outperform few-shot, chain-of-thought, and instruction-only prompting through 19-digit answers.The prompt uses three examples with answers up to five digits and evaluates questions up to 19 digits.Source: §3.1 · p. 4
- Additional intermediate details substantially improve scratchpad prompting, while the basic scratchpad format performs extremely poorly as a direct prompt.The detailed format makes carry and answer derivations explicit; comma delimiters also improve performance for Codex.Source: §3.1 · p. 4, Figure 2 · p. 5
- Reducing ambiguity in carry calculation and digit indexing yields significant gains, while natural-language descriptions improve performance beyond intermediate computations alone.The final prompt copies unprocessed digits so each step can use the last digit without variable-position indexing.Source: §3.1 · p. 5, Figure 3 · p. 6
- Systematic errors in algorithmic demonstrations reduce accuracy to near 0%, whereas irregular errors cause only a small degradation, indicating context-sensitive algorithm following.Correct final answers were always accompanied by correct intermediate steps.Source: §3.1 · pp. 5–6
3.2 Teaching other algorithms using algorithmic prompting
- Algorithmic prompting significantly outperforms few-shot or chain-of-thought baselines on subtraction, multiplication, and parity, while multiplication uses direct 1 × n-digit calculations to reduce instruction complexity.Multiplication is restricted to cases where at least one number is below 1000 and uses groups of at most three digits.Source: §3.2 · pp. 6–7
4 Skill Accumulation
Skill accumulation combines addition and subtraction procedures in one prompt, allowing the model to select the appropriate algorithm while exposing transfer between related skills.
4 Skill Accumulation
- Combined addition-subtraction prompting executes the appropriate algorithm by question type, with lower subtraction accuracy reflecting its greater algorithmic complexity.Addition-only performance changes minimally despite the presence of the additional subtraction algorithm.Source: §4 · p. 7
- Scaling to more algorithms may require more efficient strategies because strong combined-prompt performance required examples covering all positive and negative number combinations.Source: §4 · p. 7
- Removing addition examples significantly harms subtraction performance, but six subtraction examples recover the original result, demonstrating positive transfer between similar algorithms.The addition-only control is already saturated in the number of shots, so extra addition examples do not improve addition performance.Source: §4 · p. 7
5 Skill Composition
Skill composition builds larger procedures from previously taught algorithms, improving composite-task performance while context length remains a practical constraint.
5 Skill Composition
- Composed algorithmic prompts significantly outperform few-shot and chain-of-thought baselines on multi-number addition and multiplication-as-addition.The evaluation reports only questions whose algorithmic prompts fit within context for the main comparison.Source: §5 · p. 8
- No-composition prompting performs much worse than simple or augmented composition, showing that previously learned component algorithms support extended skills.Simple and augmented composition have minimal performance differences.Source: §5 · p. 8
- A second-pass strategy corrects a significant portion of incomplete questions by retaining the last completed algorithmic step for another inference pass.Source: §5 · p. 9
- Dialogue-like models with separate prompts achieve performance comparable to context-fitting results on the full dataset by delegating algorithmic subcomponents.This approach prevents intermediate subcomponent outputs from persisting in one model context after deriving the answer.Source: §5 · p. 9
6 Using skills as tools
The paper evaluates learned algorithms as tools within mathematical reasoning, finding interference when algorithmic and informal reasoning skills share context and gains when a separate model performs calculations.
6 Using skills as tools
- The tool-use study focuses on pure-addition GSM8k questions and creates GSM8k-Hard by increasing answer lengths from an average of 2.4 to 7.2 digits.GSM8k-Hard contains 50 examples with answers ranging from 3 to 12 digits.Source: §6 · pp. 9–10
6.1 Augmenting informal mathematical reasoning
- Including algorithmic output in chain-of-thought examples significantly disrupts informal mathematical reasoning, while a preceding solution plan alleviates but does not eliminate the interference.The experiments use <ALGO> and <NONALGO> flags to direct whether addition is performed algorithmically or directly.Source: §6.1 · pp. 10–11
6.2 Algorithmic prompt as tool use
- Algorithmic tool use separates informal reasoning from addition by sending marked arithmetic questions to a second model, which returns calculations without retaining algorithmic output in the first model’s context.This dialogue-like design also addresses context-length limitations and supports calling previously learned algorithms as subroutines.Source: §6.2 · pp. 11–12
- Double the accuracy is achieved on GSM8k-Hard when a second model performs addition with an algorithmic prompt, although logical accuracy still suffers from interference.The improvement comes directly from more accurate addition calculations, while specific tool-use tokens continue to disrupt informal reasoning.Source: Table 3 · p. 12, §6.2 · p. 12
7 Conclusion and Future Work
The paper concludes that in-context learning can teach algorithms, accumulate and compose skills, and use them as tools, while identifying interference and context length as continuing constraints.
7 Conclusion and Future Work
- The work studies four settings for teaching algorithmic reasoning through in-context learning: algorithmic skills, skill accumulation, skill composition, and skill use as tools.It reports significant performance boosts across algorithmic reasoning tasks and suggests more informative rationales and longer contexts as promising directions.Source: §7 · p. 12
- Interference during tool use remains a limitation, motivating future methods that retrieve or selectively attend to instructions relevant to each problem.The authors also propose studying harder tasks under extremely large context lengths.Source: §7 · p. 12
A.1 Additional Related work
- This work focuses on algorithmic reasoning for arithmetic tasks and GSM8k problems using general-purpose transformer models rather than structured architectures aligned to algorithms.The related-work discussion contrasts this approach with graph neural networks and other architecture-modification methods.Source: §A.1 · p. 15
- Prior in-context-learning research examines how training frequency, prompt components, chain-of-thought structure, and human-like biases affect few-shot performance.These studies provide context for the paper’s investigation of algorithmic prompting.Source: §A.1 · p. 15
A.2 Additional information on experimental setup
The appendix summarizes how arithmetic and parity experiments are configured, including sampling by answer length and shot counts for composition tasks.
A.2 Additional information on experimental setup
- Questions are sampled uniformly by answer length, with an average of 100 samples per length across the arithmetic and parity experiments.The setting summary covers the experimental configurations reported throughout the paper.Source: Table 4 · p. 16
- For multi-number addition and multiplication-as-addition composition tasks, shot counts refer to examples illustrating the composition skill, while additional examples of other skills may appear.The corresponding sections provide the task-specific prompt details.Source: Table 4 · p. 16
A.3 Additional results on two-number addition
Additional addition experiments show that natural-language cues support algorithmic prompting, while errors concentrate in multi-step copying and early execution steps.
A.3 Additional results on two-number addition
- The appendix illustrates addition prompts with different explanation detail levels and reports the corresponding symbols-only ablations in Figure 11.These experiments separate natural-language descriptions from intermediate computations.Source: §A.3 · p. 16, Figure 10 · p. 16
- Natural-language descriptions improve addition prompting: symbols-only prompts perform worse, uninformative replacements reduce performance further, and misleading symbols cause a significant drop.The ablations retain keywords such as Len and Max, replace them with VBZ and UXO, or substitute Str and Min associated with other operations.Source: §A.3 · pp. 16–17, Figure 11 · p. 17
- Algorithmic addition reliably handles single-step operations such as maximum digit length, carried two-digit sums, and copying the previous carry.The model struggles with multi-step digit separation and copying all digits from one step to the next.Source: §A.3 · p. 17
- Most errors occur in early execution steps, where more unprocessed digits make copying digit lists between steps difficult.Figure 12 illustrates this concentration of errors across the algorithm’s steps.Source: §A.3 · p. 17
A.4 Additional results on teaching other algorithms
Additional experiments show algorithmic prompting supports parity and multiplication beyond the lengths seen in demonstrations, while errors in addition often arise from copying and digit-list manipulation.
Error analysis
- Copying lists from previous steps, followed by counting and separating digits into list format, are the most error-prone addition operations.Source: Table 5 · p. 18
Multiplication
- Natural-language instructions plus intermediate calculations let multiplication prompts extrapolate from single splits to multiple splits for long numbers.The method restricts direct multiplication to questions with 3 or fewer digits and explains how to partition larger numbers into groups.Source: §A.4 · p. 18
Parity
- 80% accuracy persists for parity lists up to 30 digits, while few-shot and scratchpad baselines reach random chance around length 5.The prompt used two examples with answers up to 8 digits, and each plotted point averages 100 random samples.Source: §A.4 · p. 18
A.5 Additional results for Skill Accumulation
A length-controlled ablation indicates that algorithmic prompting’s advantage is not explained by simply providing more few-shot examples.
Length-controlled ablation
- Adding more few-shot examples to match the algorithmic prompt’s token count does not improve addition-subtraction performance.The ablation generates n examples whose total token count equals that of the algorithmic prompt.Source: §A.5 · p. 18
A.6 Additional results for Skill Composition
Skill-composition experiments address long outputs and show that second-pass recovery helps incomplete cases, while specialized model-to-model querying avoids context-length limits on the longest problems.
Composition setup
- The supplementary composition figures illustrate increasing complexity from two-number addition to multi-number addition and multiplication-as-addition.Source: §A.6 · pp. 18–19
Error distribution
- Addition errors occur earlier in the computation, leaving more remaining digits to process.Figure 12 measures progress as first error step divided by total steps.Source: Figure 12 · p. 19
Context-length handling
- A second-pass strategy correctly solves some questions incomplete after the first pass, but remains significantly below the upper estimate from first-pass completions.The second pass appends the last completed step from the first-pass output to the original question.Source: §A.6 · p. 19
Tool-based composition
- A dialogue-like pair of specialized models generalizes from a single prompt example and avoids context-length limitations on the longest multi-number problems.One model decomposes multi-number addition or multiplication-as-addition, while the other answers the extracted two-number addition queries.Source: §A.6 · p. 19
A.7 Additional results for Tool Use
This section details prompts and procedures for algorithmic, baseline, and tool-use experiments across arithmetic, parity, skill composition, and GSM8k. It illustrates how algorithmic prompting applies an explicit addition algorithm to multi-step quantitative problems while using a non-algorithmic format for simpler problems.
A.7 Additional results for Tool Use
- The addition tool-use strategy augments only 2 of 8 examples with algorithmic output, marks them with <ALGO>, and distinguishes evaluation from a <NONALGO> baseline.Algorithmic output appears in 86% of <ALGO> examples and 0% of <NONALGO> examples.Source: §A.7 · p. 19
B.1 Addition prompt strategies
- Algorithmic addition prompts demonstrate explicit digit-array and carry updates, while few-shot and chain-of-thought prompts provide compact numerical alternatives.The algorithmic examples solve 128+367, 9980+29, and 802+7145 through intermediate states and final answers [4,9,5], [1,0,0,0,9], and [7,9,4,7].Source: §B.1.1 · p. 24, §B.1.2 · p. 24, §B.1.3 · p. 25
B.1.4 Instruction addition prompt for addition
- The addition instruction prompt defines right-to-left digit processing, carry propagation, handling unequal lengths, and formatting the final sum.It gives explicit steps for x = int(str(abc)) and y = int(str(defg)), producing z = int(str(stuvw)).Source: §B.1.4 · p. 25
B.3.1 Algorithmic prompt for addition-subtraction
- Algorithmic prompts extend digit-wise addition to signed addition-subtraction and memorized multiplication by decomposing multiplication into partial products followed by addition.The multiplication example computes 128*367 by forming 8*367, 2*367, and 1*367, shifting them, and adding the resulting values.Source: §B.3.1 · p. 29, §B.4.2 · p. 31
B.3.2 Chain-of-thought prompt for addition-subtraction
- The appendix also supplies chain-of-thought baselines for addition, addition-subtraction, memorized multiplication, multi-addition, multiply-as-add, and the full GSM8k prompt.These prompts use worked numerical decompositions, including 802+7145+6=7953, 7+7+7=21, and 3*107=321.Source: §B.3.2 · p. 30, §B.4.1 · p. 30, §B.8 · p. 34
B.5 Algorithmic prompt for parity
- The algorithmic parity prompt iteratively removes the first element, updates a running state, and yields parity 1 for [1, 1, 0, 1, 0]; another reports parities 1 and 0.Source: §B.5 · p. 32, §B.6 · p. 32
B.7 Algorithmic prompt for multi-add and multiply-as-add
- The demonstrations solve 802+7145+6 sequentially as 802+7145=ANS1 and ANS1+6=ANS2, yielding final Answer [7,9,5,3], and rewrite 3*7 as 7+7+7.Source: §B.7 · pp. 32–33
B.10 Algorithmic prompt for multi-add with algo calls
- Algorithmic calls use <GET> to extract subproblem answers interactively, enabling sequential multi-addition and multiplication-as-addition with a single illustrative example.The calls return 7947 then 7953 for 802+7145+6, and 14 then 21 for 3*7.Source: §B.10 · p. 34, §B.11 · p. 34
B.13 Algorithmic prompt for GSM8k
- The demonstrations contrast algorithmic solutions with non-algorithmic responses, reserving explicit tool use for selected arithmetic chains while answering simpler additions directly.Examples involving clothing totals, crayons, toys, food costs, and parking cars are labeled <NONALGO>, whereas the product-pricing and initial-balance problems are labeled <ALGO>.Source: §B.13 · pp. 35–37
- The algorithmic prompt decomposes multi-step word problems into sequential addition subproblems, carries intermediate answers forward, and returns the computed total.For the product-pricing problem, it computes 467+128=595, 467+595=1062, and then 595+1062=2124 through explicit digit-level operations.Source: §B.13 · pp. 35–36
- The addition algorithm is also applied to inverse-style problems, such as reconstructing an initial amount by adding expenditures to the remaining balance.Larry’s expenditures are first summed as 5+12=17, then combined with his remaining $3 to obtain $20.Source: §B.13 · p. 37
B.2 Algorithmic prompt ablations for addition
The addition prompts progressively vary operation wording, carry explicitness, notation, and keyword reliability while preserving the same worked solution structure. Across these variants, the demonstrations compute 128+367 as [4,9,5].
B.2.1 Algorithmic prompt with uncommon operations for addition
- The appendix marks the uncommon indexing operation in red, distinguishing that implementation feature within the addition prompt.Source: §B.2.1 · p. 26
- The uncommon-operation variant explicitly indexes digit lists, computes carries, and returns [4,9,5] for 128+367.It removes digits from the ends of both numbers and processes each position sequentially.Source: §B.2.1 · p. 27
B.2.2 Algorithmic prompt with non-explicit carry for addition
- The non-explicit-carry variant omits explicit carry calculations while retaining digit-by-digit addition and the final answer [4,9,5].The prompt still tracks C[3], C[2], C[1], and C[0] during the worked example.Source: §B.2.2 · p. 27
B.2.5 Symbols-only algorithmic prompt for addition
- The symbols-only variant replaces verbal labels with compact forms such as Len, MaxLen, and indexed arrays while producing [4,9,5].The worked trace uses Len(FN), Len(SN), Len(A), and MaxLen to represent lengths and the maximum length.Source: §B.2.5 · p. 28
B.2.6 Symbols-only algorithmic prompt for addition without keywords
- The no-keyword variant removes Len and Max from the symbols-only prompt but retains the same digit, carry, and answer sequence.The replacement notation uses VBZ(FN), VBZ(SN), and UXOVBZ instead.Source: §B.2.6 · p. 28
B.2.7 Symbols-only algorithmic prompt for addition with misleading keywords
- The misleading-keyword variant replaces Len and Max with Str and Min while the worked trace still computes [4,9,5].Its notation includes Str(FN), Str(SN), and MinStr throughout the addition procedure.Source: §B.2.7 · p. 28
B.2.3 Algorithmic prompt for addition with irregular errors
The irregular- and systematic-error variants alter operand values inside otherwise similar addition traces. Both worked examples still report [4,9,5], while the passages identify the errors through red highlighting.
B.2.3 Algorithmic prompt for addition with irregular errors
- The irregular-error passage identifies the altered calculations with red highlighting.Source: §B.2.3 · p. 27
- The irregular-error trace changes selected operand digits, using 8+6+0 and 1+2+0, yet reports [4,9,5].The altered values appear in the units and leading-position calculations while the carry procedure remains explicit.Source: §B.2.3 · p. 27
- The irregular-error example preserves the original digit-list setup and sequential carry updates despite the altered arithmetic inputs.It continues to construct FN and SN, process positions from the end, and update C and A.Source: §B.2.3 · p. 27
B.2.4 Algorithmic prompt for addition with systematic errors
- The systematic-error passage also identifies its errors with red highlighting.Source: §B.2.4 · p. 27
- The systematic-error trace uses the same altered operand digits, including 8+6+0, 2+5+1, and 1+2+0, before returning [4,9,5].The substitutions recur across the worked calculation rather than appearing in only one position.Source: §B.2.4 · p. 28
- The systematic-error example likewise preserves the original digit-list and carry structure while changing multiple arithmetic operands.The trace still ends after C[0]=0 and the completion statements.Source: §B.2.4 · p. 28