Short answer
Keep a stop flag outside the agent, in a database or cache every worker can read, and check it before every model call and again right before every action that sends, pays or changes something. Checking only at the top of the loop lets one more action through after the stop. For a hard stop, also revoke the key the agent uses.
Every team running agents meets this sooner or later. A run goes wrong in front of you, or a customer is handed to a person, and you need the agent to stop now. Not at the end of the run, not after it finishes its current thought. This guide shows why the obvious stop lets one more action through, and how to build one that doesn't.
Why one more action slips out
Most stop flags are checked once, at the top of the agent's loop. Then the model thinks, which takes seconds, and then the agent acts. If the stop arrives while the model is thinking, nothing looks at it again before the action.
We tested this on 5 December 2026 with a simple agent that thinks for a moment and then sends a message, stopped by a person partway through. With the check only at the top of the loop, one more message went out after the stop, in all three runs. With the check repeated right before the send, none did.
The fix: a stop flag outside the agent, checked before every action
Keep the flag where every worker can read it: your database, Redis, or anything shared. A variable in memory only stops the process that holds it.
import sqlite3
class Stopped(Exception):
pass
db = sqlite3.connect("control.db", timeout=10) # use your shared database or Redis
db.execute("CREATE TABLE IF NOT EXISTS stops (run_id TEXT PRIMARY KEY, reason TEXT)")
db.commit()
def stop_run(run_id, reason):
"""Call this from anywhere: an admin page, a CLI, an alert, a handoff."""
with db:
db.execute("INSERT OR REPLACE INTO stops VALUES (?, ?)", (run_id, reason))
def check_not_stopped(run_id):
row = db.execute("SELECT reason FROM stops WHERE run_id = ?", (run_id,)).fetchone()
if row:
raise Stopped(f"run {run_id} stopped: {row[0]}")Then check it in two places: before each model call, and inside each function that sends, pays or changes something.
def send_message(run_id, to, text, send):
check_not_stopped(run_id) # right before the side effect,
send(to, text) # not only at the top of the loop
def agent_loop(run_id):
try:
while not done():
check_not_stopped(run_id) # before each model call
action = next_action() # the model thinks; this can take seconds
send_message(run_id, action.to, action.text, send)
except Stopped as e:
hand_over(e) # tell the person who stopped itPutting the second check inside send_message itself means no code path can send without it, including paths added later by someone who never read this guide.
Three levels of stop
- Stop one run. The flag above. Use it for a handoff or a single run going wrong, while every other run keeps working.
- Stop one agent everywhere. A flag keyed by agent name instead of run, checked the same way.
- Hard stop. Revoke the API key or token the agent uses. This works even if the agent's own code is broken or skips the check, because the agent can no longer call the model or the service at all.
What a flag cannot do
- Cut a call already in flight. A model call or tool call that has started will finish. Give every call a timeout, and see time limits for calls that hang.
- Undo what already happened. A stop prevents the next action. For the actions before it, you need idempotency keys and a ledger, and for actions that can't be undone, a person's approval.
- Cover actions outside your code. If a workflow tool or a second service also sends on the agent's behalf, it needs to read the same flag.
Test your stop the way you will use it: start a run, stop it halfway through a step, and count what happened after. A stop button that exists but was never tested mid-run is not a stop.
Doing it with SteerPlane
SteerPlane keeps a record of each live run on its server, and steerplane runs kill stops one of them. The agent learns about it at its next step and raises RunTerminatedError with the reason you gave:
from steerplane import SteerPlane
from steerplane.exceptions import RunTerminatedError
sp = SteerPlane(agent_id="support_agent")
try:
with sp.run(max_cost_usd=5, max_steps=500) as run:
print("run id:", run.run_id)
while not done():
reply = next_action()
run.log_step("send_message", cost=reply.cost) # SteerPlane checks for a stop here
send(reply)
except RunTerminatedError as e:
print("Stopped:", e)And from your terminal, or anywhere you can run a command:
pip install "steerplane[cli]" steerplane runs list --status running steerplane runs kill run_9dd0e05de785 --reason "handed to a person"
We tested this on version 1.2.1 on 5 December 2026 against a local SteerPlane server: the run stopped at its next step with the reason we gave.
One thing to know. The agent hears about a stop when it reports a step to the server. An action your code takes between two steps still runs. Log the step before the action, as in the example, and keep your own check right before sends that must never go out after a stop.
If your agent's model calls go through the SteerPlane gateway, revoking its key with steerplane keys revoke is the hard stop: every new model call made with that key is refused from then on. If the agent calls the model provider directly, the hard stop is revoking the provider key. Setup is on the setup page.
A checklist for your stop
- The stop lives outside the agent, where every worker can read it.
- It is checked before each model call and right before each send, payment or change.
- You can stop one run without stopping the rest.
- You can cut the agent off completely by revoking its key.
- Every model call and tool call has a timeout.
- You have tested a stop in the middle of a run and counted what happened after it.
Questions people ask
Why does my AI agent send one more message after it was unassigned or handed off?
The stop was checked before the model started thinking, and the send happened after. Whatever arrived in between was missed. Check the stop again right before the send, in the same function that sends.
Can a kill switch stop a model call that is already running?
A flag checked in your loop can't. To cut a call in flight you need a timeout on the call, a proxy in front of the model that can close the stream, or a revoked key.
Should a kill switch stop one run or the whole agent?
Have both. Stopping one run lets you handle the case in front of you while the rest keep working. Stopping every run, for example by revoking the key, is for when you don't trust the agent at all.