academic program
Often times, we don't want to write complete prompts, but rather we want to A prompt template that can be modified later based on additional input data before submitting it to Claude. If you wish to Claude It would be very useful to perform the same task each time, but the data Claude uses may be different each time.
Luckily, we can get the best of both worlds with the Separate the fixed frame of the prompt from the variable user input, then replace the user input in the prompt before sending the complete prompt to Claude, very easy to achieve this.
Below, we will demonstrate step-by-step how to write replaceable prompt templates and how to replace user input.
typical example
In the first example, we asked Claude to act as an animal sound generator. Note that the full prompt submitted to Claude (the third yellow box in the chain) simply replaces the input ("Cow" in the second box in this case) into the prompt template (the first box). Note that "Cow" replaces "{{ANIMAL}}" in the third yellow box.
Note: You don't actually have to give the substitution placeholder a special nameIn this example, {{ANIMAL}} is used because it is a format requirement for the exercise. In this example, {{ANIMAL}} is used because that is the formatting requirement for the exercise. But in general, we could just as easily refer to it as "{{CREATURE}}" or "{{A}}" (but It's usually best to make your placeholders specific and relevant(This makes the hints easy to understand and easy for the user to parse, even if there is no replacement). Just make sure that whatever you name the replacement placeholder, you use the same name in the replacement formula.
Example:
Input:
Prompt template
User: I will tell you the name of an animal. Please reply with the sound the animal makes. {{ANIMAL}}
+
Enter {{ANIMAL}}
Cow
=
Prompt after substitution
User: I will tell you the name of an animal. Please reply with the sound the animal makes. Cow
Output:
Claude's response
Moo.
Why are we separating and replacing inputs like this? Because.Prompt templates simplify repetitive tasks. Suppose you build a cue structure that invites third-party users to submit content (in this case, animals they want to generate sounds for). These third-party users don't have to write or view the full prompt. They just need to fill in the variables.
We're using a spreadsheet function for substitution here, but that's also coding best practice! We use the {{double braces}} format in our own code.
Attention:Hint templates can contain any number of variables.
When introducing substitution variables like this one, it's important to make sure that Claude knows where the variable begins and ends (separate from the directive or task description). Let's look at an example that doesn't separate instructions from substitution variables.
Example:
Input:
Prompt Template
User: Yo Claude. {{EMAIL}} <----- Make this email more polite, but don't change anything else.
+
Enter {{EMAIL}}
Be there tomorrow at 6am because I'm the CEO and I call the shots.
=
Replace the prompt with
User: Yo Claude. Be there tomorrow at 6am because I'm the CEO and I'm in charge. <----- Make this email more polite, but don't change anything else.
Output:
Claude's reply
Dear Claude.
I hope you are well. As CEO, I would like to request that you come to our office tomorrow morning at 6am. I realize this may be an early start time, but I believe it is necessary for the success of our company. If you have any concerns or conflicts about this time, please let me know and together we can find a suitable arrangement.
Thank you for your continued dedication and hard work. I look forward to seeing you tomorrow.
With best regards.
[Name of your CEO]
Here, Claude considers "Yo Claude" to be part of the email and rewrites it as "Dear Claude"! You can spot this because it starts with "Dear Claude" in the rewrite. It's pretty clear to humans, especially in the prompt template, where the email starts and ends, but in the replacement prompt, it's not so obvious.
How do we solve this problem?Wrap input in XML tags! We did this below, and as you can see, there is no longer a "Dear Claude" in the output.
XML tag are sharp bracketed tags like this one. They appear in pairs and consist of an open tag (e.g. ) and a closed tag (e.g. ) marked by a "/". XML tags are used to wrap content, e.g. content.
Attention: Although Claude recognizes and handles a wide range of separators and delimiters, we recommend that youSpecial use of XML tags as delimiters for Claudebecause Claude's training specifically focuses on using XML tags as a hint organization mechanism. In addition to function calls, theClaude is not trained to maximize your performance by specifically targeting certain XML tags.We intended Claude to be very flexible and customizable.
Example:
Input:
Prompt Template
User: Yo Claude. {{EMAIL}} <----- Please make this email more polite, but do not change its content.
+
Type {{EMAIL}}
Arrive at 6am, because I'm the CEO, so I'll say so.
=
Replace the prompt with
User: Yo Claude. Come in at 6 a.m. because I'm the CEO, so I say so. <----- Please make this email more polite, but don't change its content.
Output:
Claude's reply
Dear [your name].
I hope this email finds you in a pleasant mood. As the CEO, I would like you to arrive at 6:00 am. I understand that this may be a very early hour, but I believe it is necessary for the success of our organization. Please let me know if this time works for you, and thank you very much for your cooperation.
Sincerely.
[CEO's name]
Let's look at another example of how XML tags can help.
In the following hint, Claude misinterprets both the instruction part and the input part of the hint. Due to formatting issues, it incorrectly sees "Each is about an animal, like rabbits" as part of the list, which the user (the person who populates the {{SENTENCES}} variable) probably doesn't want.
Example:
Input:
Prompt template
User: The following is a set of sentences. Tell me the second sentence in the list.
- Each sentence relates to an animal, such as a rabbit.
{{SENTENCES}}
+
Enter {{SENTENCES}}
- I like the sound of cows bellowing.
- This sentence is about spiders
- This sentence looks like it's about a dog, but it's really about a pig.
=
Tip after substitution
User: Here is a set of sentences. Tell me the second sentence in the list.
- Each sentence relates to an animal, such as a rabbit.
- I like the sound of cows bellowing.
- This sentence is about spiders.
- This sentence looks like it's about a dog, but it's really about a pig.
Output:
Claude's response.
The second sentence in the list is "I like the sound of cows mooing".
To solve this problem, we simply wrap the user-entered sentence in XML tags. This will show Claude where the input begins and ends, despite the misleading dash before "Each is about an animal, like rabbits".
Example:
Input:
Prompt template
User: Here is a set of sentences. Tell me the second item in the list.
- Each sentence is related to an animal, such as a rabbit.
{{SENTENCES}}
+
Input {{SENTENCES}}
- I like the sound of a cow.
- This sentence is about spiders
- This sentence looks like it's about a dog, but it's really about a pig.
=
Replaced Prompt
User: Here is a set of sentences. Tell me the second item in the list.
- Each sentence is related to an animal, such as a rabbit.
<sentences
- I like the sound of a cow.
- This sentence is about spiders
- This sentence looks like it's about a dog, but it's actually about a pig
Output:
Claude's reply
The second item in the list is "This sentence is about spiders".
Attention: In the incorrect "Every sentence is about animals" prompt, we must include a hyphen in order for Claude to give the incorrect response in the example we want. This is an important lesson in prompt design:Details matter! It's worth the time.Check your prompts for spelling and grammatical errors. Claude is very sensitive to patterns (in its early stages, it was an unfine-tuned text-only prediction tool); it's more likely to make a mistake when you make a mistake, it behaves more intelligently when you appear smart, it behaves stupidly when you appear stupid, etc.
If you are ready to try the chapter exercises, click on the links below. Scroll down if you want to try any of the examples in this lesson!
practice
Exercise 4.1 - Haiku Themes
Write a prompt in the highlighted template box that accepts a variable named "{{TOPIC}}" and outputs a haiku about that topic. This exercise is designed to test your understanding of the variable template structure.
Example:
Input:
Prompt template
User: Write a haiku about {{TOPIC}}.
+
Input {{TOPIC}}
Pig
=
Replaced Prompt
User: Write a haiku about a pig.
Output:
Claude's reply
This is a haiku about a pig:
Arching snouts in the mud
Curly-tailed pink fat grunts
Pigs on a farm
Exercise 4.2 - Dog Problems with Spelling Errors
Fix the hints in the highlighted template box by adding XML tags so that Claude gives the correct answer.
Try not to change anything else in the prompt. This confusing and error-ridden writing is intentional and is intended to give you an idea of how Claude responds to these errors. If your prompt generates a correct answer, Claude's response will turn green.
Attention: While Claude is able to parse poor grammar and misspelled cues to some extent, theClearly written, spell-checked, and grammatically correct prompts will greatly reduce Claude's risk of making mistakes and significantly improve the quality of his output.
Example:
Input:
Prompt template
User: Hi it's me I have a question about dogs jkaerjv {{QUESTION}} jklmvca tx it helped me a lot very very much atx fst fst short short answer tx
+tx
Input {{QUESTION}}
Can a dog be brown?
=
Replaced Prompt
User: Hi it's me I have a question about dogs jkaerjv Can dogs be brown? jklmvca tx It helped me a lot, a lot, a lot atx fst fst short short answer tx
Output:
Claude's reply
Yes, dogs can be brown.
❓ If you need a prompt, click the plus sign (➕) on the far left of this line.
Exercise 4.3 - Dog Problems Part II
will notAdd XML tags to fix the prompts in the highlighted template box. Instead, remove only one or two words from the prompt.
Again, try not to change anything else in the prompt. This will demonstrate the type of language Claude is able to parse and understand. If your hint generates the correct answer, Claude's response will turn green.
Example:
Input:
Prompt template
User: Hi it's me I have a question about dogs {{QUESTION}} tx it helped me a lot very very much atx fst fst short short answer tx
+tx
Input {{QUESTION}}
Can a dog be brown?
=
Replaced Prompt
User: Hi it's me I have a question about dogs Can dogs be brown? tx It helped me a lot, a lot, a lot atx fst fst short short answer tx
Output:
Claude's reply
Yes, dogs can have brown coats. Brown is a common coat color for many breeds.
❓ If you need a prompt, click the plus sign (➕) on the far left of this line.