Principles for Prompt Engineering

Karina Nguyen, Anthropic55:31 · Oct 2023 · 9,260 views
Thumbnail for Principles for Prompt Engineering Watch on YouTube
TL;DR
  1. 1

    Prompt engineering is an iterative form of writing that requires clear objectives, constraints, examples, and hypotheses about what a model can do.

  2. 2

    Claude responds well to consistent XML tags, explicit formatting, examples, and instructions that ask it to quote source material or admit when information is missing.

  3. 3

    For long-document question answering, placing the question at the end of the prompt worked better than placing it at the beginning in Anthropic's evaluation.

Summary

Karina Nguyen explains prompt engineering through her work on Claude at Anthropic, including efforts to reduce hallucinations and improve self-correction. She describes prompting as a form of creative and technical writing. Engineers need to state the task clearly, use consistent structure, add useful examples, and iterate from hypotheses about the model's strengths and weaknesses. The talk gives practical Claude patterns for recommendation systems, long-context question answering, clustering, evaluation, and hallucination reduction. Nguyen recommends XML tags for separating parts of a prompt, explicit human and assistant formatting in the API, source quotes, refusal or uncertainty instructions, and diverse examples. She also describes decomposition, where a complex question is split into independent subquestions before the answers are recombined. In Anthropic's long-document tests, putting the question at the end worked better than putting it at the beginning. She expects prompting to remain part of product development as models generate data, evaluations, titles, and personalized experiences.

Key ideas
00:53

Prompting works by changing what the model is likely to generate

Nguyen describes language models as estimating the probability of each next word from the preceding text. A well-written prompt can therefore make desired phrases more likely. Attention mechanisms let the model focus on parts of the input, so task-specific keywords, context, and examples help direct that attention. Prompting also uses inference-time compute without retraining the model. She says prompting is hard for three reasons: people may know the goal but not how to get good model performance, they may not explain a vague goal clearly, or they may not know what they want at all.

04:05

Prompt engineering is an iterative form of creative writing

When the task is only partly clear, Nguyen recommends giving the model many diverse examples that cover edge cases. She compares this work to creative writing and says research engineers spend substantial time collaborating on prompts. The process begins with hypotheses, such as asking whether the model can self-correct. Engineers test those assumptions, observe where the model performs well or poorly, and revise the prompt. Her broader point is that prompting requires originality because a failed prompt often needs a new way of expressing the task.

04:58

Good prompts state the task clearly and keep their structure consistent

Nguyen's writing principles include clarity, conciseness, coherence, consistency, direction, grounding, and engaging examples. Prompts should use simple language, include only needed information, put context before the task, and give guidance about genre, length, or style. Formatting should remain consistent, including terminology and XML tags. Long documents and search results should be grounded with quotes or supporting information. Examples should be diverse and should cover the cases the model will actually see. She says Claude benefits from being addressed in an elaborate, human-like way.

10:54

XML tags and explicit output formats make Claude easier to control

In a clothing recommendation example, Nguyen separates the user query, item description, and answer with XML tags. The model is told to answer whether an item is relevant and to put the result in answer tags. She says Claude responds particularly well to XML and that consistent tags make the output easier to extract. A direct instruction such as asking for only the tagged answer can prevent extra explanation. She also shows how the prompt can be extended with a critique or thought section, criteria such as season and requested attributes, and a score from one to ten.

18:37

Decomposition can make long-context reasoning more faithful

Nguyen distinguishes ordinary chain-of-thought prompting from decomposition. Chain of thought asks the model to reason through a question in one sequence, where earlier steps can influence later ones. Decomposition asks the model to turn a complex question into independent, self-contained subquestions. Each subquestion includes the information it needs and is answered separately. The answers are then placed together in a new context, where the model answers the original question. She presents this as a way to reduce bias between reasoning steps and discusses research suggesting decomposition can approach chain-of-thought performance on some question-answering tasks while improving faithfulness.

26:37

Long-document question answering depends on prompt placement and source quotes

Anthropic evaluated Claude's ability to recall information from long documents by using a government document published after the model's training cutoff. They split the document into sections, used Claude to create multiple-choice questions, reassembled sections into long inputs, and measured whether Claude answered correctly. Their tests covered documents of 70K and 95K tokens, different answer positions, and different numbers of examples. Nguyen says asking the question at the end performed much better than asking it at the beginning. Pulling relevant quotes into a critique or thought section also improved accuracy, with a small latency cost.

35:33

Examples and disagreement checks can support automatic labeling

For cluster labeling, Nguyen describes embedding text, grouping it with clustering, and asking the model to name each group. Sampling several labels independently can support self-consistency, especially for quantitative questions where a majority answer can be selected. Another model can compare two samples and judge whether they agree. When Claude misses a category's nuance, Nguyen recommends contrastive instructions: show categories that are too narrow or too broad, use examples from different contexts, add analogies, and explain common misconceptions. The goal is to define the intended boundary rather than simply add more examples.

40:08

Claude API interactions need exact role formatting and uncertainty instructions

Nguyen recommends the human-assistant format for Claude API prompts, with alternating human and assistant turns. She says the model can be primed with an assistant response such as 'Yes, I understand the instructions' and can be asked to repeat the instructions. To reduce hallucinations, prompts should allow answers such as 'I don't know' or 'I don't have enough information.' When asking for document quotes, the prompt should also tell Claude to say that it found no relevant quotes when none exist. Examples should match the classification task, be diverse, and distribute answer types so the model does not always select the same option.

"The goal is to write prompts that clearly communicate the task objective while providing just enough constraints and guidance to steer the model towards producing high quality and relevant outputs."04:58
Who should watch
  • You are building Claude-powered features and need concrete prompt patterns for output formatting, source grounding, and uncertainty handling.
  • Your application feeds long documents to a model and you need ideas for evaluating recall and choosing where to place instructions.
  • You are creating labels, recommendations, titles, or evaluation data with language models and want methods for examples, decomposition, and consistency checks.