A Beginner's Guide to Using Subagents & Hooks in Claude Code
Hey there! So, you've been playing around with Claude Code & you're ready to level up your workflow. You've probably heard about subagents & hooks, but the jargon can be a bit much. Honestly, it's not as complicated as it sounds. I'm here to break it down for you, developer to developer, & show you how these features can seriously change the game. We're talking about automating the boring stuff & building a team of AI specialists to help you code. Pretty cool, right?
What in the World are Subagents?
Think of subagents as creating your own little team of AI assistants inside Claude Code. Instead of having one AI trying to do everything – writing code, reviewing it, debugging, testing – you can delegate specific jobs to specialized helpers. Each subagent is like an expert in its own little area. For example, you could have a "Code Reviewer" agent, a "Bug Fixer" agent, or a "Test Writer" agent.
The real magic here is that each subagent has its own separate memory, or context window. This means they work in their own little sandboxes, so they don't get confused or mix up information with other agents working on different tasks. It keeps everything clean & organized. Plus, you get to decide what tools & files each subagent has access to, giving you a ton of control.
Creating Your First Subagent: A Step-by-Step Vibe
Getting started with subagents is surprisingly easy. Here’s the lowdown:
- Open the Subagent Interface: In your Claude Code terminal, just type . This will bring up the subagent management screen.
- Create a New Agent: You'll see an option to "Create New Agent." You can choose to make it a "project-level" agent (only for your current project) or a "user-level" agent (available in all your projects). I’d recommend starting with project-level agents to keep things organized.
- Let Claude Do the Heavy Lifting: You can manually set up your agent, but the coolest way is to "Generate with Claude." You just describe what you want the agent to do, & Claude will write the system prompt & configure it for you.
- Define its Role: Be as comprehensive as you can in your description. For example, you could say: "An agent that specializes in writing unit tests for my React components using Jest & React Testing Library."
- Configure Tools: This is a SUPER important step. You can give your subagent access to all your tools, or you can be selective. For a testing agent, you might only give it read-only access to the code & the ability to run testing commands.
- Save & You're Done!: Claude will show you the markup file it created for your new agent. Just save it, & your new subagent is ready to go!
Practical Examples of Subagents You Can Create Today
The possibilities with subagents are pretty much endless. To get your gears turning, here are some ideas, ranging from simple to more advanced:
- The Code Reviewer: This is a classic. You can create a subagent that's an expert in your team's coding style guide. It can automatically review your code for style violations, potential bugs, & areas for improvement.
- The Documentation Writer: Hate writing docs? Same. Create a subagent that can read your code & generate documentation for it. You can even train it to follow a specific documentation format like JSDoc or reStructuredText.
- The Database Guru: This subagent is an expert in SQL. You can give it access to your database schema & have it write complex queries, optimize existing ones, or even design new tables for you.
- The Frontend Specialist: Got a favorite frontend framework? Create a subagent that's an expert in it. Whether it's React, Vue, or Svelte, this agent can help you build components, follow best practices, & troubleshoot framework-specific issues.
- The Legacy Code Modernizer: This is a more advanced one. You can create a subagent to help you refactor old code. It can identify outdated patterns, suggest modern alternatives, & even help you migrate from an old framework to a new one.
- The Security Analyst: This subagent is your personal security expert. It can scan your code for common vulnerabilities, check for insecure dependencies, & suggest ways to make your application more secure.
I found this awesome
GitHub repository with over 100 production-ready subagents for all sorts of tasks. It's a goldmine of ideas & a great starting point for building your own.
Best Practices for Juggling Your Subagent Team
As you start creating more subagents, you'll want to keep a few things in mind to avoid chaos:
- Keep them focused: It's better to have several subagents with single, clear responsibilities than one subagent that tries to do everything. This makes them more predictable & performant.
- Write detailed prompts: The more specific you are in your instructions, the better your subagent will perform. Include examples & constraints to guide its behavior.
- Be smart about tool access: Only give subagents the tools they absolutely need. This improves security & helps them focus on the task at hand.
- Use version control: Check your project-level subagents into Git. This way, your whole team can use them, contribute to them, & keep them consistent.
- Manage their memory: Remember, each subagent has its own context. This is great for focus, but it can also lead to information silos. A really interesting idea is to have a "memory" subagent whose sole job is to search past conversations & provide relevant context to other subagents. This can help your team of agents "learn" from past work.
And here’s where a tool like Arsturn can come in handy. While we're talking about building specialized AI assistants for coding, many businesses need similar assistants for their websites. Arsturn helps businesses create custom AI chatbots trained on their own data. These chatbots can provide instant customer support, answer questions, & engage with website visitors 24/7. It's the same principle of specialized AI, just applied to customer interaction instead of code.
Getting Hooked on Claude Code Hooks
Now, let's switch gears to hooks. If subagents are your AI team, hooks are your automation rules. They're user-defined shell commands that automatically run at specific points in Claude Code's lifecycle. Think of them as programmable tripwires.
Here's the thing: while Claude is amazing, it's still a probabilistic model. You can't always guarantee it will remember to do something every single time. Hooks solve this problem by providing deterministic control. You can set up a hook to, say, run a linter every time a file is saved, & it will happen EVERY. SINGLE. TIME. No more "oops, I forgot to run Prettier."
The Different Types of Hooks
Claude Code gives you a few different "events" you can hook into:
- PreToolUse: This runs before Claude uses a tool. This is your chance to do things like validate code, check for permissions, or even block the tool from running altogether.
- PostToolUse: This runs after a tool has been used successfully. It's perfect for things like auto-formatting code, running tests, or updating documentation.
- Notification: This runs whenever Claude sends a notification, like when it needs your input. You could use this to send yourself a desktop notification so you don't miss anything.
- Stop: This runs when Claude finishes generating a response. Great for cleanup tasks.
- Subagent Stop: This runs when a subagent finishes its task.
How to Set Up Your First Hook
Setting up hooks is done through the
command, which is pretty straightforward.
- Open the Hooks Configuration: Type in the Claude Code terminal.
- Choose an Event: Select the event you want to hook into, for example, .
- Add a Matcher: This is how you tell the hook when to run. You can match based on the tool name. For example, you could use to run the hook whenever Claude tries to write or edit a file. You can also use to match all tools.
- Create the Hook Command: This is the shell command you want to run. For example, if you're setting up a pre-commit hook, your command might be a script that runs your linter & tests.
- Save Your Configuration: You can save the hook at the user level (for all projects) or the project level.
Real-World Hook Examples to Automate Your Life
Here are some practical ways you can use hooks to make your life easier:
- Automatic Code Formatting: This is a must-have. Set up a hook that runs a formatter like Prettier or Black on any file that Claude edits.
- Pre-Commit Sanity Checks: Use a hook to run a script that checks for things like API keys or other secrets before Claude is allowed to make a commit.
- Chained Hooks for Complex Workflows: While Claude doesn't have a built-in way to chain hooks in a sequence, you can achieve this by having your hook command call a script that runs multiple commands in order. For example, you could have a single script that first formats the code, then runs the linter, & then runs the tests.
- Custom Notifications: Tired of missing Claude's requests for input? Set up a hook that uses a command-line tool to send a push notification to your phone or desktop.
- Intelligent Git Commits: This is a really cool one I saw. You can set up a series of hooks that automatically create a "checkpoint" commit after every file change. Then, when you're done with a task, a hook can squash all those checkpoint commits into a single, meaningful commit.
When it comes to building these kinds of automated, interactive experiences, it's not just about code. For businesses looking to automate their customer engagement, Arsturn offers a powerful solution. It allows you to build no-code AI chatbots trained on your own business data. These bots can do more than just answer questions; they can generate leads, boost conversions, & provide personalized customer experiences, all without your team having to lift a finger. It's the same kind of smart automation, just applied to your website.
Tips for Mastering Hooks
- Start Simple: Don't try to build a super complex, multi-stage hook workflow from day one. Start with a simple formatting hook, get comfortable with how it works, & then gradually add more complexity.
- Test Your Commands: Before you put a command in a hook, run it directly in your terminal to make sure it works as expected. Trust me, it'll save you a lot of headaches.
- Use the Wisely: Be specific with your matchers to avoid running hooks when you don't want to. You can use simple strings, or even regex for more complex matching.
- Keep Your Clean: For more complex hooks, it's a good idea to put the logic in an external script rather than trying to cram a bunch of shell commands into your file. This makes your hooks easier to read, debug, & maintain.
Tying It All Together
So, there you have it. Subagents are your specialized AI teammates, & hooks are your automation rulebook. By using them together, you can transform your coding workflow from a manual, repetitive process into a streamlined, automated, & dare I say, FUN experience.
You can have a subagent draft a new feature, then a series of hooks can automatically format the code, run tests, & even create a pull request for you. It's like having a hyper-efficient junior developer who never sleeps & never complains.
Honestly, the best way to learn this stuff is to just dive in & start experimenting. Create a simple subagent. Set up a basic hook. See what works for you & what doesn't. The more you play with these tools, the more you'll discover how powerful they can be.
Hope this was helpful! Let me know what you think, & I'd love to hear about the cool subagents & hooks you come up with.