DEV Community

fanama
fanama

Posted on

Creating and Optimizing Prompts

This guide outlines a structured methodology for creating, testing, and optimizing effective prompts with language models (LLMs). Whether you're a beginner or experienced, this approach will help you obtain precise, relevant, and tailored responses.


📌 Why Optimize a Prompt?

A well-designed prompt helps you:

  • Reduce errors (hallucinations, off-topic responses, fabricated data).
  • Save time by avoiding repeated corrections.
  • Get consistent responses that match your context (tone, style, format).
  • Automate repetitive tasks (writing, analysis, code generation, etc.).

🚀 Step 1: Define the Initial Need

The goal here is to clearly define what you expect from the LLM and how it should respond.

1.1 Define the LLM’s Objective

Ask yourself: "What should the model do?"

  • Write text (article, product description, email)?
  • Summarize or analyze a document?
  • Generate code or creative ideas?
  • Structure data (tables, lists, summaries)?

Example:

Objective: Create an assistant that generates product descriptions for an e-commerce site. Precision: The model should transform technical features (size, weight, materials) into engaging commercial text.


1.2 Define Expected Behavior

Specify how the model should respond:

  • Tone: Professional, casual, technical, humorous, etc.
  • Structure: Paragraphs, bullet points, table, numbered list?
  • Length: Short (1 sentence), medium (3 sentences), detailed (paragraph)?
  • Constraints: Strict adherence to data, no fabrication, specific style, etc.

Example:

Tone: Commercial and accessible (avoid technical jargon). Structure: Max 3 sentences, with keywords in bold for features. Constraint: Never alter the provided technical data.


1.3 Draft the First Version of the Prompt

Using these elements, write a simple initial prompt. This will serve as the basis for testing.

Example of an initial prompt:

"Write a product description for a [product type], in max 3 sentences, with a commercial tone. Use the following features: [list]. Do not modify the data and avoid technical jargon."


1.4 Test the Prompt

Send this prompt to an LLM (Mistral, ChatGPT, etc.) and observe the response. Goal: Check if the model understands the task and follows the instructions.


🔍 Step 2: Identify and Analyze Errors

This step is crucial: it helps you spot weaknesses in the prompt so you can fix them.

2.1 Collect Input/Output Pairs

For each test, note:

  • Input (the prompt sent).
  • Output (the LLM’s response).

Example:

Input (Prompt)

Output (LLM Response)

"Describe this smartphone: 6.5" OLED screen, 5000mAh battery, 64MP camera."

"This revolutionary smartphone features a 6.7" AMOLED screen, a 6000mAh battery, and a 108MP camera for stunning photos."


2.2 Identify Errors

Analyze each response to detect:

  • Hallucinations: The model invents data (e.g., 6.7" instead of 6.5").
  • Off-topic: The response doesn’t match the request.
  • Tone/Style Issues: Too technical, not engaging enough, etc.
  • Incorrect Format: Response too long, poorly structured, etc.

In the example above:

  • Error: The model altered technical features (screen, battery, camera).

2.3 Group Errors by Similarity

Categorize errors to address them efficiently:

  • Category 1: Fabricated/modified data.
  • Category 2: Inappropriate tone (too technical, not commercial enough).
  • Category 3: Format not respected (too long, unformatted).

Why? This allows you to fix multiple errors with a single prompt adjustment.


2.4 Generalize Isolated Cases

Some errors don’t fit into any category. To handle them:

  • Replace specific terms with more general synonyms.
  • Turn the example into a template to cover more cases.

Example:

Isolated Error: The model confuses "OLED" with "AMOLED." Solution: Replace "OLED" with "screen type" in the prompt and add an example: "If the screen type is OLED, write ‘OLED screen.’ Do not replace it with AMOLED or anything else."


🛠️ Step 3: Fix the Errors

Now that errors are identified and categorized, proceed with corrections.

3.1 Fix Categorized Errors

For each category, identify the trend and add a clear instruction to the prompt.

  • Error Category

  • Observed Trend

Instruction to Add to Prompt

  • Fabricated Data

  • Model changes numbers.

  • Solution : "Use only the provided data."

  • Overly Technical Tone

  • Jargon is confusing for the audience.

  • Solution : "Write for a general audience."

  • Response Too Long

    • Exceeds sentence limit.
  • Solution : "Limit to 3 sentences max."


3.2 Use Examples for Generalized Errors

For hard-to-categorize errors, provide concrete examples in the prompt. The model understands better with demonstrations.

Example Integrated:

Improved Prompt: "Generate a description in max 3 sentences, without altering the data. **Example:* Input: ‘6.5" OLED screen, 5000mAh battery’ Output: ‘This smartphone combines endurance (5000mAh battery) and display quality (6.5" OLED screen) for optimized daily use.’"


🔁 Step 4: Test, Refine, and Iterate

4.1 Prepare "Perfect" Test Cases

Create a list of ideal input/output pairs to validate your prompt.

Example:

Input (Data)

Expected Output (Description)

6.1" LCD screen, 4000mAh battery

"A 6.1" LCD screen and 4000mAh battery for a full day without recharging."


4.2 Test the Prompt on All Cases

Send each input to the LLM and compare the output with the expected response.

  • If the output is correct: Move to the next case.
  • If the output is incorrect: Note the error and return to Step 2.

4.3 Iterate Until Satisfied

Repeat the process: Step 2 (Identify Errors) → Step 3 (Fix) → Step 4 (Test) until you achieve a high success rate (e.g., 90% compliant responses).


🎯 Checklist for an Optimized Prompt

Clear Objective: The model knows exactly what to do.
Defined Behavior: Tone, style, and format are specified.
Integrated Examples: 1 or 2 demonstrations to guide the LLM.
Anti-Error Instructions: Directions to block erroneous tendencies.
Tested and Validated: Verified with multiple real-world cases.


💡 Additional Tips

  • Start Simple: A complex prompt is harder to debug.
  • Document Your Tests: Keep track of inputs/outputs to analyze progress.
  • Adapt the Prompt to the LLM: Different models (Mistral, ChatGPT) have unique strengths/weaknesses.
  • Use Tools: Platforms like PromptPerfect or LearnPrompting can help.

🚀 Conclusion: The Prompt, a Dialogue to Refine

An effective prompt isn’t written perfectly on the first try. It’s an iterative process that requires:

  1. Clear Definition of the objective and expected behavior.
  2. Rigorous Error Analysis.
  3. Targeted Corrections (instructions + examples).
  4. Repeated Testing to validate improvements.

Top comments (0)