Skip to content

Summary of Prompt Techniques

This section introduces basic prompt engineering techniques, primarily referencing Andrew Ng’s prompt tutorial, as a precursor to prompt injection topics. This article does not yet cover prompt injection; please refer to subsequent articles for that.

When interacting with a large language model, clear and specific instructions are the most fundamental requirement for prompts. Clarity does not mean prompts must be short; on the contrary, more detailed and longer prompts that provide richer context lead to more accurate model responses. To implement this principle, here are four techniques:

  1. Use Delimiters

Prompts often contain both instructions and the data to be processed, which can confuse the model. Using delimiters to separate instructions from data helps the model recognize and process them. For example:

Please summarize the text delimited by triple backticks, capturing its core point in one sentence:
```
Here is the text that needs to be summarized.
```

In addition to backticks, many other symbols can serve as delimiters as long as they can make the distinction: triple single quotes (''' ), triple double quotes (""" ), triple dashes (---), angle brackets (<>), custom XML-like tags (<test>), etc.

  1. Use Structured Output

If the model’s output needs to be processed further, for example by other scripts or programs, a specific format is often required. Even when the goal is simply readability, specifying the output format can make results clear at a glance. Common formats include JSON, Markdown, HTML, etc. For example:

Please generate 3 fictional book titles, authors, and genres. Provide the output in JSON format with the following keys: bookid, booktitle, author, type.
  1. Check Whether Conditions Are Satisfied

If a task can only be completed under certain conditions, have the AI perform a logical check first to avoid it fabricating plausible-sounding but incorrect answers. For instance, the following instruction obviously lacks sufficient content to be restructured into a step-by-step format:

You will receive text wrapped in triple double quotes. Please rewrite it in the following format:
Step 1: ...
Step 2: ...
Step 3: ...
"""
I love learning
"""

But we can add a logical check:

You will receive text wrapped in triple double quotes. If the text contains a series of steps, please rewrite it in the following format:
Step 1: ...
Step 2: ...
Step 3: ...
If the text does not contain relevant steps, answer "No steps found."
"""
I love learning
"""
  1. Few-Shot Prompting

You can provide a few examples in the prompt for the model to reference. This method helps adjust the tone and style of the AI’s output, as well as handle complex format conversions. For example, suppose we have the following log entry:

2025-08-08, Platform A suffered an XSS attack; upon investigation, the attack was not successful.

I want to convert it into the format 20250808:A:XSS:FAIL. For a large number of subsequent logs that need conversion, I just need to give one example:

Input: 2025-08-08, Platform A suffered an XSS attack; upon investigation, the attack was not successful.
Output: 20250808:A:XSS:FAIL
Please output the content wrapped in triple double quotes below according to the example above:
"""
2025-06-06, Platform B suffered an XXE attack; upon investigation, the attack was not successful.
2025-07-11, Platform C suffered an SSRF attack; upon investigation, the attack was not successful.
2025-08-22, Platform D suffered an RCE attack; upon investigation, the attack was successful.
"""

These four principles are also frequently applied in daily life. Delimiters are like highlighting key points when studying, telling you where to find materials at work, or taking meeting minutes. Specifying the output format is like providing a template when filling out a form. Checking conditions is like pre-flight checks, or being notified when an item is out of stock before delivery. Few-shot prompting is like referring to previous excellent examples. Therefore, many ideas and essences of prompting are closely related to everyday life and work.

The generation of content by large models is still essentially based on predicting the next word probabilistically. If the problem is too complex, the model’s answers may not be especially accurate and may even produce hallucinations. Or, if you want the model to follow your reasoning for a task, you can guide the model to reason within the prompt. For example, if I want the model to help me write an article on how to lose weight, it would generate content based on predictions, which might be relatively generic. In that case, we can guide its reasoning by adding steps and making it generate content according to our logic, like:

Help me write an article on how to lose weight by following these steps:
1. First, research the proportion of obese people worldwide, including the male and female ratios.
2. Then explain why weight loss is necessary.
3. Next, discuss how to lose weight, covering aspects such as lifestyle habits and exercise suggestions.
4. Finally, end with a motivational message encouraging everyone to work hard and succeed in their weight loss journey.

Another technique is to have the model think before giving a conclusion, similar to a teacher solving the problems themselves before grading a student’s assignment. This is necessary because current large models exhibit a compliance tendency: if you ask the model “I think X is the case, what do you think?”, the model often answers “You are right, X is indeed the case,” aligning with your view and overlooking subtle errors. The solution is to first let it generate a correct answer on its own, then compare that correct answer with the target.

For example, consider the following prompt, a seemingly simple math problem that many models answer incorrectly:

Bought 2 apples at 10 yuan each and 3 pears at 5 yuan each. Total price = 20 + 15 = 45 yuan. Help me check whether this student's calculation is correct.

The correct answer is clearly 35, but the model will say that 45 is completely correct. To address this, we can use the technique of having the model think before concluding:

Bought 2 apples at 10 yuan each and 3 pears at 5 yuan each. Total price = 20 + 15 = 45 yuan. Help me check whether this student's calculation is correct.
Please do not look at the student's answer yet. Instead, do the following:
1. First, answer the question yourself based on your own understanding and description.
2. Output your detailed calculation steps.
3. Compare your result with the student's result.
4. Finally, judge whether the student's calculation process is correct.

There is no perfect prompt; there are only continually optimized prompts. The optimization process must form a closed loop: start with an initial prompt, analyze whether the output meets expectations, then modify the instructions for refinement, and repeat this cycle until the desired result is achieved.

image-20260104163114796.png

This is easy to understand, so we won’t provide further examples here. During iterative optimization, many aspects can be considered, such as word count, target audience, output format, logical modifications, new requirements, adding guiding steps, and so on.

Persona setting is equivalent to assigning a role. At the very beginning of the prompt, you can specify an identity, such as “You are a XXX expert.” The benefit of doing so is to narrow the probability space of the model’s answers, i.e., to draw a circle around the probable range of the model’s output, making the answers more professional and aligned with your requirements. For example, if I say:

How to make braised pork belly

The model’s answer will be relatively broad, covering steps such as selecting ingredients and cooking methods. But if I give it a persona:

You are a chef at a five-star hotel. Please tell me how to make braised pork belly.

Its answer will be more professional, because when predicting subsequent words, it will stay within the scope of a five-star chef. For instance, it will add more detailed cooking techniques, how to plate the dish, and how to achieve a perfect balance of color, aroma, and taste.

Another function of persona setting is to specify the tone, style, and focus of the response. For example, if I directly ask “What is inflation?”, the model might give a general, generic answer. But if I assign a persona, such as:

You are a kindergarten teacher. Please tell me what inflation is.

It will answer through stories and games. Of course, we could also specify output requirements like making it easy to understand, using a gentle tone, and providing examples that children can grasp, but simply assigning the role of a kindergarten teacher can encompass these requirements.

Boundary setting acts as a negative constraint, similar to a negative prompt—setting red lines for the model’s response. In many everyday queries, the model tends to be overly friendly and verbose, saying unnecessary things. For example, “Please help me write a resignation letter” it may start by thanking the leader and the company, and end with blessings. If we don’t want these polite expressions, we can add a negative constraint, like:

Help me write a resignation letter, but avoid using pleasantries and formalities.

This is a tone constraint. We can also constrain content, for instance:

Recommend some places to visit in Beijing, but exclude those that require paid admission and those that are over-crowded online-famous check-in spots.

Or constrain the word count:

Summarize 3 core takeaways from this article, with each takeaway not exceeding 30 words.

This technique applies when we want to accomplish a task but are unsure what information is needed. In such cases, we can first let the AI ask us questions, for example:

I want you to help me create an annual fitness plan. To make the plan best suited for me, before you start writing, ask me 5 key questions you need to know.

The AI will then ask questions such as what your goals are, your physical condition, how much time you have per week, preferred types of exercise, dietary habits, etc., so as to better tailor the plan.

The general approach is to first specify a persona to narrow the probabilistic vocabulary space, then clearly state the requirements, providing as much detailed context as possible. Next, specify output requirements, such as format constraints and negative prompts, and finally continuously iterate and refine based on the responses.

Finally, let’s look at a holistic prompt example centered around planning a 3-day family trip to Xi’an:

[Role Setting]
You are a senior private travel consultant with 10 years of experience, skilled in uncovering a city's in-depth cultural and historical sites, and able to balance the itinerary according to the needs of different family members.
[Detailed Context]
I plan to bring my whole family (4 people: me, my spouse, a 6-year-old child, and a 65-year-old elder) on a 3-day trip to Xi'an next week. We will stay near the Bell Tower. We enjoy historical architecture and authentic local cuisine, but we don't want a rushed itinerary, as we need to accommodate the elderly and the child's stamina.
[Output Requirements]
Please generate a detailed itinerary in a Markdown table, including: time slot, attraction/activity, transportation, and tips to avoid pitfalls.
[Negative Requirements]
Do not recommend tourist-trap "food streets" that only attract out-of-towners (such as the main streets of the Muslim Quarter); instead, recommend places where locals truly go.
Do not schedule departure times before 7:00 a.m.
Do not include internet-famous check-in spots that require long queues.
Special note: Please reserve 2 hours of afternoon rest time each day.
[Iterative Guidance]
Please provide an initial plan first; I will then ask you to adjust hotel recommendations based on my budget.

The above concludes the content on prompt techniques. Thank you for reading.