Sub topic 14
Troubleshooting and Known Limits
This chapter assumes you have used agent mode in VS Code or a similar editor. It also assumes you know basic Git, can run build and test commands in a terminal, and have read the earlier chapters on the agent loop, context, tool approval, and model selection. Here you will learn to recognize common failure modes, find their causes, and decide when to fix a problem and when to stop and start over.
A Working Model for Diagnosis
Agent failures usually come from one of four places. Sorting a problem into one of them tells you where to look.
| Layer | What goes wrong | Typical symptom |
|---|---|---|
| Context | The agent lacks or loses information | It ignores a convention or edits the wrong file |
| Tools | A tool call fails or is blocked | Errors in the chat, a command that never runs |
| Model | The model reasons poorly or hits a limit | Loops, partial work, wrong claims of success |
| Task | The request is vague or too large | Runaway edits, unrelated changes |
Ask first: did the agent have the information it needed, and could it act on that information? Most problems are answered by one of those two questions.
Lost Context
What it looks like
Early in a long session the agent follows your rules. Later it forgets them. It may reintroduce a default export after you said to use named exports. It may re-ask for information you already gave. It may also contradict an earlier decision.
Why it happens
A model can only read a limited amount of text at once. This limit is the context window. Your messages, attached files, tool output, and the agent's own replies all take up space. When the conversation grows past the limit, the editor shortens it. It may summarize older turns or drop them. Details that mattered can disappear. Large tool outputs, such as a full test log or a big file read, use space quickly.
The exact limit and the shortening behavior depend on the model and the editor version. Check the documentation for your tool instead of assuming a number.
What to do
- Put stable rules in an instructions file rather than in the chat. Instructions files are sent with each request, so they are less likely to be lost than a message from an hour ago.
- Start a new chat session when the task changes or the session has grown long. Carry over only a short summary of the decisions made.
- Attach specific files instead of asking the agent to find everything itself.
- Keep tool output small. Ask for a single failing test rather than the whole suite when you are debugging one case.
# Project instructions
- This is a TypeScript project using npm. Install with `npm ci`.
- Run tests with `npm test` before finishing any change.
- Use named exports. Do not add default exports.
- Keep new code under `src/` and tests next to the file as `*.test.ts`.
- Do not edit files under `dist/` or `package-lock.json` by hand.If the agent still ignores a rule that is in the file, check that the file is in the location your editor reads. For VS Code with GitHub Copilot, a repository-level file is .github/copilot-instructions.md. Other editors use different names and paths, so confirm this in their documentation.
Runaway Edits
What it looks like
You asked for a small change. The agent touched ten files, renamed functions, reformatted code, or deleted tests to make a build pass.
Why it happens
The request left room for interpretation, or the agent found that fixing one error exposed another and kept going. A failing test is a strong signal for the agent to "make it pass", and deleting or weakening the test is one way to do that.
What to do
- Stop the session as soon as you see edits outside the area you expected. Do not wait for it to finish.
- Review the diff before accepting anything. Check for removed tests, changed assertions, and edits to generated or lock files.
- Undo what you do not want. Use the editor's undo or discard controls for agent edits, or use Git to restore files. A commit before you start gives you a clean point to return to.
- Restate the task with limits. Name the files in scope and state what must not change.
Add input validation to the createUser function in src/users.ts.
Reject empty names and malformed email addresses with a clear error.
Add unit tests for both cases next to the existing tests, then run `npm test` and fix any failures.
Do not change the public function signature.This prompt works because it gives a goal, a location, acceptance criteria, a check the agent can run itself, and one firm constraint. If the agent still strays, add a line such as "Do not modify existing tests."
Failing Tool Calls
Terminal commands
Common causes when a command fails or never finishes:
- The command waits for input. Interactive prompts, such as a confirmation question or a password, can leave the agent waiting. Use non-interactive flags where they exist.
- Long-running processes. A dev server or a test watcher does not exit. The agent cannot tell when it is "done". Run one-shot commands, for example a single test run instead of watch mode.
- Wrong environment. The terminal the agent uses may have a different working directory, shell, or environment variables than the one you use. Run
pwdand check the tool versions in that terminal. - Denied or skipped approval. If you decline a command, or approval is waiting and you did not notice it, the agent cannot proceed. Look for a pending confirmation in the chat.
File edits
An edit can fail when the file changed after the agent read it, when the file is read-only, or when the agent's proposed change does not match the current text. In these cases ask the agent to re-read the file and try again. Save any unsaved edits in the editor first, because the agent and you would otherwise work on different versions of the file.
MCP servers
An MCP server that does not start gives the agent no tools from that server. Check these points in order:
- The command is installed. For the example below,
npxneeds Node.js on your PATH. - The JSON is valid and in the file your editor reads. In VS Code that is
.vscode/mcp.jsonfor a workspace. - The server has been started. Editors often show a start or restart control and a log of the server's output.
- Paths and variables resolve.
${workspaceFolder}must expand to the folder you intend. - You have approved the server's tools if the editor asks for it.
{
"servers": {
"filesystem": {
"type": "stdio",
"command": "npx",
"args": [
"-y",
"@modelcontextprotocol/server-filesystem",
"${workspaceFolder}"
]
}
}
}Many tools in the tool list is also a problem. Each tool description uses context, and a long list can make the model pick the wrong tool. Enable only the servers and tools the current task needs.
Model-Related Problems
| Problem | Likely cause | Response |
|---|---|---|
| The agent says the work is done, but tests fail | It did not run the tests, or it misread the output | Ask it to run the command and quote the result. Then check yourself. |
| The same fix is tried repeatedly | The agent is stuck in a loop on one error | Stop, give a new hint or a different approach, or start a new session |
| Code uses an API that does not exist | The model guessed from patterns in its training data | Attach the library documentation or the type definitions |
| The agent stops partway | A limit on steps, tokens, or requests was reached | Continue with a short follow-up that names the remaining steps |
| Weak results on a hard task | The model is not suited to long multi-step work | Switch to a model recommended for agent work in your editor |
A claim of success is not evidence. The reliable signals are a passing test run that you can see, a clean build, and a diff you have read.
Known Limits
These limits apply to agent mode in general. The details vary by editor, model, and version, so treat them as things to check, not as fixed facts.
- Bounded context. The agent cannot hold an entire large repository in view. It reads parts of it through search and file reads, and it can miss files it never opened.
- Limited steps and requests. Editors may cap the number of tool calls in one turn and may apply usage quotas to your plan. When a cap is reached, the agent pauses or stops.
- No guarantee of correctness. Output can be plausible and wrong. Generated code needs the same review as code from a colleague.
- Non-deterministic behavior. The same prompt can produce different results on different runs.
- Weak spots outside the workspace. Files outside the opened folder, remote machines, and systems the agent has no tool for are out of reach unless you add a tool.
- Differences between editors. Instructions file names, approval settings, and checkpoint features differ between VS Code, JetBrains IDEs, Cursor, and others. A habit that works in one editor may need a different setting in another.
A Short Recovery Routine
When a session goes wrong and the cause is not clear, use the same steps each time.
- Stop the agent.
- Read the last few tool calls and their output.
- Check the diff and decide what to keep. Discard the rest with undo or Git.
- Write down in one or two lines what you learned, for example "tests must run with
npm test -- --run". - Add anything lasting to the instructions file.
- Start a new session with a smaller, clearer task.
Moving lessons into the instructions file is what turns a one-time fix into a lasting improvement.
Exercise
Your agent follows the rule "use named exports" for the first hour of a session, then starts adding default exports. The rule is only in an early chat message. Name the likely cause and two fixes.
Show answer
The early message has probably been summarized or dropped as the conversation outgrew the context window. Fixes: move the rule into the repository instructions file so it is sent with each request, and start a new session for the next task instead of continuing a very long one.
Exercise
The agent runs npm test --watch and the session never moves on. Why does this happen, and what should you ask it to do instead?
Show answer
Watch mode never exits, so the agent cannot tell the command has finished. Ask it to run a single, non-interactive test run, such as npm test, which ends and returns its output.
Exercise
The agent reports "All tests pass" after a change, but your own run fails. List the steps you take before trusting the agent again.
Show answer
Stop the agent and read its tool calls to see whether it actually ran the tests. Check the diff for deleted or weakened tests. Ask it to run the command and quote the real output. Confirm by running the tests yourself, and add the exact test command to the instructions file.