$npx -y skills add vibeeval/vibecosystem --skill agent-context-isolation--- name: agent-context-isolation description: Agent Context Isolation user-invocable: false ---
| 1 | # Agent Context Isolation |
| 2 | |
| 3 | Prevent agent output from polluting the main context window. |
| 4 | |
| 5 | ## Rules |
| 6 | |
| 7 | ### 1. Use Background Agents with File-Based Coordination |
| 8 | ``` |
| 9 | # RIGHT - background agent writes to file, main reads file |
| 10 | Task(subagent_type="...", run_in_background=true, prompt="... Output to: /path/to/file.md") |
| 11 | |
| 12 | # WRONG - foreground agent dumps full transcript into main context |
| 13 | Task(subagent_type="...", run_in_background=false) |
| 14 | ``` |
| 15 | |
| 16 | Background agents with `run_in_background=true` isolate their context. Have them write results to files in `.claude/cache/agents/<agent-type>/`. |
| 17 | |
| 18 | ### 2. Never Use TaskOutput to Retrieve Results |
| 19 | ``` |
| 20 | # WRONG - dumps entire transcript (70k+ tokens) into context |
| 21 | TaskOutput(task_id="<id>") |
| 22 | TaskOutput(task_id="<id>", block=true) |
| 23 | |
| 24 | # RIGHT - check expected output files |
| 25 | Bash("ls -la .claude/cache/agents/<agent-type>/") |
| 26 | Bash("bun test") # verify with tests |
| 27 | ``` |
| 28 | |
| 29 | TaskOutput returns the full agent transcript. Always use file-based coordination instead. |
| 30 | |
| 31 | ### 3. Monitor Agent Progress via System Reminders |
| 32 | ``` |
| 33 | # System reminders come automatically: |
| 34 | # "Agent a42a16e progress: 6 new tools used, 88914 new tokens" |
| 35 | |
| 36 | # To detect completion: |
| 37 | # - Watch for progress reminders to stop arriving |
| 38 | # - Poll for expected output files: find .claude/cache/agents -name "*.md" -mmin -5 |
| 39 | # - Check task output file size growth: wc -c /tmp/claude/.../tasks/<id>.output |
| 40 | ``` |
| 41 | |
| 42 | **Stuck agent detection:** |
| 43 | 1. Progress reminders stop arriving |
| 44 | 2. Task output file size stops growing |
| 45 | 3. Expected output file not created after reasonable time |
| 46 | |
| 47 | ### 4. Verify with Tests, Not Output |
| 48 | After agent work: |
| 49 | 1. Run the test suite directly: `bun test` |
| 50 | 2. Report pass/fail counts |
| 51 | 3. Only investigate failures if tests fail |
| 52 | |
| 53 | ### 5. File-Based Agent Pipeline Pattern |
| 54 | ``` |
| 55 | Research agent → .claude/cache/agents/oracle/output.md |
| 56 | ↓ |
| 57 | Plan agent → .claude/cache/agents/plan-agent/output.md (reads research) |
| 58 | ↓ |
| 59 | Validate agent → .claude/cache/agents/validate-agent/output.md (reads plan) |
| 60 | ↓ |
| 61 | Implement agent → src/module.ts (reads validated plan) |
| 62 | ``` |
| 63 | |
| 64 | Each agent reads the previous agent's file output, not TaskOutput. |
| 65 | |
| 66 | ## Why This Matters |
| 67 | |
| 68 | Agent context isolation preserves the main conversation's context budget. Reading agent outputs via TaskOutput floods context, causing: |
| 69 | - Mid-conversation compaction |
| 70 | - Lost context about user's original request |
| 71 | - Repeated explanations needed |
| 72 | |
| 73 | ## Source |
| 74 | - Session where TaskOutput flooded 70k+ tokens into main context |
| 75 | - Session 2026-01-01: Successfully used background agents with file-based coordination for SDK Phase 3 |