Prompt Engineering Principles and Chat History in Copilot
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.
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.
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
- Earlier messages in the same chat are sent as context. Here the old v1 instruction keeps leaking into new answers.
- Start a new chat (+) or type /clear when the topic changes. Copilot doesn’t remember other conversations.
- 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.
🖱️ Drag to rotate · scroll or pinch to zoom
Chat history is context too
Earlier messages in the same conversation go into the prompt. That helps follow-ups (“now add tests”) but old, unrelated turns can confuse the answer and fill the context window.
🤔 What happens if…
Read this tour as text
- A vague prompt. Copilot only knows what is in the prompt. “Write a function” leaves it to guess the language, inputs, outputs and rules, so you get something generic.
- Structure: goal, context, constraints. A good prompt has a goal (what and why), context (which code, which framework) and constraints (style, edge cases, what not to do). Short is fine; vague is not.
- How context is chosen. In chat, Copilot uses the active file and any selection, plus what you add:
#file,#codebase(search your workspace),#terminalSelection, images, or a participant such as@workspace. Inline suggestions also use open tabs, so close unrelated ones. - Zero-shot and few-shot. Zero-shot: just the instruction. Few-shot: include one or more examples of the input and the output you want, then ask. Few-shot wins when your format is unusual.
- Break it down, then iterate. Split a big task into small steps and check each answer. Then iterate: keep what’s right and ask for the next change. Prompting is a conversation.
- Custom instructions. Repository instructions (
.github/copilot-instructions.md) and path-specific.instructions.mdfiles are added to requests automatically, so the house rules travel with every prompt. - Chat history is context too. Earlier messages in the same conversation go into the prompt. That helps follow-ups (“now add tests”) but old, unrelated turns can confuse the answer and fill the context window.
- The prompt process, end to end. Your words + selected context + instructions + history → the prompt. The model answers, you review, then refine. Better input, less clean-up.
A free terminal with GitHub Copilot CLI, git and gh. No tasks: install the CLI, start a session, try slash commands and tool permissions, or call the seats API.
🎓 Education-only simulation. Your lab is saved in this browser, so you can leave and continue later.
😟 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
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 You write
One task, specific details, examples if the format matters.
-
2 IDE adds context
Active file, selection, #file / #codebase, custom instructions and earlier messages in this chat.
-
3 Proxy and model
Sent over HTTPS to the Copilot proxy (auth, filters), then the model generates a reply.
-
4 Filtered reply
Post-processing checks the answer before it appears in chat.
-
5 You review
Keep, refine with a follow-up, or start a new chat if the topic changed.
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:
| Principle | What it means | Example |
|---|---|---|
| Single | One well-defined task per prompt | “Add input validation to applyDiscount” (not validation, tests and docs at once) |
| Specific | Clear inputs, outputs, rules | “Unknown codes return the total unchanged” |
| Short | Concise; no padding | Two sentences, not a page |
| Surround | Useful context around the prompt | Related 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.
- 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. - Secure transmission. The prompt goes over HTTPS to the GitHub Copilot proxy.
- Filtering. The proxy authenticates the request and runs pre-filters, for example for harmful or offensive content and prompt-injection attempts.
- Generation. The large language model produces a reply.
- 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.
- 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
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.
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.
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.What are the 4 S's?Show
Single, specific, short, surround.
-
2.Which slash command clears the current chat?Show
/clear
-
3.Does a new chat include the previous conversation?Show
No. It starts without that history.
-
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.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
Lesson done? Make it stick with a quick gameSort It, Match the Pairs, Lightning Round and Who Am I? Earn XP and badges.