[Paper Review] Prompting Is Programming: A Query Language for Large Language Models
This paper introduces Language Model Programming (LMP) and LMQL, a high-level query language that extends natural language prompting with scripting, constraints, and control flow to enable efficient, accurate, and cost-effective language model inference. LMQL reduces model calls and computation by up to 85% through on-the-fly token-level validation and optimized decoding, while maintaining or improving accuracy on downstream tasks.
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).
Motivation & Objective
- To address the inefficiency and complexity of existing prompting methods that require manual, model-specific implementations and high computational costs.
- To enable intuitive, high-level programming of language models without requiring deep knowledge of tokenization, decoding, or model internals.
- To reduce the number of expensive language model calls in interactive and constrained prompting workflows.
- To provide a portable, constraint-aware, and efficient interface for language model interaction across different models and APIs.
- To demonstrate that constrained, script-driven prompting can achieve state-of-the-art performance with significantly lower computational and monetary costs.
Proposed method
- Proposes Language Model Programming (LMP), a paradigm that unifies natural language prompting with lightweight scripting and output constraints.
- Introduces LMQL, a domain-specific language that allows users to write prompts with Python-like control flow, variable assignment, and list comprehensions.
- Employs token-level validation and constraint checking during decoding to avoid backtracking and reduce the number of model calls.
- Uses a query optimization engine that generates efficient inference procedures by analyzing constraints and control flow in LMQL programs.
- Supports integration of external validators (e.g., grammar checkers, code executors) via extensible operators, enabling semantic and syntactic validation.
- Executes LMQL programs by translating them into optimized inference pipelines that minimize expensive model invocations.
Experimental results
Research questions
- RQ1Can a high-level query language be designed to unify natural language prompting with scripting and constraints for large language models?
- RQ2Can such a language reduce the number of model calls and computational cost while preserving or improving accuracy?
- RQ3How does the integration of control flow and constraints in prompting improve efficiency and usability compared to existing APIs?
- RQ4To what extent can LMQL express complex, state-of-the-art prompting techniques like chain-of-thought or self-consistency in a concise and efficient way?
- RQ5Can LMQL achieve significant cost savings in pay-per-use API environments without sacrificing model performance?
Key findings
- LMQL reduces the number of model calls by up to 85% compared to baseline prompting methods, significantly lowering inference cost.
- The framework achieves or improves accuracy on multiple downstream tasks, with constraints sometimes enhancing output quality.
- LMQL programs are up to 10x shorter than equivalent Python-based implementations, improving developer productivity.
- The use of on-the-fly token-level validation eliminates the need for chunk-wise decoding and backtracking, improving efficiency.
- In pay-to-use API settings, LMQL reduces billable tokens by up to 80%, resulting in substantial cost savings.
- LMQL enables complex, interactive prompting flows (e.g., iterative reasoning, constrained generation) that are difficult to implement with existing high-level APIs.
Read this paper
Open the full text in the Nubint reader and ask about any passage as you read.
Read this paper on NubintThis review was generated by AI from the original paper. Please check the paper before citing it.