Best practices for LLM prompt engineering

Writing effective prompts - a process known as prompt engineering - is essential for unlocking the full potential of large language models. The goal of prompt engineering is to design inputs that guide LLMs to generate desired outputs. The quality of a prompt directly influences the relevance, accuracy, and coherence of the model's responses. This guide provides some best practices for prompt engineering, along with illustrative examples. While the effectiveness of each strategy might vary depending on the LLM used, the following is a consideration of best practices helping you start engineering useful and accurate prompts.

If you are working with AIP Assist specifically, review our AIP Assist best practices.

Key strategies for effective prompting

Effective prompt engineering is a dynamic and iterative process that combines clarity, specificity, and contextual relevance. By following these best practices and incorporating examples, users can maximize the effectiveness of large language models. As AI technology evolves, staying informed about new strategies will further enhance prompt quality and output accuracy.

  • Be clear and specific

    • Be clear: Use straightforward language to define the task or question.
      • Example: Instead of asking "What do you know about coding?", specify "Summarize my framework options for developing a web application."
    • Specify context: Provide context to anchor the model's response.
      • Example: "As a software engineer, explain the benefits of abstraction."
  • Refine and iterate

    • Test and adjust: Experiment with different prompt structures and refine them based on output quality.
      • Example: Start with "List the advantages of web applications." If the response is too broad, refine to "List the maintenance benefits of web applications compared to native applications."
    • Feedback loop: Use model feedback to continuously improve prompt design.
      • Example: If the model misunderstands a prompt, adjust the wording and re-test.
  • Use examples

    • Demonstrate desired output: Provide examples to set expectations for format and content.
      • Example: "Translate the following sentence to French: 'Hello, how are you?' Example: 'Hello' translates to 'Bonjour'."
    • Highlight patterns: Use examples to establish a consistent response pattern.
      • Example: "For each fruit, list its color and taste. Example: Apple - Red, Sweet."
  • Manage length and complexity

    • Be concise: Provide necessary details without overloading the model.
      • Example: Instead of "Can you tell me about the history, current state, and future of robotics?", use "Briefly describe the history of robotics."
    • Avoid overloading: Break complex tasks into simpler parts.
      • Example: "First, list the steps in the semiconductor manufacturing process. Then, explain each step in detail."
  • Incorporate constraints

    • Set boundaries: Define clear constraints to guide response scope.
      • Example: "Summarize the article in no more than three sentences."
    • Limit unwanted outputs: Use negative examples or explicit instructions.
      • Example: "Generate a list of pros and cons of remote work, but exclude personal opinions."
  • Provide relevant context

    • Align with model capabilities: Tailor prompts to the strengths and limitations of the model.
      • Example: For a model trained on medical data, ask, "Explain the symptoms of diabetes," rather than unrelated topics.
    • Maintain relevance: Ensure prompts are relevant to the model's training data.
      • Example: "Discuss recent advancements in AI," aligning with the model's knowledge base.
  • Optimize the interaction

    • Role-playing: Assign roles to guide the model's tone and depth.
      • Example: "As a mechanical engineer, describe the most important sensors to deploy in a heavy manufacturing process."
    • Sequential prompting: Use a series of prompts for complex responses.
      • Example: "First, describe the semiconductor manufacturing process. Next, list three types of semiconductors and how they are manufactured."

Additional prompt engineering resources

To understand more on prompting, consider relevant documentation from the following sources: