Skip to main content
Prompt engineering is the process of designing clear and effective instructions for Large Language Models, or LLMs, to produce useful, accurate, and structured responses. A good prompt helps the model understand:
  • What task to perform
  • What context to consider
  • How specific the response should be
  • What constraints to follow
  • What format the output should use
A general prompt structure is:

1. Role Prompting

Definition

Role prompting means assigning a role, expertise, or perspective to the LLM. This helps guide the style, depth, and perspective of the response.

Example

Another Example

Why It Is Useful

Role prompting can help control:
  • Level of expertise
  • Writing style
  • Perspective
  • Type of explanation
  • Expected vocabulary

Template


2. Context

Definition

Context is the relevant background information provided to the model before or along with the task. More relevant context usually helps the model produce a more appropriate answer.

Example Without Context

The model may not know which programming language, framework, or code is involved.

Example With Context

Why Context Is Important

Context helps the model understand:
  • The user’s situation
  • The target audience
  • The domain
  • Previous decisions
  • Important requirements
  • Relevant input data

Template


3. Specificity

Definition

Specificity means clearly defining exactly what you want the model to do. A vague prompt can produce vague or unpredictable results.

Vague Prompt

Specific Prompt

Benefits

Specific prompts help:
  • Reduce ambiguity
  • Improve relevance
  • Control response length
  • Avoid unnecessary information
  • Produce more predictable results

4. Constraints

Definition

Constraints are rules or limitations that control the model’s response. They define what the model should or should not do.

Example

Common Constraints

  • Maximum word count
  • Minimum number of examples
  • Required sections
  • Language level
  • Tone
  • Topics to avoid
  • Output length
  • Formatting rules

Example


5. Output Format

Definition

Output format specifies how the LLM should structure its response. This is useful when you need consistent and predictable results.

Example

JSON Example

Why It Is Useful

Output formatting helps:
  • Create consistent responses
  • Make results easier to read
  • Support automation
  • Enable easier parsing by applications
  • Improve integration with software systems

6. Zero-Shot Prompting

Definition

Zero-shot prompting means giving the model a task without providing examples. The model uses its existing knowledge and the instructions in the prompt.

Example

Expected output:

When to Use

Use zero-shot prompting when:
  • The task is simple
  • The instructions are clear
  • Examples are not necessary
  • You want a short and efficient prompt

7. Few-Shot Prompting

Definition

Few-shot prompting means providing a few examples to demonstrate the expected task or output. The examples guide the model.

Example

The model learns the expected pattern from the examples.

When to Use

Few-shot prompting is useful when:
  • The task has a specific pattern
  • Output formatting is important
  • The classification criteria are unclear
  • You want consistent behavior

Important Point

The examples should be:
  • Relevant
  • Clear
  • Consistent
  • Similar to the actual task

8. Reasoning and Verification

Definition

Some tasks are complex and benefit from explicitly asking the model to carefully reason through the problem and verify the result. This is especially useful for:
  • Mathematics
  • Programming
  • Data analysis
  • Multi-step problems
  • Logical reasoning

Example

Verification Prompt

Important Practice

For important tasks, ask the model to:
  1. Solve the task.
  2. Check the result.
  3. Compare the result with the requirements.
  4. Correct any identified problems.

9. Structured Output

Definition

Structured output means requesting the model to return information in a predefined, machine-readable format. Common formats include:
  • JSON
  • CSV
  • XML
  • YAML
  • Tables

JSON Example

Example Output

Benefits

Structured output makes it easier to:
  • Store responses
  • Parse data
  • Use responses in APIs
  • Connect LLMs with applications
  • Automate workflows

10. Iterative Refinement

Definition

Iterative refinement means improving a prompt or output through multiple attempts. Instead of expecting the first prompt to be perfect, you evaluate the result and improve the instructions.

Example

First Prompt

Improved Prompt

Further Refinement

Iterative Process


11. Self-Evaluation

Definition

Self-evaluation means asking the model to evaluate its generated output against predefined criteria.

Example

Benefits

Self-evaluation can help improve:
  • Completeness
  • Consistency
  • Formatting
  • Requirement compliance
  • Error detection

Evaluation Template


12. Prompt Chaining

Definition

Prompt chaining means breaking a complex task into multiple smaller stages. The output from one stage can become the input for the next stage.

Example: Writing an Article

Instead of using one large prompt:
Break the workflow into stages.

Step 1: Generate Topics

Step 2: Create an Outline

Step 3: Write the Content

Step 4: Review

Workflow

Benefits

Prompt chaining helps with:
  • Complex tasks
  • Large documents
  • Multi-step workflows
  • Better control
  • Easier debugging

13. Context Management

Definition

Context management is the process of selecting, organizing, and maintaining the information given to an LLM. LLMs have limits on how much information can be processed in a request. Therefore, irrelevant or excessive context should be avoided.

Poor Context Management

This can make the important information harder to identify.

Better Context Management

Context Management Techniques

1. Include Relevant Information

Only provide information related to the task.

2. Remove Unnecessary Information

Avoid including unrelated files, messages, or data.

3. Summarize Long Content

4. Retrieve Relevant Information

For large knowledge bases:
This approach is commonly used in Retrieval-Augmented Generation, or RAG.

14. Prompt Templates

Definition

A prompt template is a reusable prompt structure containing placeholders or variables.

Example


15. Prompt Variables

Definition

A prompt variable is a placeholder whose value can change dynamically.

Template

Variable Values

Final Prompt


16. Prompt Templates and Variables in Python


17. Delimiters

Definition

Delimiters are symbols or markers used to clearly separate instructions, context, and input. They help organize complex prompts. Common delimiters include:

Example

Another example:

Why Delimiters Are Useful

They help separate:
  • Instructions
  • User input
  • Examples
  • Data
  • Context

18. Task Decomposition

Definition

Task decomposition means breaking a large problem into smaller and manageable tasks.

Example

Instead of:
Use smaller stages:

Benefits

  • Easier problem solving
  • Better control
  • Easier debugging
  • Clearer workflow
  • Improved reliability
Task decomposition and prompt chaining are closely related.

19. Input Data Separation

When providing external data, clearly separate the instructions from the data.

Example

This helps prevent confusion between:
  • Instructions
  • Input data
  • Examples
For untrusted external content, explicitly define that the content is data to analyze, not instructions to follow.
This is especially important when working with:
  • User-generated content
  • Web content
  • Documents
  • Emails
  • Retrieved knowledge

20. Negative Instructions

Definition

Negative instructions specify what the model should avoid doing.

Example

Example

Negative instructions should be clear and specific.

21. Audience Targeting

The same information can be explained differently depending on the audience.

Example

Beginner

Developer

The audience affects:
  • Vocabulary
  • Technical depth
  • Examples
  • Explanation style

22. Prompt Ordering

The order of information in a prompt can improve clarity. A recommended structure is:

Example


23. Instruction Hierarchy

When a prompt contains multiple instructions, organize them clearly.

Poor Example

Better Example

Clear sections make instructions easier to follow.

24. Handling Ambiguity

If a task is unclear, the prompt can instruct the model how to handle missing information.

Example

Another example:
This helps reduce incorrect assumptions.

25. Grounding

Definition

Grounding means instructing the model to base its response on specific provided information.

Example

Why Grounding Is Useful

It helps:
  • Reduce unsupported claims
  • Keep answers relevant
  • Improve consistency
  • Support document-based question answering

26. Prompt Injection Awareness

When an LLM processes external or untrusted content, that content may contain instructions that attempt to change the task.

Example

The model should distinguish between:
  • Trusted instructions
  • Untrusted input data
This is an important concept when building LLM applications using:
  • Web content
  • User input
  • Documents
  • RAG systems
  • External APIs

27. Temperature and Generation Settings

When using an LLM API, generation settings can affect the output.

Temperature

Temperature controls the level of randomness in generated responses.

Lower Temperature

Useful for:
  • Structured extraction
  • Classification
  • Consistent answers
  • Deterministic-style tasks

Higher Temperature

Useful for:
  • Creative writing
  • Brainstorming
  • Generating ideas
The exact effect can vary by model and API.

28. Common Prompting Mistakes

1. Being Too Vague

Better:

2. Providing Too Much Irrelevant Context

Only include information required for the task.

3. Not Specifying the Audience

Better:

4. Not Defining the Output Format

If structure matters, explicitly request it.

5. Using Contradictory Instructions

Avoid prompts such as:
Make requirements compatible.

6. Using Too Many Unnecessary Instructions

A prompt should contain enough information to guide the model, but unnecessary complexity can make it harder to understand the important task.

29. Complete Reusable Prompt Template


30. Prompt Engineering Workflow

A practical workflow for designing prompts is:

Summary

Final Key Idea

A strong prompt is not simply a question. It is a clear specification of the task.