Your profile

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

Lesson 20 of 30
❮Prev Next❯

Prompt Structure and Context for GitHub Copilot

Module 4: Prompt Engineering ⏱ 9 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

    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.

    Simulate it » No Azure account needed

    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.

    VS Code & GitHub: Give Copilot the right context Point Copilot at the right code.
    </> Embed

    Embed this simulation

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

    Loading…

    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
    1. 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.
    2. 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.
    3. 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.

    😟 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
    🛠️ Solved with: Prompt structure

    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

    PartAsk yourselfIn the contoso-shop example
    GoalWhat do I want, and why?Unknown discount codes should leave the total unchanged instead of returning NaN
    ContextWhich code, framework or data?applyDiscount(total, code) in src/discount.js, Node.js 22, ES modules
    ConstraintsWhat 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

    SurfaceContext Copilot uses
    Inline suggestionsCode before and after the cursor, file name and language, related open tabs
    Chat in the editorYour 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 presentRepository instructions (.github/copilot-instructions.md) and matching .github/instructions/*.instructions.md files
    The conversationEarlier turns in the same chat
    NeverFiles 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

    Banking

    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.
    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 types the prompt "fix the discount bug" in Copilot Chat and receives changes to the wrong function. Which revised prompt is most likely to produce the intended change?

    Question 2

    Question 2 of 5 · Select 3

    Which three items can Copilot Chat in Visual Studio Code include as context for a question? Each correct answer presents part of the solution.

    Question 3

    Question 3 of 5

    Every developer on a team adds "use ES modules and the Node test runner" to each Copilot Chat prompt. You need these rules applied automatically to chat requests in the repository. What should you create?

    Question 4

    Question 4 of 5

    A developer starts a new file and receives generic inline suggestions. The developer wants suggestions that follow the intended behaviour of a new function. What should the developer do first?

    Question 5

    Question 5 of 5

    Which description best matches a well-structured prompt for GitHub Copilot?

    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. 1.Name the three parts of a well-structured prompt.Show

      Goal, context and constraints.

    2. 2.Which chat variable searches your whole workspace?Show

      #codebase

    3. 3.Where do repository-wide custom instructions live?Show

      .github/copilot-instructions.md

    4. 4.What steers inline suggestions in the editor?Show

      Nearby code, comments, the file name and open tabs.

    5. 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

    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.