Back to Blog

Prompt Engineering Patterns That Hold Up in Production

The prompt patterns that survive contact with real traffic: structured output, decomposition, self-checking, few-shot examples that earn their tokens, and prompts treated as versioned code.

A
Admin
·5 min read
Branded cover illustration for the article Prompt Engineering Patterns That Hold Up in Production

Demos tolerate clever prompts; production does not

A prompt that works in a notebook is tested against a handful of inputs chosen by its author. Production traffic brings empty fields, mixed languages, hostile users, inputs three times longer than expected, and requests that fall outside the feature entirely. The patterns below are the ones that keep working under those conditions.

Pattern 1: Make the output shape non-negotiable

Free-form text is expensive to consume. Anything downstream — a database write, a UI component, another service — needs a predictable shape.

  • Define the schema in your code first (a Zod or JSON Schema definition), then derive the prompt instructions from it, so the contract has exactly one source of truth.
  • Use the provider's structured-output or tool-calling mode when available; it constrains generation instead of merely requesting good behaviour.
  • Validate every response anyway, and treat validation failure as a normal, expected branch: retry once with the validator's error message appended, then fall back.

Ask for the smallest structure you actually consume. Every optional field is another thing the model can get wrong.

Pattern 2: Decompose before you elaborate

When a prompt starts accumulating clauses — extract, classify, summarise, and translate — quality degrades in ways that are hard to attribute. Splitting the work into separate calls costs more tokens and buys back three things: each step can be tested on its own, failures point at a specific step, and cheaper models can handle the easy steps.

A common shape: a small, fast model classifies or routes the request; a stronger model handles only the cases that need reasoning. Most traffic never reaches the expensive path.

Pattern 3: Ground the model, then constrain it

Most incorrect answers are the model filling gaps you left. Supply the facts explicitly — retrieved documents, database rows, the current date, the user's locale — and state what to do when the supplied facts are insufficient.

Two instructions carry disproportionate weight:

  • Answer only from the provided context.
  • If the context is insufficient, say so and stop.

Without the second, the first is a suggestion.

Pattern 4: Examples over adjectives

"Be concise and professional" is interpreted differently on every call. Two or three examples of input and desired output pin the behaviour far more tightly than a paragraph of description.

Keep examples honest about the hard cases: include an edge case, an empty input, and a case where the correct answer is a refusal. Examples are tokens on every request, so audit them periodically — an example that no longer changes behaviour is pure cost.

Pattern 5: Let the model check its own work, selectively

Asking for reasoning before the answer helps on genuinely multi-step tasks and wastes tokens on lookups and classification. Use it where it pays, and keep the reasoning out of the user-facing payload (a separate field, discarded after logging).

A cheap and effective variant is a verification pass: a second, narrowly scoped call that receives the source material and the proposed answer and returns only whether every claim is supported. Run it on high-stakes paths rather than everywhere.

Pattern 6: Prompts are code

Treat them accordingly:

  • Store prompts in the repository, not in a database row edited by hand, so changes are reviewed and revertible.
  • Version them and record which version produced each logged output. Without that, regressions are unattributable.
  • Keep an evaluation set of real inputs with expected properties, and run it on every prompt change. Twenty well-chosen cases catch most regressions.
  • Change one thing at a time. Simultaneous edits to a prompt and a model produce results nobody can interpret.

Pattern 7: Design for failure, because there will be failure

Model calls fail: timeouts, rate limits, refusals, malformed output, content filters. Decide in advance what each failure means for the user.

  • Set explicit timeouts and retry with backoff on transient errors only.
  • Cap output length and reject inputs beyond a documented limit instead of silently truncating.
  • Have a non-AI fallback path where one exists — a keyword search, a template, a clear error.
  • Never let a model decide an authorisation question. Enforce permissions in code before the call.

Pattern 8: Instrument what you cannot see

Log the prompt version, model and parameters, token counts, latency, validation outcome, and any retry, for every call. Sample and review outputs regularly, and give users a one-click way to report a bad answer that lands in your evaluation set.

Tail latency and cost per successful request are the two operational metrics that surprise teams most often; both are invisible without logging.

What actually moves quality

In practice, the biggest gains come from better context, not cleverer wording: the right documents retrieved, the right schema enforced, the right task split into pieces. Prompt wording matters, but it is the last few percent — and it is only measurable once the surrounding system is instrumented enough to notice a change.

Share: