DEV Community

Darkø Tasevski
Darkø Tasevski

Posted on Originally published at darkotasevski.dev

Trying to catch coding-agent option sets written in plain chat with a Stop hook

#ai

In the post about building an ideal-option check I mentioned, in a couple of sentences, a hook I tried for options an agent writes in an ordinary chat message instead of putting them through a decision tool, and took out again the same day. This is the longer version, since two things from that hour are useful to anyone writing Claude Code hooks or measuring what agents do from their transcripts, and neither fit in the overview.

The short background is that the hooks reference has no event between the model finishing a text reply and the user seeing it. MessageDisplay can only change what's shown, so the only place to look at a finished reply is Stop, which gets last_assistant_message and can send the agent back with a reason, after I've already read the options.

Measuring a detector

Before writing the hook I wanted to know how well something could tell an option set apart from everything else an agent ends a turn with. I took the final assistant message of every user turn from my Claude Code transcripts, had a Sonnet subagent label a sample blind as APPROACHES (two or more ways to solve the task, for me to choose), PROCEDURAL (only logistics, like continue or stop, commit or not) or NONE, and scored two detectors against those labels.

A regex for two or more lettered labels ((a), A., Option A) and a line ending in ? near the end was precise, with 87 of the 111 messages it flagged being real approach sets, but it caught only 14% to 23% of them, since agents write options inline far more often than as lettered lists. Haiku, asked for a yes or no per message with the same definitions, came out at 96% to 98% precision and 81% to 89% recall, so I installed it as a prompt hook in ~/.claude/settings.json (prompt shortened):

{
  "hooks": {
    "Stop": [
      {
        "hooks": [
          {
            "type": "prompt",
            "model": "haiku",
            "timeout": 30,
            "prompt": "... Hook input: $ARGUMENTS\n\nIf \"stop_hook_active\" is true, respond {\"ok\": true}. Otherwise read \"last_assistant_message\". Does it offer the user two or more alternative approaches ...? ... If yes, respond {\"ok\": false, \"reason\": \"Your last message offered the user options in prose, which skips the decision checklist. ...\"} with that reason exactly."
          }
        ]
      }
    ]
  }
}
Enter fullscreen mode Exit fullscreen mode

Its two firings in real sessions were both noise, one on the closing message of a turn that had already asked me through the decision tool and one on a status report, and each false flag costs a full continuation of the main model, about 300,000 to 400,000 cached input tokens, several hundred times what the Haiku check costs. The measurement also hadn't measured this hook, because the installed prompt had grown exclusions the measured one didn't have, and the measured Haiku had judged messages in batches, not one at a time the way the hook sees them.

The model writes the reason

With a type: "prompt" hook, the small model returns both the verdict and the reason, and Claude Code hands that reason to the agent as hook feedback, which agents treat as coming from the user. The prompt asks Haiku to return the reason "exactly", but that's a request to a model, and if the final message quotes something hostile, like a pull request comment or a fetched page, that text could talk Haiku into returning it as the reason, which then reaches the agent looking like my words. I reasoned that out from the docs and didn't try to reproduce it. The stop_hook_active check and the exclusions have the same weakness, since they are Haiku reading text rather than code reading a field, so a message that quotes the right strings could skip the check.

So if a model sits in a hook like this at all, it should only ever answer yes or no, with the reason text fixed in code and the flags checked in code, which in practice means a command hook that calls the model rather than a prompt hook. A command hook can also read the transcript and skip turns that already asked through a tool, which a prompt hook can't see.

Picking transcripts by file date

The detector numbers came from a sample I took as "the last 21 days" of transcripts, picked by file modification time, which gave 17,243 messages, and it took me a while to notice that about a thousand older transcripts had their modification time bumped on the same day in November, so most of the sample was from April to November, before agents usually put decisions through a question tool at all. The detector comparison still holds, since both were scored on the same labelled messages, but the rates said nothing about how agents behave now.

Picking by each message's own timestamp gave 7,244 messages from 335 sessions over the 21 days, about 345 turns a day. About 24 of those a day end with an approach set, and in about 14 the same turn also asked through a tool, which leaves about 10 a day (perhaps 5 to 25, the sample is small) where the options exist only in prose, and the lettered-option regex finds none of them, since that format has fallen out of use. At that volume, zero false flags in the labelled sample only bounds the false-flag rate at about 5%, which still allows up to about 17 wasted continuations a day. A second measurement a day later (see the update below) put the prose-only rate lower, at about 3.6 a day, and I haven't worked out which of the two is closer.

Update, 2026-12-04: measuring the detectors

A day later I measured candidate detectors the way the hook would run them, one message at a time, on a new sample of turn-final messages picked by timestamp (7,075 of them, 6,005 from turns that made no decision call, so about 286 a day the hook would judge). This time two models labelled it independently, Sonnet and then Opus in a different order, and they agreed on 98.7% of messages. That sample puts prose-only approach sets at about 3.6 a day (roughly 2 to 8), below the earlier estimate of about 10, and I didn't establish whether that's because the new extraction only takes text written after the turn's last tool call or because the earlier sample was small.

Every detector caught most of them, and every one flagged far more messages that weren't option sets. Haiku asked for a one-word yes or no did best, catching about 3.5 a day with about 6 false flags a day (a precision of 0.36, or 0.53 against the Opus labels), at a median of 1.37 s. A local llama3.1 8B in Ollama, the DeBERTa zero-shot model jev-guard uses offline, and Kev-4B, an open model built like TypeSafe's Jev, all did worse, and Kev's scores for real option sets overlapped its scores for everything else, so no threshold separated them. The mistakes were the same for every model, single proposals ending in "Should I …?", "now or later" questions, and status reports listing next steps. Jev itself I didn't run, since it's only served in the cloud and the test would have sent my real messages to a third party.

With about 6 false flags a day, each costing a full continuation, a blocking Stop hook would repeat what the trial showed. What changes that is splitting the hook in two, where the Stop hook only answers yes or no and writes a marker for the session, printing nothing, and on my next prompt a UserPromptSubmit hook turns the marker into a fixed line of context, which reaches the agent together with my answer, right when it's about to act on the choice. A false flag then costs a line of context instead of a turn, the text is still fixed in code, and Haiku's precision becomes tolerable. I haven't built or tried that yet, and a detector like this is best run silently for a week or so before it does anything, logging what it would have flagged, since 20-odd positives leave every figure above with a wide interval.

Where it ended up

The gap is real, a few decisions a day that skip the checklist, but the hook cost more than the gap, so it's gone, and the table in my instructions that records what enforces each rule now says prose option sets aren't covered. A replacement would need a model that answers only yes or no, a reason fixed in code, turns that already asked skipped by reading the transcript, and a detector measured one message at a time on a sample picked by timestamp, with false flags per day stated.

A hook can stop an agent's decision before the user sees it only when the decision goes through a tool, so the dependable fix for options in prose is upstream, in getting agents to ask through the tool, and a Stop hook is at best a nudge after the fact, which should leave a note for the next prompt rather than block, so a false alarm costs a line of context instead of a full turn. Any text a model writes inside a hook reaches the agent with the user's weight, so the model should return a verdict and nothing else. And when measuring agent behaviour from transcripts, pick messages by the timestamps inside them, since file dates can be touched by anything (in my case, about a thousand files on one day).

Top comments (0)