प्रॉम्प्ट सुझाव हैं। हुक्स (Hooks) सख्त रोक हैं।

महीनों तक, मैंने Claude Code को एक जूनियर डेवलपर की तरह माना जिसे बस स्पष्ट नियमों की आवश्यकता थी। मेरे प्रोजेक्ट निर्देश स्पष्ट थे: कभी force-push न करें, कभी branches को डिलीट न करें, कभी विनाशकारी (destructive) कमांड न चलाएं। अधिकांश शामों में, यह काम कर गया। एजेंट ने टेस्ट लिखे, फंक्शन्स को रिफैक्टर किया, और git हिस्ट्री से दूर रहा। फिर एक rebase गलत हो गया।

कॉन्टेक्स्ट विंडो (context window) git एरर आउटपुट से भर गई। कॉन्फ्लिक्ट मार्कर्स, detached HEAD मैसेज, और ब्रांच डाइवर्जेंस (branch divergence) चेतावनियाँ, टोकन-दर-टोकन जमा होने लगीं। उस शोर के नीचे force-push से बचने का मेरा विनम्र निर्देश दबा हुआ था। मॉडल के लिए, थ्रेड में सबसे हालिया और महत्वपूर्ण टेक्स्ट एरर स्ट्रीम था। सांख्यिकीय ध्यान (Statistical attention) नीति (policy) पर हावी हो गया। एजेंट ने एक ऐसा कमांड चलाया जिसने दो घंटे के अनकमिमिटेड (uncommitted) लोकल बदलावों को मिटा दिया। यह दुर्भावनापूर्ण नहीं था; यह केवल विचलित (distracted) था। यह अंतर मायने रखता है। एक LLM द्वेषवश नियम नहीं तोड़ता। वह उन्हें इसलिए तोड़ता है क्योंकि कॉन्टेक्स्ट विंडो में एक तेज़ पैटर्न अस्थायी रूप से पिछले निर्देश को ओवरराइड कर देता है।

उस घटना ने एजेंट सुरक्षा के बारे में मेरी सोच बदल दी। एक गार्डरेल (guardrail) जो निन्यानवे प्रतिशत समय काम करता है, वह एक जोखिम (liability) है। यदि विफलता का तरीका आपका समय, पैसा या प्रोडक्शन डेटा बर्बाद करता है, तो आप इसे प्रॉम्प्ट के भीतर नहीं छोड़ सकते। आपको मॉडल के रीजनिंग लूप (reasoning loop) के बाहर प्रवर्तन (enforcement) की आवश्यकता है।

Claude Code hooks ठीक इसी समस्या का समाधान करते हैं। ये छोटे स्क्रिप्ट हैं जो तीन विशिष्ट क्षणों पर टूल कॉल्स को रोकते हैं: टूल चलने से पहले (PreToolUse), टूल खत्म होने के बाद (PostToolUse), और जब एजेंट यह तय करता है कि वह काम पूरा कर चुका है (Stop)। क्योंकि वे बाहरी कोड के रूप में चलते हैं, वे मॉडल की मेमोरी, मूड या कॉन्टेक्स्ट प्रेशर पर निर्भर नहीं करते हैं। मॉडल आपके द्वारा दिए गए हर निर्देश को भूल सकता है; हुक फिर भी "ना" कहेगा।

उस खोई हुई शाम के बाद मैंने जो हार्नेस (harness) बनाया, वह यहाँ है।

द गार्ड हुक: नुकसान होने से पहले रोकें

मेरा PreToolUse हुक हर Bash कमांड का निरीक्षण करता है इससे पहले कि शेल उसे छुए। मैं विनाशकारी पैटर्न की एक सख्त डिनायलिट (denylist) रखता हूँ। यदि कमांड स्ट्रिंग किसी खतरनाक चीज़ से मेल खाती है, तो हुक निष्पादन (execution) को रोक देता है और सीधे एजेंट को एरर वापस भेज देता है।

मेरे द्वारा ब्लॉक किए जाने वाले पैटर्न सरल और स्पष्ट हैं:

  • git push --force या कोई भी force-with-lease वेरिएंट जिस पर मुझे अभी भरोसा नहीं है
  • git reset --hard
  • rm -rf

यह कोई परिष्कृत सुरक्षा अनुसंधान नहीं है। यह एक सीटबेल्ट है। लेकिन महत्वपूर्ण विवरण यह है कि ब्लॉक होने के बाद क्या होता है।

मैं कभी भी सीधा “Blocked” नहीं कहता। एक सपाट इनकार एजेंट को भ्रमित

The Stop hook runs when the agent decides it has finished the task and attempts to terminate the session. I do not let it. Instead, the hook runs the full test suite. If any test fails, the hook blocks the stop command and returns the failure output to the agent.

This changes the definition of completion. “Done” is no longer a feeling the model has. It is a measurable gate. The agent can only finish when the harness confirms the code works. In practice, this creates a tight feedback loop. The agent writes code, thinks it is finished, hits the stop button, and immediately sees a pytest traceback. It then self-corrects, fixes the import error or broken assertion, and tries to stop again. I have watched agents iterate three or four times inside this loop without human intervention. The harness enforces quality; the model supplies the patches.

What This Teaches About Agent Engineering

Building reliable autonomous systems requires a shift in mindset. You move from writing longer prompts to building tighter harnesses.

Use hooks for enforcement and prompts for policy. If a rule must hold one hundred percent of the time, it belongs in code, not in natural language. Prompts excel at ambiguity, taste, and architecture. They are terrible at invariants. If a mistake would cost you an afternoon of recovery time or, worse, production uptime, write a hook.

Shorter prompts produce better results. When you move mechanical rules into scripts, the model has less to remember and less to contradict. The agent’s context window is a scarce resource. Do not fill it with formatting reminders.

Finally, accept that your role is changing. As agents gain autonomy, the human’s job shifts from generating content to designing the guardrails. You are building the harness that decides what the model can touch, when it can finish, and how it must behave when things go wrong. That is engineering, not prompting.

The source that inspired this approach and additional implementation detail can be found here.

If you are building with AI agents and want to trade notes with other practitioners, you can find the GyaanSetu learning community here.