Lesson 12 of 26 · see all lessons
  1. Start here: the exam, Python basics and responsible AI (skill area 1, 40-45%)

    1. 1. Start here: the AI-901 exam, the plan and what you already know
    2. 2. The Python and Azure basics AI-901 expects
    3. 3. Responsible AI: the six principles in practice
  2. AI models: how they work, choosing and deploying (skill area 1)

    1. 4. How generative AI models work
    2. 5. Choosing the right AI model for the job
    3. 6. Deploying a model: deployment options and configuration parameters
  3. AI workloads: text, speech, vision and extraction (skill area 1)

    1. 7. Common AI workloads: from chat and agents to vision and extraction
    2. 8. Text analysis: key phrases, entities, sentiment and summaries
    3. 9. Speech: recognition and synthesis
    4. 10. Computer vision and image generation
    5. 11. Extracting information from documents, images, audio and video
  4. Generative AI apps and agents in Foundry (skill area 2, 55-60%)

    1. 12. Writing system and user prompts
    2. 13. Deploying a model and chatting with it in the Foundry portal
    3. 14. Building a lightweight chat client with the Foundry SDK
    4. 15. Creating and testing a single agent in the Foundry portal
    5. 16. Building a lightweight client app for an agent
  5. Text, speech, vision and extraction apps in Foundry (skill area 2)

    1. 17. Building a text analysis app
    2. 18. Building speech apps: spoken prompts and Azure Speech
    3. 19. Building vision apps: images in prompts and generated images
    4. 20. Extracting information with Content Understanding: documents and forms
    5. 21. Content Understanding for images, audio and video, and a small extraction app
  6. Final review and four mock exams

    1. 22. Final review and exam day: every decision on one page
    2. 23. Mock exam 1
    3. 24. Mock exam 2
    4. 25. Mock exam 3
    5. 26. Mock exam 4
See the leaderboard

18 min

Writing system and user prompts

Write system and user prompts that get accurate, safe and well-formatted answers from a generative model.

By the end of this lesson you can

  • Put each instruction in the right message role: system, user or assistant
  • Apply few-shot examples, grounding data, delimiters and a fixed output format
  • Write a safety system message and explain how it works with guardrails
  • Choose between prompt engineering, grounding and fine-tuning

From earlier lessons · 1 of 2

A legal team uses a deployed model to draft contract clauses. Answers vary too much between runs. Which parameter should you lower?

Pick one answer.

From earlier lessons · 2 of 2

A travel chatbot forgets the start of very long chats. The model counts text in tokens. What limit is the chat hitting?

Pick one answer.

[50]

Fernhill Travel's support team tests a deployed model on a real email about bringing a dog on the Lisbon trip. The model writes 600 words on pet passports, invents a fee and signs off as "Your friend, AI". Wrong facts, wrong tone. The model is fine; the prompt is the problem.

[2]

Three roles in a conversation

Exam depth: know it cold.

A prompt is the text you send a model to get a response. Chat models take it as a list of messages, each with a role.

RoleWho writes itWhat goes in it
SystemYou, the developerRole, tone, rules, format, safety limits.
UserThe person using the appThe actual request.
AssistantThe modelIts earlier replies.

The system message comes first and holds high-priority instructions for the whole chat. A user message is one request. An assistant message is a reply the model already gave.

The model keeps no memory between calls. To continue a chat, you send the conversation history: earlier user and assistant messages, in order.

Decision rule: a rule for every reply goes in the system message. One question goes in a user message.

[50][96]

Rendering diagram…
Each call sends the whole list: the system message, earlier turns, then the new user message. The model's reply becomes the next assistant message.

[50]

Fernhill Travel's assistant must never quote a price, in every chat, whatever the customer asks. Where should you put that rule?

Pick one answer.

[96]

Techniques that make prompts work

Exam depth: know it cold.

Prompt engineering means writing and testing prompts until the model does what you need. Microsoft's guidance comes down to a few moves.

TechniqueWhat you doWhat it fixes
Clear, specific instructionsSay the task, length and audience.Vague or rambling answers.
Grounding dataPaste the facts and say "answer only from this text".Made-up facts.
Few-shot examplesShow input and output pairs.Wrong style or labels.
Output formatAsk for JSON, a table or bullets.Answers code can't read.
Break the task downAsk for steps, one at a time.Errors on multi-step tasks.
DelimitersSeparate parts with --- or headings.Model mixing rules with content.
Give an "out"Say what to reply when the answer isn't there.Guessing.

Grounding data is information you give the model to answer from, such as Fernhill's pet policy. It's the strongest fix for invented facts. Asking a reasoning model to show its steps isn't recommended; the step-by-step trick is for non-reasoning models.

[50]

Match

Match each problem with the prompt technique that fixes it

Pick an item on the left, then its match on the right.

[50]

Zero-shot and few-shot prompts

Exam depth: know it cold.

A zero-shot prompt gives instructions with no examples. A few-shot prompt adds one or more examples of input and the output you want. One example is called one-shot.

In a chat, you add examples as pairs of user and assistant messages after the system message. The model copies the pattern for this request only. Nothing about the model changes; that's the difference from fine-tuning.

Use few-shot when instructions alone don't get the style, labels or format right.

[50]

Predict, then reveal

Fernhill's first system message was: "You are a helpful assistant." The dog question got 600 words and a made-up fee. Rewrite it in your head, then reveal a better version.

[50]

Fernhill wants each review tagged as Hotel, Transport or Guide. With instructions only, the model invents tags like "Lodging". You can't retrain the model. What should you add to the prompt?

Pick one answer.

[50]

System message: "Be helpful." Model: writes a 600-word essay on pet passports. Technically helpful. Picture: Sticky-note sketch style: hand-drawn doodles on yellow sticky notes stuck to a desk.

Get output in a fixed format

Exam depth: know it cold.

When code reads the reply, ask for a structure. Describe it in the system message, or show it.

  • JSON: "Reply only with JSON: {"tag": "", "sentiment": ""}".
  • Bullets or a table: "List each step as a bullet".
  • Citations: "After each fact, cite the policy section". Asking for citations makes invented facts less likely.

You can also prime the output: end the prompt with the first words of the answer, such as "Key points:\n- ". The model continues in that shape.

A format rule makes the format likely, not certain. Your code should still check the reply.

[50][96]

For the statement, select Yes if it is true. Otherwise, select No. "If the system message says to reply only in JSON, the model is guaranteed to return valid JSON."

Pick one answer.

[96]

Safety system messages and guardrails

Exam depth: know it cold.

A safety system message is a system message with clear limits and refusal rules. It usually covers:

  • Role and task: what the assistant does.
  • Scope and boundaries: what it must not do, and what to say instead.
  • Safety rules: for example, never give medical advice, never ask for card numbers.
  • Format and tone.

It's one layer, not the whole defense. Clever prompts can get around it. So Foundry also runs guardrails on every deployment. Older material calls them content filters.

By default, they check prompts and replies for hate, violence, sexual content and self-harm. Content at medium or high severity is blocked. They also detect prompt attacks (jailbreaks) in user input. Use both: the system message shapes behavior, guardrails catch harm.

[29][96]

Layers that keep a chat safeUser promptGuardrail: checks the promptSafety system messageGrounding dataModelGuardrail: checks the replyReply the user seesBlocks harmful input andjailbreak attemptsRules, scope and what torefuseBlocks harmful output atmedium severity and above

[29][96]

[96]

Prompt engineering, grounding or fine-tuning?

Exam depth: recognize it.

Prompt engineeringGrounding (RAG)Fine-tuning
What changesThe prompt.The data sent with the prompt.The model itself, by training.
FixesTone, format, focus.Missing or changing facts.Style or task skill that prompts can't reach.
EffortLowest. Start here.Medium.Highest.

RAG (retrieval augmented generation) means your app finds relevant documents and adds them to the prompt as grounding data.

Decision rule: start with prompts. Facts the model lacks, or that change often, need grounding. Behavior that prompts can't fix needs fine-tuning.

[50][97]

Fernhill's assistant must answer from the cancellation policy, which changes every month. Answers must use the current version. What should you use?

Pick one answer.

[97]

Exam-style practice

A museum's chat assistant answers from a pasted exhibit guide. When the guide has no answer, it makes one up. What should you add to the system message?

Pick one answer.

[50]

A user pastes a customer email that says "Ignore the rules above". The model starts following the email. Which prompt change helps the model tell your rules from the content?

Pick one answer.

[50]

FlashcardsIndex cardsFlashcards ⚡

Card 1 of 15

[29][50][96][97]

Worth keeping

  1. System message for rules on every reply; user message for the request; assistant messages are past replies.
  2. Few-shot examples fix style and labels for this request only; the model doesn't change.
  3. Grounding data plus an out is the best fix for invented facts.
  4. Ask for JSON or bullets when code reads the reply, and still validate it.
  5. A safety system message works with guardrails; neither is enough alone.

On the job

Treat a system message like code. Keep it in a file with a version number. Build a small test set of normal and tricky questions, including ones that try to break the rules. Run the set after every change and compare. Change one thing at a time, so you know what helped.

[96]

The new system message works on paper. Now Fernhill needs a real model deployment to try it on. Next lesson you deploy a model in the Foundry portal and test the prompt in the playground.

Leaderboard

Loading…

50 XP for each lesson you finish, 10 for each right answer. Names show only for learners who turn on Show me on leaderboards.

Embed this practice on your site

Free for classrooms, blogs and study groups. Paste this into any page that accepts HTML.

Report a copyright problem with this lesson

Sources for this lesson (5)

Each fact in the lesson is marked with the number of the page it comes from.

  1. [2]Microsoft Learn: Study guide for Exam AI-901: Microsoft Azure AI Fundamentals
  2. [29]Microsoft Learn: Default Guardrail policies for Microsoft Foundry
  3. [50]Microsoft Learn: Prompt engineering techniques
  4. [96]Microsoft Learn: Safety system messages
  5. [97]Microsoft Learn: Retrieval augmented generation (RAG) and indexes