Subagent는 Tool Use입니다.
자손킴 @jasonkim@hackers.pub
지난 글에서는 Tool Use가 무엇이고 어떻게 동작하는지 알아보았다. 이번 글에서는 subagent가 Tool Use 위에서 어떻게 동작하는지 알아볼 것이다.
Tool Use 정리
Subagent 설명에 앞서 Tool Use를 간단히 정리해보고 넘어가자.
┌─────────────────────────────────────────────────────────────┐
│ 메인 메시지 루프 │
│ │
│ ┌──────┐ request ┌─────────┐ API call ┌─────┐ │
│ │ User │ ─────────────► │ Agent │ ────────────► │ LLM │ │
│ │ │ ◄───────────── │ │ ◄──────────── │ │ │
│ └──────┘ response │ │ tool_use └─────┘ │
│ │ │ │ │
│ │ ▼ │ │
│ ┌──────────────┐ │
│ │ Tools │ │
│ │ (Bash, Read, │ │
│ │ Glob, ...) │ │
│ └──────────────┘ │
└─────────────────────────────────────────────────────────────┘
Tool Use는 LLM이 외부 도구를 호출할 수 있게 해주는 메커니즘이다. Agent는 LLM에게 사용 가능한 도구 목록을 제공하고 LLM은 필요할 때 tool_use 응답을 반환한다. Agent는 해당 도구를 실행하고 결과를 다시 LLM에게 전달한다. 이 과정이 반복되면서 복잡한 작업을 수행한다.
Subagent란?
Subagent는 특정 작업에 특화된 AI 에이전트다. 각 subagent는 자신만의 컨텍스트 윈도우에서 독립적으로 작동하며 완료되면 결과를 메인 에이전트에게 반환한다.
Claude Code에서 subagent는 Task라는 도구로 구현되어 있다. 메인 에이전트의 시스템 프롬프트에는 Task 도구의 description으로 사용 가능한 모든 subagent 목록과 각각의 용도가 포함된다. 메인 에이전트는 이 description을 참고하여 적절한 subagent를 선택한다. 예를 들어 "코드베이스 구조 파악"이 필요하면 Explore를, "데이터베이스 스키마 설계"가 필요하면 database-schema-architect를 호출한다.
Subagent의 구조
Subagent는 마크다운 파일로 정의되고 YAML frontmatter에 다음과 같은 설정을 포함한다.
---
name: code-reviewer
description: Expert code review specialist. Use immediately after writing or modifying code.
tools: Read, Grep, Glob, Bash
model: sonnet
---
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
Review checklist:
- Code is simple and readable
- Functions and variables are well-named
...
각 필드의 역할은 다음과 같다:
| 필드 | 설명 |
|---|---|
name |
subagent의 고유 식별자 |
description |
언제 이 subagent를 사용해야 하는지 설명 |
tools |
이 subagent가 사용할 수 있는 도구 목록 |
model |
사용할 모델 (sonnet, opus, haiku 또는 inherit) |
YAML frontmatter 아래의 마크다운 본문이 subagent의 시스템 프롬프트가 된다. 이 프롬프트는 subagent가 호출될 때 API 요청의 system 필드에 주입되어 subagent의 행동을 정의한다.
이 subagent 정의는 메인 에이전트의 시스템 프롬프트에 있는 tools 섹션의 Task 도구 설명에 포함된다:
{
"name": "Task",
"description": "Launch a new agent to handle complex, multi-step tasks autonomously.
Available agent types and the tools they have access to:
- Explore: Fast agent specialized for exploring codebases. (Tools: All tools)
- Plan: Software architect agent for designing implementation plans. (Tools: All tools)
- database-schema-architect: Use this agent when you need to design database schemas...
...
"
}
Task 도구의 input schema는 다음과 같다:
{
"type": "object",
"properties": {
"subagent_type": {
"type": "string",
"description": "The type of subagent to launch"
},
"prompt": {
"type": "string",
"description": "The task to delegate to the subagent"
},
"description": {
"type": "string",
"description": "Short description of what the agent will do"
}
}
}
subagent_type: 호출할 subagent 종류 (Explore, Plan, general-purpose 등)prompt: subagent에게 전달할 작업 지시. 이 내용이 subagent의 user 메시지가 된다description: 3-5단어의 짧은 요약. 로그나 UI에 표시된다
Subagent 호출 과정
실제 메시지 흐름을 통해 subagent가 어떻게 동작하는지 살펴보자.
1단계: 메인 에이전트가 Task 도구 호출
사용자가 "선물을 주고받는 기능을 위한 entity 설계"를 요청했다고 하자. 메인 에이전트(Opus 4.5)는 먼저 코드베이스를 탐색해야 한다고 판단하고 Explorer subagent를 호출한다:
{
"content": [
{
"type": "thinking",
"thinking": "사용자가 선물을 주고받는 기능을 위한 entity 설계를 요청했습니다. Plan mode가 활성화되어 있으므로, 먼저 코드베이스를 탐색하여 기존 구조를 이해해야 합니다.\n\nPhase 1에 따라:\n1. 기존 entity 구조와 패턴을 이해하기 위해 Explore agent를 사용\n2. 선물 관련 기존 구현이 있는지 확인\n3. 사용자와 아이템 관련 entity가 어떻게 구성되어 있는지 파악\n\n먼저 Explore agent를 사용하여 기존 entity 구조를 파악하겠습니다."
},
{
"type": "text",
"text": "선물 주고받기 기능을 위한 entity 설계를 시작하겠습니다. 먼저 기존 코드베이스의 entity 구조와 관련 패턴을 탐색하겠습니다."
},
{
"type": "tool_use",
"id": "toolu_01QjShG7gnF36vLgGKCWBNjg",
"name": "Task",
"input": {
"subagent_type": "Explore",
"prompt": "이 NestJS TypeScript 프로젝트에서 entity 구조를 탐색해주세요:\n\n1. 기존 entity 파일들의 위치와 구조 파악...",
"description": "Entity 구조 탐색"
}
}
]
}
thinking 블록에서 메인 에이전트는 현재 상황(Plan mode 활성화)을 파악하고 Explore agent를 선택한 이유를 명시적으로 추론했다.
2단계: 새로운 메시지 루프에서 Subagent 실행
Agent는 Task 도구 호출을 받으면 완전히 새로운 메시지 루프를 시작한다. 이 때 위에서 보았던 YAML frontmatter 하단의 마크다운 본문이 subagent 전용 시스템 프롬프트로 주입된다.
{
"model": "claude-haiku-4-5-20251001",
"system": [
{
"type": "text",
"text": "You are Claude Code, Anthropic's official CLI for Claude."
},
{
"type": "text",
"text": "You are a file search specialist for Claude Code...
=== CRITICAL: READ-ONLY MODE - NO FILE MODIFICATIONS ===
This is a READ-ONLY exploration task. You are STRICTLY PROHIBITED from:
- Creating new files
- Modifying existing files
- Deleting files
...
Your strengths:
- Rapidly finding files using glob patterns
- Searching code and text with powerful regex patterns
- Reading and analyzing file contents
..."
}
],
"messages": [
{
"role": "user",
"content": "<system-reminder>\nAs you answer the user's questions, you can use the following context:\n# claudeMd\nCodebase and user instructions are ... <system-reminder>"
},
{
"role": "user",
"content": "이 NestJS TypeScript 프로젝트에서 entity 구조를 탐색해주세요..."
}
],
"tools": [
{"name": "Bash", ...},
{"name": "Glob", ...},
{"name": "Grep", ...},
{"name": "Read", ...}
]
}
- 모델: Explorer는 빠른 탐색을 위해 Haiku 4.5를 사용한다 (메인은 Opus 4.5)
- 시스템 프롬프트: 읽기 전용 모드로 제한된 전용 프롬프트가 주입된다
- 도구 제한: 메인 에이전트가 가진 모든 도구가 아닌, 탐색에 필요한 도구만 제공된다
- CLAUDE.md 전달: 프로젝트의
CLAUDE.md내용이 user 메시지의<system-reminder>태그 안에 포함되어 전달된다. 따라서 subagent도 프로젝트 컨벤션을 따른다 - 메시지: 메인 에이전트의 prompt가 user 메시지로 전달된다
3단계: Subagent의 도구 사용
Subagent도 자신만의 메시지 루프에서 도구를 사용한다:
{
"content": [
{
"type": "text",
"text": "이 NestJS 프로젝트의 entity 구조를 철저하게 탐색하겠습니다."
},
{
"type": "tool_use",
"id": "toolu_01VHysdohmuy9r58aVdcUQRh",
"name": "Glob",
"input": {
"pattern": "**/*.entity.ts"
}
},
{
"type": "tool_use",
"id": "toolu_01QYrMBhzbXpExYwgCFM7MLb",
"name": "Bash",
"input": {
"command": "find /Volumes/project/src -type f -name \"*.ts\" | grep -E \"(entity|entities)\" | head -20",
"description": "Find entity files in src directory"
}
}
]
}
Explorer subagent는 두 개의 도구를 병렬로 호출했다. Glob으로 entity 파일 패턴을 찾고 Bash로 디렉토리를 탐색한다. 이 과정이 반복되면서 subagent는 필요한 정보를 수집한다.
4단계: Subagent 완료 및 결과 반환
Subagent가 탐색을 완료하면 수집한 정보를 정리하여 최종 응답을 반환한다. 이 응답은 메인 에이전트의 Task 도구 호출에 대한 tool_result로 전달된다:
┌─────────────────────────────────────────────────────────────────────┐
│ 메인 메시지 루프 │
│ │
│ User ──► Agent ──► LLM(Opus) │
│ │ │
│ ▼ tool_use: Task │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ Subagent 메시지 루프 (Explorer) │ │
│ │ │ │
│ │ prompt ──► Agent ──► LLM(Haiku) │ │
│ │ │ │ │
│ │ ▼ tool_use │ │
│ │ ┌─────────┐ │ │
│ │ │ Glob │ │ │
│ │ │ Bash │ │ │
│ │ │ Read │ │ │
│ │ └─────────┘ │ │
│ │ │ │ │
│ │ ▼ │ │
│ │ 최종 응답 │ │
│ └────────────────────┬────────────────────────────────┘ │
│ │ │
│ ▼ tool_result │
│ Agent 계속 진행 │
│ │
└─────────────────────────────────────────────────────────────────────┘
메인 에이전트는 Explorer의 조사 결과를 바탕으로 다음 단계(예: database-schema-architect 호출)를 진행한다.
마무리
지금까지 subagent의 동작 방식에 대해서 알아보았다. 핵심을 정리하면:
Subagent는 Tool Use다. 메인 에이전트가 Task 도구를 호출하면, 새로운 메시지 루프가 생성되어 전용 시스템 프롬프트와 제한된 도구로 작업을 수행한다.
왜 사용하는가?
- 컨텍스트 분리: 탐색/분석 과정이 메인 대화를 오염시키지 않는다
- 전문화: 각 subagent가 특정 작업에 최적화된 프롬프트와 도구를 가진다
- 효율성: 목적에 맞게 가벼운 모델(Haiku)이나 무거운 모델(Opus)을 취사선택
주의할 점:
- Subagent는 메인 대화 히스토리를 모른다. 필요한 정보는 Task의 prompt에 명시해야 한다 (단,
CLAUDE.md는 자동 전달됨) - Subagent는 subagent를 호출할 수 없다 (무한 중첩 방지를 위해 Task 도구를 사용하지 않는다.)
- 각 호출은 새로운 컨텍스트에서 시작한다 (단, resume 파라미터로 이전 대화 이어가기 가능)
결국 subagent는 Tool Use 패턴의 확장이다. 단순한 Function Call이라면 코드 실행 결과를 텍스트로 반환하지만 subagent는 별도의 메시지 루프에서 LLM이 생성한 텍스트를 반환한다는 차이만 있다. 메인 에이전트 입장에서는 둘 다 tool_result로 받는 텍스트일 뿐이다.
