← Back to Learn
agent-safetybest-practicesmonitoring

AI agent kill switch patterns

Authensor

A kill switch is a mechanism that immediately terminates an AI agent's session, stopping all in-progress and future actions. Every production AI agent deployment should have a kill switch. This guide covers implementation patterns.

Why kill switches are necessary

When an agent goes wrong in production, you need to stop it fast. A prompt injection that changes the agent's goal, a cascading failure in a multi-agent system, or an unexpected interaction with a new tool all require immediate intervention.

Without a kill switch, you are limited to waiting for the agent to finish or restarting the entire service. Both take too long when seconds matter.

Pattern 1: Manual kill switch

An operator triggers the kill switch through a dashboard or API:

// Kill switch API endpoint
app.post('/api/sessions/:id/kill', requireRole('admin'), async (c) => {
  const sessionId = c.req.param('id');
  await sessions.terminate(sessionId, {
    reason: 'Manual kill switch triggered',
    operator: c.get('user').id,
  });
  return c.json({ status: 'terminated', sessionId });
});

The agent's guard function checks session status before every tool call:

function guard(tool: string, args: unknown) {
  if (session.isTerminated()) {
    return { action: 'block', reason: 'Session terminated' };
  }
  // Normal evaluation
}

Pattern 2: Automatic kill switch

Sentinel triggers the kill switch automatically when anomalies exceed a threshold:

sentinel: {
  onAlert: (alert) => {
    if (alert.severity === 'critical') {
      sessions.terminate(alert.sessionId, {
        reason: `Auto-kill: ${alert.metric} anomaly`,
        trigger: 'sentinel',
      });
    }
  }
}

Pattern 3: Budget kill switch

Terminate the session when it exceeds a spending or action budget:

const guard = createGuard({
  policy,
  limits: {
    maxActions: 500,
    maxCost: 100.00,
    onExceeded: (limit) => {
      sessions.terminate(session.id, {
        reason: `Budget exceeded: ${limit.type}`,
      });
    }
  }
});

Pattern 4: Time-based kill switch

Sessions should not run forever. Set a maximum session duration:

const session = createSession({
  maxDuration: 30 * 60 * 1000,  // 30 minutes
  onTimeout: () => {
    sessions.terminate(session.id, { reason: 'Session timeout' });
  }
});

Implementation requirements

A kill switch must be:

  • Fast: Takes effect within one tool call cycle
  • Reliable: Works even if the agent is misbehaving
  • Logged: Records who triggered it and why
  • Non-bypassable: The agent cannot override or ignore it

The kill switch runs in the guard function, which is outside the agent's control. The agent cannot skip the guard check because it is the mechanism through which all tool calls are made.

After the kill switch

When a session is terminated:

  1. Block all pending and future tool calls for that session
  2. Cancel any pending approval workflows
  3. Log the termination with reason and trigger
  4. Notify the operator
  5. Preserve the receipt chain for post-incident analysis

Keep learning

Explore more guides on AI agent safety, prompt injection, and building secure systems.

View All Guides