Prompt Engineering for Developers: 12 Patterns That Actually Work in Production

Prompt Engineering for Developers: 12 Patterns That Actually Work in Production

Prompt engineering for developers sounds like a writing skill, but in production it is an engineering skill. The prompt is code: it runs against an unpredictable system, it breaks when inputs change, and it needs tests like anything else you ship. This guide collects 12 llm prompt patterns that teams actually use to run LLM features day to day, from support bots to document summarizers.

Prompt engineering for developers starts with the boring parts

Most prompt failures in production have nothing to do with clever wording. They come from prompts that live in a Slack message, a hardcoded string buried in a handler, or three slightly different copies in three services. Nobody can tell which version is live, and nobody can roll back when a change makes answers worse. Treat the prompt as config: store it in a file or a database row with a version number, and deploy changes through the same review process as code. Log the prompt version with every request, along with the input and the output. When a user reports a bad answer, the version number tells you exactly which prompt produced it. If you want a deeper look at what to record, read our guide on how to monitor LLM apps in production.

The 12 patterns that actually work in production

These patterns are ordered roughly by how often they save you. Start with the first four and add the rest as your feature grows.

  1. Put role and task first. Open with who the model is and what it has to do, before any data: “You are a support agent for a SaaS billing product. Classify the ticket below into billing, bug, or feature request.” The model works better when it knows the job before it reads the input.
  2. State the output shape before the input. Tell the model the exact format, then give it the data. A prompt that says “Reply with JSON: {“category”: “…”, “confidence”: 0-100}” produces parseable output far more often than one that describes the task and hopes the format follows. Decide the shape first, write it into the prompt, and validate the result in code.
  3. Show two examples, not ten. Few-shot examples teach the model the edge cases: one normal case and one tricky case. Ten examples bloat the prompt, raise the token bill, and teach the model to copy examples instead of rules.
  4. Split big jobs into steps. A single prompt that reads a 40-page contract, extracts the dates, and drafts a summary will do all three badly. Chain the work: one prompt extracts the dates, a second checks them, a third writes the summary. The cost is extra calls and latency, so split only where the single-prompt version actually fails.
  5. Ask for the answer in the order you need it. If your code reads the first line, say so: “Put the verdict on the first line, then the explanation.” If you need reasoning, ask for it first and the verdict last, because models answer better when they work through the problem before committing.
  6. Constrain the source. When the model should answer from your documents, say so: “Answer only from the text below. If the text does not contain the answer, say ‘not found’.” This cuts most invented facts in retrieval features. It works only if the retrieved text is relevant, so a bad answer here usually means your search returned the wrong passage.
  7. Write a fallback for “I don’t know.” Tell the model what to do when the input is unclear or the request is outside its job: refuse politely, ask one clarifying question, or hand off to a human. A model that guesses is worse than a model that stops, because a wrong answer ships silently while a refusal shows up in your logs.
  8. Clean the input before it reaches the prompt. Strip control characters, cap the length, and mark where user text starts and ends. A ticket that contains “ignore previous instructions” should arrive wrapped in delimiters and labelled as untrusted data. Cleaning is cheap, and it stops a whole class of injection attempts.
  9. Keep the system prompt and the user template apart. The system prompt holds the stable rules: role, tone, safety rules, output format. The user template holds the changing parts: the ticket, the document, the question. When you mix them, every change to the task rewrites the rules too.
  10. Version prompts like code. Give every prompt a version, keep the old ones, and record which version served each request. Test a change on 5 percent of traffic first. When a new version performs worse, the fix is a one-line rollback. This is the pattern teams regret skipping most, because they only notice they need it after a bad deploy.
  11. Set temperature per task. Classification, extraction, and formatting want temperature 0: the same input should give the same output every time. Drafting and brainstorming can take 0.7 or higher. Put the temperature next to the prompt version so it is reviewed as part of the same change.
  12. Review a sample every week. Pull 20 random production outputs, read them, and sort the bad ones by cause: wrong prompt, wrong retrieved text, bad input, or model limit. The pattern of the failures tells you what to fix next. This review takes an hour and catches drift no automated check will spot.

Test prompts like you test code

A prompt without tests is a guess. Build a small golden set: 20 to 50 real inputs with the output you expect, covering the normal case, the edge cases, and the inputs that broke things before. This is basic llm evaluation: run the set every time you change a prompt, a model, or the retrieval step, and record pass or fail per case. A test that only checks “did it return JSON” is weak; check the fields that matter, such as the category or the extracted date.

Keep the golden set in your repo next to the prompt. When a user reports a bad answer, add that input to the set before you fix the prompt, so the same failure cannot come back unnoticed. Production llm prompts change often, and a small test set is the only thing that keeps those changes honest.

Keep prompts safe and cheap

Safety and cost are the same problem in production: both come from inputs you did not plan for. Prompt injection prevention starts with treating user text as untrusted data: delimiters, input cleaning, and a system prompt that states the rules. But the real defense is checking the output before it acts. Never let a model output trigger a payment, a deletion, or an email without a validation step in code. Read our walkthrough of how to add guardrails to LLM apps for the input and output checks that catch most of this, and the guide to preventing prompt injection in production for the attack patterns to test against.

On cost, the lever is tokens. Long prompts with ten examples, full documents pasted in, and chat histories that grow without a cap are the usual waste. Short prompts, two examples, and a history window that keeps only the last few turns cut the bill without hurting quality. Measure cost per request in your logs, not per month in the provider dashboard, so you can see which prompt version is eating the budget. See our guide on how to cut LLM API costs with token optimization for the practical moves.

Where these patterns fall short

Patterns help, but they do not fix a weak model or a bad data pipeline. If the retrieved documents are wrong, no prompt will produce a right answer. If the task needs exact arithmetic over long tables, a language model will guess no matter how careful the wording is; call a calculator or a database instead. When a prompt grows past a page, split the task or move the rules into code. The goal is a prompt a new teammate can read and understand in two minutes.

A rollout checklist for your next prompt

  • Write the output shape and the fallback behavior before the first draft.
  • Keep the prompt in version control with a version number and a temperature.
  • Build a golden set of 20 real inputs and run it before every change.
  • Log prompt version, input, output, and cost for every request.
  • Test with injection attempts and with empty, huge, and garbled inputs.
  • Roll out to a small share of traffic and read the first 50 real outputs yourself.

What to do next

Pick one feature and apply the first four patterns this week: role and task first, output shape stated up front, two examples, and a written fallback. Add the golden set before you change anything else, because it is the tool that makes every later change safe. If you want a broader starting point, our earlier prompt engineering guide for developers covers the basics these patterns build on.

Ditulis oleh

Firdaus Safari Putra

Founder, PT FirKaizen Digital Solutions

Firdaus Safari Putra adalah founder PT FirKaizen Digital Solutions. Ia membangun dan mengoperasikan infrastruktur server di balik panduan ini, dan menulis berdasarkan apa yang benar-benar ia jalankan, uji, dan ukur — bukan teori.