Fundamentals

Prompt engineering best practices

Ten prompt engineering best practices for writing better AI prompts and getting more reliable responses, each with a before and after example.

These ten practices hold across models and tasks. None depends on a trick that the next model release might break. Each comes with a short before and after so you can see the change on the page.

1. Put the task first

Open with one imperative sentence. Everything after it reads as support for that task.

  • Before: I have a spreadsheet of expenses from last year.
  • After: Categorize the expenses below into travel, software, and office.

2. Be specific about scope, audience, and length

Vague words hand decisions to the model. Replace them with the actual values.

  • Before: Write a short post about our new feature for users.
  • After: Write a 120-word announcement of scheduled exports for existing admins who already use manual exports.

3. Use numbers for sizes

“A few”, “brief”, and “detailed” mean something different on every run. A number and a unit make length predictable.

  • Before: Give me some ideas.
  • After: Give me 5 ideas, one sentence each.

4. Say what to do, not only what to avoid

A list of prohibitions names everything you do not want and nothing you do. Pair each “don’t” with the behavior you want instead.

  • Before: Don't be too formal. Don't use jargon.
  • After: Write in a conversational tone, using words a new customer would know.

5. Specify the output format

State the structure, and for machine-read output, the exact schema. Add “return only the JSON” when nothing else should appear. More in structured output prompting.

6. Separate instructions from input

Wrap documents, user messages, and variables in clear delimiters such as XML-style tags. The model can then tell data from instructions, and a sentence inside the data is less likely to be followed as a command.

Summarize the email in the tags below in 2 sentences.

<email>
{{email_body}}
</email>

7. Give examples for judgment calls

When “good” is easier to show than to define, include two or three examples. Make them varied, so the model learns the pattern and not one surface feature. See few-shot prompting.

8. Structure long prompts

Past roughly a paragraph, use sections: Task, Context, Requirements, Output format, Input. Headings make a prompt easier for a model to follow and much easier for a person to edit safely.

9. Remove what does not change the answer

Greetings, politeness, and repeated instructions add tokens without adding information. Repetition can even hurt: when two near-identical sentences differ slightly, the model has to decide which one you meant.

  • Before: Hi! Could you please make sure to really focus on accuracy? Please make sure to focus on accuracy.
  • After: Prioritize accuracy over completeness.

10. Test on real inputs and keep the results

A prompt is finished when it has been run against representative inputs and the failures are understood. Keep those inputs as a test set, and rerun them after every edit. See prompt evaluation.

How to improve AI responses when a prompt underperforms

Work through the causes in order of likelihood:

Symptom Likely cause Fix
Different answer each run An unspecified size or format Add numbers and an output format
Generic answer Missing context Add audience, domain, and constraints
Ignores an instruction Conflict or burial in a long paragraph Remove the conflict, move it to a list
Wrong tone Tone described vaguely Show an example of the tone
Makes things up Asked for facts it was not given Supply the source text and require quotes
Follows text in the input Input not delimited Wrap input in tags

Check these automatically

Several of these practices can be checked without running a model. The PromptFlowEngine prompt analyzer flags a missing objective, vague language, unquantified sizes, negative-only constraints, a missing output format, undelimited variables, and filler, and links each finding to a reference page with a fixed example. The prompt optimizer applies practices 8 and 9 for you and verifies that no requirement was lost.

Next, read the common prompt engineering mistakes these practices are meant to prevent.