Kill switch
A control that suspends an agent at once, with a confirmation that says what stops.
The control that an administrator reaches for when an agent misbehaves. It is easy to find and hard to press by accident: the person types the name of the agent to confirm. Try it in the preview.
Installation
- CLI
- Manual
The command adds the file to your project, with the shadcn/ui components that it uses. Your project needs shadcn/ui.
- pnpm
- npm
- yarn
- bun
pnpm dlx shadcn@latest add https://examples.lumoauth.dev/r/kill-switch.json
npx shadcn@latest add https://examples.lumoauth.dev/r/kill-switch.json
yarn dlx shadcn@latest add https://examples.lumoauth.dev/r/kill-switch.json
bunx --bun shadcn@latest add https://examples.lumoauth.dev/r/kill-switch.json
Add the shadcn/ui components that the pattern uses.
npx shadcn@latest add badge button card inputInstall the packages.
npm install lucide-reactCopy the code into your project.
components/kill-switch.tsx'use client';import * as React from 'react';import { OctagonX, Play, ShieldOff } from 'lucide-react';import { Badge } from '@/components/ui/badge';import { Button } from '@/components/ui/button';import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from '@/components/ui/card';import { Input } from '@/components/ui/input';import { cn } from '@/lib/utils';export interface KillSwitchAgent {id: string;name: string;status: 'active' | 'suspended' | 'revoked';/** Who suspended the agent, and when (ISO 8601). */suspendedBy?: string | null;suspendedAt?: string | null;}export interface KillSwitchProps {agent: KillSwitchAgent;/** Suspend the agent. Return a promise to show progress. */onSuspend?: (id: string) => void | Promise<void>;onResume?: (id: string) => void | Promise<void>;/** False when the person lacks the permission to suspend agents. */canManage?: boolean;/** What stops when the agent is suspended. Shown in the confirmation. */stops?: string[];className?: string;}const DEFAULT_STOPS = ['Every token that the agent holds stops working, also the ones that are still valid.','Requests that wait for approval are closed.','The agent loses its access to connected accounts.',];const STATUS = {active: 'border-transparent bg-emerald-500/15 text-emerald-700 dark:text-emerald-400',suspended: 'border-transparent bg-amber-500/15 text-amber-700 dark:text-amber-400',revoked: 'border-transparent bg-red-500/15 text-red-700 dark:text-red-400',};export function KillSwitch({ agent, onSuspend, onResume, canManage = true, stops = DEFAULT_STOPS, className }: KillSwitchProps) {const [confirming, setConfirming] = React.useState(false);const [typed, setTyped] = React.useState('');const [busy, setBusy] = React.useState(false);const inputId = React.useId();const matches = typed.trim() === agent.name;const run = async (action?: (id: string) => void | Promise<void>) => {setBusy(true);try {await action?.(agent.id);setConfirming(false);setTyped('');} finally {setBusy(false);}};const when = agent.suspendedAt ? new Date(agent.suspendedAt).toLocaleString([], { dateStyle: 'medium', timeStyle: 'short' }) : null;return (<Card className={cn('w-full max-w-md gap-4', className)}><CardHeader><div className="flex items-center justify-between gap-3"><CardTitle className="truncate text-base">{agent.name}</CardTitle><Badge className={cn('capitalize', STATUS[agent.status])}>{agent.status}</Badge></div><CardDescription suppressHydrationWarning>{agent.status === 'active' && 'The agent is running. Suspend it to stop everything that it does, at once.'}{agent.status === 'suspended' &&`Suspended${agent.suspendedBy ? ` by ${agent.suspendedBy}` : ''}${when ? ` on ${when}` : ''}. The agent cannot get a token or use one.`}{agent.status === 'revoked' && 'Revoked. The agent cannot be resumed. Register a new agent to replace it.'}</CardDescription></CardHeader>{confirming && agent.status === 'active' && (<CardContent className="grid gap-3 text-sm"><div className="rounded-md border border-red-500/30 bg-red-500/5 p-3"><p className="font-medium">Suspending {agent.name} stops this now:</p><ul className="mt-2 grid list-disc gap-1 pl-5 text-muted-foreground">{stops.map((line) => (<li key={line}>{line}</li>))}</ul></div><label htmlFor={inputId} className="grid gap-1.5"><span>Type <span className="font-medium">{agent.name}</span> to confirm</span><Input id={inputId} value={typed} onChange={(event) => setTyped(event.target.value)} autoComplete="off" /></label></CardContent>)}<CardFooter className="flex-wrap gap-2">{!canManage ? (<p className="flex items-center gap-2 text-sm text-muted-foreground"><ShieldOff className="size-4" aria-hidden="true" />You do not have the permission to suspend agents.</p>) : agent.status === 'active' && !confirming ? (<Button variant="destructive" onClick={() => setConfirming(true)}><OctagonX aria-hidden="true" />Suspend agent</Button>) : agent.status === 'active' ? (<><Button variant="destructive" disabled={!matches || busy} onClick={() => run(onSuspend)}><OctagonX aria-hidden="true" />{busy ? 'Suspending…' : 'Suspend now'}</Button><Button variant="ghost" disabled={busy} onClick={() => { setConfirming(false); setTyped(''); }}>Cancel</Button></>) : agent.status === 'suspended' ? (<Button variant="outline" disabled={busy} onClick={() => run(onResume)}><Play aria-hidden="true" />{busy ? 'Resuming…' : 'Resume agent'}</Button>) : null}</CardFooter></Card>);}Change the import paths if your project keeps its components in another place.
Usage
Give the control the agent and two callbacks.
'use client';
import { KillSwitch, type KillSwitchAgent } from '@/components/kill-switch';
export function AgentControl({ agent, canManage }: { agent: KillSwitchAgent; canManage: boolean }) {
const post = (action: 'suspend' | 'resume') => async (id: string) => {
await fetch(`/api/agents/${id}/${action}`, { method: 'POST' });
};
return <KillSwitch agent={agent} canManage={canManage} onSuspend={post('suspend')} onResume={post('resume')} />;
}
Your API route changes the state of the agent through the Admin API, with a credential that may manage agents. Check the permission of the person in that route too: canManage only hides the button.
Where the data comes from
| What the pattern shows | Call |
|---|---|
| Suspend, resume and revoke | Admin API, agents |
| Tell your services at once | The webhook agent.revoked, or a session-revoked security event |
| Whether the person may suspend | A permission check for the signed-in person |
States
- Suspended, with who did it and when, and a button to resume.
- Revoked. It cannot be resumed.
- No permission. The person sees the state and no button.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
agentrequired | KillSwitchAgent | The agent: id, name, status, and suspendedBy and suspendedAt for a suspended agent. | |
onSuspend | (id: string) => void | Promise<void> | Called after the person confirmed. | |
onResume | (id: string) => void | Promise<void> | Called for a suspended agent. | |
canManage | boolean | true | False when the person lacks the permission to suspend agents. |
stops | string[] | What stops when the agent is suspended. The confirmation lists it. | |
className | string | Class name for the card. |