Prompt Engineering for Large Language Models: Writing Principles and Best Practices
This article explains the core principles, structural frameworks, and common techniques for writing prompts for large language models (LLMs). It covers writing clear and specific instructions, giving models enough time to reason, building prompts from task, instruction, and role components, and choosing zero-shot, one-shot, or few-shot prompting strategies. It also outlines how prompt design fits into engineering workflows.
Large language models have advanced rapidly. General-purpose and industry-specific models can now generate text that closely resembles human writing. To get the most from these models and produce high-quality output that matches your intent, you need to know how to interact with them correctly. Prompt engineering connects human intent with model capability. This article covers the principles of writing prompts, common structural frameworks, and widely used techniques.
1. Core Principles of Prompt Engineering
A good prompt is not a random collection of words. It follows specific logic and principles. Two core principles apply: give clear and specific instructions, and give the model enough time to reason.
1.1 Write Clear and Specific Instructions
Language models do not have a human mental model. They rely entirely on the input text to infer what you want. Clearer and more specific instructions reduce the chance that the model hallucinates or drifts off topic.
Use specific delimiters. Separate instructions from the data to be processed with clear structural delimiters, such as
""",```,---,<>, or XML tags. Delimiters prevent the model from confusing instructions with data. They also reduce the risk of prompt injection, where the model treats data content as part of the instructions.Summarize the text enclosed in triple quotes below and extract three core points:"""[Insert the text to be processed here]"""
Require structured output formats. When the model’s output feeds a downstream system, require a structured format such as JSON or HTML. Structured output is easier for programs to parse, and it keeps the model’s text in a consistent format.
Extract the person and time information from the following text. Output strictly in the JSON format below, without any other text:{"person": "name of the person","time": "point in time"}Text: <text to be processed>
Check whether conditions are met. To make a system more robust, add preconditions to the prompt. If the input does not meet certain conditions, tell the model to return a specified response or follow a specific handling procedure. This prevents the model from generating unreliable content just to produce an answer.
Use few-shot prompting. For complex or easily confused tasks, a single instruction may not make the task boundaries clear. In that case, include examples of the task in the prompt. By providing pairs of inputs and expected outputs, you help the model pick up the pattern quickly and apply it to new inputs.
1.2 Give the Model Enough Time to Reason
Complex reasoning tasks need intermediate steps. If you ask for a final conclusion directly, the model may give a wrong answer because it has not worked through the logic.
- Specify the steps and their order. Break a complex task into explicit sub-steps and require the model to follow them in order. Step-by-step guidance can noticeably improve accuracy.
- Ask the model to show its working. Before the model gives a final conclusion, ask it to write out its reasoning. This chain-of-thought approach gives the model room to work through the problem before answering. It often improves results in logical reasoning and mathematical calculation.
2. Structural Framework for Prompt Design
A well-designed prompt is usually more than a single sentence. It is a set of components that work together. A standard prompt framework has three main elements: task, instruction, and role.
- Task: A clear and concise statement of the goal you want the model to achieve. The task is the core of the prompt and sets the basic direction of the output.
- Instruction: The specific rules and constraints the model should follow, such as output format, tone, and length limits.
- Role: The identity the model should adopt. Assigning a role lets the model draw on domain knowledge associated with that identity, which helps it produce content that follows domain conventions.
Framework example:
You are a senior translator. (Role)I want you to translate the Chinese text below into concise and clear English. (Task)Preserve the original meaning, use a formal business English style, and do not add extra explanations. Start translating now. (Instruction)Text to translate: <content>
3. Common Prompt Techniques
Based on the principles and framework above, you can build prompts for many different scenarios.
3.1 Instruction Prompts and Role Prompts
Instruction prompts focus on what the model should do and how it should do it. They suit tasks that generate a specific type of document. For example, “Draft a lease agreement that complies with applicable laws and regulations.” By specifying requirements for rent, lease term, and liability for breach, you can get a draft with a complete structure.
Role prompts focus more on the model’s persona. By assigning a role, such as “You are a professional divorce lawyer. Draft a divorce agreement,” the model adopts the perspective of a legal professional. It uses precise legal terminology, which helps the agreement remain lawful and rigorous.
3.2 Standard Prompt Pattern
A standard prompt combines task, instruction, and role. Take a product review as an example. You can structure the prompt with these elements:
- Task: Write a product review for a new laptop.
- Instruction: The review should be objective and informative, and it should highlight what makes the laptop distinctive.
- Role: Technical expert.
- Key points to emphasize: Powerful performance.
- Prompt formula: “You are now a technical expert. Write an objective and informative product review that highlights the powerful features of the new laptop.”
This pattern keeps the output focused and in-depth.
3.3 Sample-Based Prompting Strategies
Prompting strategies fall into three types based on the number of examples provided: zero-shot, one-shot, and few-shot.
- Zero-shot prompting: Give the instruction without any examples. This works for simple tasks where the model already has enough prior knowledge, such as “Generate a product description for this new smartwatch.”
- One-shot prompting: Provide one example to show the model the expected output style or comparison logic. For example: “Compare the new smartphone with the latest model from a competitor. Here is an example: [example].”
- Few-shot prompting: Provide two or more examples. This is especially effective when the output must follow a specific pattern or when the task includes complex edge cases.
4. Engineering Applications and Advanced Recommendations
In enterprise development, writing prompts is rarely a one-time task. It is an ongoing engineering process that requires continuous iteration. Platforms such as Baidu AI Cloud’s large model development platform can help developers manage prompt templates in a more systematic way.
In practice, developers can package the prompt frameworks described above into reusable templates and use variable substitution to adapt them to different inputs. Combined with the logging and monitoring capabilities that such platforms provide, teams can track the output quality and response latency of different prompt versions. For the example library used in few-shot prompting, developers can also use vector databases for dynamic retrieval (RAG). This approach automatically injects the most relevant examples based on the user’s specific input, which can further improve output accuracy.
In summary, mastering the principles and techniques of prompt writing is key to getting productivity from large language models. Developers need to balance clear instructions with room for the model to reason. With structured frameworks and well-chosen examples, models can become effective tools for solving complex business problems.
