Generative AI Apps and Agents in Foundry · Topic 1 of 4

System and User Prompts

AI-901 prompt writing: system, user and assistant messages, clear instructions, context and grounding, few shot examples and reducing invented answers.

  • 5 min
In a nutshell
  • The system message sets standing behaviour. The user message carries the request.
  • Be specific, give context, state the format and break tasks into steps.
  • Few shot examples show the model the format you want.

What it is

A prompt is the input that tells a generative model what to do. In a chat model, the prompt is made of messages, and each message has a role. Good prompts give the model a clear task, the information it needs and the format you want, so that answers are accurate and consistent.

Writing prompts is the cheapest way to improve an AI app, because it needs no training and no new model.

System messagestanding instructionsUser messagethe requestModelgeneratesAssistant messagethe reply

How it works

Message roles

RoleWhat it carries
systemStanding instructions for how the assistant behaves in every reply: tone, length, rules, what to refuse
userThe person's request or question, plus any data they supply
assistantThe model's earlier replies, which you resend so the model remembers the conversation
toolThe result of a function the model asked the app to call

The model keeps no memory between requests. To hold a conversation, the app sends the earlier messages again each time, or uses a service feature that stores the conversation.

messages = [
  {"role": "system", "content": "You are a support assistant for Contoso. Answer in two sentences. Use only the provided policy text."},
  {"role": "user", "content": "Can I return an opened item? Policy: Items may be returned within 30 days if unused."}
]

Qualities of an effective prompt

  • Be specific. Say what you want, for whom and in what length. "Summarise this report in three bullet points for a manager" works better than "summarise this".
  • Give context. Supply the facts the model needs, because it cannot see your documents on its own.
  • State the format. Ask for a table, JSON with named fields, or a fixed number of sentences.
  • Set the role or persona. "You are a travel agent" shapes tone and content.
  • Add rules and limits. Say what to avoid and what to do when unsure.
  • Break complex tasks into steps. Ask the model to work through the steps in order.
  • Iterate. Test the prompt on several inputs and refine it.

Examples in the prompt

StyleWhat you give
Zero shotThe instruction only
One shotThe instruction and one example
Few shotThe instruction and several examples of input and desired output

Examples are an effective way to show the exact format or style you want, without retraining the model.

Grounding and reducing made up answers

When a model lacks the information, it may invent an answer. To reduce this:

  • Provide the relevant text in the prompt, and tell the model to answer only from it.
  • Tell it to say "I don't know" when the supplied text does not contain the answer.
  • Ask it to quote or cite the source passage.
  • Keep temperature low for factual tasks.

A longer persona, a higher temperature or a request for longer replies does not add facts, and can increase invention.

Prompt injection

Prompt injection is an attack in which text from a user, or from a document the model reads, tries to override the system instructions, for example "ignore your rules". Mitigations include clear system instructions, treating supplied documents as data, filtering inputs, limiting what the model's tools can do, and using safety features such as prompt shields.

Examples of weak and strong prompts

WeakStronger
"Tell me about our returns.""Using only the policy text below, state in two sentences whether an opened item can be returned. If the policy does not say, reply 'Not covered'."
"Summarise.""Summarise the email in three bullet points. Start each with an action verb."
"Write code.""Write a Python function that reads a CSV and returns the average of the Price column. Include comments."

A worked scenario

A company builds a chatbot that answers staff questions about holiday policy. The first version invents answers when the policy does not cover a case. The team fixes this without changing the model. The system message now says: "You are an HR assistant. Answer only from the policy text provided. If the text does not contain the answer, say 'I don't know' and suggest contacting HR. Reply in no more than three sentences." The app retrieves the relevant policy section for each question and places it in the user message. The invented answers stop, and replies keep a consistent length and tone.

What to remember for the exam

  • The system message sets standing behaviour. The user message carries the request.
  • Be specific, give context, state the format and break tasks into steps.
  • Few shot examples show the model the format you want.
  • To stop invented answers, ground the model in supplied text and tell it to say it does not know.
  • Prompt injection tries to override your instructions, and layered defences reduce it.
Exam tip

When a model "invents answers when the context lacks the information", the answer is an instruction to say "I don't know", not a bigger persona or a higher temperature.

How the exam asks about this

  • "Which message role holds instructions that apply to every reply?" The system message.
  • "Which prompt instruction reduces made up answers?" Say "I don't know" if the context lacks it.
  • "Show the model the exact output format you want." Include examples, known as few shot prompting.
  • "A document tells the model to ignore its rules." Prompt injection.

Common mistakes

Putting everything in the user message. Standing rules belong in the system message.

Expecting the model to remember earlier chats. The app must send the history again.

Fixing hallucinations with a higher temperature. That adds randomness, not facts.

Writing vague prompts. The model fills the gaps with guesses.

Check yourself

  • What goes in the system message?

    Standing instructions for how the assistant behaves, such as role, tone, rules and format.

  • What is few shot prompting?

    Including several examples of input and desired output in the prompt.

  • How can a prompt reduce invented answers?

    Supply the relevant text, tell the model to answer only from it, and to say it does not know otherwise.

Topic 1 of 4 in Generative AI Apps and Agents in Foundry

Test yourself on Generative AI Apps and Agents in Foundry

The full Microsoft AI-901 question bank has 511 questions, with timed practice exams and an explanation for every answer.

This study guide is independent and is not affiliated with, endorsed by, or sponsored by Microsoft. Vendor names, certification names and exam codes are trademarks of their respective owners and are used for identification only.