Skip to main content

Prompt Engineering Best Practices

Prompt engineering turns vague requests into reliable LLM output through clear instructions, examples, and structured formats. It is the fastest, cheapest quality lever teams have before fine-tuning, and it deserves the same testing and versioning as code.

Organization
OpenAI
Published
Mar 1, 2023

Best Practice: Prompt Engineering

Prompt engineering is the discipline of writing instructions, context, and examples that steer a large language model (LLM) toward accurate, consistent output. A model is only as useful as the prompt that drives it. Clear role definition, explicit constraints, worked examples, and structured output formats turn a vague request into a reliable result. This matters because small wording changes can swing quality and cost dramatically, and good prompts are the cheapest, fastest lever teams have before reaching for fine-tuning.

Prompt engineering sits at the lowest-cost, fastest-iteration layer of the AI stack. Before a team invests in retrieval pipelines or fine-tuning, a well-structured prompt often closes most of the quality gap. The core ideas are simple but easy to neglect: be explicit about the task, give the model the context it needs, show examples of the desired output, and constrain the format so downstream systems can parse the result. As applications mature, prompts become first-class assets that deserve source control, review, and automated testing, exactly like the code that calls them.

Step-by-Step Implementation Guidance

  1. State the task and the desired output format up front; be specific and concrete.
  2. Give the model a clear role and the constraints it must respect.
  3. Provide a few high-quality examples (few-shot) when the task has a consistent shape.
  4. Ask for step-by-step reasoning for complex tasks, then a final answer.
  5. Specify a structured output schema (JSON, headings) the consumer can parse.
  6. Place long context first and the instruction last so it stays salient.
  7. Iterate against a test set; track which prompt versions win.
  8. Version-control prompts and treat changes like code changes.

A few techniques carry most of the value. Few-shot prompting shows the model two to five worked examples so it infers the pattern. Chain-of-thought prompting asks the model to reason step by step before answering, which improves accuracy on multi-step tasks. Role and system prompts set persistent behavior and constraints. Structured output, where you specify a JSON schema or a fixed set of headings, makes the result machine-parseable and reduces brittle string handling downstream.

Common Mistakes Teams Make When Ignoring This Practice

  • Vague instructions that leave the model to guess intent.
  • No examples for tasks that clearly benefit from them.
  • Mixing many tasks into one overloaded prompt.
  • Treating prompts as throwaway strings with no versioning or testing.
  • Ignoring output format, then parsing fails downstream.

Tools and Techniques That Support This Practice

  • Techniques: few-shot prompting, chain-of-thought, role prompting, structured outputs.
  • Tooling: prompt management in LangSmith, PromptLayer, Humanloop.
  • Evaluation: golden test sets, A/B prompt comparison, LLM-as-judge.

How This Practice Applies to Different Migration Types

  • Cloud Migration: Prompt a model to summarize infra-as-code diffs and flag risky changes.
  • Database Migration: Use structured prompts to generate and validate schema mappings.
  • SaaS Migration: Draft consistent integration code from API docs with few-shot examples.
  • Codebase Migration: Guide translation between languages with clear constraints and tests.

Checklist

  • Task and output format stated explicitly
  • Role and constraints defined
  • Examples added where they help
  • Reasoning requested for complex tasks
  • Output schema specified
  • Prompts versioned and tested
  • Winning prompt selected against a test set