I. What Is Prompt Engineering?
Prompt Engineering refers to a set of methods and techniques for designing and organizing the instructions, context, examples, and constraints fed into a large language model so that it can complete a specified task more accurately and reliably.
A prompt is the input the model receives, while Prompt Engineering focuses on how to construct that input.
For example, consider the same task:
Simple prompt:
Introduce Redis.
Designed prompt:
You are a senior backend architect.
Please explain how Redis works at its core to engineers with Java development experience.
Requirements:
1. Introduce Redis data structures and their underlying implementations.
2. Focus on analyzing memory management, persistence, and high-availability mechanisms.
3. Explain the differences between cache penetration, cache breakdown, and cache avalanche.
4. Provide usage recommendations suitable for production environments.
5. Use a technical documentation style and avoid vague generalities.
The difference lies not in a change to the model, but in the greater specificity of the information provided in the input.
The second prompt defines:
- Role: senior backend architect.
- Goal: explain the core principles of Redis.
- Audience: engineers with Java development experience.
- Scope: data structures, memory management, persistence, high availability.
- Focus: caching issues and production practices.
- Output style: technical documentation.
This gives the model clearer task boundaries and generation direction.
1.1 Core Goals of Prompt Engineering
Prompt engineering primarily addresses the following problems:
| Problem | Role of prompt engineering |
|---|---|
| Inaccurate task understanding | Clarify task goals and execution requirements |
| Incomplete output | Define content scope and required fields |
| Inconsistent output format | Specify structure, schema, and format constraints |
| Inconsistent response style | Define role, audience, and expression style |
| Complex tasks are prone to omissions | Break down steps and clarify task dependencies |
| The model easily deviates from the topic | Provide relevant context and boundaries |
| Results are difficult to process automatically | Constrain output to JSON, XML, or another structured format |
Prompt Engineering does not give the model new knowledge; rather, it makes more effective use of the model's existing capabilities and the information provided in the current context.
1.2 Prompt Engineering vs. Model Training
Prompt engineering and model training can both affect model performance, but they operate at different levels.
| Dimension | Prompt Engineering | Model training |
|---|---|---|
| Target | Input content | Model parameters |
| Whether parameters are modified | No | Yes |
| Data source | Current prompt and context | Training data |
| Scope of effect | Current request or session | The trained model |
| Adjustment cost | Usually lower | Usually higher |
| Primary purpose | Guide model behavior | Learn or adjust model capabilities |
Prompt engineering typically does not require retraining the model or updating model weights.
It influences what the model generates next by changing the conditional information the model receives.
II. How Prompts Work Under the Hood
To understand Prompt Engineering, you first need to understand how large language models process input.
Taking an autoregressive language model as an example, the model predicts the next token based on the existing context.
Its basic probabilistic form is:
Where:
- x_t: the token at the current position.
- x_{<t}: the tokens before the current position.
- P(x_t|x_{<t}): the probability of predicting the current token given the existing context.
The model does not directly understand the true intent behind a natural-language instruction; instead, it uses parameters learned through training and the context representation to compute the conditional probability distribution of subsequent tokens.
The role of a prompt is to change the conditional context that the model relies on when generating.
2.1 How Does a Prompt Affect Model Output?
Suppose the user inputs:
Explain database indexes.
The model must infer from limited context:
- Which database should be discussed?
- What technical level is the audience at?
- Should it explain concepts or implementation principles?
- Are code examples needed?
- How long should the output be?
- Should performance issues be discussed?
None of this information is explicitly specified, so the model can only infer it from the context and patterns formed during training.
If we add:
Please use MySQL InnoDB as an example, target mid-to-senior backend engineers,
and focus on explaining the B+Tree index structure, page splits, clustered indexes,
back-to-table lookups, and covering indexes, and analyze common index invalidation scenarios.
The model receives more task constraints, and the range of generated results narrows accordingly.
2.2 Prompt and Token Sequences
After receiving a Prompt, the model must encode it through a Tokenizer.
The processing can be abstracted as:
flowchart TD
A["Write a Prompt"]
B["Organize instructions and context"]
C["Tokenizer encoding"]
D["Generate Token ID sequence"]
E["Embedding mapping"]
F["Transformer computation"]
G["Generate next Token"]
H["Continue autoregressive generation"]
I["Get final output"]
A --> B
B --> C
C --> D
D --> E
E --> F
F --> G
G --> H
H --> G
H --> I
style A fill:#DCEFE5,stroke:#4F8B70,color:#24563F
style B fill:#EAF3EC,stroke:#83A995,color:#315D49
style C fill:#F5F8F4,stroke:#B5CBBB,color:#40594A
style D fill:#F5F8F4,stroke:#B5CBBB,color:#40594A
style E fill:#EAF3EC,stroke:#83A995,color:#315D49
style F fill:#DCEFE5,stroke:#4F8B70,color:#24563F
style G fill:#EAF3EC,stroke:#83A995,color:#315D49
style H fill:#F5F8F4,stroke:#B5CBBB,color:#40594A
style I fill:#DCEFE5,stroke:#4F8B70,color:#24563F
The design of the Prompt ultimately affects the input Token sequence and its contextual relationships.
Therefore, a prompt should not only focus on natural-language expression but also consider:
- Number of Tokens.
- Priority among instructions.
- Relevance of contextual information.
- How the input content is organized.
- The context window supported by the model.
2.3 Prompt Is Not Program Code
A Prompt can contain program-like structure, for example:
Role:
Task:
Input:
Constraints:
Output format:
But these labels are usually just text markers that help the model identify different information regions; they do not mean the model is actually executing some programming language.
A Prompt can also be organized through special Tokens, message roles, and structured content supported by the model.
The specific format depends on the model and its API protocol.
III. Basic Structure of a High-Quality Prompt
A complete prompt can usually be broken down into multiple logical parts.
Common structures include:
- Role: Role definition.
- Task: Task objective.
- Context: Background context.
- Input: Input data.
- Constraints: Execution constraints.
- Examples: Examples.
- Output Format: Output format.
3.1 Role: Role Definition
Role defines what identity, professional perspective, or working style the model should adopt.
For example:
You are a Java backend architect with more than ten years of experience,
familiar with JVM, Spring, MySQL, Redis, and distributed systems.
A role definition can help the model choose a knowledge scope and expression style suited to the task.
However, note:
Role setting does not truly change the model's professional capabilities.
Telling the model "you are a senior architect" does not give the model new architectural knowledge.
A role definition is closer to a constraint on behavior and expression direction.
3.2 Task: Task Objective
Task specifies exactly what the model is required to accomplish.
For example:
Analyze the following Java code and identify possible concurrency safety issues.
A good task definition should clearly specify:
- The action to perform.
- The object to analyze.
- The expected result.
- The scope of analysis when necessary.
Avoid overly vague expressions such as:
Please take a look at this code for me.
It can be changed to:
Check the following Java code for thread safety issues,
focusing on shared variables, lock contention, visibility, and atomicity,
and provide a runnable fix.
3.3 Context: Background Context
Context provides the background information needed to complete the task.
For example:
Project background:
This is an order system based on Spring Boot.
Technology stack:
- Java 21
- Spring Boot 3
- MySQL 8
- Redis
- RabbitMQ
Business scenario:
The order creation API needs to support high-concurrency requests,
and ensure that the same business request does not create duplicate orders.
Context can help the model reduce guesswork about the business background.
But more context is not always better.
Irrelevant information may increase Token consumption and distract the model from key content.
3.4 Input: Input Data
Input is the specific object that the model needs to process.
For example:
SQL to analyze:
SELECT *
FROM orders
WHERE user_id = 10001
AND status = 1
ORDER BY created_at DESC;
For tasks such as code analysis, data extraction, text classification, and document question answering, the boundaries of the input content should be clearly marked.
3.5 Constraints
Constraints are used to limit the execution scope of the model.
For example:
Constraints:
1. Do not modify the existing business logic.
2. Do not introduce new third-party dependencies.
3. Prefer the Java standard library.
4. Explain the reason for each modification.
5. If information is insufficient, clearly point out the missing information.
Constraints can reduce the room for the model to improvise and make the output better meet actual business requirements.
3.6 Examples
Examples are used to show the model the expected input-output pattern.
For example:
Example input:
Order status 1 indicates pending payment.
Example output:
{
"status": 1,
"status_name": "待支付"
}
For tasks that require a fixed format, fixed classification criteria, or a fixed style, examples are usually very valuable.
3.7 Output Format
Output format is used to constrain how the model organizes the final result.
For example:
Please output strictly according to the following structure:
I. Problem Description
II. Problem Cause
III. Scope of Impact
IV. Solution
V. Fixed Code
VI. Notes
If the result needs to be automatically processed by a program, structured constraints such as JSON Schema can be used further.
3.8 Complete Prompt Structure
flowchart TD
A["Prompt"]
B["Role"]
C["Task"]
D["Context"]
E["Input"]
F["Constraints"]
G["Examples"]
H["Output Format"]
I["Model executes task"]
A --> B
A --> C
A --> D
A --> E
A --> F
A --> G
A --> H
B --> I
C --> I
D --> I
E --> I
F --> I
G --> I
H --> I
style A fill:#DCEFE5,stroke:#4F8B70,color:#24563F
style B fill:#EAF3EC,stroke:#83A995,color:#315D49
style C fill:#EAF3EC,stroke:#83A995,color:#315D49
style D fill:#F5F8F4,stroke:#B5CBBB,color:#40594A
style E fill:#F5F8F4,stroke:#B5CBBB,color:#40594A
style F fill:#F7F4E8,stroke:#C7B77A,color:#675A2D
style G fill:#EAF3EC,stroke:#83A995,color:#315D49
style H fill:#EAF3EC,stroke:#83A995,color:#315D49
style I fill:#DCEFE5,stroke:#4F8B70,color:#24563F
Not all tasks need to include all of the above parts.
Simple question answering may only need Task; complex business tasks may require the complete structure.
4. Core Techniques of Prompt Engineering
Prompt engineering has developed a variety of design methods.
These methods are not mutually exclusive and can be combined according to task complexity.
4.1 Zero-shot Prompting
Zero-shot Prompting means directly asking the model to complete a task without providing specific examples.
For example:
Determine the sentiment of the following user comment.
Comment:
This product runs very fast, but the after-sales service is too poor.
Please output:
Positive, negative, or neutral.
The model completes the classification based on pre-trained knowledge and the current instruction.
Features:
- No examples need to be prepared.
- The prompt structure is simple.
- Suitable for common tasks.
- Stability may be insufficient for complex rules and special formats.
Zero-shot is the default starting point for many large-model applications.
4.2 One-shot Prompting
One-shot Prompting means providing one example for the model to refer to when completing a task.
For example:
Task: Determine the sentiment of user comments.
Example:
Input: This phone takes very good photos.
Output: Positive
Now process:
Input: This product runs very fast, but the after-sales service is too poor.
Output:
The example shows the model the task definition and output form.
One-shot does not mean the model was trained only once; rather, the current prompt contains one example.
4.3 Few-shot Prompting
Few-shot Prompting provides multiple examples in the prompt so that the model can identify the task pattern from the examples.
For example:
Task: Determine the sentiment of a comment.
Example 1:
Comment: The product performance is excellent.
Result: Positive
Example 2:
Comment: The quality is terrible, not worth buying.
Result: Negative
Example 3:
Comment: The product has been received.
Result: Neutral
Comment to classify:
Comment: The delivery was fast, but the product packaging was damaged.
Result:
The model can refer to multiple input-output samples to learn the classification boundaries and expression format of the current task.
Few-shot is especially suitable for:
- Business-specific classification.
- Data extraction in a fixed format.
- Style transfer.
- Domain terminology conversion.
- Custom label systems.
But more examples are not always better.
Examples take up context window space, and may also introduce label bias or incorrect patterns.
4.4 Comparison of Zero-shot, One-shot, and Few-shot
| Method | Number of Examples | Main Characteristics | Applicable Scenarios |
|---|---|---|---|
| Zero-shot | 0 | Describe the task directly | General tasks |
| One-shot | 1 | Provide a single reference sample | Simple format constraints |
| Few-shot | Multiple | Show multiple task samples | Special rules and domain tasks |
4.5 Role Prompting
Role Prompting adjusts the professional perspective the model adopts when answering questions by specifying a role.
For example:
You are a database kernel engineer.
Please explain the Buffer Pool of MySQL InnoDB,
focusing on page management, the LRU mechanism, and flushing of dirty pages.
Role prompting is suitable for tasks that require a clear professional perspective.
However, a role setting should not be treated as a source of facts, nor can it replace the necessary technical materials.
4.6 Instruction Prompting
Instruction Prompting tells the model what operation to perform through clear, specific instructions.
For example:
Please condense the following technical document into a summary of no more than 300 words.
Must retain:
1. Core technical principles.
2. Key configuration parameters.
3. Performance impact.
4. Main risks.
Do not add conclusions that are not mentioned in the original text.
Instructions should be as specific as possible, avoiding multiple requirements that conflict with one another.
4.7 Contextual Prompting
Contextual Prompting means providing background material relevant to the current task in addition to the task instructions.
For example:
Background information:
Our system uses Redis to cache user information.
The cache is valid for 30 minutes.
When user data is updated, the cache is deleted in sync.
Task:
Analyze the problems this caching strategy may encounter under high concurrency.
Context can come from:
- User input.
- Database query results.
- External APIs.
- RAG-retrieved documents.
- Conversation history.
- File contents.
Contextual Prompting is an important foundation for knowledge-base question answering and enterprise-grade AI applications.
V. Prompting Techniques for Complex Tasks
For simple question answering, clear instructions are usually already sufficient.
But when facing complex reasoning, code analysis, multi-step tasks, and structured decisions, a single simple instruction may struggle to produce stable results.
5.1 Chain-of-Thought Prompting
Chain-of-Thought (CoT) is a technical direction that prompts the model to reason step by step.
Its core idea is that when handling complex problems, the model does not just output the final answer directly, but adopts a staged reasoning process.
For example:
Please analyze the following problem.
A service handles 1.2 million requests per day.
What is the average number of requests per second?
Please list the calculation formula and give the result.
The model needs to identify:
- The number of seconds in a day.
- The total number of requests.
- The average request rate.
Calculation process:
CoT is commonly used in:
- Mathematical problems.
- Complex logic analysis.
- Multi-condition judgment.
- Locating code issues.
- Multi-stage task planning.
Two concepts need to be distinguished:
Asking the model to provide an explanation does not mean obtaining the model's true, complete internal reasoning process.
The reasoning explanations output by the model may contain omissions, errors, or explanations constructed after the fact, and cannot be taken directly as the internal computation trajectory.
For production systems, you can require the model to output verifiable intermediate results, calculation formulas, cited evidence, or structured steps, rather than relying on a plausible-looking passage of reasoning text.
5.2 Least-to-Most Prompting
Least-to-Most Prompting breaks a complex problem down into multiple sub-problems, from simple to complex.
For example, analyzing a distributed transaction system:
Please complete the analysis in the following order:
Step 1: Explain the basic problems of distributed transactions.
Step 2: Analyze the difference between local transactions and remote calls.
Step 3: Introduce 2PC and TCC.
Step 4: Compare Saga with reliable messaging eventual consistency.
Step 5: Give selection recommendations for different business scenarios.
This approach is suitable for:
- Complex technical research.
- Multi-stage analysis.
- Option comparison.
- Long document processing.
- Tasks that require explicit dependency relationships.
Note that after splitting a task, you also need to check whether information is fully passed between subtasks.
5.3 Self-Consistency
Self-Consistency is a technique that improves result reliability by generating multiple candidate reasoning results and then comparing their consistency.
Its basic idea is:
- Generate multiple candidate paths for the same question.
- Extract candidate answers.
- Compare results across different paths.
- Select an answer by voting or another aggregation method.
Illustrative flow:
flowchart TD
A["Input a complex question"]
B["Generate multiple candidate results"]
C["Candidate result 1"]
D["Candidate result 2"]
E["Candidate result 3"]
F["Extract each candidate answer"]
G["Consistency comparison"]
H["Aggregate and select"]
I["Output final result"]
A --> B
B --> C
B --> D
B --> E
C --> F
D --> F
E --> F
F --> G
G --> H
H --> I
style A fill:#DCEFE5,stroke:#4F8B70,color:#24563F
style B fill:#EAF3EC,stroke:#83A995,color:#315D49
style C fill:#F5F8F4,stroke:#B5CBBB,color:#40594A
style D fill:#F5F8F4,stroke:#B5CBBB,color:#40594A
style E fill:#F5F8F4,stroke:#B5CBBB,color:#40594A
style F fill:#EAF3EC,stroke:#83A995,color:#315D49
style G fill:#F7F4E8,stroke:#C7B77A,color:#675A2D
style H fill:#EAF3EC,stroke:#83A995,color:#315D49
style I fill:#DCEFE5,stroke:#4F8B70,color:#24563F
Self-Consistency can reduce accidental errors caused by certain sampling paths, but it does not guarantee that the answer is correct.
If multiple paths share the same erroneous premise, the final result may still be wrong.
5.4 ReAct: Reasoning + Acting
ReAct combines reasoning with action.
The model not only analyzes the problem but can also choose to call external tools as needed.
For example:
User: Check today's weather in Beijing and provide travel advice.
Model:
Analyze the need to obtain real-time weather data.
Tool call:
get_weather(city="Beijing")
Tool return:
Temperature 12°C, light rain, wind force level 3.
Model:
Generate travel advice based on the weather data.
The core of ReAct is not simply generating text, but forming a task loop that combines reasoning, actions, and observations.
flowchart TD
A["Receive user task"]
B["Analyze current state"]
C{"Is an external tool needed?"}
D["Generate tool call request"]
E["Validate in application and execute tool"]
F["Obtain tool result"]
G["Update task context"]
H{"Is the task complete?"}
I["Generate final answer"]
A --> B
B --> C
C -->|"Yes"| D
D --> E
E --> F
F --> G
G --> H
H -->|"No"| B
C -->|"No"| I
H -->|"Yes"| I
style A fill:#DCEFE5,stroke:#4F8B70,color:#24563F
style B fill:#EAF3EC,stroke:#83A995,color:#315D49
style C fill:#F7F4E8,stroke:#C7B77A,color:#675A2D
style D fill:#F5F8F4,stroke:#B5CBBB,color:#40594A
style E fill:#F7F4E8,stroke:#C7B77A,color:#675A2D
style F fill:#EAF3EC,stroke:#83A995,color:#315D49
style G fill:#F5F8F4,stroke:#B5CBBB,color:#40594A
style H fill:#F7F4E8,stroke:#C7B77A,color:#675A2D
style I fill:#DCEFE5,stroke:#4F8B70,color:#24563F
ReAct is a common design approach in Agent systems.
However, actual tool execution is handled by the application; a tool call request generated by the model cannot be directly treated as an external operation that has already been completed.
6. Structured Output and Format Constraints
In real business scenarios, model output is not necessarily only for display to users.
In many scenarios, the results need to be handed off to a program for further processing.
For example:
- Extract order information.
- Identify user intent.
- Generate API parameters.
- Output classification labels.
- Generate database query conditions.
- Generate business decision recommendations.
At this point, the stability of the output format is very important.
6.1 Plain Text Constraints
The simplest approach is to specify the output format directly.
Please analyze the following user feedback.
Only output one of the following three labels:
Positive
Negative
Neutral
Do not output explanations.
This method is simple to implement, but natural-language constraints are not equivalent to strict structural guarantees.
6.2 JSON Output Constraints
For example:
Extract order information from the user input.
Output JSON with the following fields:
- order_id: order number
- product_name: product name
- quantity: purchase quantity
Use null for information that is not extracted.
Do not output a Markdown code block.
Expected result:
{
"order_id": "ORD20261120001",
"product_name": "mechanical keyboard",
"quantity": 2
}
6.3 JSON Schema Constraints
For structured results that need to be consumed by programs, you can use JSON Schema.
For example:
{
"type": "object",
"properties": {
"order_id": {
"type": ["string", "null"]
},
"product_name": {
"type": ["string", "null"]
},
"quantity": {
"type": ["integer", "null"]
}
},
"required": [
"order_id",
"product_name",
"quantity"
],
"additionalProperties": false
}
Schema can define:
- Field names.
- Data types.
- Required fields.
- Allowed values.
- Nested structures.
- Whether additional fields are allowed.
If the model API supports Structured Outputs, you can use the Schema constraint capabilities supported by the server.
Note that requiring “output only JSON” in an ordinary Prompt is not the same as actually enabling structured output constraints at the API layer.
6.4 Correct Format Does Not Equal Business Correctness
Even if the output passes JSON Schema validation, it does not mean the content conforms to the real business.
For example:
{
"order_id": "ORD001",
"product_name": "mechanical keyboard",
"quantity": -10
}
This JSON may satisfy basic data type requirements, but a negative purchase quantity clearly does not conform to business rules.
Therefore, production systems need layered validation:
- Syntax validation: whether it is valid JSON.
- Schema validation: whether fields and types conform to the definition.
- Business validation: whether values, states, and business rules are correct.
- Permission validation: whether the operation is authorized for the current user.
7. Prompt and Context Management
Prompt Engineering is not just about writing a set of instructions; it also involves how to organize the context that the model can use.
For multi-turn conversations, enterprise knowledge bases, and complex Agents, Context Management is especially important.
7.1 Multi-turn Conversation Context
The request itself in most model APIs does not mean that the model automatically saves all historical conversations.
Applications usually need to reorganize the necessary historical messages into the request, or use conversation state capabilities provided by the service provider.
For example:
{
"messages": [
{
"role": "user",
"content": "What is Redis?"
},
{
"role": "assistant",
"content": "Redis is an in-memory data structure store."
},
{
"role": "user",
"content": "Is it suitable for distributed locks?"
}
]
}
The current request includes historical messages so that the model can use this context to understand that “it” refers to Redis.
7.2 Context Window Limits
Model context windows have Token count limits.
Input that is too long may cause:
- The request to exceed the context window.
- Increased input Token costs.
- Higher inference latency.
- Important information being diluted by large amounts of irrelevant content.
- Degraded ability to locate information in long contexts.
Therefore, context needs to be managed.
7.3 Context Compression
Common methods include:
- Delete irrelevant historical messages.
- Keep the most recent conversation.
- Generate summaries of earlier conversations.
- Extract key information that remains valid over the long term.
- Save historical information to external storage.
- Retrieve relevant content based on the current task.
A simple context management process:
flowchart TD
A["Collect conversation history"]
B["Evaluate context length"]
C{"Exceeds budget?"}
D["Keep necessary messages"]
E["Summarize and compress historical content"]
F["Extract key state"]
G["Assemble final context"]
H["Submit model request"]
A --> B
B --> C
C -->|"No"| G
C -->|"Yes"| D
D --> E
E --> F
F --> G
G --> H
style A fill:#DCEFE5,stroke:#4F8B70,color:#24563F
style B fill:#EAF3EC,stroke:#83A995,color:#315D49
style C fill:#F7F4E8,stroke:#C7B77A,color:#675A2D
style D fill:#F5F8F4,stroke:#B5CBBB,color:#40594A
style E fill:#EAF3EC,stroke:#83A995,color:#315D49
style F fill:#F5F8F4,stroke:#B5CBBB,color:#40594A
style G fill:#DCEFE5,stroke:#4F8B70,color:#24563F
style H fill:#DCEFE5,stroke:#4F8B70,color:#24563F
Context compression requires attention to information loss.
For example, a summary may lose key fields such as order number, time, amount, and business constraints.
For business states that require precise tracking, you should not rely solely on natural-language summaries; it is better to store structured state in a separate data store.
8. The Relationship Between Prompt Engineering and RAG
Prompt Engineering addresses how to organize input, while RAG (Retrieval-Augmented Generation) addresses how to introduce external knowledge before generation.
The two can be used together.
8.1 Why Is RAG Needed?
The parametric knowledge of large language models has limitations:
- Training data may have a temporal cutoff.
- Private enterprise data is usually not in the training data.
- Domain knowledge may be updated frequently.
- The model may generate inaccurate facts.
- Users may need traceable source references.
RAG retrieves external materials and provides the relevant content to the model as context.
8.2 Prompt Design in RAG
For example:
Role:
You are an enterprise internal knowledge base Q&A assistant.
Task:
Answer the user's question based on the provided materials.
Reference materials:
{{retrieved_documents}}
User question:
{{question}}
Constraints:
1. Prioritize answering based on the reference materials.
2. Do not fabricate facts that do not exist in the materials.
3. If the materials are insufficient, clearly state that it cannot be determined.
4. Cite the relevant material numbers.
5. Distinguish between facts and speculation in the materials.
Where:
retrieved_documents: Documents obtained through retrieval.question: The user's question.
RAG is not just about concatenating documents into the Prompt; it also requires consideration of retrieval quality, document chunking, ranking, context budget, and citation alignment.