Guides · Updated October 25, 2026

GitHub Copilot agent mode stuck in a loop: how to stop it and keep it from repeating

Copilot’s agent edits a file, runs the tests, gets the same failure, and edits the file again. Or it keeps rewriting one function back and forth. After a couple of dozen steps VS Code stops and asks whether you want it to keep going. This guide covers how to stop the loop, undo what it did, restart so it doesn’t walk straight back in, which settings bound it, and how to make the agent refuse an identical repeat with a hook.

Signs the agent is looping, not iterating

Edit, test, a different result, edit again: that’s normal work. It’s a loop when nothing changes between attempts, so the next attempt can’t turn out differently.

Stop it now

Undo what the loop changed

VS Code takes a checkpoint before each chat request (chat.checkpoints.enabled, on by default). Hover over an earlier request and choose Restore Checkpoint. That restores the files the agent changed and removes the later requests from the conversation, which is what you want after a loop: the failed attempts are gone from the context too. Redo brings them back if you went too far.

Restart without walking back into it

The settings that bound a loop

What a loop costs on Copilot

Copilot bills premium requests, not tokens. In agent mode, each prompt you send counts as one premium request multiplied by the model’s rate; the tool calls the agent makes on its own don’t add requests. So a loop inside one turn mostly costs time, but each “try again” you type, on a model with a high multiplier, eats into your allowance, and after that into paid usage.

To cap the money side, open github.com/settings/billing → Budgets and alerts, create a budget for Copilot premium requests (or a bundled premium requests budget), and tick Stop usage when budget limit is reached. Without that box ticked, a budget only sends emails. For organizations, the “Premium request paid usage” policy decides whether users can go past their allowance at all.

Why “stop after 3 tries” in your instructions doesn’t hold

A line in .github/copilot-instructions.md is context, not enforcement. In a long chat the model loses count, and it won’t notice that npm test and npx vitest run on the same file are the same attempt. A limit you can rely on has to run outside the model, on every tool call, and be able to refuse one. Copilot has that: agent hooks (in preview).

Make Copilot refuse an identical repeat with a hook

In the Copilot harness, a preToolUse hook receives the tool name and arguments as JSON on stdin and can answer {"permissionDecision": "deny"} with a reason the agent reads. Hooks load from .github/hooks/*.json in the repository or ~/.copilot/hooks/ for all your projects. Here’s a minimal repeat guard. Create .github/hooks/repeat-guard.json:

{
  "version": 1,
  "hooks": {
    "preToolUse": [
      { "type": "command", "command": "node .github/hooks/repeat-guard.cjs", "timeoutSec": 10 }
    ]
  }
}

and .github/hooks/repeat-guard.cjs (needs Node.js):

// Deny the 4th identical tool call within 10 minutes in the same session.
const fs = require('node:fs');
const os = require('node:os');
const path = require('node:path');
const crypto = require('node:crypto');

const MAX = 3, WINDOW_MS = 10 * 60 * 1000;
const ev = JSON.parse(fs.readFileSync(0, 'utf8'));
const key = crypto.createHash('sha1')
  .update(String(ev.toolName) + JSON.stringify(ev.toolArgs ?? null)).digest('hex');
const file = path.join(os.tmpdir(), 'copilot-repeat-' + String(ev.sessionId || 'x').replace(/\W/g, '') + '.json');

let seen = {};
try { seen = JSON.parse(fs.readFileSync(file, 'utf8')); } catch {}
const now = Date.now();
const hits = (seen[key] || []).filter(t => now - t < WINDOW_MS);

if (hits.length >= MAX) {
  process.stdout.write(JSON.stringify({
    permissionDecision: 'deny',
    permissionDecisionReason: 'Same tool call ' + hits.length + ' times in 10 minutes with no change. ' +
      'Stop rerunning it. Summarize the goal, what you tried and the exact error, then fix the root cause.',
  }));
} else {
  seen[key] = [...hits, now];
  fs.writeFileSync(file, JSON.stringify(seen));
  process.stdout.write('{}');
}

Start a new Copilot session so it picks up the hook, and test it on something harmless first. Things to know:

Where LoopGuard fits

LoopGuard is an open-source CLI that does this properly for Claude Code, Codex CLI and Cursor: it only counts repeats when nothing changed (it fingerprints the git working tree and each tool’s output), treats reworded commands as the same call, and adds per-session and daily dollar caps. It doesn’t install into Copilot yet, so for Copilot use the settings, budget and hook above. If you also run one of those agents, see how to stop runaway agent loops or Cursor agent stuck in a loop.

Related

LoopGuard on GitHub · Pricing