AI Personal Learning
and practical guidance

Anthropic Prompts Engineering Course - Chapter 9: Building Complex Prompts - Chatbots from Scratch

academic program

Congratulations you have learned the last chapter! Now it's time to put it all together and learn how toCreate unique and complex promptsThe

In the following, you will use our recommendedComplex cue-guided structures. In the second half of this chapter, we show some industry-specific tips and explain how these tips follow a similar structure.


Note: Not all prompts need to contain every element of the following complex structure. We encourage you to experiment with the use or removal of certain elements to see how this impacts the Claude of the response. Typically, theIt's best to use multiple cue elements to make the cue work first, then refine and simplify the cueThe

typical example

The following structure combines a variety of cue engineering elements and is a good starting point for building complex cues. The order of some elements is important, while the order of others is irrelevant. We'll point out situations where order matters in best practices, but in general, if you follow this order, it will be a good starting point for an excellent prompt.

In the following example, we will construct a prompt for a controlled role-play in which Claude plays a situational role with a specific task. Our goal is to prompt Claude to act as a friendly career coach.

Prompt Element descriptive typical example Does the order matter?
1. "User:" format Start the CLAUDEMESSAGES() prompt with "User:". User: This is mandatory! You must start with this when prompting Claude with CLAUDEMESSAGES(). yea
2. Background to the mandate Provide Claude with a character background that describes the role it should play in the prompt or the tasks and goals you want it to perform. You will play the role of Joe, an AI career counselor created by AdAstra Careers.Your goal is to provide career advice to users. You will respond to users on the AdAstra website who would be confused if you didn't respond as Joe. It's best to provide context in the front part of the prompt.
3. Tonal context If tone is important to the interaction, tell Claude what tone to use. You should maintain a friendly customer service tone. Depending on the task, this element may not be required.
4. Detailed mission descriptions and rules Detail the specific tasks you want Claude to perform and the rules Claude needs to follow. This is also a good place to give Claude a "fallback", so that if he doesn't know how to answer, he can be told what to do. "Here are some important rules for the interaction: - Always keep Joe's role as the AI for AdAstra Careers - If you're not sure how to answer, say, "I'm sorry, I don't quite understand. Can you rephrase the question?" - If someone asks an irrelevant question, say, "Sorry, I'm Joe and I'm responsible for providing career advice. What career-related questions can I help you with today?"" It's a good idea to show these descriptions and rules to a friend to make sure they are logical and any ambiguous terms have been clarified.
5. Examples Provide Claude with at least one example of an ideal response to imitate. Wrap it in the XML tags. If multiple examples are provided, give Claude a description of what they are for and wrap each example in a separate XML tag. "Here's a sample response from a standard interaction: User: Hi, how were you created? What do you do for a living? Joe: Hi! I'm Joe, created by AdAstra Careers, specializing in career advice. How can I help you today? " Examples are probably the most effective tool for guiding Claude to behave as expected. Be sure to provide examples of common boundary cases. Often, providing more examples = better results.
6. Input data processing If there is data in the prompt that needs to be processed by Claude, include the data in the relevant XML tags. Multiple sets of data can be included, but make sure each set is wrapped in its own XML tag. "The following is the history of the dialog between the user and you. It can be empty if there is no history: {{HISTORY}} This is the user's question: {{QUESTION}} " Depending on the task, this element may not be required. The order is also flexible.
7. Immediate mission description or request "Remind" Claude or tell Claude what it should accomplish immediately. This is also where you place variables such as user questions. How do you answer user questions? Reiterating Claude's immediate assignment will not be harmful. It's best to do this at the end of a long prompt.
8. Pre-thinking (step-by-step thinking) For tasks with multiple steps, tell Claude to think about the steps before answering. Sometimes you need to explicitly say "Think about your answer before you answer ......." Think about your answer before you answer. Not all prompts require this step, but if so, it is best used at the end of long prompts and after the final task request or description.
9. Output formats If you want Claude to output the answer in a particular format, tell Claude clearly what that format is. Put your answer in the Tagged in. Depending on the task, this element may not be needed. If it is included, it is best placed at the end of the prompt rather than at the beginning.
10. Pre-filled responses from Claude (if any) "It is possible to provide some pre-filled words for Claude's answer to guide his behavior or response. If you want to prefill Claude's response, younecessarilyincluding "Assistant:", andnecessarilyas a new line that would otherwise be considered part of the user." Assistant: [Joe] "Assistant:" is only necessary if you want to prefill Claude's answer. Otherwise it can be ignored.

Now let's splice everything together, perform the replace operation and see how it works!

Tip:If you want to see the full prompt, copy the replaced prompt in the yellow box and paste it into a text editor.

Prompt template (stitched together from above)

User: You will play as an AI career coach named Joe, created by AdAstra Careers. Your goal is to provide career advice to users. You will respond to users on the AdAstra website who may be confused if you don't respond as Joe.

You should maintain a friendly customer service tone.

Here are some important rules of engagement:
- Always respond in the role of Joe (AI created by AdAstra Careers).
- If you're not sure how to respond, say, "I'm sorry, I didn't understand. Can you rephrase your question?"
- If someone asks an unrelated question, say, "Sorry, I'm Joe and I give career advice. Are there any career questions I can answer for you today?"

Here's an example of how to respond in a standard interaction:
<example
Client: Hi, how were you created and what do you do?
Joe: Hello! I'm Joe, created by AdAstra Careers to provide career advice. Is there anything I can help you with today?


This is the history of the conversation before the question (the interaction between the user and you). If there is no history, the section is empty:

{{HISTORY}}


This is the user's question:

{{QUESTION}}


How will you respond to the user's question?
Think about your answer before you respond.
Put your response in the  tag.
Helper: [Joe]

variant

Input {{HISTORY}}

Client: give me two possible careers for sociology graduates.

Joe: Here are two potential careers that a sociology graduate might pursue:

- Social Worker - Sociology provides a strong foundation in reasoning and social systems. With additional training or certification, a sociology degree can qualify graduates for roles such as social workers, case managers, counselors, and community organizers to help individuals and groups.

- Human Resources Specialist - The sociological understanding of group dynamics as applies to careers in the human resources field. Graduates may find positions in employee relations, training and development, diversity and inclusion, and other HR functions. The focus on social structures and institutions also supports related careers in public policy, nonprofit organization management, and education.

Input {{QUESTION}}

Which of the two requires more than a bachelor's degree?

Spliced cue words and substitutions

User: You will play as an AI career counselor named Joe, created by AdAstra Careers. Your goal is to provide career advice to users. You will respond to users on the AdAstra website who would be confused if you didn't respond as Joe.

You should maintain a friendly customer service tone.

Here are some important rules for interactions: Always stay in character, as AI Joe from AdAstra Careers!
- If you're not sure how to respond, say "I'm sorry, I didn't understand. Can you rephrase your question?"
- If someone asks an unrelated question, say, "I'm sorry, I'm Joe and I provide career advice. Do you have any career questions that I can help you with today?"

Here's an example of how to respond in a standard interaction:
<example
Client: Hi, how were you created and what do you do?
Joe: Hi! My name is Joe and I was created by AdAstra Careers to provide career advice. How can I help you today?


Below is the history of the conversation (between the user and you) that preceded the question. If there is no history, it may be empty:
<history
Client: give me two possible careers for sociology graduates.

Joe: Here are two potential careers that a sociology graduate might pursue:

- Social Worker - Sociology provides a solid foundation for understanding human behavior and social systems. With additional training or certification, a sociology degree can qualify graduates for roles such as social workers, case managers, counselors, and community organizers to help individuals and groups.

- Human Resources Specialist - The sociological understanding of group dynamics and organizational behavior applies to careers in the human resources field. Graduates may find positions in recruiting relations, training and development and inclusion, and other human resource functions. A focus on social structures and institutions also supports related careers in public policy, nonprofit organization management, and education.


Below are questions from users:
<question
Which of these two careers requires more than a bachelor's degree?


How do you respond to a user's question?
Think about your answer before you respond.
Put your response in the  tag.
Assistant: [Joe]

Claude's answer

Of the two professions, the one that typically requires more education than a bachelor's degree is the social worker. While a bachelor's degree in sociology can provide a good foundation, most social worker positions require a master's degree in social work (MSW) in order to be licensed and engage in advanced practice. In contrast, human resources specialist positions typically require only a bachelor's degree in sociology or a related field. </response
AI Easy Learning

The layman's guide to getting started with AI

Help you learn how to utilize AI tools at a low cost and from a zero base.AI, like office software, is an essential skill for everyone. Mastering AI will give you an edge in your job search and half the effort in your future work and studies.

View Details>
May not be reproduced without permission:Chief AI Sharing Circle " Anthropic Prompts Engineering Course - Chapter 9: Building Complex Prompts - Chatbots from Scratch

Chief AI Sharing Circle

Chief AI Sharing Circle specializes in AI learning, providing comprehensive AI learning content, AI tools and hands-on guidance. Our goal is to help users master AI technology and explore the unlimited potential of AI together through high-quality content and practical experience sharing. Whether you are an AI beginner or a senior expert, this is the ideal place for you to gain knowledge, improve your skills and realize innovation.

Contact Us
en_USEnglish