Sub topic 4
Giving the agent the right context
This chapter assumes you have used chat in VS Code, know the difference between ask, edit, and agent modes, and understand the agent loop from the earlier chapters. It covers how to control what the agent sees before and during a task.
Why context decides the result
The agent works from a limited window of text. That window holds your prompt, the files and snippets you attach, the instructions files it loads, and the output of every tool call so far. If the right information is not in the window, the agent guesses. If the window fills with irrelevant material, the agent loses track of what matters.
Your goal is to put the necessary context in and keep the rest out.
Where context comes from
Context reaches the agent in three ways.
| Source | How it gets in | Typical use |
|---|---|---|
| Attachments | You add them to the prompt | Files, selections, folders, and images you already know matter |
| Editor state | The active file and selection are often included automatically | Questions about the code you are looking at |
| Agent searches | The agent calls search and read tools itself | Finding callers, tests, and related files you did not name |
Exact controls vary by version. In VS Code, the chat input has an attach button, and you can type # to reference items such as #file, #selection, #codebase, and #problems. Check the autocomplete list in your own build, because names change between releases.
Attaching files and selections
Attach a file when you know the agent must read it. Attach a selection when only one function or block is relevant. A selection is cheaper than a whole file and tells the agent exactly where to look.
Fix the off-by-one error in #selection.
Compare it with how pagination is done in #file:src/orders.ts.Attach files that define the pattern you want copied, not only the file you want changed. A sample test, a similar component, or a type definition often improves the result more than extra explanation.
Letting the agent search
For a task that touches code you cannot list in advance, let the agent find it. Workspace search tools match by name, text, or meaning, and the agent reads the hits. You can help by naming terms that appear in the code.
- Weak: "Fix the login bug."
- Better: "Users stay logged in after
logout()insrc/auth/session.ts. Find where the session token is cleared and fix it."
The second prompt gives search terms and a starting file, so the agent spends its calls on the problem instead of exploring.
Keeping the window clean
Long sessions degrade. Old tool output, abandoned approaches, and corrected mistakes all stay in the conversation. Common symptoms are the agent repeating a rejected idea, forgetting a constraint you stated earlier, or editing the wrong file.
Practical habits:
- Start a new chat for each distinct task.
- Restate hard constraints in the new prompt instead of relying on history.
- Remove attachments that no longer apply.
- Put permanent rules in an instructions file (covered in the next chapter) so they load every time.
Common errors
- Attaching too much. Dumping a whole folder buries the relevant lines. Prefer specific files.
- Assuming the agent saw unsaved or unopened work. Save files, and attach anything outside the workspace explicitly.
- Referring to earlier chat vaguely. "Do it like before" fails after a reset. Name the file or pattern.
- Stale context. If you edited files yourself mid-task, tell the agent, because it may reason from an older read.
Exercise
You ask the agent to add a field to an API response. It edits the handler but ignores your existing response-type definitions and invents a new type. Name two context changes that would likely fix this.
Show answer
Attach the file that holds the existing response types, or name it in the prompt, so the agent reads it. Also include a similar endpoint as a pattern to follow, or add a rule to the instructions file saying to reuse types from the shared types file. Starting a fresh chat with these attached also helps if the earlier conversation drifted.