When talking to someone, it's easy to forget they might lack the context that you have in your head. The same is true when prompting Claude. To reduce the risk of misinterpretation we need to be explicit and remove any ambiguity from our words.
Try writing prompts as though you're speaking to a naive nine-year-old child who happens to have an encyclopedic knowledge and exceptional vocabulary. You might include details like why you want the task done, what background information you're going to give them, and so on.
Prompt length
If you’re worried a verbose prompt will be expensive, keep in mind that we charge substantially less for prompt characters than for completion characters.
Be specific
To avoid ambiguity we must set the context and be as specific and descriptive as necessary:
How big is New York?
How big is New York state relative to other states, both by area and by population?
Or, a more creative example:
Write me a poem.
Write a Shakespearean sonnet about the mortality of specific large language model versions. You should write in iambic pentameter and use a rhyme scheme of ABAB CDCD EFEF GG.
Give examples
Including a few examples within your prompt can help improve accuracy considerably. This is known as few-shot, as opposed to one-shot answering. It gives the model a better idea of how you want it to answer.
Human: Think of a funny collective noun for nurses.
Assistant:
There are two ways to give examples. One is by putting words in Claude's mouth by crafting a previous conversation:
Human: Think of a funny collective noun for dancers.
Assistant: A pirouette of dancers.
Human: Think of a funny collective noun for hipsters.
Assistant: A fedora of hipsters.
Human: Think of a funny collective noun for engineers.
Assistant: A nerd herd of engineers.
Human: Think of a funny collective noun for nurses.
Assistant:
Another is by providing examples directly:
Human: I'd like you to suggest funny collective nouns. For example:
A funny collective noun for dancers could be "a pirouette of dancers"
A funny collective noun for hipsters could be "a fedora of hipsters"
A funny collective noun for engineers could be "a nerd herd of engineers"
Think of a funny collective noun for nurses.
Assistant:
Which of these is more effective is nuanced and can depend on the specific task at hand. We suggest trying both for your use case and seeing which one yields better results.
Describe the tone
Claude is trained to act as a chatbot, but that doesn't mean it has to be a general conversational chatbot. By instructing Claude to act in a certain way, it can play a wide variety of different roles with different voices, as necessary for your particular use case.
For example, if you want Claude to respond to customer emails, you can give guidance on the appropriate tone, just as you would to a new hire. Or, if you want Claude to help people learn math, you can describe how a math teacher would speak.
Human: What is Pythagoras' theorem?
Assistant:
Human: For the rest of this conversation, I want you to act as a math teacher. I will provide some mathematical equations or concepts, and it will be your job to explain them in easy-to-understand terms. This could include providing step-by-step instructions for solving a problem, demonstrating various techniques with visuals or suggesting online resources for further study.
Assistant: OK, got it, I'll be a math teacher!
Human: What is Pythagoras' theorem?
Assistant:
Prefill the response
Claude is a conversational agent so it'll often answer in a verbose manner, but sometimes you want it to get to the point quickly without any preamble. To achieve this, or to elicit a response in a particular manner or format, you can put words in Claude's mouth by starting the sentence yourself.
Human: Give me 5 synonyms of the word "dust". I mean the verb, not the noun.
Assistant:
Human: Give me 5 synonyms of the word "dust". I mean the verb, not the noun.
Assistant: Here are 5 synonyms of the verb "dust":
1.
This also usefully can make Claude respond more quickly, as processing input is faster than generating output.
Use XML tags
XML tags like <tag>this</tag> can be useful for defining structure in your prompt, and also in Claude's response.
For example, if you want to reference a long piece of text e.g. an article, tell Claude that you've wrapped it in tags. This makes it obvious where the article starts and ends.
Human: From the following article, please extract any quotes that address the question of where the psychologists work.
If you're stuck in what seems like an endless cycle of procrastination, guilt and chaos... [article clipped here for brevity]
Assistant: Here are some relevant quotes from the article:
Human: Here's an article in <article> tags:
<article>
If you're stuck in what seems like an endless cycle of procrastination, guilt and chaos... [article clipped here for brevity]
</article>
Human: Extract any quotes that address the question of where the psychologists work.
Assistant: Here are some relevant quotes from the article:
Tip for referencing long text
If your prompt includes a reference to a long piece of text e.g. an article, put your instruction after the article rather than before it
You can also ask Claude to use XML tags in the response to make it easy for you to extract key information. Ending your prompt with the opening XML tag is a good method for skipping the preamble too.
Human: Here's an article in <article> tags:
<article>
If you're stuck in what seems like an endless cycle of procrastination, guilt and chaos... [article clipped here for brevity]
</article>
Extract any quotes that address the question of where the psychologists work and put them in separate <quote> tags.
Assistant: Here are some relevant quotes from the article:
<quote>
Specify output length
If you want the response to be a particular length, tell Claude this in your prompt. For best results, specify an approximate number of words, paragraphs, or items in a list. Asking for a specific number of characters is typically less effective.
Human: Please tell me about George Washington.
Assistant:
Human: Please tell me about George Washington, but keep it to 240 characters.
Assistant:
Human: Please give me three facts about George Washington.
Assistant: Here are three facts about George Washington:
1.
Human: Please tell me about George Washington, but keep it to about 30 words.
Assistant: Here's a 30-word description of George Washington:
Set expectations
Suppose you’re searching for information in long documents, but the documents might not contain any information relevant to the question. You might be tempted to prompt Claude with something like:
Human: I want you to look at this document and extract word-for-word quotes that can be used to answer the question "{question}".
But this is probably confusing to Claude if the document might contain no relevant quotes. You — the trusted human! — have given it some text in order to answer a question, so it expects to see relevant quotes in that text. So Claude might even imagine it’s seen such quotes and give you back some hallucinated content in response.
One thing to try when you see a failure like this is to fully explain the task, anticipate the failure in the prompt, and explain it to Claude. In the case of information extraction in the case above, one might first state:
Human: I'm trying to look at documents to find out if they contain information I can use to answer a specific question. There might be information in some of these documents I can use to answer the question, but the documents I've found might also not be at all helpful or relevant to the question. I'm going to send you the document and I want you to find out if the document contains an answer to my question. Do you understand?
This makes it clear to Claude there there is no guarantee that the document will contain a relevant quote. I’m making sure Claude has the right expectations about what it might see.
Similarly, if you see a common failure in Claude’s response then you can anticipate this in the prompt. For example, saying “you don’t need to write out the entire list of items, just the one that you think is best” if Claude often prints an entire list of items instead of just one of them. I’m a bit hesitant to mention this, however, since it should be used with care. Too many instructions given to Claude all at once can reduce its performance on a task.