Agent Context Isolation

by parcadeid07ff4b06b62No license3.9K starsListed Oct 8, 2026Updated Oct 8, 2026Repository updated 8 months ago

Agent Context Isolation

Instructions onlyAI & Agents
AI-generated overview

Guidance for isolating subagent output from the main context window using background agents and file-based coordination.

What it does
This skill provides rules for keeping agent output out of the main conversation context. It recommends running subagents in the background and having them write results to files under a cache directory, rather than retrieving transcripts with TaskOutput. It also covers monitoring agent progress, detecting stuck agents, verifying work with tests, and chaining agents through file outputs.
When to use it
Use it when orchestrating subagents and the main context window is at risk of being flooded by large agent transcripts. It suits multi-step agent pipelines where each stage should read the previous stage's file output instead of raw task output.
Requirements
No scripts are shipped; it is instructions only. It assumes an agent environment with background task execution, file read/write access, and a test command such as bun test for verification.

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 fileTask(subagent_type="...", run_in_background=true, prompt="... Output to: /path/to/file.md")
# WRONG - foreground agent dumps full transcript into main contextTask(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 contextTaskOutput(task_id="<id>")TaskOutput(task_id="<id>", block=true)
# RIGHT - check expected output filesBash("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

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

Source and attribution

Source:parcadei/continuous-claude-v3in.claude/skills/agent-context-isolationat commitd07ff4b

License: No license

Content belongs to its original authors. SourceWeft indexes it from a public repository.

Report or request removal