Sub-agent inefficiency: repeated file read/write retries waste 100K+ tokens

Resolved 💬 3 comments Opened Apr 12, 2026 by davidlikescat Closed Apr 16, 2026

Problem

When using parallel sub-agents (Task tool) for content generation, severe token waste occurred due to agent behavior patterns.

Details

Environment: Claude Code 2.1.47, claude-sonnet-4-6 sub-agents

Scenario: Launched 3 parallel agents to generate visual HTML content for lecture pages. Total wasted tokens estimated at ~100K+.

Root Causes

  1. Agent file edit retry loop (Agent 2: 101,646 tokens, 30 tool calls, 21 min)
  • Agent generated content correctly, then attempted to write to lectures.html
  • Hit "File has been modified since read" error repeatedly (file was being modified by main agent)
  • Instead of writing to a temp file, it kept re-reading and retrying the same Edit operation
  • Expected: Agent should fall back to writing output to /tmp/ after 1-2 failed attempts
  1. Agent output token limit hit causing duplicate content (Agent 3: 61,278 tokens)
  • Assigned 3 content pieces to one agent; hit max_output_tokens (32K)
  • Agent generated duplicate sections before being cut off
  • Required manual post-processing to deduplicate and complete truncated content
  • Expected: Better chunking guidance or agent self-awareness of output limits
  1. Session change invalidated previous agent results
  • Previous session had already completed 3 agents for the same task
  • Session transition made those results inaccessible, requiring full re-execution
  • Expected: Agent results should persist across session changes or be saved to temp files proactively

Token Impact

| Agent | Tokens Used | Tool Calls | Duration | Waste |
|-------|------------|------------|----------|-------|
| Agent 1 (ipcc-1) | ~40K | ~10 | ~8 min | Low |
| Agent 2 (ipcc-2) | 101,646 | 30 | 21 min | High - retry loop |
| Agent 3 (3 lectures) | 61,278 | ~15 | 8 min | Medium - output truncation |
| Previous session | ~150K+ | ~50+ | ~30 min | Total loss - session invalidation |

Suggested Improvements

  1. Sub-agents should write output to temp files instead of editing shared files directly
  2. When file edit fails 2+ times, agent should automatically fall back to temp file output
  3. Agent results should be persisted to disk so they survive session changes
  4. Output token budget awareness — agents should estimate output size and split work accordingly

Workaround

Instruct agents to write only to /tmp/ files, then assemble in the main agent. This avoids file contention and retry loops entirely.

---
Submitted via Claude Code on behalf of user.

View original on GitHub ↗

This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗