Sign in
Explore
Add-ons · HookEveryday codingReviewed

gsd-context-monitor.js

PostToolUse hook that reads context-usage metrics and warns the agent when usage is high.

Kind
Hook
Works with
Claude Code, Gemini / Antigravity
Languages
TypeScript / JavaScript
Source
gsd-build/get-shit-done @ bdcaab2c752d
Path
hooks/gsd-context-monitor.js
License
MIT
Jobs
Context management
InstallCopy it from the source repository, or sign in and let the assistant adapt it for your project.
#!/usr/bin/env node
// gsd-hook-version: {{GSD_VERSION}}
// Context Monitor - PostToolUse/AfterTool hook (Gemini uses AfterTool)
// Reads context metrics from the statusline bridge file and injects
// warnings when context usage is high. This makes the AGENT aware of
// context limits (the statusline only shows the user).
//
// How it works:
// 1. The statusline hook writes metrics to /tmp/claude-ctx-{session_id}.json
// 2. This hook reads those metrics after each tool use
// 3. When remaining context drops below thresholds, it injects a warning
//    as additionalContext, which the agent sees in its conversation
//
// Thresholds:
//   WARNING  (remaining <= 35%): Agent should wrap up current task
//   CRITICAL (remaining <= 25%): Agent should stop immediately and save state
//
// Debounce: 5 tool uses between warnings to avoid spam
// Severity escalation bypasses debounce (WARNING -> CRITICAL fires immediately)

const fs = require('fs');
const os = require('os');
const path = require('path');
const { spawn } = require('child_process');

const WARNING_THRESHOLD = 35;  // remaining_percentage <= 35%
const CRITICAL_THRESHOLD = 25; // remaining_percentage <= 25%
…