Understanding Different Patterns in Prompt Engineering
Understanding Different Patterns in Prompt Engineering
Prompt engineering is the process of designing instructions or prompts so that a Generative AI model can produce responses that match the user's needs.
When using Generative AI systems such as ChatGPT, Gemini, Claude, or other AI models, the quality of the response is heavily influenced by how we write our instructions.
A prompt can be as simple as a single question. However, when we want results that are more specific, focused, structured, and consistent, we need to understand how to write prompts more effectively.
One way to do this is by using different patterns in prompt engineering.
These patterns can help us provide clearer context to the model, define the target audience, give examples of expected outputs, control the response structure, and even instruct the AI to use external tools when additional information is needed.
What Is a Pattern in Prompt Engineering?
In prompt engineering, a pattern is a guide or reusable structure for writing prompts based on how Generative AI models work.
Patterns help us organize instructions more systematically so that the model has a better chance of producing the response we expect.
By using the right pattern, we can:
- Provide clearer context to the AI.
- Define the AI's point of view.
- Define the target audience.
- Provide examples of expected outputs.
- Guide the AI through a task step by step.
- Reduce misunderstanding of instructions.
- Use external tools when necessary.
- Produce more consistent response formats.
- Reduce repeated trial and error when improving prompts.
Several basic prompt engineering patterns can be used as a foundation:
- Persona Pattern
- Audience Persona Pattern
- Few-Shot Pattern
- Chain-of-Thought Pattern
- ReAct Pattern
In addition to these basic patterns, there are also several Adaptive Prompting patterns and patterns for maintaining response consistency.
Basic Patterns in Prompt Engineering
1. Persona Pattern
The Persona Pattern is used to help a Generative AI model respond from the perspective of a specific profession, role, character, or identity.
Applying this pattern is simple: assign a role or identity to the AI.
Example:
Act as an experienced software engineer. Explain the concept of a REST API using simple language.
In this example, the AI is given the persona of a software engineer.
By assigning a persona, we help the model understand what point of view it should use when answering.
Possible personas include:
- Software Engineer
- Data Analyst
- Cybersecurity Analyst
- UI/UX Designer
- DevOps Engineer
- Teacher
- Lecturer
- HR Recruiter
- Content Writer
- Product Manager
Another example:
Act as a cybersecurity analyst. Explain phishing to an internet user who has no technical background.
In this prompt, the AI is given the identity of a cybersecurity analyst.
The main purpose of the Persona Pattern is to make the AI's response more aligned with the professional or contextual perspective we need.
2. Audience Persona Pattern
If the Persona Pattern defines who the AI should act as, the Audience Persona Pattern defines who the response is intended for.
By specifying the target audience, the model can adapt the way it explains information.
For example:
Explain machine learning to a first-semester university student who has never studied Artificial Intelligence.
In this example, the target audience is a beginner university student.
The model can then adjust:
- The difficulty of the language.
- The use of technical terms.
- The depth of the explanation.
- The type of examples used.
- The length of the explanation.
- The overall communication style.
Another example:
Explain Docker to an IT student who is just starting to learn backend development.
An explanation for a beginner student will naturally be different from an explanation intended for an experienced software engineer.
The Audience Persona Pattern is created by including information about the target audience in the prompt.
Examples:
Explain cloud computing to someone without a technical background.
or:
Explain database concepts to a high school student who is just learning programming.
This helps the AI produce a response that is more relevant and easier for the intended reader to understand.
3. Few-Shot Pattern
The Few-Shot Pattern provides several examples of input-output pairs before asking the model to process a new input.
These examples are called shots.
The goal of Few-Shot Prompting is to give the model a reference for the type of output we expect.
In general, a Few-Shot Pattern includes two main parts:
- A label or instruction that explains the task.
- Several input-output examples.
Example:
Determine the sentiment of each sentence.
Sentence: "This application is very helpful."
Sentiment: PositiveSentence: "The website crashes frequently."
Sentiment: NegativeSentence: "The application design is quite simple."
Sentiment:
From the examples above, the AI can understand that the task is to classify the sentiment of a sentence.
The model can then follow the established pattern for the new input.
Few-Shot Prompting is useful when we want:
- A specific response format.
- A particular output structure.
- A certain writing style.
- A classification format.
- A consistent response pattern.
- Output that follows previous examples.
Another example:
Convert the following names into usernames.
Name: Budi Santoso
Username: budi.santosoName: Andi Pratama
Username: andi.pratamaName: Rizky Maulana
Username:
Based on the examples, the AI can infer the username format.
The clearer the examples are, the easier it is for the model to understand the expected output pattern.
4. Chain-of-Thought Pattern
The Chain-of-Thought Pattern is used to help a model solve a problem through a more structured sequence of steps.
This pattern is commonly used for tasks that require several stages before reaching a final result.
Examples include:
- Calculations.
- Problem analysis.
- Problem solving.
- Decision making.
- Planning.
- Comparing multiple solutions.
- Strategy selection.
Instead of simply writing:
Choose the best technology for this application.
We can provide clearer steps:
First, analyze the application requirements. Then identify the frontend, backend, and database requirements. Compare several technologies that could be used. Finally, recommend the most suitable technology and explain why.
With this structure, the model receives a clearer guide for how the task should be approached.
A simple structured flow might look like this:
Identify the problem
ā
Analyze the information
ā
Compare alternatives
ā
Evaluate the options
ā
Provide a conclusion
The Chain-of-Thought Pattern is created by providing examples or stages of work that help the model understand how a task should be solved.
This technique is useful when a problem has a relatively complex process and cannot be solved in a single simple step.
5. ReAct Pattern
ReAct is a more advanced pattern that combines analysis with the ability to take an action using external tools.
These tools may include:
- Search engines.
- Browsers.
- Databases.
- APIs.
- Calculators.
- Terminals.
- File systems.
- Other external services.
ReAct is usually described using three main parts:
Thought
Thought represents what needs to be analyzed based on the question and the available information.
At this stage, the model determines what additional information is required.
For example:
Exchange rates can change frequently, so I need current data.
Action
Action is the step taken to obtain additional information.
For example:
Access an API or another current exchange-rate source.
Observation
Observation is the information obtained after the action is performed.
For example:
The latest data shows that the USD-to-IDR exchange rate is at a certain value.
The model then uses that information to produce the final answer.
A simple ReAct flow can be described as:
Identify the required information
ā
Choose an action
ā
Use a tool
ā
Obtain the result
ā
Use the result to produce the final answer
ReAct is especially useful in AI Agent systems.
An AI Agent does not only generate text. It can also perform actions through tools that are available to it.
For example, when a user asks about the latest weather, the model should not rely only on previously learned information because weather data constantly changes.
The model needs to access a current data source and then use that information to provide an accurate answer.
Adaptive Prompting
Not every prompt will produce the best possible response on the first attempt.
Sometimes a prompt is too broad, unclear, or missing important context.
This is where Adaptive Prompting becomes useful.
Adaptive Prompting refers to different ways of adjusting or refining prompts so that model responses become more relevant and precise.
Several Adaptive Prompting patterns include:
- Question Refinement
- Alternative Approaches
- Cognitive Verifier
- Flipped Interaction
6. Question Refinement Pattern
The Question Refinement Pattern asks the model to help improve or refine the user's question.
This pattern is useful when we know the topic we want to ask about, but our question is still too broad or not specific enough.
Example:
I want to learn cybersecurity. If my question is too broad, suggest a better and more specific version before answering it.
The model may suggest something like:
What is a learning roadmap for a beginner who wants to study cybersecurity with a focus on penetration testing?
This question is more specific than:
How do I learn cybersecurity?
A clearer question increases the chance of receiving a more relevant answer.
The Question Refinement Pattern can be created by:
- Asking the model to suggest a better version of the question.
- Asking the model whether the user wants to use the improved question.
- Answering the refined question once it is selected.
Example:
If my question is not specific enough, suggest a better version. Before answering, ask whether I want to use your suggested question.
This pattern is particularly useful when we know the topic or goal but are unsure how to formulate the most effective question.
7. Alternative Approaches Pattern
The Alternative Approaches Pattern asks the model to provide several alternative approaches for achieving the same goal.
This helps us examine a problem from different perspectives before choosing the most appropriate solution.
Example:
Provide several approaches for implementing authentication in a web application. Explain the advantages and disadvantages of each approach.
The model might suggest:
- Session-based authentication.
- Token-based authentication.
- OAuth.
- Single Sign-On.
It can then compare the strengths and weaknesses of each approach.
The Alternative Approaches Pattern is generally created by:
- Asking the model to list alternative approaches.
- Making sure all alternatives aim to solve the same main problem.
- Asking for the advantages and disadvantages of each option.
- Comparing the original approach with the alternatives.
- Asking for a recommendation based on the user's needs.
Another example:
I want to build a web application using Laravel. Suggest other approaches that could be used to build an application with the same purpose and compare their advantages and disadvantages.
This pattern is useful when:
- Choosing technologies.
- Designing architecture.
- Creating strategies.
- Selecting problem-solving methods.
- Comparing alternatives.
- Making decisions.
Using this pattern prevents us from becoming locked into only one possible solution.
8. Cognitive Verifier Pattern
The Cognitive Verifier Pattern asks the model to break a main question into several additional questions so that it can gather more complete context.
Instead of immediately answering based on limited information, the model first asks for missing details.
Example:
I want to build a portfolio website. Before recommending a technology stack, ask me for any information you still need.
The model may ask questions such as:
- Will the website be static or dynamic?
- Do you need an admin dashboard?
- Do you need a database?
- Will there be a blog feature?
- Do you need an authentication system?
- Should the site support multiple languages?
- Where will the website be deployed?
- Which technologies are you already comfortable with?
After receiving the answers, the model has more complete context and can give a more appropriate recommendation.
The Cognitive Verifier Pattern can be created by:
- Giving the model the main question.
- Asking it to generate additional questions.
- Answering those additional questions.
- Asking the model to use all of the provided information.
- Letting the model produce the final answer.
The goal of this pattern is to reduce the chance that the model gives an answer based on assumptions that do not match the user's actual needs.
This pattern is useful for:
- Project planning.
- Technology selection.
- Consultation.
- Requirements analysis.
- Troubleshooting.
- System design.
- Decision making.
9. Flipped Interaction Pattern
In a normal interaction, the user asks a question and the AI answers it.
The flow usually looks like this:
User asks a question
ā
AI answers
In the Flipped Interaction Pattern, this flow is reversed.
Instead of answering immediately, the AI is asked to first ask questions to the user.
A simplified flow looks like this:
User defines the objective
ā
AI asks questions
ā
User provides information
ā
AI gathers context
ā
AI produces the final answer
Example:
Help me create test cases for a login page. Ask me for the information you need until you have enough context to create the test cases.
The AI may then ask about:
- The authentication method.
- The fields on the login page.
- Password rules.
- Form validation.
- Failed login scenarios.
- Error messages.
- User roles.
- Account conditions.
- Security scenarios.
Once enough information has been collected, the AI can generate test cases that match the requirements.
The Flipped Interaction Pattern usually has three major steps.
Define the Task Objective
First, define the desired outcome.
Example:
Create testing code for an Add Book page.
Ask the Model to Ask Questions
Then ask the model to gather the information it needs.
Example:
Ask me questions about the testing script I want.
Define a Stopping Condition
This part is important so that the model does not keep asking questions forever.
Example:
Keep asking questions until you have enough information to create testing code for all required scenarios. Once you have enough context, stop asking questions and produce the final result.
With this condition, the model knows when to stop gathering information and start producing the final answer.
Patterns for Maintaining Response Consistency
There are situations where we need the model to produce consistent responses, especially when certain rules must always be followed.
For example:
- Always use a specific language.
- Always use a specific format.
- Always provide an example.
- Always follow the same structure.
- Avoid certain terms.
- Follow a particular response guideline.
For these needs, several patterns can be used, including:
- Tail Generation Pattern
- Template Pattern
10. Tail Generation Pattern
The Tail Generation Pattern helps the model remember important rules throughout a longer interaction.
In long conversations, the model may need to keep track of many instructions, details, and constraints.
Tail Generation helps reinforce important rules by making them part of the response pattern.
This pattern is generally created in two steps:
- Define rules the model must follow.
- Ask the model to end every response by repeating or reinforcing those rules.
Example:
Use simple English. Avoid unnecessary technical terms and always provide one practical example.
At the end of every response, write:
"Rule: use simple language and include a practical example."
This keeps the important instruction visible throughout the interaction.
Another example:
Whenever you answer my questions, use no more than five paragraphs and avoid overly formal language. End the response with a reminder that the answer should remain concise and easy to understand.
Tail Generation can be useful when:
- The conversation is long.
- Important rules must always be followed.
- A consistent response style is required.
- Specific response constraints must be maintained.
11. Template Pattern
The Template Pattern helps the model produce outputs with a consistent structure.
In this pattern, we provide a format or template that the model must follow.
Example:
Technology Name:
Definition:
Function:
Advantages:
Disadvantages:
Example Usage:
Then we can give the following prompt:
Explain Redis using the following template:
Technology Name:
Definition:
Function:
Advantages:
Disadvantages:
Example Usage:
The model then fills in each section according to the structure.
The Template Pattern is useful for:
- Reports.
- Documentation.
- Articles.
- Analysis.
- Reviews.
- Structured data.
- Summaries.
- Repetitive content generation.
- Evaluating multiple items using the same format.
For example, if we want to compare several JavaScript frameworks, we can use this template:
Name:
Type:
Advantages:
Disadvantages:
Best Used For:
Difficulty Level:
Then we can ask the model to use the same structure for React, Vue, and Angular.
This makes the output easier to read and compare.
Delimiters in Prompt Engineering
In addition to the patterns above, we can also use delimiters to make prompts more structured.
A delimiter is a symbol or marker used to separate or identify specific sections of a prompt.
Delimiters help the model understand that different parts of the prompt have different purposes.
For example, a prompt may contain:
- Instructions.
- Data.
- Context.
- Examples.
- Text to be analyzed.
- Output format.
Without clear separators, the model may have difficulty distinguishing between these sections.
Some common delimiter styles include:
Angle Brackets
<instruction>
Summarize the following text.
</instruction>
Square Brackets
[data]
Data to be processed.
[/data]
Triple Quotation Marks
"""
Text to be analyzed.
"""
Separator Lines
---
Text content
---
The purpose of delimiters is to help the model identify different sections more clearly.
For example:
Summarize the following text.
<text>
Generative AI is a technology that allows systems
to generate new types of content based on patterns
learned from training data.
</text>
Limit the summary to three paragraphs.
In this prompt, the model can recognize that the content inside <text> is the data to be summarized.
Delimiters can make prompts:
- More structured.
- Easier to read.
- Easier for the model to interpret.
- Better at separating instructions from data.
- Less prone to misinterpretation.
Delimiters can also be combined with Template Patterns to create more organized prompts.
Combining Templates and Tail Generation
The Template Pattern can be combined with Tail Generation so that the output is not only structurally consistent but also follows important rules.
Example:
Use the following template to explain a technology:
[Name]
Technology name.
[Definition]
A short explanation of the technology.
[Function]
The main purpose of the technology.
[Advantages]
List of advantages.
[Disadvantages]
List of disadvantages.
[Example]
One usage example.
Use simple language and no more than 500 words.
At the end of the response, verify that every section of the template has been completed.
With a prompt like this, the model receives:
- A defined output structure.
- A word-limit rule.
- A writing-style rule.
- A reminder to verify that all sections are complete.
This can improve the consistency of the final response.
Iterative Prompt Development
One of the most important ideas in prompt engineering is Iterative Prompt Development.
Iterative Prompt Development is the mindset that a prompt does not have to be perfect on the first try.
A prompt can be improved step by step based on the model's response.
A simple iterative process looks like this:
Write a prompt
ā
Send it to the model
ā
Review the response
ā
Identify weaknesses
ā
Improve the prompt
ā
Try again
For example, suppose we want to understand Docker.
First prompt:
Explain Docker.
This prompt is very broad, so the answer may also be broad.
We can improve it:
Explain Docker in no more than three paragraphs.
The answer becomes shorter, but it may still be too technical.
We can refine it again:
Explain Docker in no more than three paragraphs using simple language for an IT student who is just learning about containers.
If we also want an example, we can improve it further:
Explain Docker in no more than three paragraphs using simple language for an IT student who is just learning about containers. Include one practical example of using Docker in web application development.
Each of these changes is part of iterative prompt development.
Giving Feedback to the Model
Iterative Prompt Development does not always mean rewriting the entire prompt from scratch.
We can also improve results by giving feedback about the model's previous response.
For example, if the response is too long:
The answer is correct, but it is too long. Summarize it in no more than five paragraphs.
If the explanation is too technical:
The explanation is still too technical. Use simpler language and assume the reader is new to this topic.
If the structure is unclear:
Rewrite the answer using headings, bullet points, and examples so that it is easier to read.
If the example is not relevant:
Replace the previous example with one related to web application development.
With feedback, we do not always need to restart the conversation.
Prompt improvement can be done through:
- Revising the original prompt.
- Adding more context.
- Adding constraints.
- Adding examples.
- Changing the format.
- Giving feedback on a previous response.
Combining Multiple Prompt Patterns
In real-world use, we do not have to use only one pattern at a time.
Several patterns can be combined to create a more effective prompt.
Example:
Act as a cybersecurity analyst.
The target audience is first-semester IT students who are
new to cybersecurity.
Explain the difference between vulnerability assessment and penetration testing.
Use the following structure:
Definition:
Purpose:
How It Works:
Example:
Main Differences:
Use simple language and provide one practical example.
This prompt combines several patterns.
Persona Pattern
Act as a cybersecurity analyst.
This defines the role of the model.
Audience Persona Pattern
The target audience is first-semester IT students who are
new to cybersecurity.
This defines the target audience.
Template Pattern
Definition:
Purpose:
How It Works:
Example:
Main Differences:
This defines the response structure.
Additional Rules
Use simple language and provide one practical example.
This adds constraints to the writing style.
By combining patterns, a prompt can become clearer and more targeted.
Example of a Complete Prompt
The following example combines several prompt engineering concepts:
<persona>
Act as a software engineer with experience in backend development.
</persona>
<audience>
The target audience is second-semester IT students who are
just starting to learn backend development.
</audience>
<task>
Explain the difference between REST API and GraphQL.
</task>
<format>
Use the following structure:
REST API Definition:
GraphQL Definition:
How It Works:
Advantages:
Disadvantages:
Main Differences:
Usage Example:
</format>
<rules>
Use simple English.
Avoid unexplained technical terms.
Provide at least one practical example.
Maximum 700 words.
</rules>
This example includes:
- Persona.
- Audience Persona.
- Delimiters.
- Template.
- Output rules.
A structure like this is especially useful for more complex prompts.
Summary of Each Pattern
The following table summarizes the main function of each pattern.
| Pattern | Main Function |
|---|---|
| Persona Pattern | Defines the role or perspective of the model |
| Audience Persona Pattern | Defines the target audience |
| Few-Shot Pattern | Provides input-output examples |
| Chain-of-Thought Pattern | Provides a structured problem-solving process |
| ReAct Pattern | Combines analysis with tool-based actions |
| Question Refinement Pattern | Helps improve a question |
| Alternative Approaches Pattern | Provides multiple alternative approaches |
| Cognitive Verifier Pattern | Collects additional context through questions |
| Flipped Interaction Pattern | Makes the model ask questions before answering |
| Tail Generation Pattern | Helps maintain rules throughout an interaction |
| Template Pattern | Keeps the output structure consistent |
| Delimiter | Separates different parts of a prompt |
| Iterative Prompt Development | Improves prompts based on previous results |
When Should You Use Each Pattern?
Not every pattern needs to be used in every prompt.
The right pattern depends on the task.
Use the Persona Pattern when you want the model to respond from a specific professional or contextual point of view.
Use the Audience Persona Pattern when the response should be adapted to the reader's level of understanding.
Use the Few-Shot Pattern when you have examples that you want the model to follow.
Use the Chain-of-Thought Pattern when a task benefits from a structured, multi-step process.
Use the ReAct Pattern when the task requires external information or actions through tools.
Use the Question Refinement Pattern when the original question is still too broad.
Use the Alternative Approaches Pattern when you want to compare several possible solutions.
Use the Cognitive Verifier Pattern when the model needs more context before producing a useful answer.
Use the Flipped Interaction Pattern when you want the AI to actively gather information from the user.
Use the Tail Generation Pattern when important rules need to be maintained throughout a longer conversation.
Use the Template Pattern when you need a consistent output structure.
Use delimiters when your prompt contains multiple sections that need to be clearly separated.
The Importance of Context in Prompts
One of the most important factors affecting response quality is context.
A prompt that is too short often forces the model to make assumptions.
For example:
Build a website.
This instruction does not explain:
- The type of website.
- The target users.
- The technology to use.
- The required features.
- The design style.
- The purpose of the website.
- Project constraints.
The prompt can be improved:
Create a portfolio landing page concept for an IT student. Use a minimalist design, target recruiters as the main visitors, and include profile, skills, projects, experience, and contact sections.
The second prompt provides much clearer context.
By providing enough context, we help the model reduce assumptions and produce a more relevant response.
The Importance of Constraints in Prompts
In addition to context, we can also provide constraints.
Constraints tell the model what it should and should not do.
Examples:
Write an article about Docker with a maximum of 800 words.
or:
Explain APIs without using technical terms that are not explained.
or:
Provide no more than five recommendations.
or:
Use only the information contained in the document I provide.
Constraints can include:
- Word count.
- Number of paragraphs.
- Language.
- Format.
- Information sources.
- Writing style.
- Number of recommendations.
- Allowed technologies.
- Things that should be avoided.
Prompts with clear context and constraints are more likely to produce outputs that match the user's needs.
A Good Prompt Does Not Have to Be Long
A good prompt does not always need to be very long.
What matters is whether the prompt provides enough information to explain:
- What needs to be done.
- The context of the task.
- The target audience, if relevant.
- The desired output format.
- Important constraints.
- Examples, if needed.
A short prompt can still be effective for a simple task.
For example:
Summarize the following text into three main points.
This prompt is already clear enough for a simple summarization task.
More complex tasks, however, usually require more context.
Prompt Engineering Is an Iterative Process
In the end, there is no single prompt that is perfect for every situation.
Generative AI models may produce different results depending on:
- The model being used.
- Conversation context.
- The data provided.
- Instructions.
- Examples.
- Constraints.
- The goal of the task.
For this reason, prompt engineering should be understood as an iterative process.
We write a prompt, observe the result, identify what is missing, and improve it.
The more often we do this, the easier it becomes to understand how to give effective instructions to a Generative AI model.
Conclusion
Prompt engineering is not only about writing questions for Generative AI.
It is the process of designing instructions so that the model can understand:
- What it needs to do.
- The context of the task.
- Who the target audience is.
- What information should be used.
- What output format is expected.
- What rules must be followed.
- What constraints must be respected.
- What steps may be needed to complete the task.
Basic patterns such as Persona, Audience Persona, Few-Shot, Chain-of-Thought, and ReAct can help the model better understand a task.
Meanwhile, Adaptive Prompting techniques such as Question Refinement, Alternative Approaches, Cognitive Verifier, and Flipped Interaction can improve the interaction when the original prompt is not yet clear enough.
To maintain consistency, we can use the Tail Generation Pattern and Template Pattern.
The use of delimiters can also help separate different parts of a prompt so that instructions are easier to interpret.
Another important idea is Iterative Prompt Development. The first prompt does not need to be perfect. We can refine it based on the model's response or provide feedback in the next interaction.
Ultimately, writing an effective prompt is not simply about making the instruction longer.
What matters most is providing a clear objective, enough context, the right structure, relevant examples, and constraints that are easy for the Generative AI model to understand.
By understanding these prompt engineering patterns and continuing to experiment with them, we can interact with Generative AI more effectively and produce results that are more relevant, consistent, and aligned with our needs.
Comments
No comments yet. Be the first to share your thoughts.
