Skip to content

Prompts are code

Read · 1 of 3

Write prompts like you write functions

A prompt is the program you give the model. Treat it like code: build it from parts, keep it in version control, test it.

Three habits that work: be specific about the task and the format; show examples ("few-shot"); wrap each piece in tags like <example> so the model never confuses your instructions with your data.

Read this lesson as text

Clear instructions, examples and tags: prompts you can build and test.

Write prompts like you write functions

A prompt is the program you give the model. Treat it like code: build it from parts, keep it in version control, test it.

Three habits that work: be specific about the task and the format; show examples ("few-shot"); wrap each piece in tags like <example> so the model never confuses your instructions with your data.

Which prompt?

A = "Classify this review."

B = """Classify the review as exactly one word: positive, negative or mixed.
<review>
{text}
</review>"""

Which prompt will give your program answers it can rely on?

  • A: short prompts are better
  • B: it fixes the answer to one of three words and separates the data
  • Both are the same to a model
Show the answer

B: it fixes the answer to one of three words and separates the data. A leaves the format open ("This review seems quite positive overall!"), which your code then has to parse. B says exactly what to return and puts the review in tags, so text inside it can't be mistaken for instructions.

Exercise: Build a few-shot prompt

Input: an instruction line, then k, then k lines input => output, then a final query.

Print the prompt: the instruction in <instructions> tags, each example as <example>, Input: …, Output: …, </example>, then Input: <query> and Output: on the last line.