AI & ML

Prompt Engineering

The practice of crafting and optimizing input prompts to guide LLMs toward producing desired outputs, including techniques like few-shot learning and chain-of-thought.

Prompt engineering is the art and science of designing effective prompts to get the best results from large language models. A well-crafted prompt can dramatically improve output quality, accuracy, and consistency.

Key techniques include zero-shot prompting (directly asking without examples), few-shot prompting (providing examples of desired input-output pairs), chain-of-thought (asking the model to reason step by step), and role prompting (assigning a persona to the model).

Advanced techniques include tree-of-thought (exploring multiple reasoning paths), self-consistency (generating multiple answers and selecting the most common), and retrieval-augmented prompting (injecting relevant context from external sources).

System prompts define the model's behavior, personality, and constraints. They're essential for building consistent AI applications. Best practices include being specific about output format, providing constraints, and iterating based on failure cases.

How it works

Prompt engineering is changing the input to a model, rather than its weights, to get better output. Anthropic's guide lists three prerequisites before starting: a clear definition of success criteria, some way to test against them empirically, and a first-draft prompt to improve. Without a test set, changes are judged by impression.

Examples are one of the strongest levers. The same guide calls examples "one of the most reliable ways to steer" output format, tone and structure, and recommends 3–5 examples for best results. The GPT-3 paper had already shown that large models can pick up a task from a few demonstrations in the prompt alone.

Asking for intermediate reasoning helps on multi-step problems. Wei et al. (2022) found that prompting a 540B-parameter model with eight chain-of-thought exemplars, worked answers that show the steps, achieved state-of-the-art accuracy on the GSM8K maths word-problem benchmark at the time.

Structure reduces ambiguity. Anthropic recommends wrapping different kinds of content in their own XML tags (such as instructions, context and input), setting a role in the system prompt, and explaining why an instruction matters, since the reason gives the model more to work with than a bare rule.

Example

A structured prompt that separates instructions, examples and input, and asks for reasoning before the final answer.

System: You review refund requests for a software company.

<instructions>
Decide if the request qualifies under the policy. Refunds are allowed
within 30 days of purchase for annual plans. Think through the dates
in <reasoning> tags, then give APPROVE or DENY in <answer> tags.
</instructions>

<example>
Request: Bought annual plan on 3 May, asking on 20 May.
<reasoning>17 days since purchase, within 30.</reasoning>
<answer>APPROVE</answer>
</example>

<input>
Request: Bought annual plan on 1 January, asking on 15 March.
</input>

The tags make it clear which text is policy, which is an example and which is the case to decide, and the answer tag makes the result easy to parse. Changes to wording are then measured against a set of test requests with known correct answers.

Common confusion

Is prompt engineering still needed with newer models?

Models cannot guess unstated requirements such as the output format, the audience or the edge cases that matter. Clear instructions, examples and evaluation stay useful whatever the model; Anthropic's guide also notes that some problems, such as latency or cost, are better solved by choosing a different model.

What is the difference between prompt engineering and fine-tuning?

Prompt engineering changes the input and takes effect immediately with no training. Fine-tuning changes the model's weights using example data, which takes longer and needs retraining when requirements change. Prompting is usually tried first.

Further reading

Want to learn more?

Explore more developer terms or read in-depth articles on the blog.

Browse all terms