Mastering Prompt Engineering: From Beginner to Advanced Techniques
Uncover the secrets of prompt engineering to unlock the full potential of AI models like ChatGPT. Dive into advanced techniques, from zero-shot to chain-of-thought, and learn how to craft prompts that deliver accurate, engaging, and consistent results.
15 juin 2025

This comprehensive guide provides practical strategies to help you become a master at prompt engineering. By exploring techniques like zero-shot, one-shot, and few-shot prompting, as well as advanced methods like system messages, contextual prompting, and chain of thought, you'll learn how to craft prompts that unlock the full potential of large language models. Whether you're a beginner or an experienced user, this guide offers valuable insights to enhance your AI-powered workflows.
Basic Prompt Engineering Concepts
Prompt Settings and Nuances
Prompt Engineering Techniques
General Prompting (Zero-Shot)
One-Shot and Few-Shot Prompting
System Message, Contextual, and Role Prompting
Step-Back Prompting
Chain of Thought
Self-Consistency
Tree of Thought
Reason and Act (React)
Automatic Prompt Engineering
When to Use Code vs. Natural Language
Prompt Engineering Best Practices
Basic Prompt Engineering Concepts
Basic Prompt Engineering Concepts
When interacting with a large language model (LLM) like ChatGPT, Gemini, or Claude, you provide an input prompt, and the model generates an output response. The way you structure this prompt is crucial, as it guides the model to produce the desired output. This process is known as prompt engineering.
LLMs work by taking your input prompt and predicting the most likely sequence of tokens (usually words) that should follow. The model's predictions are based on the data it was trained on. Prompt engineering involves designing high-quality prompts that direct the LLM to produce accurate and relevant outputs.
Some key concepts in prompt engineering include:
-
Output Length: This setting determines the maximum number of tokens the model should output in response to your prompt. Shorter output lengths can be used for simple tasks, while longer output lengths may be necessary for more complex tasks like generating stories or code.
-
Sampling Controls: These settings, including temperature, top-K, and top-P, control the randomness and creativity of the model's output. Higher temperature leads to more diverse and creative responses, while lower temperature results in more consistent and factual outputs.
-
Zero-Shot, One-Shot, and Few-Shot Prompting: Zero-shot prompting involves providing a description of the task without any examples. One-shot and few-shot prompting involve providing one or more examples to guide the model's output.
-
System Messages, Contextual Prompting, and Role Prompting: System messages set the overall context and purpose for the model. Contextual prompting provides relevant background information. Role prompting assigns a specific character or identity for the model to adopt.
-
Chain of Thought and Self-Consistency: Chain of thought prompting asks the model to show its step-by-step reasoning. Self-consistency combines multiple model outputs to select the most consistent answer.
-
Tree of Thought and Reason-and-Act (React): Tree of thought explores multiple reasoning paths simultaneously. React combines reasoning and external tool usage in a thought-action loop.
-
Automatic Prompt Engineering: Leveraging the model's capabilities to generate prompts, including prompts that use the advanced techniques mentioned above.
By understanding and applying these prompt engineering concepts, you can significantly improve the quality and relevance of the outputs generated by large language models.
Prompt Settings and Nuances
Prompt Settings and Nuances
When interacting with large language models (LLMs) like ChatGPT, Gemini, or Claude, there are several important settings and nuances to understand:
-
Output Length: This setting controls the maximum number of tokens (roughly equivalent to words) the model should output in response to a prompt. Shorter output lengths can be more concise, but don't necessarily result in more succinct responses.
-
Sampling Controls:
- Temperature: Controls the randomness and creativity of the model's responses. Higher temperatures lead to more diverse and unpredictable outputs, while lower temperatures result in more consistent and conservative responses.
- Top K: Limits the model's vocabulary to the top K most likely tokens during generation.
- Top P (Nucleus Sampling): Limits the model's vocabulary to the minimum number of tokens whose cumulative probability exceeds the specified threshold.
-
General Prompting (Zero-Shot): Providing a description of the task without any examples.
-
One-Shot and Few-Shot Prompting: Providing one or more examples to guide the model's response.
-
System Message, Contextual Prompting, and Role Prompting:
- System Message: Sets the overall context and purpose for the language model.
- Contextual Prompting: Provides specific details or background information relevant to the current task.
- Role Prompting: Assigns a specific character or identity for the language model to adopt.
-
Step-Back Prompting: Encourages the model to consider a broader, underlying question before attempting to solve a specific problem.
-
Chain of Thought: Prompting the model to show its step-by-step reasoning process.
-
Self-Consistency: Running the same prompt multiple times and using majority voting to select the most consistent answer.
-
Tree of Thought: Exploring multiple reasoning paths simultaneously, often requiring external tools or frameworks.
-
Reason and Act (React) Prompting: Combining natural language reasoning with the ability to execute external actions or tools.
-
Automatic Prompt Engineering: Leveraging the model's capabilities to generate and refine prompts.
-
Prompting for Code vs. Natural Language: Deciding when to ask the model to write and execute code versus providing a natural language solution.
By understanding these prompt settings and techniques, you can more effectively guide large language models to produce the desired outputs for your specific use cases.
Prompt Engineering Techniques
Prompt Engineering Techniques
General Prompting (Zero-Shot)
- The simplest type of prompting, where you provide a description of the task without any examples.
- Useful for simple tasks, but may require more complex prompting for more complex tasks.
One-Shot and Few-Shot Prompting
- One-shot prompting provides a single example to guide the model.
- Few-shot prompting provides multiple examples to help the model understand the desired pattern or format.
- The number of examples needed depends on the complexity of the task, the quality of the examples, and the capabilities of the model.
System Message, Contextual, and Role Prompting
- System message sets the overall context and purpose for the language model.
- Contextual prompting provides specific details or background information relevant to the current task.
- Role prompting assigns a specific character or identity for the language model to adopt.
Step-Back Prompting
- Asks the model to first consider a general question related to the specific task, then uses the answer to inform the subsequent prompt for the specific task.
- Allows the model to activate relevant background knowledge and reasoning processes before attempting to solve the specific problem.
Chain of Thought
- Prompts the model to output its thinking process step-by-step before providing the final answer.
- Improves the accuracy and coherence of the model's responses, especially for complex tasks.
Self-Consistency
- Combines sampling and majority voting to generate diverse reasoning paths and select the most consistent answer.
- Improves the accuracy and coherence of responses generated by large language models.
Tree of Thought
- Allows the model to explore multiple different reasoning paths simultaneously, rather than just following a single linear chain of thought.
- Particularly well-suited for complex tasks that require exploration.
Reason and Act (React)
- Combines reasoning and acting into a thought-action loop, where the model first reasons about the problem, generates a plan of action, and then performs the actions and observes the results.
- Enables large language models to solve complex tasks using natural language reasoning combined with external tools, search, code interpretation, etc.
Automatic Prompt Engineering
- Asking the language model to write prompts for you, including prompts that incorporate the various techniques mentioned above.
- Allows you to add more detail and complexity to your prompts without having to write them manually.
General Prompting (Zero-Shot)
General Prompting (Zero-Shot)
Zero-shot prompting is the simplest type of prompting, where you provide a description of the task you want the model to accomplish, without giving any examples. This relies on the model's ability to understand the task and generate an appropriate response based solely on the prompt.
The key aspects of zero-shot prompting are:
- No Examples: You do not provide any examples of the desired output. You simply describe the task in natural language.
- Task Description: The prompt should contain a clear and thorough description of the task you want the model to perform.
- Suitable for Simple Tasks: Zero-shot prompting works best for relatively simple tasks, where the model can infer the desired output from the prompt alone.
- Limitations: For more complex tasks, zero-shot prompting may not be sufficient, and you may need to provide examples (one-shot or few-shot prompting) to guide the model.
Here's an example of a zero-shot prompt:
Classify the sentiment of the following movie review as positive, neutral, or negative:
"Her is a disturbing study revealing the direction humanity is headed if AI is allowed to keep evolving unchecked. I wish there were more movies like this masterpiece."
In this case, the model is able to correctly classify the sentiment as positive based on the prompt alone.
One-Shot and Few-Shot Prompting
One-Shot and Few-Shot Prompting
One-shot and few-shot prompting are techniques that involve providing the language model with examples to guide its output. These techniques are useful when you want the model to produce responses in a specific format or style.
One-Shot Prompting:
- Providing the model with a single example of the desired output.
- The model will try to mimic the structure and content of the example.
Few-Shot Prompting:
- Providing the model with two or more examples of the desired output.
- This gives the model more context to understand the expected format and content.
- The number of examples needed depends on the complexity of the task and the quality of the examples.
- As a general rule, 3-5 examples are recommended for effective few-shot prompting.
The key benefit of one-shot and few-shot prompting is that it allows you to guide the model's output to match your specific requirements, rather than relying on the model to infer the desired format. This is particularly useful for tasks like parsing structured data, generating content in a specific style, or completing complex multi-step workflows.
System Message, Contextual, and Role Prompting
System Message, Contextual, and Role Prompting
The gist of these prompting techniques is to get the language model to act as a specific role or persona. This helps the model generate responses that are consistent with the assigned role and its associated knowledge and behavior.
System Message Prompting
System message prompting sets the overall context and purpose for the language model. It defines the big picture of what the model should be doing, like translating a language, classifying a review, etc.
Contextual Prompting
Contextual prompting provides specific details or background information relevant to the current conversation or task. It helps the model understand the nuances of what's being asked and tailor the response accordingly.
Role Prompting
Role prompting assigns a specific character or identity for the language model to adopt. This helps the model generate responses that are consistent with the assigned role and its associated knowledge and behavior.
For example, the document provides an example of role prompting where the prompt instructs the model to "act as a travel guide". The model then generates suggestions for places to visit in Amsterdam, tailored to the travel guide persona.
Step-Back Prompting
Step-Back Prompting
Step-back prompting asks the model to first consider a general question related to the specific task at hand, and then feeds the answer to that general question into a subsequent prompt for the specific task.
This approach allows the LLM to activate relevant background knowledge and reasoning processes before attempting to solve the specific problem. By considering the broader and underlying principles, LLMs can generate more accurate and insightful responses. It encourages the LLMs to think critically and apply their knowledge in new and creative ways, changing the final prompt for the task by utilizing more knowledge in the LLM's parameters than would otherwise come into play when the LLM is prompted directly.
For example, instead of directly prompting the model to "Write a one paragraph storyline for a new level of a first-person shooter video game that is challenging and engaging", the step-back approach would first ask the model "Based on popular first-person shooter action games, what are five fictional key settings that contribute to a challenging and engaging level storyline in a first-person shooter video game?". The model's response to this broader question is then used to inform the specific prompt for the storyline.
This technique helps the model activate relevant background knowledge and reasoning processes, leading to more accurate and creative outputs for the final task.
Chain of Thought
Chain of Thought
Chain of thought is a powerful prompting technique that allows large language models (LLMs) to explore and explain their reasoning step-by-step. This approach encourages the model to activate relevant background knowledge and apply critical thinking to generate more accurate and insightful responses.
The key benefits of chain of thought prompting include:
-
Improved Accuracy: By walking through the reasoning process, the model is less likely to make mistakes or provide generic, surface-level responses.
-
Enhanced Transparency: The step-by-step explanation makes the model's thought process more transparent, allowing users to better understand the logic behind the output.
-
Versatility: Chain of thought can be applied to a wide range of tasks, from problem-solving and analysis to creative writing and task planning.
To use chain of thought, you simply need to append your prompt with a request for the model to "think step-by-step" or "show your work." For example:
Prompt: When my brother was 2 years old, I was double his age. Now I'm 40. How old is my brother?
Think step-by-step and show your work.
The model will then provide a detailed, multi-step explanation of how it arrived at the final answer. This approach can be particularly powerful when combined with other prompting techniques, such as few-shot or self-consistency.
Overall, chain of thought is a versatile and impactful prompting strategy that can significantly improve the quality and transparency of LLM outputs across a wide range of applications.
Self-Consistency
Self-Consistency
LLM's ability to reason is often seen as a limitation that cannot be overcome solely by increasing model size. The model can be prompted to generate reasoning steps like a human solving a problem. However, Chain of Thought uses a simple greedy decoding strategy, limiting its effectiveness.
Self-consistency combines sampling and majority voting to generate diverse reasoning paths and select the most consistent answer. This improves the accuracy and coherence of responses generated by large language models.
For example, to classify emails as important or not important:
- Identify the purpose of the email - to inform the recipient of a bug in the contact form.
- Assess the potential impact of the bug.
- Consider the credibility of the sender.
- Conclusion: The email should be classified as important. The recipient should take immediate action to fix the bug and protect their website.
By running the prompt multiple times and having the model vote on the best response, self-consistency helps ensure the most consistent and accurate output.
Tree of Thought
Tree of Thought
Tree of Thought allows LLMs to explore multiple different reasoning paths simultaneously rather than just following a single linear chain of thought. Here's how it works:
- The input is provided to the model.
- At each step, the model tests different outputs that lead to the next set of outputs, ultimately leading to the final output.
- This uses a combination of self-consistency and chain of thought techniques.
You can visualize it as a tree-like structure, where the input is the root, and the model explores different branches of reasoning before arriving at the final output.
Doing this strictly between a user and the prompt box is not really viable, as it involves too much complexity. Instead, you would need to implement Tree of Thought using code or a framework to handle the multiple reasoning paths and decision-making.
This approach makes Tree of Thought particularly well-suited for complex tasks that require exploration, as it allows the model to consider various possibilities and select the most consistent and accurate solution.
Reason and Act (React)
Reason and Act (React)
React prompting is a paradigm for enabling large language models to solve complex tasks using natural language reasoning combined with external tools, such as search, code interpreters, and other functionalities.
React mimics how humans operate in the real world. It can be thought of as an agent-based system, where the core large language model is combined with various tools to accomplish tasks. The process involves a thought-action loop:
- The language model first reasons about the problem and generates a plan of action.
- It then performs the actions in the plan and observes the results.
Many of the latest frontier models have this capability built-in, allowing you to toggle different tools on and off, and leveraging chain-of-thought reasoning. However, you can also implement React prompting with older or smaller models by using frameworks like LangChain or Crew AI.
Here's an example of using React prompting in Python:
from langchain.agents import load_tools, initialize_agent, AgentType
# Load the necessary tools
tools = load_tools(["serpapi"])
# Initialize the agent
agent = initialize_agent(tools, llm=llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)
# Prompt the agent to solve a task
result = agent.run("How many kids did the band members of Metallica have?")
print(result)
In this example, the agent is given access to a search tool (SerpAPI) and is prompted to find the number of kids the Metallica band members have. The agent uses the thought-action loop to plan its approach, execute searches, and arrive at the final answer.
React prompting is particularly well-suited for complex tasks that require exploration and the use of external tools or knowledge sources. By combining reasoning and acting, it allows language models to tackle real-world problems more effectively.
Automatic Prompt Engineering
Automatic Prompt Engineering
Automatic prompt engineering is a powerful technique that allows you to have AI write prompts for you. This can be particularly useful when you need to generate detailed prompts for complex tasks, or when you want to leverage advanced prompting techniques like chain of thought or self-consistency.
Here's how the process works:
-
Provide a high-level description: Start by writing a brief, sentence-level description of the task or output you want the language model to generate.
-
Ask the model to write a detailed prompt: Take your high-level description and ask the language model to write a detailed prompt that incorporates prompting techniques like chain of thought, self-consistency, or role-playing.
-
Use the generated prompt: Take the prompt generated by the language model and use it as the input for your final task. The model will then use the detailed prompt to generate the desired output.
This approach has several benefits:
-
Saves time and effort: Manually crafting detailed prompts can be time-consuming, especially when incorporating advanced techniques. Automatic prompt engineering allows you to quickly generate high-quality prompts.
-
Leverages the model's capabilities: Language models are adept at understanding prompting techniques and generating prompts that effectively guide the model's output. By letting the model write the prompt, you can tap into its inherent understanding of prompt engineering.
-
Promotes experimentation: Automating the prompt generation process makes it easier to try out different prompting techniques and see which ones work best for your use case.
To illustrate this process, consider the example of asking a language model to write code based on a high-level description. Instead of providing a detailed requirements document, you could simply give the model a brief description and ask it to write a prompt that includes a step-by-step chain of thought and self-consistency checks. The model would then generate a comprehensive prompt that you could use to obtain the desired code output.
By embracing automatic prompt engineering, you can unlock the full potential of large language models and streamline your workflow, allowing you to focus on the core problem at hand rather than the intricacies of prompt design.
When to Use Code vs. Natural Language
When to Use Code vs. Natural Language
There are times when it's more effective to have the model write and execute code rather than just provide a natural language solution. This approach can be particularly useful for tasks where the model's ability to write accurate code exceeds its ability to provide the correct natural language answer.
For example, when asked "How many Rs are in the word 'strawberry'?", many language models may get the answer wrong. However, by prompting the model to write code that counts the number of Rs in a given word, you can ensure the correct answer is obtained, as long as the code is implemented correctly.
The key is to explicitly instruct the model to write and execute code for the solution, rather than just asking for the natural language answer. This prompting technique can be summarized as follows:
- Prompt the model to write code: Provide a prompt that directs the model to write code to solve the problem, such as "Write code to count the number of Rs in the word 'strawberry'."
- Prompt the model to execute the code: After the model has written the code, prompt it to execute the code and provide the result.
- Verify the output: Review the output to ensure the code executed correctly and the solution is accurate.
By using this approach, you can leverage the model's code-writing capabilities to get reliable, accurate solutions, even for tasks that the model may struggle with when asked for a natural language response.
Prompt Engineering Best Practices
Prompt Engineering Best Practices
-
Provide Examples: Use zero-shot, one-shot, or few-shot prompting techniques to provide the model with examples, especially when you're trying to get consistent outputs.
-
Design with Simplicity: Start with simple prompting and only add more instructions or nuanced instructions when absolutely necessary. Keep your prompts as simple as possible.
-
Be Specific About the Output: Clearly specify the desired format, style, or content of the response. Don't leave it up to the model to guess what you're expecting.
-
Use Instructions Over Constraints: Provide explicit instructions on the desired output rather than setting limitations or boundaries.
-
Control the Max Token Length: Optimize for latency and cost by controlling the maximum number of tokens the model should output.
-
Use Variables: Incorporate variables in your prompts to make them more flexible and reusable.
-
Stay Up-to-Date: Keep track of the latest capabilities and limitations of the language models you're using to ensure you're formatting your prompts effectively.
FAQ
FAQ

