Structuring Prompts with Delimiters and Templates
A real prompt often mixes several kinds of content: your instructions, a document to work on, some examples and a question. If they run together, the model may treat part of the document as an instruction or miss where the data ends.
Delimiters
A delimiter marks where a section starts and ends. Common choices are triple quotes, Markdown headings and XML-style tags.
Summarise the customer review in one sentence.
<review>
I ordered the standing desk in March. It arrived two weeks late and one
leg was scratched, but support sent a replacement within three days and
the desk itself is sturdy and easy to adjust.
</review>
Tags have two advantages. They can be named for what they contain, and they can be nested. There are no special tag names. Choose names that describe the content and use them consistently.
Refer to sections by name
Once content is tagged, your instructions can point to it.
You will be given a contract and a list of questions.
<contract>
{contract text}
</contract>
<questions>
1. What is the notice period?
2. Who owns work created during the engagement?
</questions>
Answer each question using only the text in the contract tags.
If the contract does not answer a question, write "Not stated".
Several documents
Wrap each document separately and include its details.
<documents>
<document>
<source>refund-policy.pdf</source>
<content>
{text of the refund policy}
</content>
</document>
<document>
<source>shipping-policy.pdf</source>
<content>
{text of the shipping policy}
</content>
</document>
</documents>
Which document covers what happens when a parcel is lost? Quote the relevant sentence.
Where to put long content
With a long document, put the document first and your instructions and question after it. A question placed at the end, close to where the model starts writing, tends to get better answers than one placed before thousands of words of material.
Prompt templates
In an application, the same prompt is used again and again with different inputs. Separate the fixed part from the variable part.
template = """Classify the support ticket into one of these categories:
{categories}
<ticket>
{ticket}
</ticket>
Reply with the category name only."""
prompt = template.format(
categories="billing, technical, account, other",
ticket="I was charged twice for my subscription this month.",
)
print(prompt)
Templates make prompts testable: you can run one template against a hundred inputs and compare versions.
Ask for structured output with tags
You can ask the model to use tags in its answer, which makes the parts easy to pull apart in code.
Analyse the proposal below.
Put the three main risks in <risks> tags.
Put your overall recommendation, in one sentence, in <recommendation> tags.
<proposal>
{proposal text}
</proposal>
A layout that works for most prompts
1. Role or context for the task
2. Long documents or data, in tags
3. Detailed instructions and rules
4. Examples, in tags
5. The specific question or input
6. The output format
You will not always need every part, but keeping this order makes prompts easier to read, for you and for the model.
Practice
Take a prompt you have written recently that included pasted text. Rewrite it with the text in tags and the question after the text.