PLUGIN brewtools / Skills
On this page
Agent Return Setup
sonnet opt-in project | globalSubagent 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
| Field | Value |
|---|---|
| Command | /brewtools:agent-return-setup |
| Args | [prompt] [status|install|upgrade|enable|disable|uninstall|purge] [project|global] [pass] [file] [--unattended] |
| Model | sonnet |
| Defaults | no args = status; passTokens 2500, fileTokens 6000; ABSENT config = DISABLED (enabled must be exactly true) |
| Output | 3 hook files, 3 settings.json entries, agent-return.json |
Modes
| Mode | Does |
|---|---|
status | Report installed scopes, wiring, governing config (default) |
install | Copy 3 hook files, merge settings, write config |
upgrade | Re-emit hooks from the current plugin, thresholds kept |
enable | Set enabled: true |
disable | Set enabled: false, files stay |
uninstall | Unwire and delete hooks, keep config |
purge | Uninstall 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.
| Size | Order |
|---|---|
t <= passTokens (2500) | pass |
up to fileTokens (6000) | compress: re-send shortened, no new work |
above fileTokens | file: write the full report, return path + verdict + summary |
above fileTokens, agent cannot write | essential 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.
- Status first
Both scopes are checked for hook files, wiring and config before anything changes.
- Plan, then questions
The plan is printed; scope and the threshold pair are asked only if missing.
- Delegated file work
One
brewcode:hook-creatorspawn per scope copies the files and merges settings. See Delegation. - Live status
Wiring and config changes apply to the running session; no restart,
/hooksconfirms.
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.
passTokensmust stay belowfileTokens; the config write aborts on an inversion.
Only the final message is sized: see Limits below.
Technical details
Hook files, tiers, one-challenge rule #
| File | Event | Behavior |
|---|---|---|
agent-return-budget.mjs | never registered | Shared module: config, thresholds, read-only list, estimateTokens, contract text |
agent-return-contract.mjs | SubagentStart | Injects the budget as advisory additionalContext |
agent-return-guard.mjs | SubagentStop | Sizes last_assistant_message, blocks once |
agent-return-guard.mjs | PreToolUse, matcher SubagentHandback | Auto-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 #
| Scope | Hooks dir | settings.json | Config |
|---|---|---|---|
| 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"
}
| Key | Meaning |
|---|---|
enabled | Exactly true; anything else, or no config, makes both hooks no-ops |
passTokens / fileTokens | Positive integers; defaults 2500 / 6000 |
readOnlyAgentTypes | Matched on agent_type (plugin agents as plugin:name); an array replaces the default, [] = nobody |
reportDir | Optional, added by hand; default .claude/reports. Relative = project root, ~/ = $HOME |
version / content_version / generated_by / last_updated | Provenance, 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:
| p10 | p25 | p50 | p75 | p90 | max |
|---|---|---|---|---|---|
| 502 | 761 | 1404 | 2256 | 3164 | 7931 |
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/4is 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
reportDiris under.claude/, a protected path: prompted indefault/acceptEdits, denied indontAskand headless runs. The agent then returns a compressed inline report. For unattended runs setreportDiroutside.claude/. - No hook payload exposes an agent’s tool inventory: write ability is known only from
readOnlyAgentTypesand plan mode. List unlisted read-only agent types. - Fail-open, never exit 2: malformed input or any runtime throw gives
{}and exit 0. A missingagent-return-budget.mjsis 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.
Related
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