- Blog
- Tips for effective prompt chaining
Tips for effective prompt chaining
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:
- 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.
- 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.
- 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.
- 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
- 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.
- 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
{{QUOTES}}
output from Prompt 1): Answering the question
Prompt 2 (using 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
{{ERRORS}}
output from Prompt 1): Double checking that the list is comprehensive
Prompt 2 (using 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.