Anthropic Prompt Engineering Interactive Tutorial

repository·master·Indexed 12 days ago

https://github.com/anthropics/prompt-eng-interactive-tutorial

An interactive, step-by-step educational resource for mastering prompt engineering best practices for Claude models. The course covers basic prompt structure, role prompting, XML tag delineation, output formatting, and prefilling techniques. It includes lessons on avoiding hallucinations and building complex prompts for industry use cases, utilizing Claude 3 Haiku via the Anthropic Python SDK and Amazon Bedrock.

Tokens
37.7K
Snippets
112
Records
151
Agent score
98%

What's inside Anthropic Prompt Engineering Tutorial

  1. Overview of the Prompt Engineering Interactive Tutorial

    master

    This tutorial is a step-by-step course designed to teach users how to engineer optimal prompts for Claude. It covers basic prompt structures, common failure modes, Claude's specific strengths/weaknesses, and building complex prompts for industry use cases.

    Key Learning Objectives:

    • Master basic prompt structure.
    • Apply '80/20' techniques to address common failure modes.
    • Understand Claude's capabilities.
    • Build prompts from scratch for common use cases.
  2. How to use the Prompt Engineering Tutorial

    master

    The course is designed to be completed in order, moving through 9 chapters and an appendix. Each chapter includes a lesson and accompanying exercises.

    Course Features:

    • Example Playgrounds: Located at the bottom of each lesson, these allow you to experiment with prompts and observe how Claude's responses change.
    • Answer Key: An answer key is provided for the exercises.
    • Model Used: The tutorial uses Claude 3 Haiku (the smallest, fastest, and cheapest model). While the tutorial is optimized for Haiku, the principles apply to Claude 3 Sonnet and Claude 3 Opus.

    Alternative Format: For a more user-friendly experience, you can use the Google Sheets version via the Anthropic Claude for Sheets extension.

  3. What is Few-Shot Prompting?

    master

    Few-shot prompting involves giving Claude examples of how you want it to behave (or how you want it not to behave) within the prompt. This is extremely effective for:

    • Getting the right answer.
    • Getting the answer in a specific format.

    Terminology:

    • Zero-shot: No examples provided.
    • One-shot: One example provided.
    • N-shot: Multiple examples provided.

    Instead of writing long, complex instructions to describe a tone or a format, you can simply provide a few examples of ideal input/output pairs, and Claude will extrapolate the pattern.

  4. Perform Human Grading for complex tasks

    master
    For tasks that are difficult to evaluate with code (e.g., essay scoring or creative writing), use Human Grading. This involves providing human graders with a specific Grading Rubric (the golden_answer) to assess the model's output against qualitative standards like structure, thesis clarity, and persuasiveness.
  5. Structure complex prompts using the 10-element framework

    master

    For complex tasks, use a guided structure to ensure Claude has all necessary context and constraints. While not every element is required for every prompt, using many elements initially and then refining is a recommended best practice.

    Recommended Prompt Elements & Ordering:

    1. Task Context: Define the role (e.g., "You are an expert lawyer") and overarching goals. Place this early.
    2. Tone Context: Specify the desired tone (e.g., "friendly customer service").
    3. Detailed Task Description and Rules: Expand on specific tasks and constraints. Include "outs" (e.g., what to say if the answer is unknown).
    4. Examples: Provide ideal response examples enclosed in <example></example> XML tags. This is highly effective for controlling behavior.
    5. Input Data to Process: Include data (like research or history) enclosed in relevant XML tags (e.g., <history>, <question>, <legal_research>).
    6. Immediate Task Description/Request: Reiterate exactly what Claude should do right now. Place this toward the end.
    7. Precognition (Thinking Step-by-Step): Instruct Claude to think before answering (e.g., "Think about your answer first..."). Best placed near the end.
    8. Output Formatting: Explicitly define the required format (e.g., "Put your response in <response></response> tags").
    9. Prefilling Claude's Response: Use the assistant role in the API call to start Claude's response (e.g., prefill="[Joe] <response>"). This steers the response.

    Key Principles:

    • XML Tags: Use XML tags to clearly demarcate different types of data and examples.
    • Ordering: Generally, put context early and immediate tasks/formatting/precognition toward the end.
    • Role Requirement: Ensure the Messages API call always starts with a user role.
  6. How to 'speak for Claude' using prefilling

    master

    Prefilling is a technique where you provide a partial response in the assistant turn. By passing a string (like an opening XML tag <haiku> or a JSON bracket {) as the prefill argument, you force Claude to continue directly from that point. This is highly effective for enforcing specific formats like XML or JSON and for steering the tone or direction of the response.

    # Example: Prefilling an XML tag
    ANIMAL = "Cat"
    PROMPT = f"Please write a haiku about {ANIMAL}. Put it in <haiku> tags."
    PREFILL = "<haiku>"
    
    print(get_completion(PROMPT, prefill=PREFILL))
  7. Apply the Golden Rule of Clear Prompting

    master

    Claude responds best to clear and direct instructions because it has no inherent context regarding your goals. To ensure high-quality responses, follow the Golden Rule of Clear Prompting:

    Show your prompt to a colleague or friend and have them follow the instructions themselves to see if they can produce the result you want. If they're confused, Claude's confused.

    Key strategies for clarity include:

    • Explicitly request formatting constraints: If you want to avoid conversational filler (preambles), explicitly tell Claude to "Skip the preamble; go straight into the [output type]."
    • Force decisions: If Claude is being non-committal or providing multiple perspectives when you need a single answer, instruct it to pick one (e.g., "If you absolutely had to pick one, who would it be?").
    • Constraint enforcement: Use the system parameter or the prompt to define strict output formats, such as requesting "ONLY the name... with no other words or punctuation."
  8. Improve accuracy by letting Claude think step by step

    master

    For complex tasks, Claude's accuracy improves when it is given time to 'think' before providing a final answer. Crucially, thinking only counts when it is done out loud. You cannot simply ask Claude to think and then only output the answer; the reasoning process must be part of the generated text.

    Best Practices:

    • Explicitly spell out steps: Instruct Claude to perform specific reasoning steps (e.g., "First, write the best arguments for each side...").
    • Use XML tags for reasoning: Use tags like <positive-argument>, <negative-argument>, or <brainstorm> to structure the thinking process. This helps separate the reasoning from the final answer.
    • Be aware of ordering sensitivity: Claude can be sensitive to the order of options or arguments provided in the prompt. In some cases, it may be biased toward the second of two options.
    # Example of prompting Claude to think using XML tags
    SYSTEM_PROMPT = "You are a savvy reader of movie reviews."
    PROMPT = """Is this review sentiment positive or negative? First, write the best arguments for each side in <positive-argument> and <negative-argument> XML tags, then answer.
    
    This movie blew my mind with its freshness and originality. In totally unrelated news, I have been living under a rock since 1900."""
    
    print(get_completion(PROMPT, SYSTEM_PROMPT))
  9. Define tools using the XML-based system prompt format

    master

    To enable tool use, you must provide a system prompt that includes two parts:

    1. General Explanation: Instructions on how to use the <function_calls> structure. Claude is trained to use specific tags like <invoke name="$FUNCTION_NAME"> and <antml:parameter name="$PARAMETER_NAME">.
    2. Specific Tool Definitions: A list of available tools described in a format similar to JSONSchema, wrapped in <tools> and <tool_description> tags.

    Recommended Tool Call Structure:

    <function_calls>
    <invoke name="function_name">
    <antml:parameter name="param_name">value</antml:parameter>
    </invoke>
    </function_calls>
    system_prompt_tools_general_explanation = """You have access to a set of functions... [Instructions on <function_calls> structure] ..."""
    
    system_prompt_tools_specific_tools = """Here are the functions available in JSONSchema format:
    <tools>
    <tool_description>
    <tool_name>calculator</tool_name>
    <description>...</description>
    <parameters>
    <parameter>
    <name>first_operand</name>
    <type>int</type>
    <description>...</description>
    </parameter>
    </parameters>
    </tool_description>
    </tools>"""
    
    system_prompt = system_prompt_tools_general_explanation + system_prompt_tools_specific_tools
  10. How to handle distractor information in prompts

    master

    When providing Claude with a document to answer a question, the document may contain "distractor information" (data that is similar but not exactly what was asked for). Without specific prompting, Claude might fall for these distractors.

    Best Practice: While this tutorial places the question at the top for readability, it is generally best practice to place the question at the bottom of the prompt, after the reference text or document, to ensure the model processes the context before the instruction.

  11. Minimize hallucinations by "giving Claude an out"

    master

    Claude may hallucinate (make untrue or unjustified claims) when it tries to be overly helpful, even if it doesn't know the answer. One technique to mitigate this is to "give Claude an out" by explicitly instructing it to only answer if it is certain, or to decline the question if it cannot provide a verified answer.

    Example Prompting Pattern: Instead of asking a direct question like: "Who is the heaviest hippo of all time?"

    Use a constrained prompt: "Who is the heaviest hippo of all time? Only answer if you know the answer with certainty."

    # Prompt
    PROMPT = "Who is the heaviest hippo of all time? Only answer if you know the answer with certainty."
    
    # Print Claude's response
    print(get_completion(PROMPT))