Your profile

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

Lesson 17 of 30
❮Prev Next❯

How GitHub Copilot Builds a Prompt from Context

Module 3: Data & Architecture ⏱ 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

    Copilot never sees "your project" as a whole. For an inline suggestion, the IDE extension gathers the code before and after your cursor, the file name and language, and related snippets from open tabs, then ranks and trims them into a prompt that fits the model's limited context window. In chat, the prompt also includes your question, the active file or selection, anything you attach (#file, #selection, #codebase, #terminalLastCommand, images), custom instructions and earlier turns of the conversation. Content exclusions remove files from that context, and Used n references shows what was really sent.

    Simulate it » No Azure account needed

    Exam objective: 3.1.2 Describe input processing and prompt building. (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 Open tabs, #file and the references Copilot really used.
    </> 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

    Copilot keeps suggesting code from an unrelated file.

    😊 THE SOLUTION

    Understand how the prompt is built from your editor, and shape it.

    • Random open tabs✓ Keep relevant tabs open
    • Wrong code referenced✓ Select it or use #file
    • Huge files✓ The context window is limited
    🛠️ Solved with: Prompt building

    Start with a question

    Copilot suggests a function name that doesn't exist in your project. Was the model wrong, or did it never see the right file?

    Picture a tailor stitching a shirt for you over the phone. If you only say “make it nice”, you get a standard shirt. If you send your measurements, a photo of a shirt you love and the fabric, you get something that fits.

    Copilot is that tailor. It only knows what’s in the prompt, and for an inline suggestion the IDE builds that prompt for you from whatever is near your cursor and open in your editor. When the right file isn’t in that bundle, the model guesses, and guesses can look confident.

    Explain it like I’m 10

    The technical explanation

    Context for inline suggestions

    When you pause while typing, the Copilot extension gathers:

    • Code before the cursor (the prefix) and code after it (the suffix), so the suggestion fits in the middle.
    • The file name, path and language. tests/discount.test.js tells the model you’re writing a test in JavaScript.
    • Related snippets from open tabs (neighbouring tabs). Having src/discount.js open while you write its test helps a lot. Repository custom instructions are used by chat, agents and code review, not by inline completions, so don’t count on them to steer ghost text.

    Then it ranks and trims. A model reads a limited number of tokens per request (the context window), and completions need to come back quickly, so only the most relevant pieces go in. Files covered by a content exclusion are skipped.

    Context for chat

    Chat prompts carry more, because you’re asking a question rather than continuing a line.

    PieceWhere it comes from
    Your questionWhat you typed, plus any slash command such as /explain or /tests
    Active file or selectionIncluded automatically in the editor’s chat
    Chat variables#file, #selection, #codebase, #changes, #terminalSelection, #terminalLastCommand, or files and images you drag in
    Participants@workspace (your project), @terminal, @vscode, @github
    Custom instructions.github/copilot-instructions.md and matching .github/instructions/*.instructions.md files
    Chat historyEarlier turns in the same conversation

    All of that has to fit the context window too. Long conversations push older turns out or crowd the useful context, which is why starting a new chat for a new topic helps.

    You:  Why does applyDiscount return NaN for unknown codes? #file:src/discount.js
          📎 Used 2 references: discount.js, cart.js

    Check what was really used

    Under a chat answer, expand Used n references to see exactly which files and snippets went in. If the file you care about is missing, attach it and ask again. If it never appears even when attached, it may be excluded. The VS Code & GitHub tab walks through this with open tabs, a #file chip and the references list.

    In the Copilot CLI, @path adds a file to your prompt and /context shows how full the context window is. You can try /context in the Practice Lab sandbox.

    Architect level Why the suffix matters

    Older autocomplete only looked backwards. Copilot also sends the code after your cursor, which lets the model fill a gap instead of just continuing. If you’re writing the middle of applyDiscount and the return line already exists below, the suggestion will define the variable that return needs. That’s also why a half-finished file with a clear function signature and a comment above it gets better suggestions than an empty one: there’s more signal on both sides of the cursor.

    Real-world business scenario

    Online retail

    Tailspin Toys (fictional)

    The problem
    Developers on a 400-file Node.js repo complained that Copilot invented helper names. Most had a dozen old tabs open from other features, and they asked chat vague questions without attaching any file.
    What they used
    A short team guide: open the file under test beside the test file, close unrelated tabs, attach code with #file or #selection, use #codebase for broad questions, and check Used n references before trusting an answer.
    Why that choice
    Inline suggestions read open tabs and nearby code, and chat answers come from whatever context is attached. Better context means fewer invented names.
    The result
    In their next retro, developers reported far fewer made-up helpers, and code reviewers stopped flagging calls to functions that didn't exist.

    When to use it and when not to

    ✓ Use it when…

    • Open the related source file when you write its tests or callers.
    • Attach #file or #selection when you ask chat about specific code.
    • Use #codebase or @workspace when you don't know which file holds the answer.

    ✕ Think twice when…

    • Leaving stale or unrelated tabs open while you work on something new.
    • Pasting a huge file into chat when one function would do. It crowds the context window.

    ↔ Alternatives to consider

    • Custom instructions for rules every chat and agent request should follow.
    • Copilot Spaces to share a curated set of files 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 · Select 3

    A developer is typing inside a function in src/cart.js in Visual Studio Code. Which three sources of context can the Copilot extension include when it builds the prompt for an inline suggestion? Each correct answer presents part of the solution.

    Question 2

    Question 2 of 5

    Why does the Copilot extension rank and trim context before it sends a prompt?

    Question 3

    Question 3 of 5

    A developer asks Copilot Chat about a bug, and the answer ignores src/discount.js. The developer wants to confirm which files were used to produce the answer. What should the developer do?

    Question 4

    Question 4 of 5

    A developer writes tests in tests/discount.test.js, but inline suggestions keep using function names from an unrelated file. Several old files are open in other tabs. What should the developer do first?

    Question 5

    Question 5 of 5

    In Copilot Chat, a developer wants Copilot to search the whole workspace for code related to a question about discount codes. Which context should the developer add to the prompt?

    Interview preparation

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

    Beginner Why do Copilot's suggestions get better when I open a related file?

    Model answer

    Because open tabs are part of the context for inline suggestions. If I'm writing a test and the code under test is open in another tab, the extension can pull matching snippets from it into the prompt, so the model sees the real function names.

    Interviewer follows up

    Can open tabs make things worse?

    You

    Yes. An old, unrelated file in a tab can pull suggestions off course. I close tabs I'm not using.

    Intermediate What does Copilot Chat include in the prompt when I ask a question?

    Model answer

    My question, the active file or my selection, anything I attach with # variables or by dragging files in, context from a participant like @workspace, the repository's custom instructions, and the earlier turns of the same conversation. All of that has to fit the model's context window.

    Experienced developer Chat gave an answer that ignored the file you cared about. How do you check and fix it?

    Model answer

    I expand Used n references under the answer to see what was actually sent. If the file isn't there, I attach it with #file or select the code, and ask again. If the file should be there but never shows up, I check whether it's covered by a content exclusion.

    Scenario A developer asks why Copilot doesn't just read the whole monorepo for every suggestion.

    Model answer

    The model's context window is limited, and inline suggestions need to be fast. So the extension picks the most relevant bits: the code around the cursor, the file's language and name, and related snippets from open tabs. For broader questions in chat, #codebase or @workspace searches the workspace and adds the best matches.

    Quick revision

    Key points

    • Inline suggestion context: code before and after the cursor, file name and language, related snippets from open tabs.
    • The context window is limited, so context is ranked and trimmed before it's sent.
    • Chat adds: your question, active file or selection, # variables, @ participants, custom instructions and chat history.
    • #file, #selection, #codebase, #changes, #terminalSelection and #terminalLastCommand attach specific context.
    • Content exclusions remove files from context in supported IDEs.
    • Used n references shows what context a chat answer really used.

    Important terminology

    Context
    The information Copilot adds to a request so the model knows what you're working on, such as nearby code, open tabs or attached files.
    Context window
    The maximum amount of text (in tokens) a model can read in one request. Context has to be ranked and trimmed to fit.
    Neighbouring tabs
    Other files open in your editor. Copilot looks in them for snippets related to the code you're writing.
    Chat variable
    A "#" reference in Copilot Chat, such as
    Chat participant
    An "@" expert in Copilot Chat, such as @workspace, @terminal, @vscode or @github, that brings its own context and tools.
    Used n references
    The expandable list under a chat answer that shows which files and snippets Copilot used as context.

    Common mistakes

    • Assuming Copilot reads the whole repository for every inline suggestion.
    • Leaving unrelated tabs open and wondering why suggestions drift.
    • Not checking Used n references when an answer ignores a file.

    Exam tips

    • "Improve inline suggestions with related code" = open the related file in a tab.
    • "Ask chat about one specific file" = #file (or select the code).
    • "Search the whole workspace from chat" = #codebase or @workspace.

    Five quick revision questions

    1. 1.Name two things the extension includes for an inline suggestion.Show

      Code around the cursor and the file name or language (also open tabs).

    2. 2.Why does context have to be trimmed?Show

      The model's context window is limited.

    3. 3.Which chat variable attaches the code you've highlighted?Show

      #selection

    4. 4.Where do you see which files a chat answer used?Show

      Used n references, under the answer.

    5. 5.Does an excluded file get used as context?Show

      No, not in supported IDEs.

    Official references

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

    Next up: Proxy Filtering and Post-Processing

    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.