In a production prompt for generating a single SQL query, what's the right way to make sure the model returns only the SQL without trailing commentary?
Use a stop sequence as the primary control, max_tokens as the cost and latency guardrail, and a post-hoc parser as the edge-case net. Each layer catches what the others miss.
Imagine you asked a chatty friend to write you a recipe and only the recipe. Three things keep them honest. First, you write 'END' at the bottom of the page and tell them to stop writing there; that is the stop sequence. Second, you only hand them a small piece of paper; that is max_tokens. Third, when they hand it back, you read only up to where it says END and ignore anything they scribbled after; that is the post-hoc parser. Any one alone fails sometimes. All three together always give you just the recipe.
Detailed answer & concept explanation~6 min readEverything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example.
Everything important, quickly.
3 min: stop sequence as primary control + max_tokens as guardrail + post-hoc parser as net + system-prompt as orientation + how the layers compose.
Real products, models, and research that use this idea.
- Anthropic's prompt-engineering cookbook explicitly recommends fenced code blocks with the closing fence as a stop sequence for Claude Opus 4.7 SQL and code generation, paired with one or two few-shot examples.
- OpenAI's structured outputs documentation shows the same fence plus stop pattern for GPT-5.5 code generation, alongside JSON mode for cases that need schema siblings.
- Vanna AI and other text-to-SQL production stacks running on Claude Sonnet 4.x and GPT-5.5 ship the three-layer pattern: stop sequence on the closing fence, max_tokens at the 99th percentile of historical SQL length, plus a sqlparse-based extractor as the parser layer.
What an interviewer would ask next. Try answering before peeking at the approach.
QHow do you pick a stop sequence that will not collide with legitimate output?
QWhen would JSON mode beat the fence plus stop pattern?
QHow does this pattern interact with streaming?
Don't say thisRed flags and common mistakes that signal junior thinking. Click to expand.
Red flags and common mistakes that signal junior thinking. Click to expand.
Relying on a system-prompt instruction alone to suppress commentary, then catching a regression weeks later when one in twenty SQL outputs ships with a trailing 'Note: this assumes X' that breaks the downstream parser.
The night-before-the-interview bullets. Scan these on the way to the call.
Primary sources. Skim if you want the original framing.
Same topic, related formats. Practice these next.