Chain prompts

You can think of working with large language models like juggling. The more tasks you have Claude handle in a single prompt, the more liable it is to drop something or perform any single task less well. Thus, for complex tasks that require multiple steps or subtasks, we recommend breaking those tasks down into subtasks and chaining prompts to ensure highest quality performance at every step.


What is prompt chaining?

Prompt chaining involves using the output from one prompt as the input for another prompt. By chaining prompts together, you can guide Claude through a series of smaller, more manageable tasks to ultimately achieve a complex goal.

Prompt chaining offers several advantages:

  • Improved accuracy and consistency in the generated output at each distinct step
  • Easier troubleshooting by isolating specific subtasks that may be particularly error-prone or challenging to handle

When to use prompt chaining

Consider using prompt chaining in the following scenarios:

  1. Multi-step tasks: If your task requires multiple distinct steps, such as researching a topic, outlining an essay, writing the essay, then formatting the essay, chaining prompts can help ensure each step of the task has Claude's full focus and is executed at a high level of performance.

  2. Complex instructions: When a single prompt contains too many instructions or details, Claude may struggle to follow them consistently. Breaking the task into a series of chained subtasks can improve performance for each subtask.

  3. Verifying outputs: You can use chaining to ask Claude to double-check its own outputs with a given rubric and improve its response if needed, ensuring higher quality results. For example, after generating a list of items, you can feed that list back to Claude and ask it to verify the list's accuracy or completeness.

  4. Parallel processing: If your task has multiple independent subtasks, you can create separate prompts for each subtask and run them in parallel to save time.


Tips for effective prompt chaining

  1. Keep subtasks simple and clear: Each subtask should have a well-defined objective and simple instructions. This makes it easier for Claude to understand and follow.

  2. Use XML tags: Enclosing inputs and outputs in XML tags can help structure the data and make it easier to extract and pass on to the next step when chaining prompts.


Examples

Here are a few examples showcasing how to use chaining prompts and breaking tasks into subtasks:

Answering questions using a document and quotes

Here we want Claude to, given a document and a question, generate an answer using relevant quotes from the document.

Prompt 1: Extracting the quotes

RoleContent
UserHere 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".

Prompt 2 (using {{QUOTES}} output from Prompt 1): Answering the question

RoleContent
UserI want you to use a document and relevant quotes from the document to answer a question.

Here is the document:
<document>
{{DOCUMENT}}
</document>

Here are direct quotes from the document that are most relevant to the question:
<quotes>
{{QUOTES}}
</quotes>

Please use these to construct an answer to the question "{{QUESTION}}"

Ensure that your answer is accurate and doesn't contain any information not directly supported by the quotes.

Validating outputs

In this example, the goal is to have Claude identify grammatical errors in an article, then double-check that the list of errors is complete.

Prompt 1: Generating a list of errors

RolePrompt 1
UserHere is an article:
<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."

Prompt 2 (using {{ERRORS}} output from Prompt 1): Double checking that the list is comprehensive

RolePrompt 2
UserHere is an article:
<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."

Parallel processing

In this example, the goal is to have Claude explain a concept to readers at three different levels (1st grade, 8th grade, college freshman) by first creating an outline, then expanding it into a full explanation.

Prompt 1 (create three different versions, one for each reading level): Create an outline

RolePrompt 1
UserHere 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.

Prompt 2 (using {{OUTLINE}} output from Prompt 1, one per reading level): Create full explanations using the outline

RolePrompt 2
UserHere is an outline:
<outline>
{{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: {{LEVEL}}.

Additional resources

  • Prompt engineering techniques: Explore other strategies for optimizing your prompts and enhancing Claude's performance.
  • Anthropic cookbook: Browse a collection of Jupyter notebooks featuring copy-able code snippets that demonstrate highly effective and advanced techniques, integrations, and implementations using Claude.
  • Prompt library: Get inspired by a curated selection of prompts for various tasks and use cases.