AIUnlimited
🌳

أسس الذكاء الاصطناعي

🌱
AI Seeds

Start from zero

🌿
AI Sprouts

Build foundations

🌳
AI Branches

Apply in practice

🏕️
AI Canopy

Go deep

🌲
AI Forest

Master AI

🔨

إتقان الذكاء الاصطناعي

✏️
AI Sketch

Start from zero

🪨
AI Chisel

Build foundations

⚒️
AI Craft

Apply in practice

💎
AI Polish

Go deep

🏆
AI Masterpiece

Master AI

📘

تطبيق الذكاء الاصطناعي

📖
فهم النماذج مفتوحة المصدر

أسس وموارد للنماذج مفتوحة المصدر

🎯
من المشكلة إلى مهمة النموذج

تحويل مشاكل الأعمال إلى مهام نموذجية

⚡
تشغيل نموذجك الأول

شاهد نتائجك الأولى في 30 دقيقة

🔧
التحسين الدقيق والتقييم

حسّن النماذج وقيّم الأداء

🚀
أنظمة التطبيقات

بناء تطبيقات ذكاء اصطناعي واقعية

🎨
الذكاء الاصطناعي التوليدي

استكشف نماذج AIGC مفتوحة المصدر

🤖
الوكيل

تعلم أطر عمل الوكيل وأدوات MCP

📐
أسس تكميلية

أساسيات LLM والتقييم

🎓

أكاديمية كلاود

🤖
Claude 101

Learn AI basics with Claude

💻
Claude Code 101

Code with Claude as your pair programmer

🤝
Introduction to Claude Cowork

Collaborate with Claude on complex projects

⚙️
Claude Platform 101

Build apps with the Claude API

المختبر

تم تحميل 7 تجارب
🧬ملعب الشبكة العصبية🤖ذكاء اصطناعي أم إنسان؟🥋دوجو التوجيهات🏁سباق الخوارزميات🧠مسابقة معلومات الذكاء الاصطناعي🏗️لوحة تصميم النظام
🎯مقابلة تجريبيةدخول المختبر→
🚀

التطوير المهني

🚀
منصة انطلاق المقابلات

ابدأ رحلتك

🌟
إتقان المقابلات السلوكية

أتقن المهارات الشخصية

💻
المقابلات التقنية

تفوّق في جولة البرمجة

🤖
مقابلات الذكاء الاصطناعي وتعلم الآلة

إتقان مقابلات تعلم الآلة

🏆
العرض وما بعده

احصل على أفضل عرض

ابدأ الآن
AIUnlimited

رخصة MIT

沪ICP备18025655号-11

تعلّم

  • أساسيات الذكاء الاصطناعي
  • تطبيق الذكاء الاصطناعي
  • أكاديمية كلاود
  • المختبر
  • التطوير المهني

المجتمع

  • عن المنصة
  • الأسئلة الشائعة

الدعم

  • footer.terms
  • footer.privacy
  • footer.contact
البرامج الأكاديمية للذكاء الاصطناعي والهندسة›💻 Claude Code 101›الدروس›Hooks
🪝
Claude Code 101 • متقدم⏱️ 6 دقيقة للقراءة

Hooks

Hooks

Hooks let you run commands at specific points in Claude Code's lifecycle. The key difference between hooks and everything else covered in this course is that hooks are deterministic — they always run.

Why Use Hooks

You can tell Claude in your CLAUDE.md to run Prettier after every file edit. Most of the time it will. But sometimes it won't. A hook makes it happen every single time, no exceptions.

Common use cases include:

  • Auto-formatting after file edits
  • Logging all executed commands for compliance
  • Blocking dangerous operations like modifying production files
  • Sending yourself notifications when Claude finishes a task

How They Work

Hooks are configured in your settings.json. You pick an event, optionally set a matcher for which tools it applies to, and provide a command to run. Some of the most common events are:

  • PreToolUse — runs before a tool call
  • PostToolUse — runs after a tool call completes
  • UserPromptSubmit — runs when you submit a prompt, before Claude processes it
  • Stop — runs when Claude finishes responding
  • Notification — runs when Claude sends a notification

These are just a few of the events you can hook into — Claude Code supports many more. See the hooks reference for the full list.

You configure them through the /hooks command inside Claude Code, or by editing settings.json directly.

A Practical Example

The most common hook: auto-formatting after edits. Set a PostToolUse hook with a matcher of "Edit|MultiEdit|Write" so it fires whenever Claude modifies a file. The command checks the file extension and runs the appropriate formatter — Prettier for TypeScript, gofmt for Go, whatever your project uses.

Blocking with PreToolUse

الدرس 12 من 120٪ مكتمل
←MCP

مناقشة

تسجيل الدخول للانضمام إلى النقاش

PreToolUse hooks can block tool calls before they execute. Your hook receives the tool name and input as JSON on stdin. The exit code determines the behavior:

  • Exit code 0 — proceed normally.
  • Exit code 2 — block the action. The stderr message gets fed back to Claude as feedback so it knows why it was blocked and can adjust.
  • Any other exit code — a non-blocking error that gets shown to you but doesn't stop anything.

This is how you enforce hard rules. Block writes to a production config directory. Block bash commands that contain rm -rf. Block commits to main. Whatever your team needs to be guaranteed, not suggested.

Sharing Hooks with Your Team

Hooks configured in .claude/settings.json are project-level and can be checked into your repo. This means your entire team gets the same hooks automatically. Use the CLAUDE_PROJECT_DIR environment variable in your commands to reference scripts stored in your project, so they work regardless of Claude's current working directory.

Recap

Hooks give you deterministic control over Claude Code's behavior. Use PostToolUse for auto-formatting and logging. Use PreToolUse to block dangerous operations. Configure them with /hooks or in settings.json. And check them into your repo so your team gets them too.

If something needs to happen every time without fail, don't put it in a prompt. Put it in a hook.