Prompt Engineering: Crafting Effective Instructions for Large Language Models

Core Principles of Prompt Engineering

Two fundamental principles guide effective prompt construction: clarity and specificity combined with allocating sufficient processing time for the model to reason through complex tasks.


Delimiters and Input Organization

Delimiters serve as explicit boundaries within prompts, separating instructions from input content. Common choices include triple backticks (```), quotes, angle brackets (< >), XML-style tags, or colons. This separation prevents prompt injection—where user input might inadvertently override your intended instructions and cause the model to produce unrelated or nonsensical outputs.

Consider this example demonstrating delimiter usage:

from tool import get_completion

passage = """
Effective prompts provide clear, specific guidance about the desired task.
Avoid conflating brevity with clarity—longer prompts often yield better 
results by offering additional context and reducing ambiguity.
"""

task_instruction = f"""
Summarize the content within triple backticks in a single sentence.
```{passage}```
"""

result = get_completion(task_instruction)
print(result)

Output:

Effective prompts provide clear, specific guidance about the desired task, avoiding confusion between brevity and clarity while offering better results through additional context.

Clarity and Specificity

Structured Output Formats

When requiring machine-readable responses, explicitly specify the desired format:

query = f"""
Generate a list of three fictional Chinese books with titles, authors, and genres.
Return the response as JSON with these keys: book_id, title, author, genre.
"""
output = get_completion(query)
print(output)

Sample response:

{
  "books": [
    {
      "book_id": 1,
      "title": "Echoes of Tomorrow",
      "author": "Wei Chen",
      "genre": "Science Fiction"
    },
    {
      "book_id": 2,
      "title": "The Jade Gateway",
      "author": "Mei Lin",
      "genre": "Fantasy"
    },
    {
      "book_id": 3,
      "title": "Digital Dreams",
      "author": "Alex Zhou",
      "genre": "Science Fiction"
    }
  ]
}

Conditional Checking

When inputs may or may not contain required elements, instruct the model to verify conditions before proceeding:

# Input containing sequential instructions
recipe_text = """
Making tea is simple. First, boil water. While waiting, grab a cup and 
place a tea bag inside. Once the water reaches proper temperature, 
pour it over the tea bag. Wait a few minutes for steeping. Remove the 
bag and enjoy. Add sugar or milk if desired.
"""

task = f"""
Analyze the text within triple quotes. If it contains step-by-step 
instructions, rewrite them as:
Step 1 - ...
Step 2 - ...
...
Step N - ...

If no sequential instructions exist, respond with "No steps provided."
"""{recipe_text}"""
"""

response = get_completion(task)
print("Analysis Result:")
print(response)

Output:

Analysis Result:
Step 1 - Boil water.
Step 2 - Grab a cup and place a tea bag inside.
Step 3 - Pour hot water over the tea bag.
Step 4 - Wait several minutes for steeping.
Step 5 - Remove the tea bag.
Step 6 - Add sugar or milk if desired.

When the input lacks instructions, the model corectly identifies this:

# Input without instructions
narrative = """
The sun shines brightly today. Birds sing in the trees.
A perfect afternoon for a stroll through the park.
"""

task = f"""
Analyze the text within triple quotes. If it contains step-by-step 
instructions, rewrite them as:
Step 1 - ...
Step 2 - ...
...
Step N - ...

If no sequential instructions exist, respond with "No steps provided."
"""{narrative}"""
"""

response = get_completion(task)
print("Analysis Result:")
print(response)

Output:

Analysis Result:
No steps provided.

Few-Shot Learning

Provide examples before requesting the actual task to establish the expected response pattern:

conversation = f"""
Maintain consistency in tone and style throughout.

<Child>: Teach me about patience.
<Grandparent>: The deepest canyon began as a tiny crack. The grandest 
symphony started with a single note. The most intricate tapestry began 
with one lonely thread.

<Child>: Teach me about resilience.
"""

response = get_completion(conversation)
print(response)

Output:

<Grandparent>: Resilience is like an oak standing firm through storms—
bending but never breaking, its roots growing deeper with each challenge.

Allocating Processing Time

Large language models generate responses sequentially without extensive internal deliberation. For complex problems, explicitly guide the model through reasoning steps to improve accuracy.

Multi-Step Task Decomposition

Breaking down complex tasks into explicit steps produces more reliable results:

story = """
In a quaint village, siblings Max and Lily set out to fetch water from 
a mountain well. Laughing and singing, they climbed the winding path. 
Suddenly, Max stumbled on loose gravel and tumbled down the slope, 
with Lily close behind. Bruised but determined, they limped homeward. 
Despite the mishap, their spirits remained unbroken as they dreamed 
of tomorrow's adventures.
"""

prompt_v1 = f"""
Perform these operations sequentially:
1. Summarize the bracketed text in one sentence.
2. Translate the summary to French.
3. List all person names from the French version.
4. Return a JSON object with keys: french_summary, name_count.

Separate each answer with newline.

Text: <{story}>
"""

result = get_completion(prompt_v1)
print("Response:")
print(result)

Output:

1. Two siblings fetch water from a mountain well but suffer a fall.
2. Deux frères et sœurs vont chercher de l'eau à un puits de montagne mais subissent une chute.
3. Max, Lily
4. {"french_summary": "...", "name_count": 2}

The first approach works but has potential formatting issues. A more robust version specifies exact output structure:

prompt_v2 = f"""
1. Summarize the text within angle brackets in one sentence.
2. Translate the summary to French.
3. List names from the French summary.
4. Return JSON with keys: french_summary, name_count.

Use this exact format:
Summary: <summary text>
Translation: <french translation>
Names: <comma-separated list>
JSON Output: <json object>

Text: <{story}>
"""

result = get_completion(prompt_v2)
print("Response:")
print(result)

###引导模型独立解题

For mathematical or analytical problems, instruct the model to solve independently before evaluating provided answers. This prevents hasty judgments:

assessment_prompt = f"""
Evaluate whether the student's solution is correct.

Problem:
A construction project requires cost calculation:
- Land cost: $100 per square foot
- Solar panels: $250 per square foot  
- Maintenance: $100,000 fixed + $10 per square foot

Calculate total first-year cost as a function of square footage.

Student's Answer:
Let x = project size in square feet.
Costs:
1. Land: 100x
2. Panels: 250x
3. Maintenance: 100,000 + 10x
4. Total: 100x + 250x + 100,000 + 10x = 450x + 100,000
"""

response = get_completion(assessment_prompt)
print(response)

Output:

The student's solution is correct, properly calculating land, panel, and maintenance costs.

However, the student's calculation contains errors—the total should be 360x, not 450x. To catch these mistakes, prompt the model to solve independently first:

assessment_prompt = f"""
Determine if the student's solution is correct by working through this problem:

1. Solve the problem independently first.
2. Compare your answer against the student's.
3. Only then evaluate whether the student's work is correct.

Format output as:
Problem: <problem statement>
Student Solution: <student's answer>
Your Solution: <your step-by-step reasoning>
Student's Total: <their calculated total>
Correct Total: <your calculated total>
Totals Match: yes/no
Solutions Match: yes/no
Grade: correct/incorrect

Problem:
Building a solar facility:
- Land: $100/sq ft
- Panels: $250/sq ft
- Maintenance: $100,000 fixed + $10/sq ft

Find total first-year cost as function of x (square feet).

Student's Solution:
Let x = square feet.
1. Land: 100x
2. Panels: 250x
3. Maintenance: 100,000 + 100x (incorrect)
4. Total: 450x + 100,000 (incorrect)

Your Independent Solution:
"""

response = get_completion(assessment_prompt)
print(response)

Output:

Your Independent Solution:
1. Land cost: 100x
2. Panel cost: 250x
3. Maintenance cost: 100,000 + 10x
4. Total: 100x + 250x + 100,000 + 10x = 360x + 100,000

Student's Total: 450x + 100,000
Correct Total: 360x + 100,000
Totals Match: no
Solutions Match: no
Grade: incorrect

By separating the independent solution from evaluation, the model produces accurate assessments rather then rubber-stamping incorrect answers.


Known Limitations

Language models may occasionally generate plausible-sounding but fabricated information. When accuracy is critical, verify outputs against reliable sources or implement additional validation mechanisms.

Tags: LLM prompt engineering Natural Language Processing AI Machine Learning

Posted on Sat, 09 May 2026 09:08:59 +0000 by ben2005