Your profile

One profile for every MSDEVBUILD site. Only you can see it, and the site owner if you write in.

Lesson 22 of 30
❮Prev Next❯

Prompt Engineering Principles and Chat History in Copilot

Module 4: Prompt Engineering ⏱ 10 min read Updated

Your note

My notes for this lesson

Select any text in the lesson to highlight it or add a note. Saved in this browser.

    No highlights yet. Select a sentence in the lesson, then pick a colour or add a note.

    All my bookmarks and notes

    Reading settings

    For every lesson, saved in this browser.

    Text size
    Line spacing
    Font

    With IaaS you rent the virtual machine. Microsoft looks after the building, power and hardware; you patch the operating system.

    30-second answer

    Prompt engineering means shaping what you send Copilot so it can act on it: one clear task, specific details, short and relevant context, examples when the format matters, then refine. Microsoft Learn sums it up as the 4 S's: single, specific, short and surround (keep related files open, use clear names). When you send a chat message, the IDE adds your selection, the active file, any # references, custom instructions and the earlier messages in the same chat, sends that prompt over HTTPS through the Copilot proxy to the model, and filters the reply before you see it. Chat history makes follow-ups like "now add tests" work, but old or unrelated turns can confuse answers and use up the context window, so start a new chat or type /clear when the topic changes.

    Simulate it » No Azure account needed

    Exam objective: 4.2.1 Explain prompt engineering principles. 4.2.2 Describe prompt process flow and chat history usage. (Skills measured as of August 7, 2026)

    Simulations

    Feel it before you read it. No account needed, nothing leaves this page.

    VS Code & GitHub: Chat history and starting fresh See history leak into answers, then fix it.
    </> Embed

    Embed this simulation

    Free for trainers, teachers and bloggers. Paste this into your page:

    Loading…

    Earlier messages in the <strong>same chat</strong> are sent as context. Here the old v1 instruction keeps leaking into new answers.

    👆 Click the glowing item on screen to continue

    Read the steps as text
    1. Earlier messages in the same chat are sent as context. Here the old v1 instruction keeps leaking into new answers.
    2. Start a new chat (+) or type /clear when the topic changes. Copilot doesn’t remember other conversations.
    3. Put the facts that matter in the prompt, point to an example file, and keep follow-ups on the same topic. Principles: clear goal, relevant context, examples, small steps, then refine.

    🧪 Simulation for learning. Screens are simplified versions of Visual Studio Code and github.com with GitHub Copilot; names, files and suggestions are examples. Not affiliated with GitHub or Microsoft, and nothing here touches your real account.

    😟 THE PROBLEM

    After an hour of chat, the answers get worse.

    😊 THE SOLUTION

    Use prompt engineering principles and manage the chat history.

    • One giant request✓ Small steps
    • Old turns steer answers✓ New chat or /clear
    • Accepting the first answer✓ Iterate and refine
    🛠️ Solved with: Prompt engineering

    Start with a question

    If you told a colleague at 9 am to use the v1 pricing API, then at 11 am said 'actually we moved to v2', which one would they use at 4 pm?

    A person would probably remember the correction. Copilot Chat sees the whole conversation every time you send a message, both the 9 am instruction and the 11 am correction, and it has to weigh them. In a long thread, the old one can win.

    That’s what happened to Asha on the contoso-shop team. She asked for a discount endpoint late in the day, and Copilot called /v1/prices. Nothing was wrong with the model. The history was. She opened a new chat, wrote “Using pricing API v2 (/v2/prices), add a discount endpoint, follow #file:src/orders.js”, and got the right answer first time.

    Explain it like I’m 10

    Visual: the prompt loop

    1. 1 You write

      One task, specific details, examples if the format matters.

    2. 2 IDE adds context

      Active file, selection, #file / #codebase, custom instructions and earlier messages in this chat.

    3. 3 Proxy and model

      Sent over HTTPS to the Copilot proxy (auth, filters), then the model generates a reply.

    4. 4 Filtered reply

      Post-processing checks the answer before it appears in chat.

    5. 5 You review

      Keep, refine with a follow-up, or start a new chat if the topic changed.

    What goes into one Copilot Chat request

    The technical explanation

    Principles of prompt engineering

    Prompt engineering is just giving the model what it needs to do the job. Microsoft Learn’s GitHub Copilot training uses the 4 S’s:

    PrincipleWhat it meansExample
    SingleOne well-defined task per prompt“Add input validation to applyDiscount” (not validation, tests and docs at once)
    SpecificClear inputs, outputs, rules“Unknown codes return the total unchanged”
    ShortConcise; no paddingTwo sentences, not a page
    SurroundUseful context around the promptRelated files open, clear file and function names, #file: references

    Add to that the ideas from the few-shot lesson: give examples when the format matters, break big work into steps, and iterate instead of expecting perfection first time.

    The prompt process flow

    When you press Enter in Copilot Chat, more than your sentence goes to the model.

    1. Context gathering. The extension collects your message, the active file and any selection, references you added (#file, #selection, #codebase, @workspace), repository custom instructions, and the chat history of this conversation. Everything has to fit in a limited context window, so the extension picks what matters most.
    2. Secure transmission. The prompt goes over HTTPS to the GitHub Copilot proxy.
    3. Filtering. The proxy authenticates the request and runs pre-filters, for example for harmful or offensive content and prompt-injection attempts.
    4. Generation. The large language model produces a reply.
    5. Post-processing. The response is checked again (for example quality, insecure patterns, offensive content, and public code matching where that policy applies) before it reaches you.
    6. You review and continue. Your next message repeats the loop, now with this answer in the history.

    Expand Used n references under a reply to see exactly which files went in.

    How chat history helps

    History is what makes a conversation possible:

    You:     Explain applyDiscount in #file:src/discount.js
    Copilot: It looks up the code in CODES and subtracts that percentage...
    You:     What happens for an unknown code?
    Copilot: CODES[code] is undefined, so the result is NaN...
    You:     Fix it and write a test for that case.

    “It”, “that case” and “fix it” only make sense because the earlier turns are part of the prompt.

    How chat history hurts

    The same mechanism causes trouble when the thread drifts:

    • Stale facts. A correction doesn’t delete the original instruction. Both are still in the history.
    • Topic bleed. Answers about the payment module leak into a question about the report module.
    • Crowding. A long thread takes space in the context window that your actual code could use.

    The fixes are simple. Start a new chat (the + button) or type /clear when the topic changes. Put the facts that matter in the new prompt rather than relying on something said an hour ago. Copilot doesn’t remember other conversations, so anything that should apply every time belongs in .github/copilot-instructions.md. In Copilot CLI, /clear does the same, /context shows how full the context is, and /compact summarises a long session to free space.

    Architect level Why the model doesn't just 'know' the latest instruction wins

    A model doesn’t keep a list of facts that get overwritten. Each request is a fresh pass over the whole prompt, and the model predicts the most likely reply given all of it. Recent turns usually carry more weight, but an earlier detailed instruction can still pull the answer its way, especially if the correction was brief. Restating the current facts in one clear prompt, in a clean conversation, removes the conflict instead of hoping the model resolves it.

    Real-world business scenario

    Online retail

    Contoso (fictional)

    The problem
    Developers on contoso-shop kept one Copilot Chat open all day. After the pricing service moved from v1 to v2, generated code still called /v1/prices in about one answer in five, and those bugs reached code review.
    What they used
    A team habit: one chat per task, /clear or a new chat when switching topics, and the current API version and endpoint written into .github/copilot-instructions.md.
    Why that choice
    Old turns stopped leaking into new answers, and the API version reached every request through instructions rather than through history.
    The result
    v1 calls disappeared from new pull requests within a week, and reviewers stopped seeing answers that mixed two topics.

    When to use it and when not to

    ✓ Use it when…

    • Keep one chat for one task so follow-ups like "now add tests" can rely on history.
    • Start a new chat or /clear when you change topic, file area or a key fact changes.
    • Apply the 4 S's to every prompt: single, specific, short, surround.

    ✕ Think twice when…

    • Relying on a correction buried in a long thread. Restate the facts in a fresh prompt.
    • Expecting Copilot to remember yesterday's conversation. It doesn't carry history between chats.

    ↔ Alternatives to consider

    • Custom instructions for rules that should apply to every conversation.
    • Copilot Spaces to share a curated set of files, issues and notes as context with your team.
    Ready for more?The full lesson adds the technical explanation, when to use it, interview questions and revision notes.

    Test yourself with exercises

    5 original practice questions in the GH-300 style. Pick your answers, then check them. You will see why each option is right or wrong. Score 75% or more and this topic is marked complete.

    Question 1

    Question 1 of 5

    A developer uses one GitHub Copilot Chat conversation all day. Early in the conversation the developer said to use version 1 of an internal pricing API, and later said the team moved to version 2. New responses still call version 1. What is the most likely cause?

    Question 2

    Question 2 of 5

    A developer finishes a task in GitHub Copilot Chat and starts work on an unrelated module. The developer wants the next answers not to be influenced by the previous discussion. What should the developer do?

    Question 3

    Question 3 of 5 · Select 2

    Which TWO items can be included in the prompt that Visual Studio Code sends when a developer submits a message in GitHub Copilot Chat?

    Question 4

    Question 4 of 5

    Microsoft Learn describes prompt engineering principles for GitHub Copilot as the 4 S's. A developer opens related files and uses descriptive file and function names so that Copilot has useful context. Which principle does this represent?

    Question 5

    Question 5 of 5

    A developer submits a prompt in GitHub Copilot Chat. Which sequence describes how the request is processed?

    Interview preparation

    Say your answer out loud first, then open the model answer. Interviewers care more about your reasoning than the exact words.

    Beginner Does Copilot Chat remember what you said earlier?

    Model answer

    Within the same conversation, yes. The earlier messages go along with each new prompt, which is why "now write tests for it" works. A new chat starts empty, and Copilot doesn't carry memory from one conversation into another.

    Interviewer follows up

    So how do I make something stick across chats?

    You

    Put it in custom instructions, like .github/copilot-instructions.md, which are added to requests automatically. Don't rely on an old chat.

    Intermediate Walk me through what happens when you press Enter in Copilot Chat.

    Model answer

    The extension builds a prompt from my message, the active file and selection, any #file or #codebase references, custom instructions and the chat history. It sends it over HTTPS to the Copilot proxy, which authenticates it and runs filters, for example for harmful content and prompt injection. The model generates a reply, it's post-processed and filtered again, and it appears in chat. Then I review it and either follow up or start fresh.

    Experienced developer Answers in a long chat have started mixing up an old API version with the new one. Why, and what's your fix?

    Model answer

    The old instruction is still in the history, so it keeps being sent as context, and a long thread also crowds the context window. I'd start a new chat, put the current facts in the first prompt (API v2, the endpoint, an example file) and keep that thread to one topic.

    Scenario A teammate writes one giant prompt asking for a feature, tests, docs and a migration. The result is shallow. How would you coach them?

    Model answer

    Break it into small steps in one focused chat: the function first, check it, then the tests, then the docs. Each prompt is single and specific, and the history from earlier steps helps the later ones. That's the 4 S's in practice.

    Quick revision

    Key points

    • Principles: one task, specific details, short and relevant context, examples, iterate.
    • Microsoft Learn's 4 S's: single, specific, short, surround.
    • Prompt flow: your words + selection/active file + # references + instructions + chat history → proxy (auth, filters) → model → post-processing → chat.
    • Chat history is sent with each message in the same conversation; that's what makes follow-ups work.
    • Old or off-topic history confuses answers and uses context window. Start a new chat or /clear.
    • Copilot doesn't remember other conversations. Standing rules belong in custom instructions.

    Important terminology

    Prompt engineering
    Writing and structuring the input to a model (instruction, context, examples, constraints) so the output is useful with less clean-up.
    4 S's
    Microsoft Learn's prompt principles for Copilot - single task, specific details, short prompts, and surround (relevant context such as open files and clear names).
    Chat history
    The earlier questions and answers in the current Copilot Chat conversation, which are sent as context with each new message.
    Context window
    The limited amount of text a model can take in for one request. History, files and instructions all compete for it.
    /clear
    A chat slash command that clears the current conversation so the next prompt starts without old history.

    Common mistakes

    • Keeping one endless chat for every topic of the day.
    • Correcting a fact mid-thread and expecting old answers to be forgotten.
    • Asking for several unrelated things in one prompt.

    Exam tips

    • "Answers reference something from earlier in the conversation" = chat history is part of the prompt.
    • "Responses became confused after switching topics" = start a new chat or /clear.
    • "Rules must apply to every conversation" = custom instructions, not chat history.

    Five quick revision questions

    1. 1.What are the 4 S's?Show

      Single, specific, short, surround.

    2. 2.Which slash command clears the current chat?Show

      /clear

    3. 3.Does a new chat include the previous conversation?Show

      No. It starts without that history.

    4. 4.Name two things besides your message that go into a chat prompt.Show

      The active file or selection and the chat history (also

    5. 5.Why can a very long chat give worse answers?Show

      Old turns compete for the limited context window and can steer answers off topic.

    Official references

    Finished this lesson? Tick it off. Your progress is saved in this browser.

    Next up: Generate, Refactor and Document Code

    Studying with a friend? Send them this lesson

    It's free and needs no sign-up. Their progress saves in their own browser.

    Lesson done? Make it stick with a quick gameSort It, Match the Pairs, Lightning Round and Who Am I? Earn XP and badges. Play ❯

    Install MSDEVBUILD Azure

    Free · no account · works offline

    Save the whole course for offline ›

    Hi, I’m your Nimbus

    Your study buddy. Pick what we do next.

    MSDEVBUILD · Multi-Stack Dev Build

    Sign in to MSDEVBUILD

    One account for Azure certifications and the blog: sign in here and you are signed in on both. Your XP, streak, practice deck and certificate stay in sync on every device. Optional: everything works without an account.