Algorithm of thoughts (AoT) is a prompting technique in which in-context examples demonstrate an explicit search procedure, such as depth-first search with backtracking, so that a large language model explores, evaluates and abandons candidate solutions inside a single generation. In the prompting branch of artificial intelligence, it was proposed in 2023 as a cheaper alternative to multi-query search methods, because the whole exploration happens in one or very few model calls.
The core intuition is that a model can imitate the trace of an algorithm it has seen in the prompt. Instead of an external program that calls the model many times to expand and score branches, the prompt itself teaches the model to write out a compact search, prune weak paths and continue from promising ones.
How algorithm of thoughts structures a prompt
An AoT prompt consists of task instructions plus worked examples whose reasoning is written as an algorithmic trace. Each example lists a candidate step, briefly judges whether it can lead to a solution, backtracks when a branch fails and records the path that finally succeeds. The model then receives a new problem and continues in the same style.
The examples are deliberately not perfect, linear solutions. They include dead ends, because the goal is to teach the procedure of exploring rather than the habit of guessing the answer at once. This is the main difference from ordinary chain-of-thought demonstrations, which typically show only the successful line of reasoning.
The following table lists the components of a typical AoT prompt and their purpose.
| Component | What it contains | Why it matters |
|---|---|---|
| Task instruction | Goal, rules and output format | Defines the search space |
| Search demonstration | Step-by-step trace with branches | Teaches the exploration pattern |
| Evaluation cues | Short judgments of each branch | Enables pruning of weak paths |
| Backtracking markers | Explicit returns to earlier states | Prevents fixation on one path |
| Final answer slot | Fixed marker for the result | Makes parsing reliable |
Search styles that AoT can imitate
The original formulation showed that a model can follow both depth-first and breadth-first search patterns when the examples use them consistently. The choice of pattern shapes how the generated reasoning looks and how many tokens it consumes.
| Pattern | How the trace looks | Best suited to |
|---|---|---|
| Depth-first with backtracking | Follow one path deeply, retreat on failure | Puzzles with quick dead-end detection |
| Breadth-first exploration | List several options per level first | Problems needing comparison of alternatives |
| Heuristic-guided search | Try the most promising option first | Tasks where intuition ranks moves well |
Reported results suggest that the model does not copy the algorithm mechanically. It often visits fewer nodes than the textbook algorithm would, because its learned intuition ranks promising options early. This behavior is useful, but it also means the search is neither exhaustive nor guaranteed to find a solution.
Algorithm of thoughts compared with related prompting techniques
AoT belongs to a family of methods that make reasoning more deliberate. Its closest neighbors are tree of thoughts and graph of thoughts, which also explore multiple branches, and self-consistency, which samples several independent answers. The distinguishing feature of AoT is that the exploration lives inside one context rather than across many orchestrated calls.
| Technique | Number of model calls | Exploration style | Main cost |
|---|---|---|---|
| Chain-of-thought | One | Single linear path | Low, but no recovery from errors |
| Self-consistency | Several to dozens | Independent samples, majority vote | Proportional to sample count |
| Tree of thoughts | Many, often dozens to hundreds | External search controller | High call count and latency |
| Algorithm of thoughts | One or a few | Search trace within one output | Long outputs, context use |
The trade-off is clear: AoT saves calls and orchestration code but places the full burden of tracking states on the model. Tree of thoughts can enforce rigorous bookkeeping in external code, whereas AoT depends on the model keeping its own trace consistent across a long generation.
When to use AoT and how to write the examples
AoT works best on combinatorial problems with checkable intermediate states, such as arithmetic puzzles, constraint satisfaction, scheduling with a few hard rules and small planning tasks. It is less useful for open-ended writing or factual questions, where there is no clear notion of a failed branch.
- Match the demonstrations to the task structure, because a trace for one puzzle type transfers poorly to another.
- Show genuine dead ends and short reasons for abandoning them, not only the winning path.
- Keep state notation compact, such as remaining numbers or filled cells, to limit token growth.
- Fix an answer marker so downstream code can extract the result.
- Validate the final answer externally whenever a programmatic checker exists.
Modern reasoning models trained with long internal deliberation already perform similar exploration without demonstrations. For them, AoT-style prompts can still help when a specific search order or output trace is required, but the marginal gain is usually smaller than with standard chat models.
Limitations and common mistakes
The main risk of AoT is unreliable self-evaluation. A model may declare a branch dead when it was viable or accept an invalid state as correct, and nothing in the prompt can enforce the rules.
| Problem | Cause | Remedy |
|---|---|---|
| Search stops too early | Examples are too short | Include longer traces with several retreats |
| Invalid intermediate states | Model miscalculates silently | Add explicit state checks in examples |
| Output exceeds token limit | Search space is too large | Restrict depth or split the problem |
| Answer contradicts its own trace | Weak final consolidation | Require restating the winning path |
Another mistake is treating AoT as a guarantee of correctness. It improves exploration efficiency, yet the result remains a model output that needs verification wherever errors are costly.
Understanding it simply
Think of solving a maze on paper. You try one corridor. If it ends in a wall, you go back and try another. You keep going until you reach the exit.
Algorithm of thoughts teaches an AI model to do the same thing in writing. The prompt shows it a few solved puzzles, including the wrong turns. The model learns to try, check, back up and try again, all in one answer.
The benefit is speed and cost. The model asks itself the questions instead of waiting for a separate program to guide each step.
Applied in practice
A logistics planner uses a language model to draft shift rosters for a small warehouse team of eight people over five days. Each roster must respect rest periods, skill requirements and personal availability, so many first drafts break at least one rule.
The planner writes an AoT prompt with two worked examples. Each example assigns shifts day by day, checks the constraints after every assignment, records the conflict when a rule fails and backtracks to the last valid day. The new request follows the same format in a single model call.
Compared with a plain request, the share of rosters that pass the planner’s automatic rule checker rises noticeably. The remaining failures are mostly arithmetic slips in counted hours, so the planner keeps the external checker as the final gate and asks the model to repair any roster that fails it.