Prompt Chaining

Another way to accomplish tasks with known and well-defined subtasks is to feed Claude's response to a prompt into the input for another prompt. This technique is called prompt chaining.

Prompt chaining can allow you to accomplish a complex task by passing Claude multiple smaller and simpler prompts instead of a very long and detailed one. It can sometimes work better than putting all of a task's subtasks in a single prompt.

Turning a long and complex prompt into a prompt chain can have a few advantages:

  • You can write less complicated instructions.
  • You can isolate parts of a problem that Claude is having trouble with to focus your troubleshooting efforts.
  • You can check Claude's output in stages, instead of just at the end.

Here are a few use cases for prompt chaining.

Answering a question using a document and quotes

In this example, we will give Claude a document, and a question we want it to answer based on that document. Telling Claude to answer a question using both the document text and relevant quotes can often be more accurate than text or quotes alone.

With our first prompt we ask Claude to extract direct document quotes that are relevant to our question:



Human: Here is a document, in <document></document> XML tags:

<document>
{{DOCUMENT}}
</document>

Please extract, word-for-word, any quotes relevant to the question {{QUESTION}}. Please enclose the full list of quotes in <quotes></quotes> XML tags. If there are no quotes in this document that seem relevant to this question, please say "I can’t find any relevant quotes".

Assistant:

We can then substitute the quotes Claude gives us (including the <quotes></quotes> XML tags) into another prompt:



Human: I want you to use a document and relevant quotes from the document to answer the question "{{QUESTION}}"

Here is the document, in <document></document> XML tags:
<document>
{{DOCUMENT}}
</document>

Here are direct quotes from the document that are most relevant to the question "{{QUESTION}}": {{QUOTES}}

Please use these to construct an answer to the question "{{QUESTION}}" as though you were answering the question directly. Ensure that your answer is accurate and doesn’t contain any information not directly supported by the document or the quotes.

Assistant:

Response validation / extra diligence

Prompt chaining is also handy for automatically asking Claude to re-check a previous response to a prompt.

Using our example from Ask Claude to evaluate its outputs:



Human: Here is an article, contained in <article> tags:

<article>
{{ARTICLE}}
</article>

Please identify any grammatical errors in the article. Please only respond with the list of errors, and nothing else. If there are no grammatical errors, say "There are no errors."

Assistant:

We can substitute Claude's response from Prompt 1 into the {{ERRORS}} placeholder in Prompt 2:



Human: Here is an article, contained in <article> tags:

<article>
{{ARTICLE}}
</article>

Please identify any grammatical errors in the article that are missing from the following list:
<list>
{{ERRORS}}
</list>

If there are no errors in the article that are missing from the list, say "There are no additional errors."

Assistant:

Parallel tasks

Multi-step prompts can be run in parallel, in series, or a combination.

Let's say we want to explain a certain concept to readers at three different levels: 1st graders, 8th graders, and college freshmen. Also, we want Claude to write an outline first, then expand that outline into a full explanation.

We can start with the following prompt template:



Human: Here is a concept: {{CONCEPT}}

I want you to write a three sentence outline of an essay about this concept that is appropriate for this level of reader: {{LEVEL}}

Please only respond with your outline, one sentence per line, in <outline></outline> XML tags. Don't say anything else.

Assistant:

We already know the level of student we want to write explanations for, so we can create three different versions of this prompt (one for each reading level). We can then give Claude a concept and have the prompts for each reading level run in parallel to generate three outlines.

Then we can pass each outline Claude generates (including the <outline></outline> XML tags) into another set of three prompts that differ by reading level. This second set of prompts can again run in parallel to expand each sentence in the outline into a paragraph.



Human: Here is an outline:
{{OUTLINE}}

Please expand each sentence in the outline into a paragraph. Use each sentence word-for-word as the first sentence in its corresponding paragraph. Make sure to write at a level appropriate for this type of reader: {{TYPE}}

Assistant: