Skip to content

Sensors

Sensors are shell scripts wired into Claude Code’s hook system. They fire automatically on specific events — you don’t invoke them, and the agent can’t bypass them. Sensors enforce boundaries, inject context, and monitor session health without any manual intervention.

The mechanical flow is:

  1. A hook event fires in Claude Code (e.g., the agent is about to edit a file)
  2. Claude Code checks .claude/settings.json for any hooks registered to that event
  3. If a hook has a matcher, it only fires when the tool name matches (e.g., Edit|Write)
  4. The sensor’s shell script runs with the event context piped to stdin
  5. The script can output text (injected as context the agent must read), or exit with code 2 to block the action

Bonsai generates the shell scripts from templates during bonsai init and bonsai add, and automatically wires them into .claude/settings.json. You never need to configure hooks manually.

Claude Code provides six hook events that sensors can listen to:

EventWhen it firesCan block?Use case
SessionStartBeginning of a conversationNoInject startup context, check routine dashboard
UserPromptSubmitBefore Claude processes a user messageYes (exit code 2)Inject behavioral constraints, detect session wrap-up
PreToolUseBefore a tool executesYes (exit code 2)Block dangerous commands, validate dispatches, check code
PostToolUseAfter a tool executesNoReview completed agent work
StopAfter every Claude responseYes (exit code 2)Show status bar, monitor session health
SubagentStopWhen a dispatched subagent finishesNoTrigger review checklist for completed work

PreToolUse and PostToolUse sensors can include a matcher to filter which tools they apply to. Without a matcher, the sensor fires for every tool use on that event.

MatcherTools matched
Edit|WriteFile editing and writing tools
BashShell command execution
AgentSubagent dispatch tool

All 12 sensors in the Bonsai catalog, grouped by function.

These sensors use PreToolUse to intercept and block actions before they happen.

SensorEventMatcherAgentsWhat it does
scope-guard-filesPreToolUseEdit|WriteallBlocks edits outside the agent’s workspace and .env files. Required for all agents.
scope-guard-commandsPreToolUseBashtech-lead, securityBlocks application execution commands (tests, builds, servers). Prevents non-code agents from running the app.
dispatch-guardPreToolUseAgenttech-leadBlocks dispatches that lack worktree isolation or a plan reference. Enforces structured orchestration.
iac-safety-guardPreToolUseBashdevopsBlocks dangerous infrastructure commands — terraform destroy, kubectl delete namespace, unsafe docker operations.

Context Injection — Add Information at Key Moments

Section titled “Context Injection — Add Information at Key Moments”

These sensors inject context that the agent reads and acts on.

SensorEventMatcherAgentsWhat it does
session-contextSessionStartallInjects identity, memory, protocols, INDEX, status, field notes, and health warnings at conversation start. Required for all agents.
context-guardUserPromptSubmitallInjects tiered behavioral constraints before each prompt. At 30% context usage, nudges conciseness. At 70%+, restricts to current task only. At 85%+, refuses new work. Also detects session wrap-up trigger words. Required for all agents.
routine-checkSessionStartallParses the routine dashboard (agent/Core/routines.md) and flags overdue routines. Auto-installed when routines are present, auto-removed when the last routine is removed.

These sensors trigger review processes after agents finish work.

SensorEventMatcherAgentsWhat it does
agent-reviewPostToolUseAgenttech-leadOutputs a review checklist after a dispatched agent completes work.
subagent-stop-reviewSubagentStoptech-leadOutputs a structured review checklist when a dispatched subagent finishes — checks plan compliance, security, verification steps.
SensorEventMatcherAgentsWhat it does
status-barStopallShows a compact status line after every response — context usage %, turn count, git state, memory health, and routine status. Required for all agents.

These sensors inspect code changes as they happen.

SensorEventMatcherAgentsWhat it does
api-security-checkPreToolUseEdit|Writebackend, fullstack, securityDetects security anti-patterns — SQL injection, hardcoded secrets, eval(), CORS wildcards.
test-integrity-guardPreToolUseEdit|Writebackend, frontend, fullstackCatches test quality issues — .skip(), .only(), removed assertions, empty test bodies, debug artifacts.

Four sensors are required for all agent types and cannot be unchecked during installation:

SensorWhy it’s required
session-contextWithout startup context injection, the agent doesn’t know who it is or what to do
scope-guard-filesWithout file scope enforcement, agents could edit files in other agents’ workspaces
context-guardWithout behavioral constraints, agents don’t self-regulate context usage or handle session wrap-up
status-barWithout the status line, users have no visibility into session health

Two sensors work as a pair to give agents real-time self-awareness:

status-bar fires after every response. It shows the user a compact status line with context usage %, turn count, and session health warnings — uncommitted files, stale memory, overdue routines.

context-guard fires before every prompt. It reads session state and injects behavioral constraints the agent must follow:

Context usageConstraint injected
Below 30%Normal operation
30-70%Nudges toward conciseness
70-85%Restricts to current task only
Above 85%Refuses new work, pushes to wrap up

Session wrap-up: When the user says “session done”, “that’s all”, or similar phrases, the context-guard injects a structured wrap-up checklist — commit check, memory update, backlog review, status update, session notes.

You can create your own sensors by adding shell scripts with YAML frontmatter to agent/Sensors/. See Creating Custom Sensors for the full guide.