Skip to content

Instantly share code, notes, and snippets.

@anntnzrb
Created May 21, 2025 23:50
Show Gist options
  • Select an option

  • Save anntnzrb/c28b3ce85b536b9c441a0c8e74dbcef7 to your computer and use it in GitHub Desktop.

Select an option

Save anntnzrb/c28b3ce85b536b9c441a0c8e74dbcef7 to your computer and use it in GitHub Desktop.
Prompt Architechting

The Art of Prompt Engineering: A Comprehensive Guide

Introduction

Welcome to the world of prompt engineering! This guide is designed to help you craft effective prompts to get the best possible results from large language models (LLMs). A prompt is essentially an instruction or a query you give to an AI model. The quality of the prompt significantly influences the quality of the output. By mastering prompt engineering, you can unlock the full potential of AI models for a wide range of tasks.

Why is Prompt Engineering Important?

  • Precision: Well-crafted prompts lead to more accurate and relevant responses.
  • Control: It allows you to guide the AI's behavior, tone, and output format.
  • Efficiency: Good prompts can reduce the need for multiple iterations and save time.
  • Creativity: It opens up new possibilities for using AI in innovative ways.

This guide will cover fundamental principles and advanced techniques to help you become a proficient prompt engineer.

Core Principles of Effective Prompting

1. Be Clear, Direct, and Detailed

The AI model is like a very capable assistant that needs explicit instructions. The more precise and unambiguous your instructions, the better the outcome.

  • Specificity: Avoid vague language. Instead of "Write about dogs," try "Write a 500-word blog post about the benefits of daily walks for golden retrievers, aimed at new dog owners."
  • Context is Key: Provide necessary background information. If you're asking for a summary of a text, provide the text. If you're asking for content in a specific style, describe that style or provide an example.
    • What the task results will be used for.
    • What audience the output is meant for.
    • What workflow the task is a part of.
    • The end goal or what successful completion looks like.
  • Sequential Steps: For complex tasks, break down your instructions into a numbered or bulleted list. This helps the model follow each step accurately.
  • Define the Output Format: Specify if you want a list, a paragraph, JSON, XML, a table, etc.

Example: Vague vs. Clear Prompting

  • Vague: "Summarize this meeting."
  • Clear & Detailed:
    Your task is to summarize the following meeting transcript.
    <transcript>
    {{MEETING_TRANSCRIPT_TEXT}}
    </transcript>
    
    Instructions:
    1. Identify the main discussion topics.
    2. List key decisions made for each topic.
    3. Extract all action items, assigning each to a responsible person if mentioned.
    4. Keep the summary concise, under 300 words.
    5. Output the summary in the following format:
       ## Meeting Summary
       **Date:** {{MEETING_DATE}}
       **Attendees:** {{ATTENDEES_LIST}}
    
       **Main Topics & Decisions:**
       - [Topic 1]: [Decision 1]
       - [Topic 2]: [Decision 2]
    
       **Action Items:**
       - [Action Item 1] (Assigned to: [Person])
       - [Action Item 2] (Assigned to: [Person])
    

2. Structure Your Prompts with Tags (e.g., XML-like Tags)

When prompts have multiple components (instructions, context, examples, input data), using tags can significantly improve the model's ability to understand and parse the different parts of your prompt. This leads to more accurate and structured outputs.

  • Clarity: Tags clearly delineate different sections of the prompt.
  • Organization: They help structure complex information.
  • Parsing Accuracy: Models can better distinguish instructions from data.

Tagging Best Practices:

  • Use clear and descriptive tag names (e.g., <document>, <instructions>, <question>, <example_input>, <example_output>).
  • Be consistent with your tagging structure.
  • Nest tags logically if needed.
  • Ensure tags are properly closed.

Example: Using Tags for Clarity

  • Without Tags:
    User: Here is a document: The quick brown fox jumps over the lazy dog. Here is another document: She sells seashells by the seashore. Question: Which document is about animals?
    
  • With Tags:
    User:
    <documents>
      <document id="1">
        <content>The quick brown fox jumps over the lazy dog.</content>
        <source>Folklore</source>
      </document>
      <document id="2">
        <content>She sells seashells by the seashore.</content>
        <source>Tongue Twister</source>
      </document>
    </documents>
    
    <question>
    Which document is about animals?
    </question>
    
    <instructions>
    Respond with only the document ID.
    </instructions>
    

Advanced Prompting Techniques

1. Role Prompting (Assigning a Persona)

Assigning a role or persona to the AI model can dramatically improve its performance for specific tasks. This helps the model adopt a certain tone, style, and level of expertise. This is often done using a "system prompt" or as the very first instruction.

  • Why Use Role Prompting?
    • Contextual Expertise: Guides the model to respond as an expert in a specific domain (e.g., "You are a master historian specializing in ancient Rome").
    • Tone and Style: Helps achieve a desired communication style (e.g., "You are a friendly and encouraging fitness coach").
    • Improved Focus: Narrows the scope of the AI's responses, leading to more relevant outputs.

How to Give the Model a Role: Clearly state the role at the beginning of your prompt. Be specific about the characteristics and responsibilities of this role.

Example: Role Prompting for Technical Explanation

  • Without Role:
    User: Explain quantum entanglement.
    
  • With Role:
    System: You are a physics professor renowned for your ability to explain complex topics to undergraduates in a clear, engaging, and accurate manner. Avoid overly technical jargon where possible, and use analogies to illustrate difficult concepts.
    
    User: Professor, can you explain quantum entanglement for me?
    

2. Chain of Thought (CoT) / Step-by-Step Thinking

For complex reasoning tasks, math problems, or multi-step analysis, instructing the model to "think step-by-step" or to "show its work" before providing the final answer can significantly improve accuracy.

  • Why Encourage Step-by-Step Thinking?
    • Accuracy: Breaking down problems reduces errors.
    • Coherence: Leads to more logical and well-organized responses.
    • Debugging: Seeing the model's thought process helps identify where instructions might be unclear or where the model might be going astray.

How to Prompt for Thinking:

  • Explicitly ask the model to "Think step-by-step," "Work through this problem one step at a time," or "Explain your reasoning before giving the answer."
  • You can use tags like <thinking> or <scratchpad> to instruct the model where to put its intermediate thoughts, and then ask for the final answer in a separate tag like <answer>.

Example: Math Problem with Step-by-Step Thinking

User:
Solve the following word problem. Show your reasoning step-by-step inside <thinking> tags, and then provide the final answer inside <answer> tags.

Problem: A bat and a ball cost $1.10 in total. The bat costs $1.00 more than the ball. How much does the ball cost?

Assistant (prefill):
<thinking>

(The model will then fill in its reasoning and the final answer)

3. Few-Shot / Multishot Prompting (Using Examples)

Providing a few examples (shots) of the desired input-output format within your prompt can dramatically improve the model's performance, especially for tasks requiring specific structures, styles, or nuanced understanding.

  • Why Use Examples?
    • Clarity: Examples are often more effective than lengthy descriptions for conveying desired output.
    • Consistency: Helps enforce uniform structure and style.
    • Adaptability: Shows the model how to handle variations or edge cases.

Crafting Effective Examples:

  • Relevant: Examples should closely mirror your actual use case.
  • Diverse: Cover a range of scenarios, including potential edge cases.
  • Clear: Clearly separate input and output in your examples. Use tags like <example>, <input>, <output>.
  • Sufficient: 3-5 examples are often a good starting point. More complex tasks might benefit from more.

Example: Sentiment Classification with Few-Shot Prompting

User:
Classify the sentiment of the following customer reviews as Positive, Negative, or Neutral. Follow the format of the examples provided.

<examples>
  <example>
    <review_text>The product is amazing, I love it!</review_text>
    <sentiment>Positive</sentiment>
  </example>
  <example>
    <review_text>It broke after one day, terrible quality.</review_text>
    <sentiment>Negative</sentiment>
  </example>
  <example>
    <review_text>The shipping was on time.</review_text>
    <sentiment>Neutral</sentiment>
  </example>
</examples>

Now, classify this review:
<review_text>The product is okay, but the manual is confusing.</review_text>
<sentiment>

(The model will complete the sentiment)

4. Prompt Chaining (Breaking Down Complex Tasks)

For very complex tasks that involve multiple distinct steps, each requiring in-depth thought, it's often better to break the task into smaller, manageable sub-tasks. Each sub-task is handled by a separate prompt, and the output of one prompt can become the input for the next.

  • Why Chain Prompts?
    • Accuracy: Each sub-task gets the model's full attention, reducing errors.
    • Clarity: Simpler sub-tasks mean clearer instructions and outputs.
    • Traceability: Easier to pinpoint and fix issues in a specific part of the chain.
    • Modularity: Allows for easier updates and maintenance of individual steps.

How to Chain Prompts:

  1. Identify Sub-tasks: Break your complex task into distinct, sequential steps.
  2. Design Prompts for Each Sub-task: Each prompt should have a single, clear objective.
  3. Manage Handoffs: Use clear structures (like XML tags) to pass outputs from one prompt to the input of the next.
  4. Iterate: Refine individual prompts in the chain based on performance.

Example: Chained Workflow for Report Generation

  • Task: Analyze a dataset, generate insights, and draft an email summarizing these insights.

  • Prompt 1: Data Analysis

    User:
    Analyze the following sales data and identify the top 3 performing product categories and the bottom 3.
    <sales_data>
    {{CSV_SALES_DATA}}
    </sales_data>
    Output your findings in <analysis_results> XML tags.
    

    (Model outputs <analysis_results>...</analysis_results>)

  • Prompt 2: Insight Generation (using output from Prompt 1)

    User:
    Based on the following analysis results, provide two key strategic recommendations for the business.
    <analysis_results>
    {{OUTPUT_FROM_PROMPT_1}}
    </analysis_results>
    Output your recommendations in <recommendations> XML tags.
    

    (Model outputs <recommendations>...</recommendations>)

  • Prompt 3: Email Drafting (using outputs from Prompt 1 & 2)

    User:
    Draft a concise email to the sales team summarizing the recent sales performance and strategic recommendations.
    <sales_performance_summary>
    {{OUTPUT_FROM_PROMPT_1}}
    </sales_performance_summary>
    <strategic_recommendations>
    {{OUTPUT_FROM_PROMPT_2}}
    </strategic_recommendations>
    The email should be professional and encouraging.
    Output the email draft in <email_draft> XML tags.
    

Self-Correction Chains: An advanced form of prompt chaining involves having the model review its own work. - Prompt 1: Generate content. - Prompt 2: Review the generated content against specific criteria (e.g., accuracy, completeness, tone) and provide feedback. - Prompt 3: Revise the original content based on the feedback.

5. Prefilling the Model's Response

You can guide the model's output by providing the beginning of its expected response. The model will then continue from where you left off. This is useful for:

  • Enforcing Output Format: Start the response with an opening bracket for JSON ({) or an XML tag.
  • Skipping Preamble: If the model tends to be too conversational, prefill with the direct start of the answer.
  • Maintaining Character: In role-play, if the model might break character, prefill with a phrase in character.
  • Guiding Specific Content: If you need a list to start with a particular item, prefill that item.

How to Prefill: In your API call or interface, structure the conversation so that the "Assistant" or model's turn starts with your prefilled text. The model's actual generation will append to this prefill.

Example: JSON Output with Prefill

User:
Extract the name, email, and city from the following text as a JSON object:
"John Doe lives in New York and his email is [email protected]."

Assistant (prefill):
{
  "name": "John Doe",

(The model will likely complete the JSON object correctly)

Note: The prefill content should not end with trailing whitespace.

6. Using Prompt Templates and Variables

When building applications, parts of your prompt will be static (the core instructions, examples) and parts will be dynamic (user input, data from a database). Prompt templates combine these, using placeholders for dynamic content.

  • Benefits of Prompt Templates:
    • Reusability: Define a prompt structure once and reuse it with different data.
    • Consistency: Ensures all calls use the same well-engineered prompt structure.
    • Maintainability: Easier to update prompts as you only change the template.
    • Testability: Allows for systematic testing with various inputs.

How to Use Templates: Use placeholders (e.g., {{VARIABLE_NAME}} or <variable_name>) in your prompt for dynamic content. Your application code will then replace these placeholders with actual values before sending the prompt to the model.

Example: Translation Template

System: You are a helpful translation assistant.
User:
Translate the following English text to {{TARGET_LANGUAGE}}.
<english_text>
{{TEXT_TO_TRANSLATE}}
</english_text>

Translated text:

In this template, {{TARGET_LANGUAGE}} and {{TEXT_TO_TRANSLATE}} are variables.

Handling Long Contexts

Modern LLMs can process very long inputs (e.g., hundreds of thousands of tokens). Here are tips for working with long contexts:

  1. Position Matters:
    • Place lengthy documents or large blocks of data at the beginning of your prompt, before your specific questions, instructions, or examples. This can significantly improve recall and performance.
  2. Structure is Crucial:
    • When providing multiple documents, wrap each one in clear XML tags, e.g., <document index="1">...</document>.
    • Inside each document tag, consider further structuring with tags like <title>, <author>, <content>, <source_url>.
  3. Quote for Grounding:
    • For tasks like question-answering over long documents, ask the model to first find and quote the most relevant passages from the document(s) before answering the question. This helps the model focus on the pertinent information.
    • Example instruction: "First, find quotes from the provided documents that are most relevant to answering the question. Print them in numbered order. Then, answer the question based on these quotes. Do not include or reference quoted content verbatim in the answer itself, but you can add bracketed numbers at the end of sentences to reference the quotes."

Example: Multi-Document Q&A with Quoting

User:
<documents>
  <document id="doc_A">
    <title>The History of Tea</title>
    <content>
    ... (long text about tea history) ... Tea was first discovered in China.
    </content>
  </document>
  <document id="doc_B">
    <title>Coffee Around the World</title>
    <content>
    ... (long text about coffee) ... Coffee cultivation began in Ethiopia.
    </content>
  </document>
</documents>

<question>
Where was tea first discovered?
</question>

<instructions>
1. Identify the most relevant quote(s) from the documents to answer the question. Output them within <quotes> tags, each quote in a <quote doc_id="...">...</quote> tag.
2. Then, answer the question based *only* on the extracted quotes. Output the answer within <answer> tags.
</instructions>

Iteration and Refinement

Prompt engineering is an iterative process. Your first prompt is rarely your best.

  1. Define Success Criteria: Before you start, know what a good output looks like. What are your metrics for success (accuracy, tone, format, completeness)?
  2. Develop Test Cases: Create a diverse set of inputs to test your prompts against. Include common cases, edge cases, and tricky scenarios.
  3. Start Simple, Then Add Complexity: Begin with a basic prompt and gradually add more instructions, examples, or constraints as needed.
  4. Analyze Failures: When the model produces a suboptimal output, try to understand why. Was the instruction unclear? Was context missing? Did it misunderstand a term?
  5. Experiment: Try different phrasings, structures, and techniques. Small changes can sometimes have a big impact.
  6. Consider Using AI for Assistance:
    • Prompt Generation: You can describe your task to an AI model and ask it to generate a first-draft prompt for you.
    • Prompt Improvement: Provide your current prompt and examples of good/bad outputs, and ask the AI to suggest improvements to your prompt.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment