How to stop an agent in seconds and put its memory and config back
Put one flag file in the harness that every loop reads before each tool call, make credential and tool revocation one command each, and snapshot memory and config with hashes. In the run below, the flag alone stopped an agent in 0.81 s but a background job it had started ran 6.6 s longer; the full kill command canceled the job within 0.1 s. Restoring a snapshot removed a planted memory entry and a changed config value. Eight tests fail on an agent with no flag check (three of them because the off setup has no cancel, snapshot or restore command) and pass with the switch wired in.
Updated 29 Nov 2026
What this control stops
The kill switch limits how long a bad agent run lasts; rollback limits what it leaves behind. It addresses:
- Excessive agency: OWASP LLM Top 10 LLM06:2025 Excessive Agency and LLM03:2026 Excessive Agency.
- Memory and context poisoning: OWASP Agentic AI Threats and Mitigations T1 Memory Poisoning, OWASP Agentic Top 10 ASI06 Memory & Context Poisoning, MITRE ATLAS AML.T0080 AI Agent Context Poisoning.
- Cascading failures: ASI08 Cascading Failures, T5 Cascading Hallucination Attacks.
- Rogue agents: ASI10 Rogue Agents, T13 Rogue Agents in Multi-Agent Systems.
- Agent communication poisoning: ASI07 Insecure Inter-Agent Communication, T12 Agent Communication Poisoning.
- Tool and supply chain compromise: ASI04 Agentic Supply Chain Vulnerabilities, T17 Supply Chain Compromise, ATLAS AML.T0110 AI Agent Tool Poisoning.
- Resource overload: T4 Resource Overload, LLM10:2025 Unbounded Consumption, ATLAS AML.T0034.002 Agentic Resource Consumption.
The frameworks name three parts. OWASP Securing Agentic Applications V2, section 3.3.8, says "Detection without an automated response at agent speed is \"just expensive logging,\"" and that the switch "operates in graduated tiers (warn → limit → suspend → kill)".1 ASI04 asks you to "Implement emergency revocation mechanisms that can instantly disable specific tools, prompts, or agent connections across all deployments when a compromise is detected".2 The OWASP third-party MCP guide says "Automate rollback procedures in case of regression or compromise."3
The setup
Files are in the run directory. Python 3.14.4, standard library only (the live-model run adds boto3 1.40.72). Three agent processes (a1, a2, a3) loop over tool calls against a local tool server. Each agent starts a 10-second background job on the server at its third iteration and writes a note to a shared memory.json every fourth iteration.
1. The flag. One JSON file, read at every loop iteration, editable without a deploy. Scope is global or per agent. The tiers are run, warn, limit, suspend, kill, and the most severe of the global and agent values wins. An unreadable file means kill:
ORDER = ["run", "warn", "limit", "suspend", "kill"]
def state(agent_id):
try: f = json.loads((HERE / "killswitch.json").read_text())
except (FileNotFoundError, json.JSONDecodeError): return "kill" # unreadable flag = fail closed
a, g = f.get("agents", {}).get(agent_id, "run"), f.get("global", "run")
return max(a, g, key=ORDER.index)
The loop checks it before each tool call. limit sleeps 2 seconds per call, suspend waits, kill exits:
s = "run" if NOKILL else killswitch.state(AGENT)
if s == "kill": print(f"{AGENT} kill flag seen, exiting", flush=True); sys.exit(0)
while s == "suspend": time.sleep(0.1); s = killswitch.state(AGENT)
if s == "limit": time.sleep(2)
2. One command to stop, revoke and cancel. ops.py kill AGENT cancels the agent's background job while its credential still works, sets the flag, then revokes the credential. Order matters: the cancel needs the credential, and revocation catches an agent that ignores the flag. Other commands:
python ops.py kill AGENT|global cancel its background job, set the kill flag, revoke the credential(s)
python ops.py revoke AGENT credential only
python ops.py revoke-tool TOOL disable a tool for every agent, no deploy
python ops.py snapshot | restore NAME
This is the usage text from the top of ops.py. ops.py has no process-group kill; if a process outlives the grace period, the supervisor has to kill it (the test harness kills process groups in its own teardown).
The tool server checks on every request that the credential is not revoked and that the tool is enabled in tools.json. That second check is what lets one edit revoke a tool for every agent.
3. Snapshots and restore. snapshot copies memory.json and config.json and writes their hashes to a manifest; restore refuses if a hash no longer matches.
The test that fails with the control off
MODE=off is an agent loop with no flag check (NOKILL=1) against a tool server with no revocation or tool-enabled checks (NOCHECK=1), with no job-cancel, snapshot or restore commands. MODE=on is the setup above. Timings are measured from the moment the operator command returns.
Control off (test-off.log, 29 Nov 2026; the failure details are in the log):
$ MODE=off python -W ignore -m unittest -v test_killswitch
# 2026-11-29 Python 3.14.4 Linux 7.0
test_1_kill_one_agent_stops_it_in_seconds_and_leaves_the_others (test_killswitch.T.test_1_kill_one_agent_stops_it_in_seconds_and_leaves_the_others) ... FAIL
test_2_global_kill_stops_every_agent (test_killswitch.T.test_2_global_kill_stops_every_agent) ... FAIL
test_3_revoked_credential_is_rejected_on_the_next_call (test_killswitch.T.test_3_revoked_credential_is_rejected_on_the_next_call) ... FAIL
test_4_revoking_a_tool_stops_it_for_every_agent_and_leaves_other_tools (test_killswitch.T.test_4_revoking_a_tool_stops_it_for_every_agent_and_leaves_other_tools) ... FAIL
test_5_background_job_started_by_the_agent_is_cancelled_too (test_killswitch.T.test_5_background_job_started_by_the_agent_is_cancelled_too) ... FAIL
test_6_restore_removes_a_planted_memory_entry_and_config_change (test_killswitch.T.test_6_restore_removes_a_planted_memory_entry_and_config_change) ... FAIL
test_7_a_tampered_snapshot_is_refused (test_killswitch.T.test_7_a_tampered_snapshot_is_refused) ... FAIL
test_8_tiers_limit_slows_and_suspend_pauses (test_killswitch.T.test_8_tiers_limit_slows_and_suspend_pauses) ... FAIL
[... 121 lines omitted: failure details ...]
Ran 8 tests in 50.841s
FAILED (failures=8)
# a1: last tool call +6.30 s after kill command, process exit after 5.05 s
# all agents exited after 5.05 s
# a2: 4 calls started after revoke succeeded; 1 call(s) already in flight finished
# calls in 2 s windows: run 7, limit 13 per 4 s, suspend 5, run again 6
In this mode the only way to stop a1 was killing its process from outside. Three of the eight failures (tests 5 to 7) are by construction: the off setup has no cancel, snapshot or restore command, so those tests report that ("no job-cancel command in this setup", "no snapshot or restore command in this setup"). The other five show behavior of the off setup: a1 kept running 5 s after the flag, and 4 calls with a revoked credential and 3 send_email calls after tool revocation succeeded. Compare the last lines of test-off.log and test-on.log: in off mode the agents were still running when the 5 s wait ended (the log prints 5.05 s), and in on mode all exited after 0.41 s. Under limit, the off agents made 13 calls in the window and the on agents made 2. In the off log, a1 shows "last tool call +6.30 s after kill command, process exit after 5.05 s" because the test waits at most 5 s for the process to exit and then reads the log 1.5 s later: a1 had not exited, so it was still making calls, and 5.05 s is the end of that wait, not an exit time.
Control on (test-on.log):
$ MODE=on python -W ignore -m unittest -v test_killswitch
# 2026-11-29 Python 3.14.4 Linux 7.0
test_1_kill_one_agent_stops_it_in_seconds_and_leaves_the_others (test_killswitch.T.test_1_kill_one_agent_stops_it_in_seconds_and_leaves_the_others) ... ok
test_2_global_kill_stops_every_agent (test_killswitch.T.test_2_global_kill_stops_every_agent) ... ok
test_3_revoked_credential_is_rejected_on_the_next_call (test_killswitch.T.test_3_revoked_credential_is_rejected_on_the_next_call) ... ok
test_4_revoking_a_tool_stops_it_for_every_agent_and_leaves_other_tools (test_killswitch.T.test_4_revoking_a_tool_stops_it_for_every_agent_and_leaves_other_tools) ... ok
test_5_background_job_started_by_the_agent_is_cancelled_too (test_killswitch.T.test_5_background_job_started_by_the_agent_is_cancelled_too) ... ok
test_6_restore_removes_a_planted_memory_entry_and_config_change (test_killswitch.T.test_6_restore_removes_a_planted_memory_entry_and_config_change) ... ok
test_7_a_tampered_snapshot_is_refused (test_killswitch.T.test_7_a_tampered_snapshot_is_refused) ... ok
test_8_tiers_limit_slows_and_suspend_pauses (test_killswitch.T.test_8_tiers_limit_slows_and_suspend_pauses) ... ok
----------------------------------------------------------------------
Ran 8 tests in 43.177s
OK
# a1: last tool call +0.16 s after kill command, process exit after 0.39 s
# all agents exited after 0.41 s
# a2: 0 calls started after revoke succeeded; 1 call(s) already in flight finished
# a1 background job: last heartbeat -0.05 s after kill command
# calls in 2 s windows: run 7, limit 2 per 4 s, suspend 0, run again 6
Test 1 also requires that a2 and a3 keep calling after a1 is killed; they did. These timings are one machine and a local file; a flag in a networked store adds its propagation delay, and each agent loop step here takes about 0.25 s.
What stayed running
The drill (drill.log) compares setting only the flag with the full command, on two agents that each started a 10-second background job:
$ python drill.py
# 2026-11-29 Python 3.14.4 Linux 7.0
step agent exit last tool call job heartbeats after kill
flag only (a1) 0.81 s +0.58 s 66 (job ran 6.6 s more)
ops.py kill (a2) 0.24 s +0.02 s 1 (job ran 0.1 s more)
a3 (untouched) still calling: True
- Work the agent already started. With the flag alone, the agent process exited in 0.81 s, but the job it had started on the tool server kept running for 6.6 more seconds. The loop was stopped; its side effects were not. The full command cancels the job: one heartbeat after the command, 0.1 s of work.
- Calls in flight. A tool call that began before the stop finishes. In test 3, one call that had already started when the credential was revoked completed; none that started afterward did. Keep tool calls short, and for long ones give the tool server a cancel path.
- Anything the flag does not reach. The flag only stops loops that read it. A different process, a scheduled trigger or another agent instance keeps going. If a process outlives the grace period, the supervisor has to kill its process group; the test harness does this in teardown, which is why test 1 off still ends.
With a real model in the loop
agent_llm.py runs a hosted model (temperature 0) on a task designed to run long: search topics 1 to 400 one at a time, each tool call taking 1 second. After 6 seconds an operator process runs ops.py kill a1. The loop checks the flag before every model call and before every tool call (agent-llm.log):
$ python agent_llm.py
# 2026-11-29 Python 3.14.4 boto3 1.40.72
tool calls made before the stop: 4
last tool call: +0.39 s relative to kill command; loop exit: +0.39 s; reason: kill flag seen before model call
The one call in flight when the command ran finished 0.39 s later, and the loop exited at the next check. Run environment: model us.anthropic.claude-haiku-4-5-20251001-v1:0 on Amazon Bedrock, temperature 0, 29 Nov 2026. The delay includes at most one model call in flight, because the flag is read before sending the next request; with a slower model or a longer tool call the delay is that long.
The same revocation on AWS (configuration, not run here)
This run cannot create IAM roles. The cloud form of "revoke the credential" is documented by AWS: revoking a role's sessions attaches an inline policy that denies all actions, with a condition that applies only to credentials issued before the revocation time, so everything issued earlier becomes invalid and new sessions are unaffected.4 For a kill switch that matters: an agent that can still call sts:AssumeRole gets fresh, valid credentials about 30 seconds later, so also remove its ability to assume the role (change the trust policy or deny sts:AssumeRole to the principal), or the revocation lasts only until the next assume. AWS notes the deny covers sessions "approximately 30 seconds into the future" to allow for propagation.4 That is a platform delay your measured stop time has to include.
Evidence an auditor accepts
- The runbook with named owners for the flag, revocation and restore, and the commands above in the repository.
- A drill record with timings, like
drill.logandtest-on.log: time from command to last tool call, to process exit, to the last background-job heartbeat, plus what kept running. - An append-only change history of the flag file or store, showing who set which state and when (in this run,
last_changein the flag file records only who made the latest change and what it set; it keeps no time and no earlier changes, so a real store needs an append-only change log). - Snapshot and restore test output: the planted entry present before, absent after, the hash check passing, and a tampered snapshot refused.
Where this control stops working
- A restore is only as good as the snapshot you pick. Test 6 restores a snapshot taken before the planted entry, so you need to know when the bad entry arrived. That comes from action logging and reconstruction. Hygiene that keeps bad entries out of memory in the first place is memory hygiene.
- Rollback covers state you snapshot. Emails sent, money moved and data deleted in other systems are not restored by a file copy. Gate those beforehand with human approval gates, and keep the credential scoped and short-lived so revocation has little left to revoke.
- Detection comes first. The switch is only used if something notices. Per-agent call, spend and loop limits are rate, cost and loop limits; they can set the
limitorkillstate automatically. - Revocation outside your control. Revoking a tool you use from a third-party server stops your calls, not the server. Pinning and reviewing tools is pin and review tools, MCP servers and packages.
- Who may pull it. The flag file is a high-value target: an attacker who can write
runback overkillre-enables the agent. Restrict write access and log every change. - Timings are local. A multi-region deployment, cached credentials or a gateway in front of the tool server each add delay. Repeat the drill in your environment and record the numbers.
Sources
- OWASP Securing Agentic Applications V2 (v2.0, October 2026), section 3.3.8 Behavioral Circuit Breakers / Kill Switches, https://genai.owasp.org/download/58849/?tmstv=1794197547, read 29 Nov 2026
- OWASP Top 10 for Agentic Applications 2026, ASI04 Agentic Supply Chain Vulnerabilities, Prevention and Mitigation Guidelines, https://genai.owasp.org/wp-content/uploads/2026/11/OWASP-Top-10-for-Agentic-Applications-2026-12.6-1-1.pdf, read 29 Nov 2026
- OWASP A Practical Guide for Securely Using Third-Party MCP Servers (v1.0, 23 Oct 2025), Discovery & Connection: Verification, https://genai.owasp.org/download/51928/?tmstv=1762283701, read 29 Nov 2026
- Revoke IAM role temporary security credentials, AWS Identity and Access Management User Guide, AWS, https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_use_revoke-sessions.html, read 29 Nov 2026