Paper commentary
Prompting Is Programming: A Query Language for Large Language Models
- Problem Driving a language model through its decoder takes many model calls, and constraints on legal words or phrases must be wired in by hand, which makes interactive prompts costly and hard to control. Each token costs a call, and pay-per-token APIs make wasted generation expensive.
- Method Introduces LMQL, a query language whose where-clause constraints are checked on partial outputs and turned into token masks during decoding.
- Results Reached 34.52% accuracy on Odd One Out with GPT-J-6B, against 33.33% for Standard Decoding, while Model Queries fell by 43.16%. Reduced Billable Tokens by 84.93% on the GSM8K arithmetic case with GPT-J 6B.

Table 3 . Average performance statistics (over queries) for constrained LMQL chain-of-thought decoding compared with standard chunk-wise decoding for the Odd One Out and Date Understanding datasets.
Original paper · CC BY 4.0 · Luca Beurer-Kellner, Marc Fischer, Martin Vechev · Prompting Is Programming: A Query Language for Large Language Models · Original paper figure · Extracted the figure area from page 18 of the original PDF.Why driving a language model is hard to control
A language model emits one sub-word token per step, so a single answer can need many calls, and each call gets more expensive as the prompt and the generated text grow. Interactive techniques add a further problem, since they need repeated exchange between the model and a user, or task-specific control logic written outside the model.
Meta prompting makes the difficulty concrete: the model first names an expert for a question, and that name has to be copied into a second prompt before the answer is generated. Done by hand, each step is run separately and the decoder treats the expert name as fixed text, so the name is never chosen jointly with the answer. The manual loop also keeps a person in the process, which blocks automated use over many inputs.
For API-gated models, every processed and generated token is billed, so a wasted call translates directly into cost.
A query language with holes, scripting and constraints
Language model programming keeps the natural-language character of prompting but adds lightweight scripting and constraints on the output, which lets the user work at a level that hides tokenization and model architecture. LMQL is the language that implements this idea, combining declarative SQL-like elements with an imperative scripting syntax, and its runtime needs only a change in the decoder logic.
The query body behaves like a Python function body with two additions: each top-level string is a direct prompt to the model, and a bracketed name marks a hole that the model fills, while a braced name inserts a value already in scope. A program names a decoder such as argmax, sample or beam, a model string, and an optional where clause that constrains the holes; a distribute clause can end the program.
Fig. 1a builds a joke from two question-and-answer pairs, with stop phrases and word and character counts on the holes, while Fig. 1b loops over a Python list to generate two items, each limited to at most two words. The same query can produce different traces depending on the decoder, so argmax yields one list while sample with n=2 returns two traces.
The where clause accepts conjunctions and disjunctions of comparisons and membership tests, and any deterministic pure Python function can be used in it. Built-in functions such as words, sentences, stops_at and len get additional semantics, which the runtime uses for early validation and mask generation. A distribute clause does not generate its last variable; it scores each value of the given set and returns a probability for each, which suits classification such as sentiment.
Turning constraints into token masks
The query runs line by line with an interaction trace that starts empty; each top-level string is appended, each hole is decoded and stored in the scope, and a placeholder is replaced by its current value. A naive runtime would generate a full sequence, check it, and backtrack on failure, which spends many model calls on sequences that are later dismissed.
Each expression is evaluated twice: value semantics gives the ordinary result, and final semantics attaches one of four annotations, fin, var, inc or dec, that says whether the value is fixed, may still change, or only grows or shrinks monotonically. Final annotations let a conjunction be decided as soon as one side is definitely false, even while the other side still depends on future tokens.
Because validation runs on each new token, invalid partial outputs are rejected before the sequence is complete.
A FollowMap takes the trace so far and a candidate next token, and returns the approximated future value of an expression together with a final annotation. Take a hole that holds Steph under a constraint that the text must be one listed name: the FollowMap marks a candidate as definitively violating unless it continues the name, so the decoder can mask all other tokens.
Because one string can be split into sub-word tokens in several ways, the mask has to include every vocabulary token that can begin a valid continuation.
The construction is sound rather than complete: a token is masked only when the constraint would definitely fail after it, so some tokens that could still fail later are kept, and constraints of this kind fall back to backtracking. The authors relate this limit to context-free parsing and give Brzozowski derivatives as a second view of the same soundness property.
Chain-of-thought results on Odd One Out and Date Understanding
The chain-of-thought study compares an LMQL program with a generate()-based Python program on Odd One Out and Date Understanding, using GPT-J-6B and OPT-30B. The LMQL program gives two few-shot examples, limits REASONING to fewer than 40 words, disallows newlines and the word Pick, stops at several phrases, and decodes RESULT as a distribution over the options, with REASONING decoded by argmax.
Model Queries fall from 73.04 to 41.51, a 43.16% reduction, and Decoder Calls fall from 7.96 to 5.96. With OPT-30B, accuracy is 34.52% under both decoders, and Model Queries fall from 73.04 to 40.70.
On Date Understanding, accuracy is 22.89% under both decoders with GPT-J-6B and 29.16% under both with OPT-30B. Model Queries drop from 103.38 to 57.26 with GPT-J-6B and to 57.00 with OPT-30B, and Billable Tokens drop from 4131.28 to 2844.90 with GPT-J-6B.
The authors trace the Odd One Out gain to the REASONING step: the word limit and the disallowed phrases change the reasoning, and RESULT is conditioned on that reasoning, so the answer changes too. Standard Decoding cannot enforce these constraints, so its reasoning differs and the comparison includes that difference.
The running text gives the query reduction as up to 41% and the billable token reduction as up to 31%, while the table lists Model Queries reductions of 43.16% to 44.86% across these runs; the table values are the ones used here. LOC of the LMQL program is 26% of the baseline in one comparison and 34% in the other, according to the text.
As a control, the same queries run on text-davinci-003 keep Odd One Out accuracy at 42.86%, and Date Understanding rises from 85.29% under Standard Decoding to 86.10% under LMQL.
On-the-fly arithmetic evaluation on GSM8K
The arithmetic case follows the ReAct idea: when the model opens an arithmetic expression, the query evaluates it with a calculator and writes the result back into the trace, after which generation continues. The query captures REASON_OR_CALC, EXPR and RESULT, stops at the calculation marker and at the answer phrase, and constrains RESULT to an integer.
The baseline makes one decoder call per arithmetic expression, so it needs 7 decoder calls, while LMQL uses 1 because the evaluation happens inside one loop. The text gives the model query reduction as 65%, while the table gives 66.19%; the table value is the one used here.
The LMQL program is 18 lines of code, while the Python baseline is 78 lines. GPT-J 6B does not solve this problem correctly, so the case shows how the loop works rather than an accuracy gain.
Table 5 also reports the ReAct case, where Decoder Calls fall from 5 to 1, Model Queries from 150 to 95, and Billable Tokens from 3,404 to 807.
Limitations
The generate() baseline works chunk by chunk and does parsing and validation by hand, so multi-token and character-level constraints cannot be enforced there. The comparison therefore measures LMQL against a baseline that lacks token-level control, and the OpenAI API only allows logit masking over complete sequences.
The evaluation uses GPT-J 6B, OPT-30B and gpt2-xl, which are publicly available and accessible for the decoding loop, while GPT-3 is evaluated only in a limited way. Cost figures are estimates from Billable Tokens at $0.02 per 1K tokens for GPT-3 davinci, not measured invoices.
No user study with prompt engineers was run, so the usability claims rest on the three case studies and on lines of code.
Because the follow abstraction over-approximates valid tokens, some constraints can only be enforced by backtracking.
The accuracy results are the same or slightly better in the reported runs, and the chain-of-thought gain comes with the change in reasoning that the constraints cause.
Paper info
- Authors
- Luca Beurer-Kellner, Marc Fischer, Martin Vechev
- Venue
- arXiv · Preprint
- First published
- 2022-12-12
- Added
- 2026-11-6
- Updated
- 2023-5-30
- Version
- v3
- arXiv
- 2212.06094
- DOI
- 10.1145/3591300
- Category
- cs.CL · cs.AI
- Original paper terms
- CC BY 4.0
- What we show
- Paper info, commentary and permitted figures from the original paper
AI commentary is a separate explanation from the original paper and may contain errors. The original paper and figures follow each source's terms of use and any rights noted separately.
Abstract
Large language models have demonstrated outstanding performance on a wide range of tasks such as question answering and code generation. On a high level, given an input, a language model can be used to automatically complete the sequence in a statistically-likely way. Based on this, users prompt these models with language instructions or examples, to implement a variety of downstream tasks. Advanced prompting methods can even imply interaction between the language model, a user, and external tools such as calculators. However, to obtain state-of-the-art performance or adapt language models for specific tasks, complex task- and model-specific programs have to be implemented, which may still require ad-hoc interaction. Based on this, we present the novel idea of Language Model Programming (LMP). LMP generalizes language model prompting from pure text prompts to an intuitive combination of text prompting and scripting. Additionally, LMP allows constraints to be specified over the language model output. This enables easy adaption to many tasks while abstracting language model internals and providing high-level semantics. To enable LMP, we implement LMQL(short for Language Model Query Language), which leverages the constraints and control flow from an LMP prompt to generate an efficient inference procedure that minimizes the number of expensive calls to the underlying language model. We show that LMQL can capture a wide range of state-of-the-art prompting methods in an intuitive way, especially facilitating interactive flows that are challenging to implement with existing high-level APIs. Our evaluation shows that we retain or increase the accuracy on several downstream tasks, while also significantly reducing the required amount of computation or cost in the case of pay-to-use APIs (26-85% cost savings).
Quote from the paper
Luca Beurer-Kellner, Marc Fischer, Martin Vechev (2022). Prompting Is Programming: A Query Language for Large Language Models. arXiv:2212.06094v3. https://doi.org/10.1145/3591300
BibTeX
@article{beurerkellner2022prompting,
title = {{Prompting Is Programming: A Query Language for Large Language Models}},
author = {Luca Beurer-Kellner and Marc Fischer and Martin Vechev},
year = {2022},
eprint = {2212.06094},
archivePrefix = {arXiv},
doi = {10.1145/3591300},
url = {https://arxiv.org/abs/2212.06094v3}
}