Skip to content

PreToolUse/PostToolUse hooks intercept small Bash/WebFetch calls without a size threshold (Claude Code) — unlike Gemini CLI's size-aware BeforeTool matcher #817

@monossido

Description

@monossido

Environment: context-mode v1.0.162, Claude Code (plugin install), Linux

Description

On Claude Code, the PreToolUse/PostToolUse hooks fire on every Bash invocation and every WebFetch/curl/wget call regardless of expected output size, redirecting the model toward ctx_batch_execute / ctx_execute_file / ctx_fetch_and_index even for outputs of a few hundred bytes to a few KB.

This contrasts with the Gemini CLI integration, whose README explicitly restricts the BeforeTool matcher to "tools that produce large output... avoids unnecessary hook overhead on lightweight tools."

Evidence (3 A/B tests on a ~137K LOC backend repo)

  1. git log --stat -30 (~6-7KB raw output) via plain Bash vs ctx_batch_execute (same command + 2 queries): the batch call returned 18.1KB to context — more than the direct command — while padding the "without context-mode" baseline by +36KB (largely duplicated content across the two queries). The reported "51% kept out" ratio is an artifact of that inflated baseline.

  2. Direct Read (offset/limit) of a 41-line function (~870B): zero context-mode overhead, zero captures — correct, matches the tool's own "small file → Read directly" guidance.

  3. Same 41-line function via ctx_execute_file: added +1KB to context (echoes the executed snippet plus the result), added a "sandbox" capture, and the "kept out" ratio actually dropped 51%→50%.

  4. curl/wget to fetch a small README is hard-blocked with no size check, forcing ctx_fetch_and_index even when a direct fetch would return a small, immediately-useful payload.

Suggested fix

Add a size-aware threshold to the Claude Code hook config (e.g., only intercept Bash/WebFetch when expected/observed output exceeds ~X KB), mirroring the Gemini CLI BeforeTool matcher design. This would let small commands and small file reads pass through untouched, reserving the ctx_* sandbox tools for genuinely large outputs where they deliver the documented savings.

🤖 Filed with Claude Code

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions