TL;DR
A program-aided language model (PAL) answers a question by writing a short program instead of a final number. The model writes the steps as Python code with a few comments, and a Python Interpreter runs the code to produce the answer. The model's job is to turn the question into steps; the computer does the calculating, so the small arithmetic slips that language models often make disappear.
Builds on: Chain-of-Thought Prompting, In-Context Learning · Leads to: Tool Use, the PAL paper
Where it's used
Grade-school math word problems. The GSM8K benchmark (GSM8K) holds roughly 8,500 grade-school word problems. In the PAL paper, a Codex model using PAL solves 72.0 percent of the test problems, against 65.6 percent for the same model using chain-of-thought prompting. On a harder version with large numbers, the gap is much larger: 61.2 percent against 23.1 percent.
Puzzles that track state. Several tasks from the BIG-Bench Hard suite need the model to keep track of objects, positions or dates. In the Penguins task, a table of penguins is updated by additions and removals, and the question asks about the penguins that remain. PAL reaches 93.3 percent there, against 79.2 percent for chain-of-thought. In Date, which asks for dates after adding or subtracting periods of time, it reaches 76.2 percent against 64.8 percent.
Assistants that run code. Many chat assistants today can write code, run it in a sandbox, and report the output, for example to analyse a spreadsheet or check a calculation. This is the same split as PAL: the model decides what to compute, and a runtime computes it.
How it works
Take one small problem and trace it through. The prompt is a few-shot prompt, so the model learns the pattern from examples in its context rather than from retraining.
Roger has 5 tennis balls. He buys 2 cans of tennis balls, and each can holds 3 balls. How many balls does he have now?
-
Put solved examples in the prompt. The prompt holds a few worked examples. Each one is a question followed by a short program with comments. The programs do not contain a final answer typed in by hand. A program for the Roger problem looks like this:
# Roger started with 5 tennis balls tennis_balls = 5 # 2 cans with 3 balls each bought_balls = 2 * 3 answer = tennis_balls + bought_ballsCheck: running the program gives 5 + 6 = 11 as
answer, even though 11 never appears in the text. -
Append the new question. The model continues the prompt after the new question and writes its own program. It has learned the pattern: name each quantity, write each operation as a line, and store the result in
answer. Here it writes the same three lines for the new problem. Check: the model's decision was that the two cans multiply. That is a reading of the words, which is the part language models are good at. -
Run the program. The text is handed to a Python interpreter. The interpreter ignores the comments and runs each line in order.
tennis_ballsbecomes 5,bought_ballsbecomes 6, andanswerbecomes 11. Check: 5 + 6 = 11. -
Read the result. The value of
answeris the output, 11. Nothing in the chain relies on the model doing the multiplication in its head.
The paper's main results use greedy decoding, which always picks the most likely next token. So the same prompt and question give the same program every time.
flowchart LR Q[Question] --> P[Prompt with solved program examples] P --> M[Language model writes a program] M --> I[Python interpreter runs the program] I --> A[Answer]
The comments matter for the model, not for the interpreter. The paper keeps the names of the variables meaningful, such as num_apples_in_basket, so that each line of code points back to an entity in the question.
The math
A PAL prompt is a list of pairs. Each pair holds a question and a program , with no final answer. Write for joining the pairs in order.
Given worked examples , where each is a program with comments, the prompt is
Here means "followed by", and means "followed by the next example".
Step 2 in symbols: the model writes a program
For a new question , the model produces its program one token at a time. A program is a sequence of tokens , and the model's probability for the whole program is
Each factor is the probability of the next token given everything before it. Greedy decoding picks, at each step, the token with the highest probability, so the program is taken token by token.
Step 3 in symbols: the interpreter computes the answer
Let be the interpreter. It executes and returns the final value of the variable answer:
Unlike chain-of-thought, where each arithmetic result is itself a sequence of predicted tokens, is an exact function. For the Roger program the lines give and , so .
Scoring
On a benchmark with questions and gold answers , the solve rate is
where is 1 when the statement is true and 0 otherwise.
Worked examples
- Question: "I have 3 apples and I buy 4 more. How many do I have?"
- The model writes
apples = 3,bought = 4,answer = apples + bought. - The interpreter stores 3 in
applesand 4 inbought. - It computes
answeras 3 + 4 = 7. - Output: 7. The model's only decision was "add the two numbers".
- Question: "Roger has 5 tennis balls. He buys 2 cans, each with 3 balls. How many does he have now?"
- The model writes
tennis_balls = 5,bought_balls = 2 * 3,answer = tennis_balls + bought_balls. - The interpreter stores 5 in
tennis_balls. - It computes
bought_ballsas 2 × 3 = 6. - It computes
answeras 5 + 6 = 11. - Output: 11. The model decided that the cans multiply and that the two totals add; the computer did both multiplications and the addition.
Question: "A library has 4,815 books. In January it lends out 1,377 books and gets back 856 that were lent earlier. Then it adds 2 boxes with 150 books each. How many books does it have now?"
The model writes:
books = 4815
lent = 1377
returned = 856
new_boxes = 2 * 150
answer = books - lent + returned + new_boxes- Start with
books= 4,815. - Books on loan are not on the shelf, so subtract them: 4,815 − 1,377 = 3,438.
- Add the books returned: 3,438 + 856 = 4,294.
- The two boxes hold 2 × 150 = 300 books.
- Add the new books: 4,294 + 300 = 4,594.
- Output: 4,594.
This is where a model that writes numbers one token at a time is most error-prone: several multi-digit operations in a row. The interpreter does each one exactly, and the model only has to get the structure right.
Try it: A shop has 2,500 pencils. It sells 1,189 of them and then receives a delivery of 640. How many pencils does it have now?
Answer: 2,500 − 1,189 = 1,311, and 1,311 + 640 = 1,951. The shop has 1,951 pencils.
Common mistakes and tradeoffs
It is tempting to think PAL makes the model a calculator. In fact the model still decides every step: which quantities matter, which operation applies, and in what order. A wrong program produces a confidently wrong answer, because the interpreter runs exactly what it is given. PAL moves the calculation out of the model, not the reasoning.
It is tempting to think the wording in the prompt is decoration. In fact the paper's ablation shows that meaningful variable names matter a great deal. On the math prompts, replacing the descriptive names with single letters dropped the average solve rate from 71.8 percent to 59 percent. The paper also finds that comments and names together work best, which fits a model that uses the words to stay grounded in the problem.
It is tempting to think any question can be solved this way. In fact PAL needs a problem whose solution can be written as a short deterministic program. Open-ended questions, questions that need judgement, and questions about the meaning of a text have no program to run.
It is tempting to think running model-written code is harmless. In fact the program can do whatever the interpreter can do, so a real system must run it in a sandbox with limits on time, memory and access to files and the network.
When to use PAL: arithmetic-heavy questions, counting, date arithmetic, and anything that tracks state through several updates.
When not to use it: short commonsense questions, where a program adds steps without adding accuracy; and any setting where running code is too slow, too costly or too risky.
Alternatives: plain chain-of-thought needs no runtime. Self-consistency samples many reasoning chains and takes a vote, which helps when the errors are random. Native Tool Use lets the model call a fixed function with arguments, which is a narrower form of the same idea.
History
By 2022, chain-of-thought prompting had shown that asking a model to write out its steps improved its accuracy on multi-step problems. The steps were often right, but the numbers inside them were not always right. Writing a program for a problem is an older idea, studied under the name Program Synthesis, but it was not a standard way to prompt language models.
Codex, a version of GPT-3 trained on public source code, was described in 2021. It made it practical for a language model to write working Python. A team at Carnegie Mellon University then published PAL on arXiv in November 2022. Its results were reported on the benchmarks listed above, and it showed that the same model could do better by writing code than by writing text. Later work on connecting language models to outside tools, including Tool Use, built on the same split between reasoning and computation.
Recap
- Chain-of-thought prompting makes the model write its steps in words, but the numbers in those steps are still predicted one token at a time.
- PAL asks for the steps as Python lines with comments, ending with a variable called
answer. - An interpreter runs the program, so the arithmetic is exact once the program is right.
- The model's job is translation from question to program. Meaningful variable names help it stay grounded.
- Read next: Tool Use for the general pattern of handing work to an outside function, and the PAL paper for the full set of results.
Check yourself
QuestionIn PAL, which part of the work does the language model do, and which part does the interpreter do?Show answer
The language model reads the question and writes the program. It decides which quantities matter, which operations apply and in what order. The interpreter runs the program and computes the numbers exactly.
QuestionA PAL program contains the lines `bought = 4 * 6` and `answer = 10 + bought`. What is `answer`?Show answer
First bought is 4 × 6 = 24. Then answer is 10 + 24 = 34.
QuestionWhy might a PAL prompt that uses `num_apples_in_basket` work better than one that uses `a`?Show answer
The interpreter treats both names the same, but the model does not. A descriptive name ties each quantity in the program to an entity in the question, which helps the model write the right operations. The paper's ablation found that replacing descriptive names with single letters lowered accuracy on the math tasks.
QuestionA user asks for a short poem about autumn. Would PAL help here? Explain.Show answer
No. There is no arithmetic or deterministic procedure to hand to an interpreter. The task needs judgement about language, and writing a program would only add steps that do not produce the answer.