Agent 上下文 Isolat
- 作者仓库星标 3,783
- 作者仓库 Continuous-Claude-v3
Agent Context Isolation
Prevent agent output from polluting the main context window.
Rules
1. Use Background Agents with File-Based Coordination
# RIGHT - background agent writes to file, main reads file
Task(subagent_type="...", run_in_background=true, prompt="... Output to: /path/to/file.md")
# WRONG - foreground agent dumps full transcript into main context
Task(subagent_type="...", run_in_background=false)
Background agents with run_in_background=true isolate their context. Have them write results to files in .claude/cache/agents/<agent-type>/.
2. Never Use TaskOutput to Retrieve Results
# WRONG - dumps entire transcript (70k+ tokens) into context
TaskOutput(task_id="<id>")
TaskOutput(task_id="<id>", block=true)
# RIGHT - check expected output files
Bash("ls -la .claude/cache/agents/<agent-type>/")
Bash("bun test") # verify with tests
TaskOutput returns the full agent transcript. Always use file-based coordination instead.
3. Monitor Agent Progress via System Reminders
# System reminders come automatically:
# "Agent a42a16e progress: 6 new tools used, 88914 new tokens"
# To detect completion:
# - Watch for progress reminders to stop arriving
# - Poll for expected output files: find .claude/cache/agents -name "*.md" -mmin -5
# - Check task output file size growth: wc -c /tmp/claude/.../tasks/<id>.output
Stuck agent detection:
- Progress reminders stop arriving
- Task output file size stops growing
- Expected output file not created after reasonable time
4. Verify with Tests, Not Output
After agent work:
- Run the test suite directly:
bun test - Report pass/fail counts
- Only investigate failures if tests fail
5. File-Based Agent Pipeline Pattern
Research agent → .claude/cache/agents/oracle/output.md
↓
Plan agent → .claude/cache/agents/plan-agent/output.md (reads research)
↓
Validate agent → .claude/cache/agents/validate-agent/output.md (reads plan)
↓
Implement agent → src/module.ts (reads validated plan)
Each agent reads the previous agent's file output, not TaskOutput.
Why This Matters
Agent context isolation preserves the main conversation's context budget. Reading agent outputs via TaskOutput floods context, causing:
- Mid-conversation compaction
- Lost context about user's original request
- Repeated explanations needed
Source
- Session where TaskOutput flooded 70k+ tokens into main context
- Session 2026-01-01: Successfully used background agents with file-based coordination for SDK Phase 3
- 流狐分类
- AI 智能
- 作者声明 Agent
- 未找到明确声明;不据此推断已兼容或已测试
- 静态检查
- 88 / 100 · 启发式扫描,不代表运行安全
- 作者 / 版本 / 许可
- @parcadei · 未声明 license
- 流狐 Token 估算
- 低消耗
- 流狐接入估算
- 即装即用
- 是否需要外部 API Key
- 未发现要求
- 检测到的系统要求
- macOS · Linux · Windows
- 底层运行要求
- 未声明
- 检测到的文件与系统行为
-
- 只读
- 允许写入 / 修改
- 检测到的网络行为
- 仅限本地
- 安装命令数
- 无(仅作为资料)
档案由构建时根据 SKILL.md 与安装命令自动衍生,可能与作者实际意图存在差异。
需要注意: 未限定 allowed-tools,默认拥有全部工具权限。
# 2. Never Use TaskOutput to Retrieve Results
# WRONG - dumps entire transcript (70k+ tokens) into context
TaskOutput(task_id="<id>")
TaskOutput(task_id="<id>", block=true)
# RIGHT - check expected output files
Bash("ls -la .claude/cache/agents/<agent-type>/")
Bash("bun test") # verify with tests Rules
Background agents with runinbackground=true isolate their context. Have them write results to files in .claude/cache/agents/<agent-type>/.
TaskOutput returns the full agent transcript. Always use file-based coordination instead.
Stuck agent detection: Progress reminders stop arriving Task output file size stops growing
After agent work: Run the test suite directly: bun test Report pass/fail counts
Each agent reads the previous agent's file output, not TaskOutput.
# Agent Context Isolation
Prevent agent output from polluting the main context window.
## Rules
### 1. Use Background Agents with File-Based Coordination
```
# RIGHT - background agent writes to file, main reads file
Task(subagent_type="...", run_in_background=true, prompt="... Output to: /path/to/file.md")
# WRONG - foreground agent dumps full transcript into main context
Task(subagent_type="...", run_in_background=false)
```
Background agents with `run_in_background=true` isolate their context. Have them write results to files in `.claude/cache/agents/<agent-type>/`.
### 2. Never Use TaskOutput to Retrieve Results
```
# WRONG - dumps entire transcript (70k+ tokens) into context
TaskOutput(task_id="<id>")
TaskOutput(task_id="<id>", block=true)
# RIGHT - check expected output files
Bash("ls -la .claude/cache/agents/<agent-type>/")
Bash("bun test") # verify with tests
```
TaskOutput returns the full agent transcript. Always use file-based coordination instead.
### 3. Monitor Agent Progress via System Reminders
```
# System reminders come automatically:
# "Agent a42a16e progress: 6 new tools used, 88914 new tokens"
# To detect completion:
# - Watch for progress reminders to stop arriving
# - Poll for expected output files: find .claude/cache/agents -name "*.md" -mmin -5
# - Check task output file size growth: wc -c /tmp/claude/.../tasks/<id>.output
```
**Stuck agent detection:**
1. Progress reminders stop arriving
2. Task output file size stops growing
3. Expected output file not created after reasonable time
### 4. Verify with Tests, Not Output
After agent work:
1. Run the test suite directly: `bun test`
2. Report pass/fail counts
3. Only investigate failures if tests fail
### 5. File-Based Agent Pipeline Pattern
```
… 作者原文负责流程事实;流狐只索引当前章节、要点、文件与命令。
章节 -> Rules → 1. Use Background Agents with File-Based Coordination → 2. Never Use TaskOutput to Retrieve Results → 3. Monitor Agent Progress via System Reminders → 4. Verify with Tests, Not Output → 5. File-Based Agent Pipeline Pattern
要点 -> Stuck agent detection · Prevent agent output from polluting the main context window. · Background agents with runinbackground=true isolate their context. · TaskOutput returns the full agent transcript. · Stuck agent detection: 1. · Each agent reads the previous agent's file output, not TaskOutput. · Agent context isolation preserves the main conversation's context budget.
文件/命令 -> runinbackground=true · .claude/cache/agents/<agent-type>/ · bun test · path/to/file.md · .claude/cache/agents · tmp/claude/.../tasks · pass/fail · .claude/cache/agents/oracle/output.md
内容 SHA-256 -> 0a563b12cba7
原文结构
适用与边界
原文中的明确线索
runinbackground=true、.claude/cache/agents/<agent-type>/、bun test、path/to/file.md、.claude/cache/agents、tmp/claude/.../tasks、pass/fail、.claude/cache/agents/oracle/output.md