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.
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.
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
}
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',
});
}
}
}
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}`,
});
}
}
});
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' });
}
});
A kill switch must be:
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.
When a session is terminated:
Explore more guides on AI agent safety, prompt injection, and building secure systems.
View All Guides