Giving a Coding Agent the Right Files and Context

JS
Jai Singh
20 Nov, 2026 4 min read 46 views

An agent works best when it sees the right code, no more and no less. Too little context and it invents; too much and it loses focus and burns budget. You control this by naming files, pointing at examples, supplying error output and letting the agent search for the rest. This post covers practical ways to do all four.

Name the files you already know

If you know where the change belongs, say so. Most tools also let you reference files directly. In Claude Code and in Cursor you can type @ followed by a path to attach a file to your message. GitHub Copilot chat has its own ways to add files as context. The exact syntax differs by tool, so check yours, but the principle is the same: attached files are placed straight into the conversation.

In @src/orders/checkout.ts the total is computed before the discount is
applied. Fix the order, and add a test in @src/orders/checkout.test.ts.

Point at a pattern to copy

The best context for "write a new X" is "look at an existing X". It shows your naming, error handling and structure more reliably than any written rule.

Add a new endpoint GET /api/invoices/:id following the same structure as
src/routes/customers.ts (route, service, schema, test). Reuse the
error helper in src/lib/http-errors.ts.

Let the agent search when you do not know

Agents can search the repository. A prompt like "find where password reset emails are sent" is a fine start. Searching costs steps but saves you from guessing wrong. To help it, use the vocabulary of the code: the function name, the error string, the route path.

# What the agent is effectively doing under the hood
grep -rn "resetPassword" src
grep -rn "Password reset" src --include=*.ts

Search finds exact words well. If the code uses "recover" rather than "reset", a good agent will try synonyms, but giving the right term saves time.

Share errors and outputs verbatim

When something fails, copy the actual message and the command that produced it. Do not paraphrase. Exact text such as TypeError: Cannot read properties of undefined (reading 'id') and the stack trace line numbers give the agent a precise place to start. Trim the output to the relevant part if it is very long.

Describe what the code cannot show

Some context lives outside the repository: why a strange workaround exists, which customers depend on a behaviour, what the ticket says. Put it in the prompt or, if it is permanent, in a comment or the instruction file.

Background: the 30-second timeout in src/http/client.ts is deliberate; the
partner API is slow on Mondays. Do not lower it. The bug is that retries
double-charge when the first attempt actually succeeded.

Do not over-attach

Attaching ten files "just in case" has costs. The model must read them all, relevant parts get diluted, and the context window fills. A good rule is to attach the file you want changed, one example to copy and one test file. Let the agent open more if it needs them.

Keep irrelevant and sensitive files out of reach

  • Exclude build output, vendored code and large data files from search where your tool supports ignore files.
  • Keep secrets such as .env files out of the project directory the agent can read, or block them with permissions. Later posts cover how.
  • Do not paste customer data into prompts to "show the shape". Use fake samples.

Context is a trade-off, not a maximum

It is tempting to believe that more context always helps. In practice, the model attends best to a focused set of facts. If you attach a large file when only one function matters, say which function. If a log is long, quote the failing lines and mention where the full log is, so the agent can read more only if needed. Over time you will develop a feel for the smallest set of material that lets the agent do the job, and that habit also makes sessions cheaper and faster.

A context checklist for each task

  1. Which file(s) will change?
  2. Which existing code shows the right pattern?
  3. Which test file covers this area?
  4. What exact error or behaviour prompted the task?
  5. What background would a new teammate not know?

Answer those five in your prompt, and most tasks start well. If the agent still goes the wrong way, it is often because one of them was missing.

Was this article helpful?

Comments

No comments yet. Have a question or a tip? Be the first to share it.

Leave a comment

Comments are reviewed before they appear.