Skip to content

hookCode quality

Block Large Writes

A Claude Code hook that rejects Write calls over a size limit, which catches runaway generated files before they land in your repository.

npx workspacetemplates add block-large-writes --tool claude-code

Writes 1 file to .claude/

.claude/settings.jsonmerge into your file16 lines · 844 BFile
{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Write",
        "hooks": [
          {
            "type": "command",
            "command": "node -e \"process.on('uncaughtException',()=>process.exit(0));let d='';process.stdin.on('data',c=>d+=c).on('end',()=>{let j={};try{j=JSON.parse(d)||{}}catch(e){process.exit(0)}const i=j.tool_input||{};const f=String(i.file_path||'');const fs=require('fs');const c=i.content;if(typeof c!=='string')process.exit(0);const kb=Number(process.env.MAX_WRITE_KB)||200;const n=Buffer.byteLength(c)/1024;if(n>kb){console.error('Blocked: this write is '+Math.round(n)+' KB, over the '+kb+' KB limit. Write a smaller file, split it, or generate it with a script instead of pasting its text.');process.exit(2)}process.exit(0)})\"",
            "timeout": 10
          }
        ]
      }
    ]
  }
}

What it does

A model that loops can paste a huge blob into a single file, such as a minified bundle or a repeated table. This PreToolUse hook measures the content of each Write call and refuses anything over 200 KB.

Set the MAX_WRITE_KB environment variable to move the limit. Smaller writes and Edit calls are untouched.

Example

you › Write the full vendor library inline into src/lib.js.

agent › The write is blocked with its size and the limit. The agent installs the package or splits the content instead.

Questions

How do I change the limit?
Set MAX_WRITE_KB to a number of kilobytes in the environment that starts Claude Code. Without it the cap is 200.
Does it check edits too?
No, only whole-file Write calls carry full content, so that is the only tool it watches.
  • Block Generated File Edits

    HookCode quality

    Free
  • Block Vendor Edits

    HookCode quality

    Free
  • Ensure Final Newline

    HookCode quality

    Free
  • Format After Edit

    HookCode quality

    Free