Notebooks
A
Anthropic
09 Complex Prompts From Scratch

09 Complex Prompts From Scratch

prompt_engineering_interactive_tutorialAnthropic 1Panthropic-courses

Chapter 9: Complex Prompts from Scratch

Setup

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

[ ]

Lesson

Congratulations on making it to the last chapter! Now time to put everything together and learn how to create unique and complex prompts.

Below, you will be using a guided structure that we recommend for complex prompts. In latter parts of this chapter, we will show you some industry-specific prompts and explain how those prompts are similarly structured.

Note: Not all prompts need every element of the following complex structure. We encourage you to play around with and include or disinclude elements and see how it affects Claude's response. It is usually best to use many prompt elements to get your prompt working first, then refine and slim down your prompt afterward.

Example - Career Coach Chatbot

The following structure combines multiple prompt engineering elements and is a good starting point for complex prompts. The ordering matters for some elements, not for others. We will note when best practices indicate ordering matters, but in general, if you stick to this ordering, it will be a good start to a stellar prompt.

For the following example, we will be building a prompt for a controlled roleplay wherein Claude takes on a situational role with a specific task. Our goal is to prompt Claude to act as a friendly career coach.

Read then run the cell below to compile the various prompt elements into one whole prompt.

[ ]

Now let's run the prompt! Run the cell below to see Claude's output.

[ ]

Example - Legal Services

Prompts within the legal profession can be quite complex due to the need to:

  • Parse long documents
  • Deal with complex topics
  • Format output in very specific ways
  • Follow multi-step analytical processes

Let's see how we can use the complex prompt template to structure a prompt for a specific legal use-case. Below, we've detailed out an example prompt for a legal use-case wherein we ask Claude to answer questions about a legal issue using information from a legal document.

We've changed around the ordering of a few elements to showcase that prompt structure can be flexible!

Prompt engineering is about scientific trial and error. We encourage you to mix and match, move things around (the elements where ordering doesn't matter), and see what works best for you and your needs.

[ ]

Now let's run the prompt! Run the cell below to see Claude's output.

[ ]

If you would like to experiment with the lesson prompts without changing any content above, scroll all the way to the bottom of the lesson notebook to visit the Example Playground.

Exercise 9.1 - Financial Services Chatbot

Prompts within the financial profession can also be quite complex due to reasons similar to legal prompts. Here's an exercise for a financial use-case, wherein Claude is used to analyze tax information and answer questions. Just like with the legal services example, we've changed around the ordering of a few elements, as our solution prompt makes more sense with a different flow (however, other structures would also work).

We suggest you read through the variable content (in this case, {QUESTION} and {TAX_CODE}) to understand what content Claude is expected to work with. Be sure to reference {QUESTION} and {TAX_CODE} directly in your prompt somewhere (using f-string syntax like in the other examples) so that the actual variable content can be substituted in.

Fill in the prompt element fields with content that match the description and the examples you've seen in the preceding examples of complex prompts. Once you have filled out all the prompt elements that you want to fill out, run the cell to see the concatenated prompt as well as Claude's response.

Remember that prompt engineering is rarely purely formulaic, especially for large and complex prompts! It's important to develop test cases and try a variety of prompts and prompt structures to see what works best for each situation. Note that if you do change the ordering of the prompt elements, you should also remember to change the ordering of the concatenaton in the COMBINE ELEMENTS section.

[ ]

❓ If you want to see a possible solution, run the cell below!

[ ]

Exercise 9.2 - Codebot

In this exercise, we will write up a prompt for a coding assistance and teaching bot that reads code and offers guiding corrections when appropriate. Fill in the prompt element fields with content that match the description and the examples you've seen in the preceding examples of complex prompts. Once you have filled out all the prompt elements that you want to fill out, run the cell to see the concatenated prompt as well as Claude's response.

We suggest you read through the variable content (in this case, {CODE}) to understand what content Claude is expected to work with. Be sure to reference {CODE} directly in your prompt somewhere (using f-string syntax like in the other examples) so that the actual variable content can be substituted in.

[ ]

❓ If you want to see a possible solution, run the cell below!

[ ]

Congratulations & Next Steps!

If you made it through all the exercises, you are now in the top 0.1% of LLM whisperers. One of the elite!

The techniques you've learned, from thinking step by step to assigning roles to using examples to general all-around clear writing, can be merged, remixed, and adapted in countless ways.

Prompt engineering is a very new discipline, so keep an open mind. You could be the one to discover the next great prompting trick.

If you want to see more examples of good prompts for inspiration:

  • Learn from examples of production-ready prompts from our cookbook
  • Read through our prompting guide
  • Check out our prompt library for inspiration
  • Try our experimental metaprompt to get Claude to write prompt templates for you!
  • Ask questions in our discord server
  • Learn about the Anthropic API parameters like temperature and max_tokens
  • If you're feeling academic, read some papers on prompt engineering
  • Practice building prompts to get Claude to do something you're interested in

If you want to learn about some truly advanced prompting techniques beyond the scope of this tutorial, click through to the appendix! But first, run the cell below.

[ ]

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.

[ ]
[ ]