Skip to content

Lesson 4 ยท Prompting Claude

Examples, Structure and XML Tags

Use examples to show the output you want, XML-style tags to separate instructions from data, and roles to set a viewpoint, with templates you build in Python.

Some things are hard to describe and easy to show. A tone, a labeling style or an exact layout is often clearer from two examples than from ten rules. And once a prompt contains instructions, examples and pasted documents, you need a way to keep them apart. This lesson covers both.

Use examples on purpose

Giving examples is often called few-shot prompting; see zero-shot and few-shot prompting for the general idea. Claude pays close attention to details in examples, including ones you did not mean to teach. If every example is exactly one sentence, expect one-sentence answers.

Good examples are:

  • Relevant: close to your real inputs.
  • Varied: they cover different cases, including a hard or borderline one.
  • Correct: a mistake in an example is likely to be copied.
  • Clearly marked as examples, not as the task itself.

Separate parts with XML tags

XML-style tags are a simple way to label sections. The tag names are not magic, and you do not need a fixed list. Choose names that describe the content, use them consistently, and refer to them in your instructions.

TEXT
You will label messages from users of a gym app.

<categories>
billing: charges, refunds, subscriptions
technical: crashes, login problems, sync errors
other: anything else
</categories>

<examples>
  <example>
    <message>I was charged twice this month.</message>
    <label>billing</label>
  </example>
  <example>
    <message>The app closes when I open my history.</message>
    <label>technical</label>
  </example>
  <example>
    <message>Do you sell water bottles?</message>
    <label>other</label>
  </example>
</examples>

<message>
{{customer_message}}
</message>

Label the text inside <message>. Reply with the label only.

The braces mark a slot that your program or you will fill. Tags help Claude tell the instructions from the material. They also make long prompts easier for people to edit.

You can ask for tagged output too, for example "put your final answer inside answer tags". That makes it easy to pull out one part. If a program needs guaranteed machine-readable data, prefer the API's structured outputs.

Build prompts from templates

When you fill a template with outside text, that text might contain your closing tag. Escaping the angle brackets stops pasted text from pretending to leave its box.

Python Fill a tagged prompt template safely

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Only your own closing tag remains. This is helpful hygiene, but it is not a security boundary. A model reads all the text, and instructions hidden in data can still influence it. The lesson on privacy, safety and prompt injection explains what else you need.

Give Claude a role

A short role can set the viewpoint, vocabulary and level of detail. In the API, it usually goes in the system parameter. In the apps, you can simply write it in your message or in project instructions.

TEXT
You are reviewing this lesson plan as an experienced teacher of adult
beginners. Point out places where a nervous first-time learner would get lost.

A role works best with a concrete situation and job. "You are the world's greatest expert" adds little. A role also does not give Claude knowledge or credentials it lacks, so a "doctor" role does not make medical answers safe.

Match the style you want

The style of your prompt can influence the style of the answer. A prompt written as plain paragraphs tends to invite paragraphs. If you want plain text for a form field that cannot show Markdown, say that directly: "Write plain text with no headings, asterisks or tables."

Measure examples with a tiny test

When you change examples, test the prompt again on a fixed set of inputs whose correct labels you wrote yourself. The program below is not a model. Its simple keyword rules stand in for the classifier so that you can see how scoring works.

Python Stand-in: score a classifier on a small test set

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

With a real model you would replace fake_classifier with an API call and keep the test list unchanged. Then you can compare prompt versions fairly.

When not to use examples

Examples are not always the best tool. If you want varied, creative output, a single example may pull every result toward it. In that case, describe the qualities you want, or give several examples that differ strongly and say that they only show the range. If the rule is simple and exact, such as "dates as YYYY-MM-DD", one sentence is shorter and clearer than a set of examples.

A checklist for structured prompts

Before you send a long prompt, read it once as a stranger and check these points:

  • Can you tell where each document and example starts and ends?
  • Does every tag you mention in the instructions really exist?
  • Do the examples agree with the written rules? If they conflict, the model has to guess which one you mean.
  • Is there an example of the hard case, such as a message that fits two categories?
  • Does the prompt say what to do when nothing fits?

The last point is easy to miss. A classifier with only "billing" and "technical" must force every message into one of them. An explicit "other" or "unclear" choice gives you a signal that a person should look. Keep your prompt template in a file under version control if a program uses it, so that you can see what changed when the results change.

Keep real data out of examples

Examples are often copied from real work. Replace names, addresses and account numbers with invented ones before you save a prompt, because saved prompts tend to be shared and reused.

Common mistake: examples that all look alike. If all your billing examples mention "charged twice", the model may learn the phrase instead of the category. Vary wording, length and difficulty.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the main use of XML-style tags in a prompt?

    Descriptive tags make the structure clear. They are optional and offer no encryption.
  2. 2. All your examples are one sentence long. What is likely?

    Models pick up patterns in examples, including unintended ones.
  3. 3. Does escaping pasted text fully prevent prompt injection?

    Hidden instructions can still affect a model, so you also need limits and review.
  4. 4. What should stay fixed when you compare two prompt versions?

    A fixed test set makes the comparison fair.

Summary

  • Show relevant, varied and correct examples when a format or style is hard to describe.
  • Use descriptive XML tags to keep instructions and data apart.
  • A concrete role sets the viewpoint but adds no real credentials.
  • Test prompt changes against a fixed set of inputs.

Next: Long Documents and Multi-Step Prompting

Your progress is saved in this browser. No account needed.