Prompt Structure and Context for GitHub Copilot
30-second answer
A good Copilot prompt has three parts: a goal (what you want and why), context (which code, framework or data it applies to) and constraints (style, edge cases, what not to change). Copilot only knows what reaches the prompt. In the editor, context comes from the current file, code around the cursor and open tabs. In chat, it comes from the active file or selection, what you attach with #file, #selection, #codebase or images, @ participants such as @workspace, custom instructions in .github/copilot-instructions.md, and earlier chat history. Be specific, point at the right code, and check Used n references to see what was used.
Exam objective: 4.1.1 Describe prompt structure and context. 4.1.2 Understand how context is determined. (Skills measured as of August 7, 2026)
Simulations
Feel it before you read it. No account needed, nothing leaves this page.
For inline suggestions, Copilot looks at the file you’re in and <strong>open tabs</strong>. An unrelated old file in a tab can pull suggestions off course. Close it.
👆 Click the glowing item on screen to continue
Read the steps as text
- For inline suggestions, Copilot looks at the file you’re in and open tabs. An unrelated old file in a tab can pull suggestions off course. Close it.
- In chat, the active file and any selection are included. Add more with #file, #selection, #codebase or by dragging files in. Be explicit about which code you mean.
- Expand Used n references to see exactly what context went into the answer. If the right file isn’t there, add it and ask again.
🧪 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
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.
🤔 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.
Stop repeating yourself in every prompt. Write repository instructions, add path-specific instructions for tests, and create a custom agent that only writes tests, then watch Copilot follow them.
🎓 Education-only simulation. Your lab is saved in this browser, so you can leave and continue later.
😟 THE PROBLEM
“Write a function” gets you something generic in the wrong style.
😊 THE SOLUTION
Give a goal, the context and the constraints.
- Vague asks✓ Say what, why and the rules
- Missing context✓ Point to the code
- No constraints✓ Edge cases and style up front
Start with a question
You ask Copilot to 'fix the discount bug' and it rewrites the wrong function. Whose fault is that?
Think of an auto-rickshaw driver. “Take me somewhere nice” gets you a ride, just not to where you wanted. “MG Road metro station, the gate near the bank, and please avoid the flyover, it’s jammed” gets you there.
Copilot works the same way. It can only act on what’s in the prompt: your words plus the context it can see. Give it a clear goal, point it at the right code and tell it the rules, and you spend less time cleaning up.
Explain it like I’m 10
The technical explanation
Goal, context, constraints
| Part | Ask yourself | In the contoso-shop example |
|---|---|---|
| Goal | What do I want, and why? | Unknown discount codes should leave the total unchanged instead of returning NaN |
| Context | Which code, framework or data? | applyDiscount(total, code) in src/discount.js, Node.js 22, ES modules |
| Constraints | What rules must the answer follow? | Never return a negative total, don’t change the function signature, add a test in tests/ |
Here’s the difference in chat:
Vague: fix the discount bug
Specific: In #file:src/discount.js, applyDiscount returns NaN for unknown
codes. Return the total unchanged when the code isn't in CODES,
never return a negative number, and keep the signature.
Then suggest a test for tests/discount.test.js.
Short is fine. Vague isn’t. When the output format matters, add an example (that’s few-shot prompting, covered in the next lesson), and break big jobs into small steps you check one at a time.
Prompting in the editor
Inline suggestions don’t have a chat box, so your prompt is the code itself:
// Return the total after the discount for `code`.
// Unknown codes: return total unchanged. Never return less than 0.
export function applyDiscount(total, code) {
A clear function name, a comment with inputs, outputs and rules, and meaningful variable names all go into the prompt. Copilot also reads related open tabs, so open a similar file you’d like it to imitate and close ones that have nothing to do with the task.
How context is determined
| Surface | Context Copilot uses |
|---|---|
| Inline suggestions | Code before and after the cursor, file name and language, related open tabs |
| Chat in the editor | Your question, the active file or selection, plus what you add |
| What you can add in chat | #file, #selection, #codebase, #changes, #terminalSelection, #terminalLastCommand, dragged-in files and images |
| Participants | @workspace, @terminal, @vscode, @github |
| Always added when present | Repository instructions (.github/copilot-instructions.md) and matching .github/instructions/*.instructions.md files |
| The conversation | Earlier turns in the same chat |
| Never | Files covered by a content exclusion (in supported IDEs) |
After an answer, expand Used n references to check what was sent. The VS Code & GitHub tab shows an old tab pulling suggestions off course, a #file chip in chat, and the references list.
Put house rules in instructions, not every prompt
If you keep typing “use ES modules, tests use node --test, never log secrets”, move it into .github/copilot-instructions.md. Those rules are added to chat, agent and code review requests in the repo automatically. Path-specific rules go in .github/instructions/tests.instructions.md with applyTo: "tests/**".
Architect level Why specific beats polite or long
The model predicts text that fits the prompt. Vague prompts fit thousands of possible answers, so you get the most average one. Each specific detail (a file name, a version, an edge case, an example) removes possibilities and moves the answer towards yours. Background that doesn’t change the answer just uses up the context window. So a good prompt reads like a clear ticket: one goal, the code it applies to, the acceptance criteria. If the first answer is close, keep the conversation going and refine rather than starting over with a longer prompt.
Real-world business scenario
Woodgrove Bank (fictional)
- The problem
- New developers on the payments team got generic Copilot answers that ignored the team's logging rules and test style. Senior engineers spent review time fixing the same issues in every pull request.
- What they used
- A prompt checklist (goal, context, constraints) in the team wiki, #file and #selection for every chat question about specific code, and a .github/copilot-instructions.md with the logging and testing rules.
- Why that choice
- Specific prompts with the right context get specific answers, and repository instructions carry the house rules into every chat and agent request automatically.
- The result
- Review comments about logging and test style dropped sharply over the next month, and new joiners said Copilot's answers finally looked like the team's code.
When to use it and when not to
✓ Use it when…
- Name the file, function and expected behaviour when you ask chat for a change.
- Write a clear comment and function signature before you wait for an inline suggestion.
- Move rules you repeat into repository or path-specific instructions.
✕ Think twice when…
- One-word prompts like "fix" or "optimize" with no target and no rule.
- Pasting secrets or real customer data as context.
- Adding long background that doesn't change the answer.
↔ Alternatives to consider
- Prompt files (.github/prompts/*.prompt.md) for a task your team runs often, invoked with /name.
- Few-shot prompting when the output must follow an unusual format.
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 What makes a prompt for Copilot "good"?
Model answer
It says what I want, gives the right context and lists the rules. "Write a function" is vague. "In src/discount.js, make applyDiscount return the total unchanged for unknown codes, and never return a negative number" has a goal, points at the code and sets constraints.
Interviewer follows up
Does a good prompt have to be long?
You
No. Short is fine. Vague isn't. One clear sentence with the file and the rule beats a paragraph of background.
Intermediate How does Copilot Chat decide which code to look at?
Model answer
It uses the active file and my selection by default, plus anything I add: #file, #selection, #codebase to search the workspace, terminal context, images, or a participant like @workspace. Custom instructions and earlier turns in the conversation are included too. I check Used n references to confirm.
Experienced developer Your team keeps repeating "use ES modules and the Node test runner" in every prompt. How do you fix that?
Model answer
Put it in .github/copilot-instructions.md. Repository instructions are added automatically to chat, agent and code review requests in that repo. For rules that only apply to tests, I'd add a path-specific .instructions.md file with applyTo set to tests/**.
Scenario Copilot's inline suggestions in a new file are generic and miss the project's conventions. What do you change?
Model answer
Give it more to work with. I'd write a clear function name and a comment describing inputs, outputs and rules above the cursor, open a similar file from the project in another tab, and close unrelated tabs. Inline suggestions are driven by nearby code and open tabs.
Quick revision
Key points
- Prompt structure: goal + context + constraints. Add examples when the format matters.
- Copilot only knows what reaches the prompt.
- Editor context: current file, code around the cursor, open tabs. Comments steer inline suggestions.
- Chat context: active file or selection, # variables, @ participants, custom instructions, chat history.
- Repository instructions in .github/copilot-instructions.md travel with chat, agent and review requests.
- Check Used n references to see what context was used.
Important terminology
- Goal
- The part of a prompt that says what you want Copilot to produce and, ideally, why.
- Constraints
- Rules the answer must follow, such as language version, style, edge cases to handle or files not to touch.
- Context
- The code, files and information Copilot uses alongside your words to answer.
- Repository custom instructions
- A .github/copilot-instructions.md file whose rules are added to chat, agent and code review requests in that repository.
- Comment prompt
- A code comment that describes what you want next, used to steer inline suggestions in the editor.
- #codebase
- A chat variable that searches your workspace for code relevant to the question and adds it as context.
Common mistakes
- Vague prompts like "fix this" with no file, no expected behaviour and no constraints.
- Asking about a file that isn't open, selected or attached.
- Repeating the same house rules in every prompt instead of using custom instructions.
Exam tips
- "Improve a vague prompt" = add the goal, the context (file, function) and constraints.
- "Make Copilot use a specific file" = #file or select the code.
- "Apply team rules to every chat request in a repo" = .github/copilot-instructions.md.
Five quick revision questions
-
1.Name the three parts of a well-structured prompt.Show
Goal, context and constraints.
-
2.Which chat variable searches your whole workspace?Show
#codebase
-
3.Where do repository-wide custom instructions live?Show
.github/copilot-instructions.md
-
4.What steers inline suggestions in the editor?Show
Nearby code, comments, the file name and open tabs.
-
5.How do you check which files a chat answer used?Show
Expand Used n references.
Official references
Finished this lesson? Tick it off. Your progress is saved in this browser.
Next up: Zero-Shot and Few-Shot Prompting
Lesson done? Make it stick with a quick gameSort It, Match the Pairs, Lightning Round and Who Am I? Earn XP and badges.