How GitHub Copilot Builds a Prompt from Context
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.
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.
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
The IDE builds a prompt
The extension gathers context: code before and after the cursor, the file name and language, and related snippets from other open tabs. That bundle is the prompt. A model has a limited context window, so the extension picks what matters most.
🤔 What happens if…
Read this tour as text
- You type, then pause. Copilot doesn’t read your mind. When you pause while typing, the Copilot extension in your IDE starts a request for a completion.
- The IDE builds a prompt. The extension gathers context: code before and after the cursor, the file name and language, and related snippets from other open tabs. That bundle is the prompt. A model has a limited context window, so the extension picks what matters most.
- Content exclusions are checked. An admin can exclude paths or whole repositories. Excluded files are not used as context, and Copilot gives no inline suggestions inside them.
- The Copilot proxy. The prompt goes over HTTPS to a GitHub-hosted proxy. It authenticates the request and runs pre-filters, for example for harmful or offensive content and prompt-injection attempts, before anything reaches the model.
- The model predicts code. A large language model predicts the most likely next tokens. It matches patterns from training data and your context; it doesn’t run, test or truly understand your code. That’s why output can be wrong, outdated or insecure.
- Post-processing filters. Before you see it, the response is post-processed: checks for code quality and common insecure patterns, offensive content, and (if your policy says Block) a check for matches with public code of about 150 characters or more.
- Ghost text: accept or reject. The suggestion appears as grey ghost text. Tab accepts, Esc dismisses, Alt+] / Alt+[ cycle alternatives, Ctrl+→ accepts word by word. You decide what goes into your code.
- What data is kept. Prompts and suggestions are encrypted in transit. For Copilot Business and Enterprise, your code is not used to train GitHub’s models, and prompts and suggestions from IDE code completion are not retained after the response. Engagement data (accepts, rejects, errors) is kept to run and improve the service and to power usage metrics.
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
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
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.jstells the model you’re writing a test in JavaScript. - Related snippets from open tabs (neighbouring tabs). Having
src/discount.jsopen 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.
| Piece | Where it comes from |
|---|---|
| Your question | What you typed, plus any slash command such as /explain or /tests |
| Active file or selection | Included 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 history | Earlier 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
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.
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 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.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.Why does context have to be trimmed?Show
The model's context window is limited.
-
3.Which chat variable attaches the code you've highlighted?Show
#selection
-
4.Where do you see which files a chat answer used?Show
Used n references, under the answer.
-
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
Lesson done? Make it stick with a quick gameSort It, Match the Pairs, Lightning Round and Who Am I? Earn XP and badges.