claude-code - 💡(How to fix) Fix Sub-agent inefficiency: repeated file read/write retries waste 100K+ tokens [1 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
anthropics/claude-code#46968Fetched 2026-04-13 05:45:01
View on GitHub
Comments
1
Participants
2
Timeline
4
Reactions
0
Timeline (top)
labeled ×3commented ×1

Error Message

  • Hit "File has been modified since read" error repeatedly (file was being modified by main agent)

Root Cause

Root Causes

Fix Action

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.

RAW_BUFFERClick to expand / collapse

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
  2. 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
  3. 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

AgentTokens UsedTool CallsDurationWaste
Agent 1 (ipcc-1)~40K~10~8 minLow
Agent 2 (ipcc-2)101,6463021 minHigh - retry loop
Agent 3 (3 lectures)61,278~158 minMedium - output truncation
Previous session~150K+~50+~30 minTotal 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.

extent analysis

TL;DR

Instructing agents to write output to temporary files in /tmp/ can help avoid file contention and retry loops, reducing token waste.

Guidance

  • Identify and modify the agent configuration to write output to temporary files instead of directly editing shared files like lectures.html.
  • Implement a fallback mechanism for agents to switch to temporary file output after 1-2 failed edit attempts due to file modification errors.
  • Consider persisting agent results to disk to prevent loss of work across session changes.
  • Review and adjust the output token budget allocation to prevent hitting the max_output_tokens limit, potentially by splitting work into smaller chunks.

Example

# Example of how an agent might be configured to write to a temp file
import tempfile

# ...

def generate_content(agent_id):
    # Generate content
    content = generate_visual_html_content()
    
    # Write content to a temp file
    with tempfile.NamedTemporaryFile(dir='/tmp/', prefix=f'agent_{agent_id}_output') as tmp_file:
        tmp_file.write(content.encode())
        tmp_file.flush()
        # Assemble or process the temp file as needed in the main agent

Notes

The provided workaround and suggested improvements aim to address the root causes of token waste, including file contention, output truncation, and session invalidation. However, the effectiveness of these solutions may depend on the specific implementation details of the Claude Code and sub-agents, which are not fully specified in the issue.

Recommendation

Apply the workaround by instructing agents to write output to /tmp/ files and assemble the content in the main agent, as this directly addresses the identified issues of file contention and retry loops, offering a clear path to reducing token waste.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING