Search

Search papers and lessons

Program-Aided Language Model

Level 4 · 10 min read

A program-aided language model writes its reasoning as a short program, and a Python interpreter runs that program to get the answer, so the model does not have to do the arithmetic itself.

On this page

  • Colour = topic mix
  • Neural network basics
  • Calculus
  • Discrete math
  • Linear algebra
  • Probability
  • Architectures
  • ReadReady to readNot yet

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?

  1. 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_balls

    Check: running the program gives 5 + 6 = 11 as answer, even though 11 never appears in the text.

  2. 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.

  3. Run the program. The text is handed to a Python interpreter. The interpreter ignores the comments and runs each line in order. tennis_balls becomes 5, bought_balls becomes 6, and answer becomes 11. Check: 5 + 6 = 11.

  4. Read the result. The value of answer is 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 xix_i and a program tit_i, with no final answer. Write ∥\| for joining the pairs in order.

Definition: PAL prompt

Given kk worked examples (x1,t1),…,(xk,tk)(x_1, t_1), \ldots, (x_k, t_k), where each tit_i is a program with comments, the prompt is

p=⟨x1⋅t1⟩ ∥ ⟨x2⋅t2⟩ ∥ ⋯ ∥ ⟨xk⋅tk⟩.p = \langle x_1 \cdot t_1 \rangle \,\|\, \langle x_2 \cdot t_2 \rangle \,\|\, \cdots \,\|\, \langle x_k \cdot t_k \rangle .

Here ⋅\cdot means "followed by", and ∥\| means "followed by the next example".

Step 2 in symbols: the model writes a program

For a new question xx, the model produces its program tt one token at a time. A program tt is a sequence of NN tokens s1,s2,…,sNs_1, s_2, \ldots, s_N, and the model's probability for the whole program is

P(t∣p,x)=∏j=1NP ⁣(sj∣p,x,s1,…,sj−1).P(t \mid p, x) = \prod_{j=1}^{N} P\!\left(s_j \mid p, x, s_1, \ldots, s_{j-1}\right).

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 t^=arg⁡max⁡tP(t∣p,x)\hat{t} = \arg\max_t P(t \mid p, x) taken token by token.

Step 3 in symbols: the interpreter computes the answer

Let Run\mathrm{Run} be the interpreter. It executes t^\hat{t} and returns the final value of the variable answer:

y=Run(t^).y = \mathrm{Run}(\hat{t}).

Unlike chain-of-thought, where each arithmetic result is itself a sequence of predicted tokens, Run\mathrm{Run} is an exact function. For the Roger program the lines give bought=2×3=6\text{bought} = 2 \times 3 = 6 and answer=5+6=11\text{answer} = 5 + 6 = 11, so y=11y = 11.

Scoring

On a benchmark with MM questions and gold answers y1∗,…,yM∗y^*_1, \ldots, y^*_M, the solve rate is

solve rate=100M∑m=1M1 ⁣[ ym=ym∗ ](percent),\text{solve rate} = \frac{100}{M} \sum_{m=1}^{M} \mathbf{1}\!\left[\,y_m = y^*_m\,\right] \quad \text{(percent)},

where 1[⋅]\mathbf{1}[\cdot] is 1 when the statement is true and 0 otherwise.

Worked examples

Example
A tiny case: apples
  1. Question: "I have 3 apples and I buy 4 more. How many do I have?"
  2. The model writes apples = 3, bought = 4, answer = apples + bought.
  3. The interpreter stores 3 in apples and 4 in bought.
  4. It computes answer as 3 + 4 = 7.
  5. Output: 7. The model's only decision was "add the two numbers".
Example
The standard case: Roger's tennis balls
  1. Question: "Roger has 5 tennis balls. He buys 2 cans, each with 3 balls. How many does he have now?"
  2. The model writes tennis_balls = 5, bought_balls = 2 * 3, answer = tennis_balls + bought_balls.
  3. The interpreter stores 5 in tennis_balls.
  4. It computes bought_balls as 2 × 3 = 6.
  5. It computes answer as 5 + 6 = 11.
  6. Output: 11. The model decided that the cans multiply and that the two totals add; the computer did both multiplications and the addition.
Example
A realistic case: a library's books

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
  1. Start with books = 4,815.
  2. Books on loan are not on the shelf, so subtract them: 4,815 − 1,377 = 3,438.
  3. Add the books returned: 3,438 + 856 = 4,294.
  4. The two boxes hold 2 × 150 = 300 books.
  5. Add the new books: 4,294 + 300 = 4,594.
  6. 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.

Sources