DEV Community

Our agent sent one more message after we stopped it. Here's why.

A person took over a conversation. The agent had been told to stop. It still sent one more message.

If you run agents in production, you will hit this sooner or later. A run goes wrong in front of you, or a customer is handed to a person, and the agent needs to stop now. Not after it finishes its current thought.

We reproduced it

We built a tiny agent: it thinks for a moment (a sleep standing in for the model), then sends a message. A person stops it partway through.

With the stop flag checked only at the top of the loop, one more message went out after the stop, in all three runs. With the flag checked again right before the send, none did.

Why it happens

The flag is read. Then the model thinks, which takes seconds. Then the agent acts. A stop that lands while the model is thinking is never looked at again before the action.

The fix: a stop flag outside the agent, checked twice

Keep the flag somewhere every worker can read it: your database, Redis, 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 every 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 it

Putting the second check inside send_message itself means no code path can send without it, including paths someone adds later.

Three levels of stop

  • Stop one run. The flag above. Every other run keeps working.
  • Stop one agent everywhere. The same flag, keyed by agent name instead of run.
  • Hard stop. Revoke the API key the agent uses. This works even if the agent's code is broken or skips the check.

What a flag can't do

  • Cut a call already in flight. A model call that has started will finish. Give every call a timeout.
  • Undo what already happened. For that you need idempotency keys, and a person's approval for actions that can't be undone.
  • Cover actions outside your code. If a workflow tool also sends on the agent's behalf, it needs to read the same flag.

Test your stop the way you'll use it: start a run, stop it halfway through a step, and count what happened after. A stop button that was never tested mid-run is not a stop.

The full guide, with a checklist and how to do it with SteerPlane's runs kill command, is here: https://www.steerplane.com/guides/ai-agent-kill-switch

Read on DEV Community ↗ ← Back to News

Comments

No comments yet. Start the discussion.