Notebooks
A
Anthropic
10.1 Appendix Chaining Prompts

10.1 Appendix Chaining Prompts

prompt_engineering_interactive_tutorialAnthropic 1Panthropic-courses

Appendix 10.1: Chaining Prompts

Setup

Run the following setup cell to load your API key and establish the get_completion helper function.

[ ]

Lesson

The saying goes, "Writing is rewriting." It turns out, Claude can often improve the accuracy of its response when asked to do so!

There are many ways to prompt Claude to "think again". The ways that feel natural to ask a human to double check their work will also generally work for Claude. (Check out our prompt chaining documentation for further examples of when and how to use prompt chaining.)

Examples

In this example, we ask Claude to come up with ten words... but one or more of them isn't a real word.

[ ]

Asking Claude to make its answer more accurate fixes the error!

Below, we've pulled down Claude's incorrect response from above and added another turn to the conversation asking Claude to fix its previous answer.

[ ]

But is Claude revising its answer just because we told it to? What if we start off with a correct answer already? Will Claude lose its confidence? Here, we've placed a correct response in the place of first_response and asked it to double check again.

[ ]

You may notice that if you generate a respnse from the above block a few times, Claude leaves the words as is most of the time, but still occasionally changes the words even though they're all already correct. What can we do to mitigate this? Per Chapter 8, we can give Claude an out! Let's try this one more time.

[ ]

Try generating responses from the above code a few times to see that Claude is much better at sticking to its guns now.

You can also use prompt chaining to ask Claude to make its responses better. Below, we asked Claude to first write a story, and then improve the story it wrote. Your personal tastes may vary, but many might agree that Claude's second version is better.

First, let's generate Claude's first version of the story.

[ ]

Now let's have Claude improve on its first draft.

[ ]

This form of substitution is very powerful. We've been using substitution placeholders to pass in lists, words, Claude's former responses, and so on. You can also use substitution to do what we call "function calling," which is asking Claude to perform some function, and then taking the results of that function and asking Claude to do even more afterward with the results. It works like any other substitution. More on this in the next appendix.

Below is one more example of taking the results of one call to Claude and plugging it into another, longer call. Let's start with the first prompt (which includes prefilling Claude's response this time).

[ ]

Let's pass this list of names into another prompt.

[ ]

Now that you've learned about prompt chaining, head over to Appendix 10.2 to learn how to implement function calling using prompt chaining.


Example Playground

This is an area for you to experiment freely with the prompt examples shown in this lesson and tweak prompts to see how it may affect Claude's responses.

[ ]
[ ]
[ ]
[ ]
[ ]
[ ]
[ ]
[ ]