Prompt engineering techniques - Microsoft Foundry | Microsoft Learn
URL: https://learn.microsoft.com/en-us/azure/foundry/openai/concepts/prompt-engineering
Microsoft Prompt Engineering Techniques
These techniques aren't recommended for reasoning models like gpt-5 and o-series models.
Prompt construction can be difficult. In practice, the prompt acts to help the model complete the desired task, but it's more of an art than a science, often requiring experience and intuition to craft a successful prompt. This article attempts to capture general concepts and patterns that apply to all GPT models.
Basics
This section covers the basic concepts and elements of GPT prompts.
Text prompts are how users interact with GPT models. As with all generative language models, GPT models attempt to produce the next series of words that are most likely to follow from the previous text. Regardless of the prompt that is provided, the model is simply responding with what it determines is most likely (given its training data and training targets).
Prompt components
Distinct sections of the prompt are sent to the API as an array of dictionaries with associated roles: system, user, and assistant.
Instructions
Instructions are simply instructions to the model on what to do. While simple in concept, they can be complex in practice.
- Simple instruction:
Write an introduction for a weekly newsletter. - Complex instruction (detailed criteria):
Write an introduction for a weekly newsletter to my company named Contoso. It should mention the great all hands meeting we had last week and thank the team for all their hard work over the tough past few months. - Complex instruction (bulleted criteria):
Write an introduction for a weekly newsletter, including the following: - My company is named Contoso - An exuberant greeting - Thanking the team for their hard work over a couple tough months - Positive outlook for the coming quarter - Signed by the SLT
Primary content
Primary content refers to text that the model processes or transforms. Typically, you use primary content with instructions. Example (English text is primary content, the prompt is instruction):
Can you please tell me how to get to the museum?
Translate to French:
Primary content can be structured. GPT models can easily interpret tabular formatted data (e.g. TSV) to answer questions:
Belgian Beer Brands
Beer name Beer style color ABV Brewery
"Chimay Gold" trappist pale ale gold 4.80% Chimay Brewery
"Chimay Blue" trappist dark ale dark 9.00% Chimay Brewery
...
Referencing the table above, how many beers are less than 6% ABV?
Examples
Successful prompts often rely on the practice of "one-shot" or "few-shot" learning. This involves including one or more examples of the desired behavior (input/output pairs).
With the Chat Completion API, you typically add few-shot learning examples to the messages array in the form of example user and assistant interactions.
- Zero-shot: Headline and direct query.
- Few-shot: Headline-topic pairs provided as context to establish the categorization pattern:
(Model completes with "Basketball".)Headline: Twins' Correa to use opt-out, test free agency Topic: Baseball Headline: Qatar World Cup to have zones for sobering up Topic: Soccer Headline: Yates: Fantasy football intel for Week 6 Topic: Football Headline: Coach confident injury won't derail Warriors Topic:
Cue
Cues act as a "jumpstart" for the output of the model, helping to direct it to the desired output. They're often a prefix that the model can build onto (e.g. “Here’s a bulleted list of key points:\n- ”).
Supporting content
Supporting content is information that the model can use to influence the output. It differs from primary content in that it's not the main target of the task, but is used alongside it (e.g. current date, user preferences). Example: providing a list of workshops as primary content, and a list of "My Important Topics" as supporting content, asking the model to group the workshops based on those topics.
Scenario-specific guidance
All of the examples were tested against a base GPT-4 model. Some techniques may produce different results with newer models.
Few-shot learning
When using the Chat Completions API, a series of messages between the User and Assistant can serve as examples for few-shot learning.
- System message:
Assistant is an intelligent chatbot designed to help users answer their tax related questions. Instructions: - Only answer questions related to taxes. - If you're unsure of an answer, say "I don't know" and recommend the IRS website. - Few-shot examples:
User: "When do I need to file my taxes by?"Assistant: "In 2023, you'll need to file by April 18th... See https://www.irs.gov/filing/..."User: "How can I check the status of my tax refund?"Assistant: "Visit https://www.irs.gov/refunds"
Non-chat scenarios
The Chat Completion API can be used for single-turn tasks (e.g. sentiment analysis) by setting system instructions and using a single user turn:
- System:
You're an assistant designed to analyze sentiment from speech data... Rate 1-10 and explain why. - User:
[Paste transcript string] - Assistant:
Sentiment rating: 8. [Explanation...]
Start with clear instructions
Telling the model the task you want it to do at the beginning of the prompt, before sharing additional context or examples, can help produce higher-quality outputs.
Repeat instructions at the end
Models can be susceptible to recency bias (information at the end has more influence). Experiment with repeating the instructions at the end of the prompt and evaluating the impact.
Prime the output
Include a few words or phrases at the end of the prompt to force the output format (e.g. “One possible search query is:” or “Here's a bulleted list:”).
Add clear syntax
Use clear syntax like separators (---) between sources or steps. Headings or variables in UPPERCASE help differentiate sections. XML or Markdown syntax works best because models are trained on large quantities of XML and Markdown web content.
Break the task down
LLMs perform better if a task is broken down into smaller steps.
Example: instead of extracting facts and generating queries in one step, instruct the model to first list FACTUAL CLAIMS and then list QUERIES using a specific tool structure (e.g. SEARCH("query")).
Use of affordances
We can get the model to use an affordance (like Google Search or database access tools) instead of relying on its own parameters. You can stop generation once the affordance calls are generated by the model, run the tool, and paste the outcomes back into the prompt.
Chain of thought prompting
This technique is only applicable to non-reasoning models. o-series and gpt-5 models handle reasoning internally.
Instruct the model response to proceed step-by-step and present all the steps involved before providing the final answer. E.g.: ANSWER is:.
Specifying the output structure
Asking for the model response to include citations can significantly reduce hallucinations. Inline citations are more effective than citations at the end of the content.
You can also specify strict tuple structures for extraction, e.g. (entity1, relationship, entity2).
Provide grounding context
If your use case relies on up-to-date, reliable information, provide grounding data. The closer your source material is to the final form of the answer, the less opportunity there is for error.
Example: You'll provide answers exclusively from the below text and respond in 100 words or less: ...
Space efficiency
GPT models break words into tokens.
- Tables: Tables are a space-efficient way to include data compared to JSON, because they avoid repeating field keys.
- White space: Consecutive white spaces are treated as separate tokens, which can waste space. Avoid unnecessary punctuation and spaces.