[BUG] Context management fires on every turn with empty `applied_edits`, causing all tool calls to execute twice
Preflight Checklist
- [x] I have searched existing issues and this hasn't been reported yet
- [x] This is a single bug report (please file separate reports for different bugs)
- [x] I am using the latest version of Claude Code
What's Wrong?
<p>When running <code>claude -p</code> (non-interactive) with a large system prompt (~35K tokens), context management triggers on every single assistant turn despite using only ~5% of the 1M context window. Each context management event has <code>applied_edits: []</code> (nothing is trimmed), yet the model re-generates all pending tool calls with new IDs, causing every operation to execute twice.</p>
<p>This doubles token usage, wall-clock time, and — for mutating operations like file edits — risks data corruption.</p>
<h2 data-heading="Environment">Environment</h2>
<ul>
<li>Claude Code CLI, non-interactive (<code>claude -p</code>)</li>
<li>System prompt injected via <code>--system-prompt</code> (~35K tokens including persona, skills reference, memory)</li>
<li>Context window: 1M tokens</li>
</ul>
<h2 data-heading="Possible root cause">Possible root cause</h2>
<p>Something about the system prompt size or structure appears to trigger context management prematurely. The system prompt in this case was ~35K tokens (persona definition, skills reference, persistent memory, channel memory, recent conversation context — all injected via <code>--system-prompt</code>). This may be hitting an internal threshold that's calibrated for smaller prompts.</p>
<p>The re-generation of tool calls after a no-op context management pass suggests that the context management step is inserting itself into the conversation flow in a way that causes the model to "forget" it already issued those calls, even though nothing was actually trimmed.</p>
What Should Happen?
<li>Context management should not trigger at 5% context utilization</li>
<li>If context management runs and trims nothing (<code>applied_edits: []</code>), it should not cause the model to re-generate tool calls</li>
<li>Tool calls that have already been dispatched should not be re-emitted</li>
Error Messages/Logs
Steps to Reproduce
<h2 data-heading="Evidence from task execution">Evidence from task execution</h2>
<p><strong>Log file:</strong> <code>f2b7cbfc-d51b-4c3c-a823-f2196f032fdf.jsonl</code> (51 JSONL lines, ~359KB)</p>
<h3 data-heading="Context utilization was never close to the limit">Context utilization was never close to the limit</h3>
Log line | Timestamp (UTC) | Total input tokens | % of 1M window
-- | -- | -- | --
4 | 01:04:56 | 35,743 | 3.6%
12 | 01:12:13 | 47,626 | 4.8%
34 | 01:15:03 | 55,089 | 5.5%
50 | 01:17:43 | 58,510 | 5.9%
<p>Context management ran on <strong>13 of 51 log entries</strong> (25%) and trimmed nothing each time.</p>
<h3 data-heading="Every tool call was duplicated">Every tool call was duplicated</h3>
<p>After each context management event, the model re-emitted its tool calls with new <code>tool_use</code> IDs, causing every operation to run twice.</p>
<p><strong>Example 1: Agent subagent dispatched twice</strong></p>
<p>Line 5 (01:05:11):</p>
<pre><code class="language-json">{
"type": "tool_use",
"id": "toolu_01PV55hkAT2DUsemtMxMtBxn",
"name": "Agent",
"input": {
"description": "Research NC app development",
"prompt": "Research Nextcloud app development thoroughly..."
}
}
</code></pre>
<p>Line 7 (01:05:32) — <strong>after context_management with <code>applied_edits: []</code></strong>:</p>
<pre><code class="language-json">{
"type": "tool_use",
"id": "toolu_01623rMcJPTSC7JM9bNUQiJ3",
"name": "Agent",
"input": {
"description": "Research NC app development",
"prompt": "Research Nextcloud app development thoroughly..."
}
}
</code></pre>
<p>Both Agent subagents ran for ~7 minutes each, performing redundant web searches.</p>
<p><strong>Example 2: Same file read twice</strong></p>
<p>Line 11 (01:12:09):</p>
<pre><code class="language-json">{
"type": "tool_use",
"id": "toolu_01Q2J1yqUchMPWK8piHPXMr5",
"name": "Read",
"input": {"file_path": ".../Nextcloud app spec.md"}
}
</code></pre>
<p>Line 12 (01:12:13) — <strong>after context_management</strong>:</p>
<pre><code class="language-json">{
"type": "tool_use",
"id": "toolu_01KZsWmWzjxPtHTuF2EBU3Tw",
"name": "Read",
"input": {"file_path": ".../Nextcloud app spec.md"}
}
</code></pre>
<p>3 seconds later, identical read.</p>
<p><strong>Example 3: Edit duplicated — risk of file corruption</strong></p>
<p>Line 19 (01:13:36):</p>
<pre><code class="language-json">{
"type": "tool_use",
"name": "Edit",
"input": {
"file_path": ".../Nextcloud app spec.md",
"old_string": "## Open Questions\n\n- ~~TOML library for PHP~~..."
}
}
</code></pre>
<p>Line 20 (01:14:32) — <strong>after context_management</strong>:</p>
<pre><code class="language-json">{
"type": "tool_use",
"name": "Edit",
"input": {
"file_path": ".../Nextcloud app spec.md",
"old_string": "## Open Questions..."
}
}
</code></pre>
<p>The second Edit targeted the same section. If the first Edit had already modified the <code>old_string</code>, the second would have failed or produced a corrupted result.</p>
<p><strong>Example 4: Destructive file copy duplicated</strong></p>
<p>Line 43 (01:17:04):</p>
<pre><code class="language-json">{"name": "Bash", "input": {"command": "cp \"...spec.md\" \"...spec.md.bak\" && cp /tmp/spec_updated.md \"...spec.md\""}}
</code></pre>
<p>Line 44 (01:17:08):</p>
<pre><code class="language-json">{"name": "Bash", "input": {"command": "cp \"...spec.md\" \"...spec.md.bak\" && cp /tmp/spec_updated.md \"...spec.md\""}}
</code></pre>
<p>Same destructive operation, 4 seconds apart.</p>
<h3 data-heading="Complete list of duplicated tool call pairs">Complete list of duplicated tool call pairs</h3>
<p>Lines: (5,7), (11,12), (14,15), (19,20), (22,23), (25,26), (28,29), (32,34), (36,37), (39,41), (43,44), (46,47)</p>
<p><strong>12 duplicate pairs = every tool call in the task was executed twice.</strong></p>
<h3 data-heading="Impact">Impact</h3>
<ul>
<li><strong>Task duration:</strong> 12m 50s (significant portion wasted on duplicates)</li>
<li><strong>Wasted output tokens:</strong> 7,522 tokens on duplicate generations</li>
<li><strong>Wasted subagent time:</strong> full redundant Agent research run (~7 minutes of web searches)</li>
<li><strong>Data integrity risk:</strong> Edit and Bash file-write operations were duplicated, risking corruption</li>
</ul>
<ol>
<li>Run <code>claude -p "your prompt"</code> with a system prompt of ~25K+ tokens</li>
<li>The task should require multiple sequential tool calls (reads, edits, bash commands)</li>
<li>Observe the JSONL log — context management fires after nearly every tool result</li>
</ol>
<p>The behavior may be triggered by system prompt size relative to some internal threshold, not actual context utilization.</p>
Claude Model
Opus
Is this a regression?
Yes, this worked in a previous version
Last Working Version
_No response_
Claude Code Version
2.1.87
Platform
Anthropic API
Operating System
Ubuntu/Debian Linux
Terminal/Shell
Other
Additional Information
_No response_
This issue has 6 comments on GitHub. Read the full discussion on GitHub ↗