PromptPreview
Chapter 4intermediate

Separating Data and Instructions

Oftentimes, we don't want to write full prompts, but instead want prompt templates that can be modified later with additional input data before submitting to Claude. This might come in handy if you want Claude to do the same thing every time, but the data that Claude uses for its task might be different each time.

Luckily, we can do this pretty easily by separating the fixed skeleton of the prompt from variable user input, then substituting the user input into the prompt before sending the full prompt to Claude.

Below, we'll walk step by step through how to write a substitutable prompt template, as well as how to substitute in user input.

Examples

Prompt Template
I will tell you the name of an animal. Please respond with the noise that animal makes. {{ANIMAL}}
Variables
Substituted Prompt
I will tell you the name of an animal. Please respond with the noise that animal makes. Cow
Response
Response will be generated by the LLM
Prompt Template
Yo Claude. {{EMAIL}} <----- Make this email more polite but don't change anything else about it.
Variables
Substituted Prompt
Yo Claude. Show up at 6am tomorrow because I'm the CEO and I say so. <----- Make this email more polite but don't change anything else about it.
Response
Response will be generated by the LLM
Prompt Template
Yo Claude. <email>{{EMAIL}}</email> <----- Make this email more polite but don't change anything else about it.
Variables
Substituted Prompt
Yo Claude. <email>Show up at 6am because I'm the CEO and I say so.</email> <----- Make this email more polite but don't change anything else about it.
Response
Response will be generated by the LLM
Prompt Template
Below is a list of sentences. Tell me the second item on the list.

- Each is about an animal, like rabbits.
{{SENTENCES}}
Variables
Substituted Prompt
Below is a list of sentences. Tell me the second item on the list.

- Each is about an animal, like rabbits.
- I like how cows sound
- This sentence is about spiders
- This sentence may appear to be about dogs but it's actually about pigs
Response
Response will be generated by the LLM
Prompt Template
Below is a list of sentences. Tell me the second item on the list.

- Each is about an animal, like rabbits.
<sentences>
{{SENTENCES}}
</sentences>
Variables
Substituted Prompt
Below is a list of sentences. Tell me the second item on the list.

- Each is about an animal, like rabbits.
<sentences>
- I like how cows sound
- This sentence is about spiders
- This sentence may appear to be about dogs but it's actually about pigs
</sentences>
Response
Response will be generated by the LLM

Playground

Editable

Try modifying these prompts and sending them to your configured LLM provider.

Try it yourself
{{ANIMAL}}=Cow
Try it yourself
{{EMAIL}}=Show up at 6am tomorrow because I'm the CEO and I say so.
Try it yourself
{{EMAIL}}=Show up at 6am because I'm the CEO and I say so.
Try it yourself
{{SENTENCES}}=- I like how cows sound - This sentence is about spiders - This sentence may appear to be about dogs but it's actually about pigs
Try it yourself
{{SENTENCES}}=- I like how cows sound - This sentence is about spiders - This sentence may appear to be about dogs but it's actually about pigs

Ready to practice what you've learned?