ప్రాంప్ట్‌లు సూచనలు మాత్రమే. హుక్స్ (Hooks) అనేవి ఖచ్చితమైన నిలిపివేతలు.

నెలల తరబడి, నేను Claude Codeని కేవలం స్పష్టమైన నియమాలు అవసరమయ్యే ఒక జూనియర్ డెవలపర్‌లా పరిగణించాను. నా ప్రాజెక్ట్ సూచనలు చాలా స్పష్టంగా ఉండేవి: ఎప్పుడూ force-push చేయవద్దు, బ్రాంచ్‌లను డిలీట్ చేయవద్దు, వినాశకరమైన (destructive) కమాండ్‌లను రన్ చేయవద్దు. చాలా సాయంత్రాలు, అది పని చేసింది. ఏజెంట్ టెస్ట్‌లను రాసేది, ఫంక్షన్‌లను రీఫ్యాక్టర్ చేసేది మరియు git హిస్టరీని తాకకుండా చూసుకునేది. కానీ ఒకసారి rebase సరిగ్గా జరగలేదు.

Context window అంతా git ఎర్రర్ అవుట్‌పుట్‌తో నిండిపోయింది. Conflict markers, detached HEAD మెసేజ్‌లు మరియు branch divergence హెచ్చరికలు ఒక్కొక్క టోకెన్‌లా పేరుకుపోయాయి. ఆ శబ్దాల మధ్య, force-push చేయవద్దని నేను ఇచ్చిన మర్యాదపూర్వకమైన సూచన అణచివేయబడింది. మోడల్‌కు, ఆ థ్రెడ్‌లో ఉన్న అత్యంత ఇటీవలి మరియు ముఖ్యమైన టెక్స్ట్ ఆ ఎర్రర్ స్ట్రీమ్ మాత్రమే. గణాంక ప్రాధాన్యత (Statistical attention) విధానం (policy) కంటే బలంగా పనిచేసింది. ఏజెంట్ రెండు గంటల uncommitted local మార్పులను తుడిచివేసే కమాండ్‌ను అమలు చేసింది. అది దురుద్దేశంతో చేసినది కాదు; అది కేవలం పరధ్యానంలో ఉంది. ఆ తేడా చాలా ముఖ్యం. ఒక LLM ద్వేషంతో నియమాలను ఉల్లంఘించదు. Context windowలో ఉన్న ఒక పెద్ద ప్యాటర్న్ మునుపటి సూచనను తాత్కాలికంగా అధిగమించినప్పుడు అది నియమాలను ఉల్లంఘిస్తుంది.

ఆ సంఘటన ఏజెంట్ భద్రత (agent safety) గురించి నా ఆలోచనా విధానాన్ని మార్చింది. తొంభై తొమ్మిది శాతం సమయం పనిచేసే గార్డ్‌రైల్ (guardrail) అనేది ఒక రిస్క్ (liability). ఒకవేళ ఆ వైఫల్యం వల్ల మీకు సమయం, డబ్బు లేదా ప్రొడక్షన్ డేటా నష్టపోతే, దానిని కేవలం ప్రాంప్ట్‌లో మాత్రమే వదిలేయలేరు. మోడల్ యొక్క రీజనింగ్ లూప్ వెలుపల మీరు దానిని అమలు చేయాలి (enforcement).

Claude Code hooks సరిగ్గా ఇదే సమస్యను పరిష్కరిస్తాయి. ఇవి మూడు నిర్దిష్ట సమయాల్లో టూల్ కాల్స్‌ను అడ్డుకునే చిన్న స్క్రిప్ట్‌లు: టూల్ అమలు కావడానికి ముందు (PreToolUse), టూల్ పూర్తయిన తర్వాత (PostToolUse), మరియు ఏజెంట్ పని పూర్తయిందని నిర్ణయించినప్పుడు (Stop). ఇవి ఎక్స్‌టర్నల్ కోడ్‌గా రన్ అవుతాయి కాబట్టి, ఇవి మోడల్ యొక్క మెమరీ, మూడ్ లేదా context ఒత్తిడిపై ఆధారపడవు. మోడల్ మీరు ఇచ్చిన ప్రతి సూచనను మర్చిపోవచ్చు; కానీ హుక్ మాత్రం "వద్దు" అని చెబుతుంది.

ఆ కోల్పోయిన సాయంత్రం తర్వాత నేను రూపొందించిన హార్నెస్ (harness) ఇక్కడ ఉంది.

ది గార్డ్ హుక్ (The Guard Hook): నష్టం జరగకముందే అడ్డుకోండి

నా PreToolUse హుక్, షెల్ (shell) తాకకముందే ప్రతి Bash కమాండ్‌ను తనిఖీ చేస్తుంది. వినాశకరమైన ప్యాటర్న్‌ల కోసం నేను ఒక కఠినమైన denylistని ఉంచుతాను. కమాండ్ స్ట్రింగ్ ఏదైనా ప్రమాదకరమైన దానితో సరిపోలితే, హుక్ అమలును నిలిపివేసి, నేరుగా ఏజెంట్‌కు ఎర్రర్‌ను తిరిగి పంపుతుంది.

నేను బ్లాక్ చేసే ప్యాటర్న్‌లు సరళమైనవి మరియు స్పష్టమైనవి:

  • git push --force లేదా నేను ఇంకా నమ్మని ఏదైనా force-with-lease వెరియంట్
  • git reset --hard
  • rm -rf

ఇది క్లిష్టమైన సెక్యూరిటీ రీసెర్చ్ కాదు. ఇది ఒక సీట్‌బెల్ట్ వంటిది. కానీ ఇక్కడ ముఖ్యమైన విషయం ఏమిటంటే, బ్లాక్ చేసిన తర్వాత ఏం జరుగుతుంది అనేది.

నేను ఎప్పుడూ కేవలం “Blocked” అని మాత్రమే చెప్పను. ఒక మొరటు తిరస్కరణ ఏజెంట్‌ను అయోమయానికి గురి చేస్తుంది మరియు అదే వినాశకరమైన కమాండ్‌ను మళ్ళీ మళ్ళీ ప్రయత్నించే లూప్‌లో చిక్కుకునేలా చేస్తుంది. దానికి బదులుగా, ఎర్రర్ మెసేజ్‌లో ఒక ఎస్కేప్ రూట్ (escape route) ఉంటుంది. హుక్ ఒక hard resetను గుర్తించినప్పుడు, అది ఏజెంట్‌కు ఇలా చెబుతుంది: “uncommitted పనులను రక్షించడానికి ఈ కమాండ్ బ్లాక్ చేయబడింది. ముందుగా ఒక checkpoint ని కమిట్ చేయండి, ఆపై మళ్ళీ ప్రయత్నించండి.” ఆ అదనపు వాక్యం ఏజెంట్ ప్రవర్తనను పూర్తిగా మారుస్తుంది. అది నష్టాన్ని కలిగించే ప్రయత్నం నుండి భద్రతను సృష్టించే దిశగా మళ్లుతుంది. హుక్ అనేది కేవలం ఒక గోడ మాత్రమే కాదు; అది ట్రాఫిక్ కంట్రోల్.

నేను షెల్ కమాండ్‌ల కోసం allowlist కంటే denylistనే ఎంచుకున్నాను. మొదట, నేను కేవలం కొన్ని సురక్షితమైన git సబ్-కమాండ్‌లను మాత్రమే అనుమతించాలని అనుకున్నాను. కానీ అది త్వరగానే విఫలమైంది. ఏజెంట్‌లు సృజనాత్మకంగా మరియు అక్షరాలా (literally) పనిచేస్తాయి. అవి స్టేట్‌ను తనిఖీ చేయడానికి git stash push -m "wip" లేదా git branch --show-current వంటి చట్టబద్ధమైన కానీ ఊహించని కమాండ్‌లను రన్ చేస్తాయి. మోడల్ ఒక చెల్లుబాటు అయ్యే కానీ జాబితాలో లేని కమాండ్‌ను సృష్టించిన వెంటనే allowlist సాధారణ వర్క్‌ఫ్లోను దెబ్బతీస్తుంది. నిజంగా వినాశకరమైన ప్యాటర్న్‌ల యొక్క చిన్న, క్యూరేటెడ్ denylist, సరిహద్దులను రక్షిస్తూనే ఏజెంట్‌కు పని చేయడానికి అవకాశం ఇస్తుంది.

ది ఫార్మాటర్ హుక్ (The Formatter Hook): పనిని ఆటోమేట్ చేయండి

"ఫైల్‌ను ఎడిట్ చేసిన తర్వాత ఎల్లప్పుడూ ఫార్మాటర్‌ను రన్ చేయండి" అని ఏజెంట్‌కు చెప్పడానికి నేను ప్రాంప్ట్ టోకెన్లను వృథా చేసేవాడిని. అది సగం సమయాల్లో మర్చిపోయేది. మిగిలిన సగం సమయాల్లో, అది ఆగి ఫార్మాట్ చేయాలా అని అడిగేది, దీనివల్ల ఒకే సరైన సమాధానం ఉన్న నిర్ణయం కోసం ఒక టూల్ కాల్ వృథా అయ్యేది.

ఇప్పుడు నేను దానిని PostToolUse హుక్ ద్వారా నిర్వహిస్తున్నాను. ఏజెంట్ ఒక ఫైల్‌ను ఎడిట్ చేసిన తర్వాత, హుక్ ఆ ఫైల్ ఎక్స్‌టెన్షన్‌ను తనిఖీ చేస్తుంది. అది Python అయితే, Ruff రన్ చేస్తుంది. అది JavaScript లేదా TypeScript అయితే, Prettier రన్ చేస్తుంది. అది Go అయితే, gofmt రన్ చేస్తుంది. ఫార్మాటర్ ఉనికి గురించి ఏజెంట్‌కు తెలియదు. దానికి తెలియాల్సిన అవసరం కూడా లేదు.

దీనిని ప్రాంప్ట్ నుండి బయటకు తీయడం వల్ల రెండు ప్రభావాలు కలిగాయి. మొదటిది, మోడల్‌పై మానసిక భారం (cognitive load) పెంచకుండానే కోడ్ నిరంతరం శుభ్రంగా ఉంటుంది. రెండవది, నా ప్రాజెక్ట్ సూచనలు చిన్నవిగా మారాయి. మీరు ప్రాంప్ట్ నుండి తొలగించే ప్రతి “always” మరియు “never” అనేది మోడల్ అసలు సమస్య పరిష్కారం కోసం ఉపయోగించగల ఒక టోకెన్. హుక్ అనేది నిరంతర నియమాన్ని (invariant) నిర్వహిస్తుంది; ప్రాంప్ట్ అనేది ఉద్దేశ్యాన్ని (intent) నిర్వహిస్తుంది.

ది క్వాలిటీ గేట్ (The Quality Gate): “పూర్తయింది” అనే దానిని పునర్నిర్వచించడం

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.