Aydin Aliyev
EN

Bu səhifə yalnız ingilis dilindədir.Эта страница есть только на английском.

← Log

Claude Code docs: subagents get their own context but share usage limits

In short

Claude Code subagents get their own context window but draw on your usage limits. Give each one narrow tools and a short description, and check them in CI.

Claude Code subagents are separate workers that Claude hands a side task to. Anthropic's docs say each one runs in its own context window with a custom system prompt, specific tool access and independent permissions [1], and that its requests count toward the same usage limits as your main conversation [2].

That makes a subagent the place for work that reads a lot and reports a little, such as a long test log, while your main conversation stays short. Two fields in its file need care. With no tools line, it gets every tool available to subagents [3]. Its description takes up context even in a session where it never runs [4].

What the docs say about subagents

A subagent starts with a fresh context window. It doesn't see your conversation history, the skills you have already invoked or the files Claude has already read. It works from the task message Claude writes for it, its own system prompt and your CLAUDE.md files. A fork is the exception: it inherits the parent conversation [1].

Its file is Markdown with YAML frontmatter, and the body becomes its system prompt. Put it in .claude/agents/ for one project, or in ~/.claude/agents/ for every project on your machine. Only name and description are required. These optional fields decide what the subagent can reach and what it loads [3]:

FieldWhat it setsWithout it
toolsThe tools the subagent may use, as an allowlistEvery tool available to subagents
disallowedToolsTools taken out of the inherited or listed setNothing is taken out
modelhaiku, sonnet, opus, fable, a full model ID, or inheritThe model of the conversation that starts it
omitClaudeMdtrue starts it without your user, project and local CLAUDE.md files (Claude Code v2.1.271 or later)Those CLAUDE.md files load
isolationworktree runs it in a temporary git worktree, an isolated copy of the repositoryIt starts in the main conversation's working directory

Write a narrow subagent

This one does a single routine job: it reads test and build logs after a failure and reports only what failed.

.claude/agents/test-log-reader.md
---
name: test-log-reader
description: Reads test and build logs and reports only the failures. Use after a test run fails.
tools: Read, Grep, Glob
model: haiku
---
 
Read the log files named in your task. For each failing test, report its
file, the assertion that failed and the first stack frame inside this
repository. Leave out passing tests, warnings and timing lines.

The description says what the subagent does and when to use it, and nothing more, since Claude delegates on it. The steps go in the body, which only loads when that subagent runs [4].

The tools line is an allowlist, and Bash is the entry to watch. A subagent that has Bash can still change files through it, even with no Edit or Write tool. To keep Bash and stop one command, such as git push, listing Bash(git push *) under disallowedTools doesn't work: an entry with a specifier still removes the whole tool from the subagent [5]. Add a deny rule to your settings instead.

.claude/settings.json
{
  "permissions": {
    "deny": ["Bash(git push *)"]
  }
}

A scoped rule like this leaves the tool available and blocks matching calls when Claude attempts them [6], and it applies to the main conversation and to subagents [5].

Without a model line, a subagent runs on the same model as the conversation that starts it. The docs list routing tasks to faster, lower-cost models like Haiku as a way to control costs [7]. For a job as narrow as reading logs, I'd set model: haiku.

Check every subagent file in CI

This script runs over .claude/agents/ in CI. It fails when a subagent file names neither tools nor disallowedTools, and it prints each description's length, so a long one shows up in review.

scripts/check-agents.sh
#!/bin/sh
# Prints each subagent's description length, and fails when a subagent
# names neither tools nor disallowedTools, since it then inherits every tool.
status=0
for file in .claude/agents/*.md; do
  [ -e "$file" ] || continue
  front=$(awk 'NR == 1 && $0 == "---" { inside = 1; next } inside && $0 == "---" { exit } inside' "$file")
  description=$(printf '%s\n' "$front" | sed -n 's/^description:[[:space:]]*//p')
  echo "$file: description is ${#description} characters"
  if ! printf '%s\n' "$front" | grep -Eq '^(tools|disallowedTools):'; then
    echo "$file: no tools or disallowedTools, so it inherits every tool"
    status=1
  fi
done
exit "$status"

On a folder with the subagent above and a release-notes subagent that has no tools line, the script prints this and exits with status 1:

Output
.claude/agents/release-notes.md: description is 57 characters
.claude/agents/release-notes.md: no tools or disallowedTools, so it inherits every tool
.claude/agents/test-log-reader.md: description is 84 characters

Characters are not tokens, so the script can't tell you when you pass the docs' limit. Claude Code can: when the combined descriptions of your own subagents pass 15,000 tokens, it shows a warning at startup with the total token count [8].

When to keep the work in the main conversation

The docs advise staying in the main conversation when a task needs frequent back-and-forth or iterative refinement, when you're making a quick, targeted change, and when latency matters, since a subagent that isn't a fork starts fresh and may need time to gather context [9].

A subagent also lives inside one session [10]. Work that has to run across separate sessions needs one of the other features the docs point to, such as background agents or agent teams.

What to do

  1. Give every subagent a tools list with only the tools its job needs, and count Bash as write access.
  2. Block single commands, such as git push, with a rule in permissions.deny, not with a disallowedTools entry.
  3. Keep each description to what the subagent does and when to use it, and put the steps in the body.
  4. Set model on routine subagents instead of letting them run on your conversation's model.
  5. Run scripts/check-agents.sh in CI, and read the total in the startup warning if it appears.
  6. Keep back-and-forth work and quick changes in the main conversation.

Discussed on LinkedIn

Sources

  1. code.claude.com/docs/en/sub-agentsEach subagent runs in its own context window with a custom system prompt, specific tool access, and independent permissions.
  2. code.claude.com/docs/en/sub-agentsIt also sends its own requests, which count toward the same usage limits as your main conversation.
  3. code.claude.com/docs/en/sub-agentsInherits every tool available to subagents if omitted.
  4. code.claude.com/docs/en/sub-agentsmove detail into each subagent's system prompt, which only loads when that subagent runs
  5. code.claude.com/docs/en/sub-agentsstill removes the whole tool from the subagent, not only the matching commands
  6. code.claude.com/docs/en/permissionsleaves the tool available and blocks matching calls when Claude attempts them
  7. code.claude.com/docs/en/sub-agentsruns on the same model as the conversation that starts it
  8. code.claude.com/docs/en/sub-agentsWhen the combined descriptions of your subagents, except the built-in ones, exceed 15,000 tokens, Claude Code shows a warning at startup with the total token count.
  9. code.claude.com/docs/en/sub-agentsThe task needs frequent back-and-forth or iterative refinement
  10. code.claude.com/docs/en/sub-agentsSubagents work within a single session.