PLUGIN brewtools / Skills

Agent Return Setup

sonnet opt-in project | global

Subagent reports are the largest single context cost of a session that delegates. The hooks state a token budget when the agent starts and enforce it when it returns: an oversized report is sent back once to be shortened or filed. No LLM judge, only a number comparison.

Quick reference

FieldValue
Command/brewtools:agent-return-setup
Args[prompt] [status|install|upgrade|enable|disable|uninstall|purge] [project|global] [pass] [file] [--unattended]
Modelsonnet
Defaultsno args = status; passTokens 2500, fileTokens 6000; ABSENT config = DISABLED (enabled must be exactly true)
Output3 hook files, 3 settings.json entries, agent-return.json

Modes

ModeDoes
statusReport installed scopes, wiring, governing config (default)
installCopy 3 hook files, merge settings, write config
upgradeRe-emit hooks from the current plugin, thresholds kept
enableSet enabled: true
disableSet enabled: false, files stay
uninstallUnwire and delete hooks, keep config
purgeUninstall and delete config

Under --unattended, upgrade covers one scope and skips with nothing written unless the prompt names exactly one of project or global; it also skips when that scope has an orphan config, shares a config from ~/.claude (upgrade by hand), is not installed, uses a custom launcher or has an invalid settings.json. A config kept by uninstall is not an install: the note is not installed (uninstalled, config kept) and the hooks are not wired again. See how the flag works.

Example

/brewtools:agent-return-setup install project 2500 6000

The skill prints the status table, states its plan (3 files into .claude/hooks/, config with both thresholds, 3 settings entries), skips the questions because scope and thresholds are given, delegates to brewcode:hook-creator and re-prints status: hook_files=3/3 settings_refs=2 enabled=true registrations=3/3.

How it works

The size t is chars/4 of the final message.

SizeOrder
t <= passTokens (2500)pass
up to fileTokens (6000)compress: re-send shortened, no new work
above fileTokensfile: write the full report, return path + verdict + summary
above fileTokens, agent cannot writeessential part only

Each agent is blocked at most once, on either delivery path (SubagentStop classic, SubagentHandback in auto mode). Agent types in readOnlyAgentTypes (default Explore, Plan) get the essential-part order instead of a file.

  1. Status first

    Both scopes are checked for hook files, wiring and config before anything changes.

  2. Plan, then questions

    The plan is printed; scope and the threshold pair are asked only if missing.

  3. Delegated file work

    One brewcode:hook-creator spawn per scope copies the files and merges settings. See Delegation.

  4. Live status

    Wiring and config changes apply to the running session; no restart, /hooks confirms.

Good to know

  • Opt-in: the hooks are not in brewtools/hooks/hooks.json, so installing the plugin changes nothing until you run this skill.
  • A global install is cheap: the hooks fire only at spawn, stop and hand-back, about 30-53 ms each.
  • passTokens must stay below fileTokens; the config write aborts on an inversion.

Only the final message is sized: see Limits below.

Technical details

Hook files, tiers, one-challenge rule #
FileEventBehavior
agent-return-budget.mjsnever registeredShared module: config, thresholds, read-only list, estimateTokens, contract text
agent-return-contract.mjsSubagentStartInjects the budget as advisory additionalContext
agent-return-guard.mjsSubagentStopSizes last_assistant_message, blocks once
agent-return-guard.mjsPreToolUse, matcher SubagentHandbackAuto-mode path (Claude Code 2.1.271+): sizes tool_input.message, denies once

The three files install as a unit: with 2 of 3 present, ESM resolution fails and both hooks exit 1 with a hook-error banner on every spawn and return.

Boundaries are inclusive on the low side: exactly passTokens passes, exactly fileTokens still compresses. A denied file write is never retried; the agent returns a compressed inline report.

A marker agent-return-state/<sha256(agent_id)>.json beside the config (pruned after 24 h) records the challenge; stop_hook_active === true is checked before any sizing. A payload without agent_id (main session, internal agents) is never challenged. A compress round that lands slightly over budget is not challenged again.

Installation targets and config discovery #
ScopeHooks dirsettings.jsonConfig
Project<repo>/.claude/hooks/<repo>/.claude/settings.json<repo>/.claude/agent-return.json
Global<config dir>/hooks/<config dir>/settings.json<config dir>/agent-return.json

<config dir> is $CLAUDE_CONFIG_DIR when set, else ~/.claude/. The state directory sits beside the config that enabled the feature.

Discovery starts at the project root (CLAUDE_PROJECT_DIR, then an upward walk for .git or .claude, then cwd), probes <dir>/.claude/agent-return.json up to 16 levels, then $CLAUDE_CONFIG_DIR/agent-return.json, then ~/.claude/agent-return.json. Project wins, even without wired hooks; a malformed project config is skipped. Global writes go through Bash only, because the config dir is a protected path for Write/Edit.

Config shape and threshold precedence #
{
  "enabled": true,
  "passTokens": 2500,
  "fileTokens": 6000,
  "readOnlyAgentTypes": ["Explore", "Plan"],
  "version": "X.Y.Z",
  "content_version": "X.Y.Z",
  "generated_by": "brewtools:agent-return-setup",
  "last_updated": "YYYY-MM-DD"
}
KeyMeaning
enabledExactly true; anything else, or no config, makes both hooks no-ops
passTokens / fileTokensPositive integers; defaults 2500 / 6000
readOnlyAgentTypesMatched on agent_type (plugin agents as plugin:name); an array replaces the default, [] = nobody
reportDirOptional, added by hand; default .claude/reports. Relative = project root, ~/ = $HOME
version / content_version / generated_by / last_updatedProvenance, inert at runtime

Per threshold, first hit wins: config key, then env var (AGENT_RETURN_PASS / AGENT_RETURN_FILE, parsed with Number()), then the built-in value. Only positive integers are accepted; 1.7, abc, -5, 0 fall through. Values are read on every hook call.

Upgrade details and uninstall file effects #

upgrade asks nothing: it reads both thresholds from the config and replays the install against the current assets, adding the SubagentHandback entry an older install lacks. A disabled setup stays disabled. The pair 1000 / 2500 with no readOnlyAgentTypes key (the pre-6.5 defaults) migrates once to 2500 / 6000. One scope per run.

uninstall deletes the hook files and strips the settings entries (an emptied settings.json and hooks dir are removed) but keeps the config. purge also deletes the config and an emptied project .claude/, never the global dir.

Orphan project config #

uninstall project keeps .claude/agent-return.json by design. While the global scope still registers the hooks, that file keeps governing them: the nearest project config is read first. The uninstall prints WARNING: kept <cfg> still GOVERNS ... and the skill offers purge project in the same confirmation.

status prints governing_config, governing_scope (project, global or none) and orphan_config=yes when the governing config is project-side, the project has 0/3 registrations and global has at least one. An orphan is a finding, not stale. Fix: /brewtools:agent-return-setup purge project. Keeping it on purpose is valid. upgrade is never offered, because it would re-wire the project and undo the uninstall.

Why 2500 / 6000 #

Measured over 80 real Agent returns in 4 session transcripts, sized chars/4:

p10p25p50p75p90max
5027611404225631647931

58% of the 136.7k total est-tokens is overflow above 800. 2500 is about p78, so only the heaviest fifth is challenged. 6000 sits between p90 and the maximum and is a judgement call: below it one compression round can still reach the budget, above it the detail needs a durable home.

Limits: read before trusting it #
  • chars/4 is not a tokenizer on purpose: the thresholds are fitted to it. Change the heuristic and re-fit both numbers.
  • Only the final message is sized; the work behind it is not budgeted.
  • The hook does not validate passTokens < fileTokens; inverted values degrade to a contradictory file order, no error, exit 0.
  • The default reportDir is under .claude/, a protected path: prompted in default/acceptEdits, denied in dontAsk and headless runs. The agent then returns a compressed inline report. For unattended runs set reportDir outside .claude/.
  • No hook payload exposes an agent’s tool inventory: write ability is known only from readOnlyAgentTypes and plan mode. List unlisted read-only agent types.
  • Fail-open, never exit 2: malformed input or any runtime throw gives {} and exit 0. A missing agent-return-budget.mjs is not catchable (see the unit rule above).
  • Timings (Node v24.19.0, one machine) are not a guarantee; re-measure before quoting.
Coexistence with agent-deadline-setup and tests #

Both skills install side by side; each merge and strip touches only entries naming its own scripts. An agent challenged once here continues on its original deadline clock, and the deadline guard always allows SubagentHandback. A pre-6.5 agent-deadline install also denies the hand-back past its limit: upgrade it too.

Tests: tests/run.sh in the skill source, run from a checkout.

Brewtools overview

Every brewtools skill and agent.

Agent Deadline Setup

Soft wall-clock budget for subagents; installs side by side with this skill.

Setup Status

Dashboard across every -setup skill; upgrade brings the stale ones current.

GitHub source

SKILL.md, the install runbook and the hook files.

Update: /brewtools:plugin-update, /reload-plugins, then /brewcode:setup-status upgrade - FAQ