Manage your Prompts with PROMPT01 Use "THEJOAI" Code 50% OFF

Claude Code Agent Packs Bundle

Claude Code Agent Packs Bundle
Launch Date: Nov. 23, 2026
Pricing: No Info
Claude Code, AI Agents, Software Development, Automation, SDK Integration

Claude Code Agents & Subagents: The Complete Guide

What Are Claude Code Agents?

Claude Code operates as an agentic AI system. It does not just answer questions. It autonomously plans and executes multi-step coding tasks. When you give Claude Code a complex instruction like "refactor the authentication system to use JWT tokens," it breaks the work down. It reads relevant files. It makes edits. It runs tests. It iterates until the task is complete.

The key innovation is subagents. Claude Code can spawn independent child processes via the Task tool. Each subagent gets its own context window and tool access. This enables parallel execution. It allows context isolation. It enables specialization. These are the same architectural patterns used in distributed systems. They are now applied to AI coding.

Why Agents Matter

Without agents, Claude Code would be limited to a single conversation thread with one context window. Subagents let it tackle ambitious tasks. These tasks include migrating an entire codebase. They include running a comprehensive test suite. They include researching a complex question. The system divides work across specialized child processes. These processes report results back to the parent.

Agent Architecture: How It Works

Claude Code's agent system has three layers. The primary agent is your main Claude Code session. It manages the overall workflow. Subagents handle specific parts of the work. They return findings to the parent. The primary agent synthesizes results. It then makes the necessary edits.

Key Architectural Concepts

  • Context isolation— Each subagent has its own context window. This prevents the parent from being overwhelmed by large search results or verbose output.
  • Parallel execution— Multiple subagents can run simultaneously. This dramatically speeds up tasks like searching across multiple files or running independent tests.
  • Tool specialization— Each agent type has access to specific tools. An Explore agent can read files but not edit them. A Bash agent can run commands but not search the web.
  • Result aggregation— Subagents return a single summary message to the parent. This keeps the primary context clean and focused.
  • Background execution— Subagents can run in the background. The primary agent can continue working on other tasks while they run.

Built-in Agent Types

Claude Code ships with several specialized subagent types. Each is designed for a specific class of work.

  • Explore: Fast codebase exploration. It finds files. It searches code. It answers questions about architecture.
  • Bash: Command execution. It runs git. It runs npm. It runs docker. It runs tests. It handles other terminal operations.
  • Plan: Architecture design. It creates implementation plans. It identifies critical files. It considers trade-offs.
  • general-purpose: Versatile agent for research. It handles multi-step tasks. It answers complex questions requiring multiple tool calls.

Plugins can register additional agent types. For example, the feature-dev plugin adds specialized agents for code review. It adds agents for code exploration. It adds agents for architecture design.

  • code-explorer: Deep analysis of existing features. It traces execution paths. It maps architecture.
  • code-reviewer: Reviews code for bugs. It checks for security issues. It checks for quality. It uses confidence-based filtering.
  • code-architect: Designs feature architectures. It creates implementation blueprints. It defines build sequences.

The Task Tool: Spawning Subagents

The Task tool is how Claude Code spawns subagents. The primary agent uses this tool when it encounters a subtask. This subtask benefits from isolation or parallelism. The agent delegates the work to a subagent.

Task Tool Parameters

  • subagent_type— Which agent type to use. Options include Explore, Bash, Plan, general-purpose, or custom plugin agents.
  • description— A short 3-5 word description of the task.
  • prompt— Detailed instructions for the subagent.
  • runinbackground— Set true to run without blocking the parent agent.
  • model— Override the model. Use haiku for fast, cheap tasks.
  • isolation— Set worktree to give the agent an isolated copy of the repository.
  • max_turns— Limit the number of API round-trips.
  • mode— Permission mode. Use plan to require approval. Use bypassPermissions for CI.

Parallel Subagent Execution

One of the most powerful patterns is spawning multiple subagents simultaneously. When tasks are independent, they can run in parallel. Results are returned as they complete.

Example:Claude Code spawns three subagents in parallel. One finds all API route handlers. Another finds all database migration files. The third runs the test suite and reports failures.

Creating Custom Agents in Plugins

You can create custom agent types through the agents directory in your plugin. This allows you to tailor the system to your specific needs.

Agent File Structure

The structure is simple. You define your agents in markdown files within the agents folder.

Agent Definition Format

The definition consists of frontmatter metadata followed by the system prompt. The frontmatter includes required fields like name and description. It also lists the tools the agent can access. You can override the model here too.

System Prompt (Body Content):The Markdown body below the frontmatter becomes the agent's system prompt. Structure it clearly. Start with a role statement. Define the step-by-step workflow. Specify the output format. List constraints. This tells the agent exactly what to do.

Critical Note on Descriptions:The agent's description field is the trigger mechanism. Write it with explicit trigger phrases. Say "Use this agent when the user says X" or "Trigger when Y condition exists." Without clear triggers, your agent may never be invoked automatically.

The Claude Code Agent SDK

For building standalone applications powered by Claude Code's agent capabilities, Anthropic provides the Claude Code SDK. This allows developers to integrate the system into their own workflows.

Installation

You install the SDK using npm. The package name is @anthropic-ai/claude-code.

Basic Usage (TypeScript)

You can run Claude Code programmatically. You pass a prompt and a working directory. You specify allowed tools. You set a maximum number of turns. The SDK returns the result messages.

Python SDK

You can also use the SDK in Python. The function signature is similar to the TypeScript version. You pass the prompt and directory. You define the allowed tools. You get the result back.

SDK Use Cases

  • CI/CD Integration— Run Claude Code in your CI pipeline. It can auto-fix linting errors. It can generate missing tests. It can review PRs.
  • Code Review Bots— Build a GitHub bot that reviews every PR. It uses Claude Code's agent capabilities.
  • Migration Scripts— Automate large-scale codebase migrations. Use programmatic Claude Code invocations.
  • Custom IDE Extensions— Build VS Code extensions. Leverage Claude Code's autonomous capabilities.
  • Batch Processing— Process multiple files or repositories in parallel. Use orchestrated agent sessions.

Multi-Agent Patterns & Workflows

Pattern 1: Research-then-Act

The most common pattern involves spawning Explore subagents first. They gather information. The results inform the next steps. The primary agent then makes changes based on the research.

Pattern 2: Worktree Isolation

For experimental or risky changes, spawn a subagent in an isolated git worktree. If the changes do not work out, the worktree is discarded automatically. This is a safe way to test new ideas.

Pattern 3: Background Monitoring

Spawn long-running subagents in the background. Continue working interactively. Get notified when tests complete. This keeps your workflow smooth.

Pattern 4: Fan-out / Fan-in

Distribute work across multiple subagents. Then aggregate results. This is ideal for large codebases. Search different folders in parallel. Combine all findings into one report.

Best Practices

Writing Effective Agent Prompts

  1. Be specific about output format— Tell the subagent exactly what to return. Specify file paths. Specify function names. Specify a one-line summary for each finding.
  2. Set clear boundaries— Specify what the agent should and should not do. Say "Only search, do not modify any files."
  3. Provide context— Include relevant information the subagent needs. Provide file paths. Share error messages. State constraints.
  4. Use the right agent type— Do not use a general-purpose agent for simple searches. Use Explore for speed.

Performance Tips

  • Parallelize independent tasks— If tasks do not depend on each other, spawn them simultaneously.
  • Use model haiku for simple agents— This saves cost and latency for straightforward tasks like running commands or simple searches.
  • Limit max_turns— Prevent subagents from going on long tangents. Cap their API calls.
  • Prefer Explore over general-purpose for search— Explore agents are optimized for fast codebase navigation.
  • Use background execution wisely— Great for test suites and builds. Do not forget to check results.

Security Considerations

  • Restrict agent tools to only what is needed. A search agent should not have Edit or Bash access.
  • Use isolation worktree for agents that might make destructive changes.
  • Set mode plan for agents that need human approval before executing.
  • Never pass secrets in agent prompts. Use environment variables instead.

Real-World Examples

Example: Feature Development Agent

A common plugin pattern combines multiple agents into a guided feature development workflow. The agent understands the codebase. It designs the architecture. It implements the feature. It verifies the code. It reports what was built.

Example: Security Audit Agent

This agent runs a comprehensive security audit. It triggers on specific user requests. It scans the codebase for OWASP Top 10 vulnerabilities. It checks for SQL injection. It checks for XSS. It checks for authentication issues. It checks for SSRF. It checks for dependency vulnerabilities. It reports findings with severity. It includes file paths and recommended fixes.

Frequently Asked Questions

Can subagents spawn their own subagents?

Yes. If the subagent has access to the Task tool, it can spawn its own subagents. This enables hierarchical agent workflows. A high-level orchestrator delegates to mid-level agents. Mid-level agents delegate to specialized workers. Keep nesting shallow. Two to three levels is the maximum to avoid excessive token usage.

How many subagents can run in parallel?

There is no hard limit on concurrent subagents. Each one consumes API tokens and compute resources. In practice, three to five parallel subagents is the sweet spot for most tasks. Going beyond ten rarely provides additional benefit. It increases costs significantly.

Do subagents share context with the parent?

No. Each subagent starts with a fresh context window. It contains only the prompt you provide. It cannot see the parent's conversation history. Results are returned as a single summary message.

What happens if a subagent fails?

The parent agent receives the error or partial results. It can then retry with a different approach. It can spawn a new subagent with adjusted instructions. It can handle the failure gracefully. Subagents do not crash the parent session.

How do I debug subagent behavior?

Use runinbackground true and check the output file for the full transcript. You can also use max_turns to limit execution. Inspect intermediate steps. For plugin agents, test them interactively before deploying.

Are agents the same as MCP servers?

No. They are different technologies with different capabilities.

NOTE:

This content is either user submitted or generated using AI technology (including, but not limited to, Google Gemini API, Llama, Grok, and Mistral), based on automated research and analysis of public data sources from search engines like DuckDuckGo, Google Search, and SearXNG, and directly from the tool's own website and with minimal to no human editing/review. THEJO AI is not affiliated with or endorsed by the AI tools or services mentioned. This is provided for informational and reference purposes only, is not an endorsement or official advice, and may contain inaccuracies or biases. Please verify details with original sources.

Comments

Loading...