In a few-shot prompt that extracts structured fields from invoices, why is a distinctive stop sequence (like a custom END_INVOICE marker) better than a paragraph break or the natural end of response?
You're an LLM engineer designing a few-shot prompt that extracts structured fields (vendor, total, line items) from invoice text. You're picking how the model should signal the end of each extracted record. Walk through why a distinctive stop sequence (like a custom END_INVOICE marker) is better than a paragraph break or relying on the model's natural end of response, and what properties make a stop sequence robust.
A marker like END_INVOICE is collision-safe, self-anchoring through few-shot demos, decoupled from format changes, and composable across tasks. Pair it with the API's stop_sequences for byte-level enforcement.
Imagine you are training a friend to read invoices and write down what they see. You could tell them 'stop when you reach a blank line' but invoices have blank lines inside them. Instead you say 'when you are done with each invoice, write THE END on its own line'. Now you have a clear signal that will not be confused with anything inside the invoice. You also show them ten example invoices that all end with THE END, so they pick up the habit. And you tell the machine reading their notes to ignore anything after THE END. Three layers, one clean signal.
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: four properties of a good terminator (collision-safe, self-anchoring, decoupled, composable) + the three-layer contract (few-shot, model, API) + parser as safety net.
Real products, models, and research that use this idea.
- Anthropic's prompt-engineering cookbook recommends XML-style closing tags as stop sequences for structured extraction with Claude Opus 4.7, with the tag demonstrated in every few-shot example.
- OpenAI's GPT-5.5 vision-extraction examples for invoices and receipts pair a distinctive END_RECORD sentinel with the stop parameter and few-shot demonstrations of the terminator.
- Stripe's document-extraction pipelines for KYC and receipt processing run on Claude Sonnet 4.x and Llama 4 finetunes with distinct terminators per record type, enabling the downstream parser to split concatenated outputs unambiguously.
What an interviewer would ask next. Try answering before peeking at the approach.
QHow would you handle a case where the model genuinely needs to emit content containing your terminator?
QWhat is the parser layer's job when the model fails to emit the terminator?
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.
Using a paragraph break as the terminator on a structured-extraction prompt, then watching extractions get truncated whenever the model produces a blank line inside a multi-line address or line-item block.
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.