ai newsDi., 1. Dez.

Claude Code Subagents: Eine Markdown-Datei, eigenes Modell, eigene Tools

Ein Custom Subagent in Claude Code ist eine Markdown-Datei mit Name, Beschreibung und System-Prompt im Verzeichnis .claude/agents/ und damit teamweit versionierbar. Jeder Subagent läuft in frischem Kontext mit eigener Tool-Liste und eigenem Modell, zurückgegeben wird nur das Ergebnis.

dev.toMontag 08:17 · 30.11.DE6 Min. Lesezeit
Bild: dev.to

Posted on Nov 30 • Originally published at bridge-terminal.com • Fully Autonomous

How do you create a custom subagent in Claude Code? Write a Markdown file with a name, a description and a system prompt, and save it in.claude/agents/ (project) or ~/.claude/agents/ (all your projects). Claude then delegates matching tasks to it, in its own context window, with the tools and model you chose.

This guide covers where the files go, which fields matter, three working examples, how to cut cost, and the limits to know. Everything below comes from the Claude Code subagents documentation. Fields and version numbers change, so check that page against your installed version.

A subagent is a specialised assistant that runs in its own context window with a custom system prompt, restricted tool access and independent permissions. When it finishes, its result comes back to your main conversation. That keeps verbose output such as search results, logs and file contents out of the context you are paying to carry.

Claude Code also ships built-in subagents. Explore does fast, read-only codebase search, Plan does research while in plan mode, and general-purpose handles multi-step work with every tool available to subagents. You can block a specific one in settings with permissions.deny, for example Agent(Explore).

When two subagents share a name, the higher-priority location wins:

Claude Code scans these directories recursively, so subfolders are fine. Identity comes only from the name field, so keep names unique. Changes are detected within seconds, though files in directories added with --add-dir need a restart.

For a team, commit.claude/agents/ to the repository. The docs list "check into version control" among their best practices, and it is the simplest way to make every engineer's agent behave the same. To share across repositories, package subagents in a plugin. Plugin subagents ignore hooks, mcpServers and permissionMode, so put hooks in the plugin's own hooks instead.

Only name and description are required. Multi-word field names are camelCase, and unrecognised fields are ignored without an error, so a typo fails quietly. Run claude --debug and search the log for does not recognize or apply to find them.

--- name: code-reviewer description: "Expert code review specialist. Proactively reviews code for quality, security, and maintainability. Use immediately after writing or modifying code." tools: Read, Grep, Glob, Bash model: inherit --- You are a senior code reviewer ensuring high standards of code quality and security.

When invoked: 1. Run git diff to see recent changes 2. Focus on modified files 3.

Begin review immediately Provide feedback organized by priority: - Critical issues (must fix) - Warnings (should fix) - Suggestions (consider improving) Enter fullscreen mode Exit fullscreen mode This is shortened from the docs' example. Note the docs' own caveat: leaving out Edit and Write does not make the reviewer read-only, because it can still change files through Bash. If that matters, use a hook (Example 3).

--- name: debugger description: Debugging specialist for errors, test failures, and unexpected behavior. Use proactively when encountering any issues. tools: Read, Edit, Bash, Grep, Glob --- You are an expert debugger specializing in root cause analysis. When invoked: 1. Capture error message and stack trace 2. Identify reproduction steps 3. Isolate the failure location 4. Implement minimal fix 5. Verify solution works Enter fullscreen mode Exit fullscreen mode The difference from the reviewer is one word in tools: Edit. Give a subagent the narrowest list that lets it finish its job.

When a tools list is too coarse, add a PreToolUse hook to the subagent. This definition allows Bash but runs a script before every command:

--- name: db-reader description: Execute read-only database queries. Use when analyzing data or generating reports. tools: Bash hooks: PreToolUse: - matcher: "Bash" hooks: - type: command command: "./scripts/validate-readonly-query.sh" --- You are a database analyst with read-only access. Execute SELECT queries only. Enter fullscreen mode Exit fullscreen mode The script reads the hook's JSON from stdin and exits with code 2 to block:

#!/bin/bash INPUT = $( cat ) COMMAND = $( echo "$INPUT" | jq -r '.tool_input.command // empty' ) [ -z "$COMMAND" ] && exit 0 if echo "$COMMAND" | grep -iE '\b(INSERT|UPDATE|DELETE|DROP|CREATE|ALTER|TRUNCATE|REPLACE|MERGE)\b' > /dev/null; then echo "Blocked: Write operations not allowed. Use SELECT queries only." >& 2 exit 2 fi exit 0 Enter fullscreen mode Exit fullscreen mode Run chmod +x on the script on macOS or Linux, or the hook fails instead of blocking. The docs call the hook a backstop: the prompt already tells the subagent to refuse writes, and the hook catches an attempt anyway. A keyword filter like this is crude, so treat it as a guard rail, not a security boundary.

For one-off or scripted use, --agents takes JSON for the current session:

claude --agents '{ "code-reviewer": { "description": "Expert code reviewer. Use proactively after code changes.", "prompt": "You are a senior code reviewer. Focus on code quality, security, and best practices.", "tools": ["Read", "Grep", "Glob", "Bash"], "model": "sonnet" } }' Enter fullscreen mode Exit fullscreen mode In non-interactive mode, --agents also accepts a path to a JSON file, for example claude -p --agents./agents.json "Review my changes". The docs say the file form needs v2.1.281 or later, and an interactive session refuses a file path.

Subagents are a context tool first. Used well they cut cost, because noisy work such as a long log read stays in the subagent and you carry only its result. Used carelessly they add cost: the docs warn that running many subagents that each return detailed results can consume significant context, and each subagent spends tokens of its own while it runs.

A related feature is the fork, a subagent that inherits the whole conversation so far. Because a fork's system prompt and tool definitions match its parent's, its first request reuses the parent's prompt cache, so it costs less than a fresh subagent when the task needs the same context. Fork mode is on by default from v2.1.302. You can start one yourself with /subtask followed by a task.

If subagents edit files in parallel, give them isolation: worktree so their edits land in a separate git worktree.

Custom subagents are cheap to write and easy to share: one file, a sharp description, a short tool list and a deliberate model. They cut context, not plan usage, so measure before you multiply them.

Teams that run many Claude Code sessions at once meet the same trade-offs at larger scale. Atlas is built on Claude Code and gives each engineer its own session, written role and model and effort for that role; its own helpers that read files run on Haiku. Managing subagent definition files is not something Atlas does today; that is coming to Atlas, built in, so it needs no setup.

Originaltitel: Claude Code Custom Subagents: Files, Tools and Model CostQuelle: dev.to (dev.to). Original öffnen