Sub topic 9
Give chat the right context
Copilot Chat answers from two sources: the model's general training and whatever context you put in front of it. General training knows how React hooks work. It does not know what your utils.ts contains. This chapter covers how to supply that context, how to check what was sent, and what goes wrong when the context is too thin or too broad.
This chapter assumes you have installed the extension, signed in, and opened the Chat view (earlier chapters). You should also be comfortable opening a folder as a workspace and using the Command Palette. Menu names and some features change between VS Code and Copilot releases, so check your version if something differs from what is described here.
Why context decides answer quality
A chat request is a prompt plus attached context. If you ask "Why does this fail?" with nothing attached, Copilot may guess or ask you to paste code. If you attach the failing file and the error text, it can answer about your code.
Two kinds of context reach the model:
- Implicit context. VS Code adds some things automatically, such as the active editor's file or your current selection. Which items are added depends on the mode, the version, and your settings.
- Explicit context. You add items yourself: files, folders, symbols, terminal output, or search results.
Explicit context is the one you control, so it is the more reliable. The rest of this chapter focuses on it.
Attach context with # references
Type # in the chat input to open a list of context variables. Pick one, or type its name. The prompt below attaches a specific file:
Explain what #file:utils.ts does and suggest one way to simplify it.Because the file is named in the prompt, the answer is based on its contents instead of on a guess from the function names.
Common references include the following. The exact list varies by version, so type # to see what your install offers.
| Reference | What it adds |
|---|---|
| `#file` | The contents of a file you choose |
| `#selection` | The text currently selected in the editor |
| `#codebase` | Relevant code found by searching the workspace |
| `#terminalLastCommand` | The last terminal command and its output |
| `#terminalSelection` | Text you selected in the terminal |
| `#problems` | Entries from the Problems panel |
You can combine references in one prompt:
Using #problems and #file:server.ts, explain why the build fails and propose a fix.Other ways to attach
The # syntax is not the only route:
- Use the Add Context (paperclip) button in the Chat view to pick files, symbols, or other items from a list.
- Drag a file from the Explorer into the chat input.
- Right-click a selection in the editor and choose a Copilot action. The selection is sent along with it.
- Attach an image, if your model and plan support it, such as a screenshot of an error dialog.
Attached items appear as chips above or inside the input. If a chip is there, it is part of the request. If it is not there, do not assume Copilot saw it.
Chat participants and slash commands
Two other prefixes shape a request.
@selects a chat participant, a specialist that handles a domain. For example,@workspaceis oriented toward questions about your whole project, and@terminaltoward shell questions. Available participants depend on your version and installed extensions. In recent releases some of this behavior has moved into#codebaseand agent mode, so treat participants as one option, not a requirement./selects a slash command, a shortcut for a common task such as/explain,/fix,/tests, or/doc. Type/to see the current list.
A slash command combined with a file reference is a compact, repeatable prompt:
/tests Write unit tests for #file:utils.tsChoosing between #file, #selection, and #codebase
Pick the narrowest reference that contains what the question needs.
- Question about one function: select it and use
#selection, or name the file with#file. - Question about how two or three files interact: attach each file explicitly.
- Question you cannot locate yourself, such as "Where do we validate email addresses?": use
#codebaseand let Copilot search.
#codebase is convenient, but it is a search, so it can miss the right file or return unrelated ones. Naming files yourself is more predictable. When an answer cites the wrong file, attach the right one explicitly and ask again.
Verify what Copilot used
After a response, look at the references list shown with the answer. It names the files and ranges that were included. Use it to check three things:
- The file you expected is listed.
- Unrelated files are not crowding the answer.
- The range covers the code you care about, not just the top of a long file.
If a file is missing, attach it explicitly. If a file you excluded from Copilot through content exclusion (covered in the privacy chapter) is missing, that is expected: excluded content is not sent.
Context limits
Every model has a context window, the maximum amount of text it can take in for one request, counting your prompt, attached files, conversation history, and the reply. Limits differ by model.
Practical effects:
- A very large file may be truncated or only partly included.
- A long conversation carries its history forward, which consumes space. Old, unrelated turns can also distract the model.
- Attaching many files at once can push out the details that matter.
Habits that help:
- Start a new chat when you change topics. Use the New Chat button in the Chat view.
- Attach a function or range, not an entire large file, when the question is narrow.
- Remove chips that no longer apply before sending the next message.
Persistent context: instructions and settings
Explicit references are per request. For rules that apply to every request in a project, use custom instructions, which the next chapter covers in detail. The file looks like this when saved as .github/copilot-instructions.md:
# Copilot instructions
- Use TypeScript with strict typing; avoid `any`.
- Write unit tests with Vitest.
- Prefer named exports over default exports.The split is practical. Put stable conventions in the instructions file. Put the task-specific material, such as which file to change, in the prompt with # references. Do not paste long conventions into every prompt, and do not put one-off file names in the instructions file.
Common mistakes
| Symptom | Likely cause | Fix |
|---|---|---|
| Answer describes code you do not have | Nothing relevant attached, so the model guessed | Attach the file with `#file` and repeat the question |
| Answer ignores a file you mentioned by name in plain text | Naming a file in prose does not attach it | Use `#file:name` or the Add Context button |
| `#codebase` returns unrelated results | Search matched on loose keywords | Attach the files yourself, or add more specific terms |
| Answers drift or contradict earlier turns | Long history from an unrelated topic | Start a new chat |
| Reference is not offered after typing `#` | Your version or mode does not provide it | Update VS Code and the extension, or use Add Context |
| Expected file is missing from the references list | File is excluded, too large, or was not found | Check content exclusion, then attach a smaller range |
Be specific about the task as well
Context supplies facts. The instruction still has to say what to do with them. Compare:
Look at #file:utils.ts.Review #file:utils.ts for functions that mutate their arguments, and list each one with a one-line fix.The second states the scope, the criterion, and the output shape. With the same context, it gives a far more checkable answer.
A worked sequence
Suppose a test fails and you do not know why.
- Run the test in the integrated terminal so the failure output is there.
- Open the Chat view and start a new chat.
- Send:
Why does this test fail? #terminalLastCommand #file:cart.test.ts #file:cart.ts - Read the references list and confirm both source files and the terminal output are included.
- If the explanation points to a helper in another file, attach that file and ask a follow-up.
- When the topic changes, start a new chat.
This keeps the context small, relevant, and visible.
Exercise
You ask chat "Why does parseDate return null?" and the answer describes a generic date parser that does not match your project. Name two likely causes and two concrete ways to fix the request.
Show answer
Likely causes: no file containing parseDate was attached, so the model guessed, or a long earlier conversation on another topic is distorting the answer. Fixes: attach the source with #file: (or select the function and use #selection) and include the failing input or error, for example "Why does parseDate return null for '2026-13-01'? #file:dates.ts". If the chat has unrelated history, start a new chat first. Then check the references list to confirm the file was included.
Exercise
A teammate says "I mentioned utils.ts in my prompt, so Copilot read it." Is that reliable? What should they do, and where can they confirm the file was used?
Show answer
It is not reliable. Writing a file name in plain text does not attach the file. They should use #file:utils.ts, the Add Context button, or drag the file into the chat input, and then check the chip in the input and the references list shown with the response.