[Bug] Subagent Skills Leak into Main Context After Compaction - extremely costly bug that breaks everything

Status Fixed / completed
Reported on v2.1.31
Maintainer reply ✓ Yes — amorriscode
Activity 9 comments · opened Feb 5, 2026 · closed Feb 17, 2026
💡 Likely answer: A maintainer (amorriscode, contributor) responded on this thread — see the highlighted reply below.

This is a very surprising and very silly bug that has probably cost me several hundred bucks (or maybe more!) in wasted tokens. Can I actually be the only one to notice this??!

Bug Description
The basic problem is that skills that are loaded by subagents should really stay only in that subagent's system prompt, and they do, until compaction runs. When compaction runs, all the skills that were previously only in the context windows of individual subagents, ALL of them get loaded into the main context window.

This of course, ruins progressive disclosure entirely. But it also can cause comical (and painful) thrashing of compaction events if you have a large number of skills.

Environment Info

  • Platform: darwin
  • Terminal: Apple_Terminal
  • Version: 2.1.31
  • Feedback ID: e0fcb3f7-e241-43dd-b112-096a7363b868

Reproduction
Reproducing this minimally is quite easy for me. I asked claude to do the following:

```claude-code-session
❯ I'm trying to replicate a bug that I often see in long running claude code sessions that compact multiple times. When we launch multiple parallel subagents that each load skills, after compaction, I often notice that the skills that the subagents loaded get "restored"
into the main session context as if they were loaded by the main session!

I'd like you to launch a very large number of subagents that do essentially nothing other than load skills - not loading ANY yourself - before exiting. We'll do this a few times, and then I'll run compact and see if they get "restored". I've run claude code in
--verbose --debug mode, so later I may ask you to launch subagents to read and search /Users/alexanderriccio/.claude/debug/aa3fef07-cbf8-4e77-b1d3-c0a75694a7b0.txt.


...and it dutifully launched a swarm, like a bunch of these:
```claude-code-session
⏺ Task(Load skill: debugging-and-investigating) Haiku 4.5
  ⎿  Prompt:
       Your only task: Use the Skill tool to load the skill named "debugging-and-investigating". After loading it, immediately return with a message saying "Loaded debugging-and-investigating successfully".
  ⎿  ❯ Your only task: Use the Skill tool to load the skill named "debugging-and-investigating". After loading it, immediately return with a message saying "Loaded debugging-and-investigating successfully". 

  ⎿  Skill(debugging-and-investigating)

  ⎿  

  ⎿  Response:
       Loaded debugging-and-investigating successfully.

       I now have access to the systematic debugging skill with its 6-phase workflow (Understand Symptom → Apply Fail-Fast Refactoring → Progressive Exploration → Root Cause Analysis → Implement Fix → Verify and Document), along with reference files for fail-fast
       patterns, exploration strategies, iOS/Swift scenarios, and quick references.

       The skill emphasizes the core principle: NEVER ASSUME. ALWAYS VERIFY. - using tools to answer questions rather than relying on intuition or assumptions.

       Ready to help with debugging tasks.
  ⎿  Done (1 tool use · 52.6k tokens · 5s)

I repeated that a few times just to be sure. C'mon guys, wanna give me a small refund?

Then I ran /compact, and voila:

```claude-code-session

▗ ▗ ▖ ▖ Claude Code v2.1.31
Opus 4.5 · Claude Max
▘▘ ▝▝ ~/Documents/GitHub/DeeDee-worktree-debug-skill-loading-issues

Debug mode enabled
Logging to: /Users/alexanderriccio/.claude/debug/aa3fef07-cbf8-4e77-b1d3-c0a75694a7b0.txt
─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── ctrl+e to show 1689 previous messages ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

❯ /compact
⎿ Compacted
⎿ Skills restored (hipaa-security-rule-compliance, detecting-and-cleaning-context-poisoning, building-deedee-skills, skill-creator, self-improvement-patterns, guided-feature-development, feature-dev, iterative-cloud-pr-review-loop, local-parallel-review-loop,
parallel-subagent-orchestration-patterns, using-claude-code-tools-correctly, editing-swift-code-safely, understanding-swift6-concurrency, healthkit-integration-patterns, understanding-deedee-architecture, orchestrating-tasks, debugging-and-investigating,
prime-scores-calculation, prime-flock, audit-recent-github-issues, prime-backend, reverse-engineer-claude-env, process-feedback, create-backend-worktree, todo-issue-sync, oncall-triage, garden, interview, dedupe, auto-task-picker, commit-push-review, orchestrate,
using-observability-mcp-in-subagents, using-ios-dev-mcp-in-subagents, todo-issue-linking, reverse-engineering-claude-code-env, reuse-extend-or-write-new-code, processing-feedback, preparing-prs-with-intelligent-cleanup, managing-mock-vs-real-data-flow,
implementing-server-side-sms-architecture, efficient-pre-planning, e2e-testing-with-localhost-backend, developer-interviewing, claude-config-editing-permissions, backend-management, autonomous-issue-resolution, api-contract-validation,
creating-meaningful-data-visualizations, validating-health-alerts-with-medical-evidence, testing-with-modern-swift-testing-framework, swiftdata-migration-safety, sourcekit-lsp-troubleshooting, refactoring-without-breaking, elderly-user-interface-patterns)

❯ /context
⎿ Context Usage
⛁ ⛁ ⛁ ⛁ ⛁ ⛁ ⛁ ⛁ ⛁ ⛁ claude-opus-4-5-20251101 · 284k/200k tokens (142%)
⛁ ⛁ ⛁ ⛁ ⛁ ⛀ ⛁ ⛁ ⛁ ⛁
⛁ ⛁ ⛁ ⛁ ⛁ ⛁ ⛁ ⛁ ⛁ ⛁ Estimated usage by category
⛁ ⛁ ⛁ ⛁ ⛁ ⛁ ⛁ ⛁ ⛁ ⛁ ⛁ System prompt: 3.6k tokens (1.8%)
⛁ ⛁ ⛁ ⛁ ⛁ ⛁ ⛁ ⛁ ⛁ ⛁ ⛁ System tools: 15.5k tokens (7.7%)
⛁ ⛁ ⛁ ⛁ ⛁ ⛁ ⛁ ⛁ ⛁ ⛁ ⛁ Custom agents: 1.4k tokens (0.7%)
⛁ ⛁ ⛁ ⛁ ⛁ ⛁ ⛁ ⛁ ⛁ ⛁ ⛁ Memory files: 9.1k tokens (4.6%)
⛁ ⛁ ⛁ ⛁ ⛁ ⛁ ⛁ ⛁ ⛁ ⛁ ⛁ Skills: 3.5k tokens (1.7%)
⛁ ⛁ ⛁ ⛁ ⛁ ⛁ ⛁ ⛁ ⛁ ⛁ ⛁ Messages: 251.2k tokens (125.6%)
⛁ ⛁ ⛁ ⛁ ⛁ ⛁ ⛁ ⛁ ⛁ ⛁ ⛝ Autocompact buffer: 33k tokens (16.5%)



There are multiple things to note here.

`ctrl+e to show 1689 previous messages` indicates how many messages leaked out into the main context.
`251.2k tokens` worth of skills is a lot. There's no way the model could actually have tried to load this many skills, since it would've run out of context.


I've aggressively distilled my skills a LOT over the past several weeks to try to work around this, but given claude infamously likes to ignore directions to read files, this means most of the stuff I extract to `REFERENCE.md` gets ignored completely. There's obviously nothing I can do to work around all my skills loading!

I honestly don't know how nobody else has reported this. Is there a bug that causes this to happen more in my specific setup? I have no clue.


**Errors**
```json
[{"error":"Error: 404 {\"type\":\"error\",\"error\":{\"type\":\"not_found_error\",\"message\":\"model: haiku\"},\"request_id\":\"req_011CXpCC9uuTPzAyvGktqLev\"}\n    at g4.generate (file:///Users/alexanderriccio/.nvm/versions/node/v24.10.0/lib/node_modules/@anthropic-ai/claude-code/cli.js:405:8768)\n    at _C.makeStatusError (file:///Users/alexanderriccio/.nvm/versions/node/v24.10.0/lib/node_modules/@anthropic-ai/claude-code/cli.js:422:2195)\n    at _C.makeRequest (file:///Users/alexanderriccio/.nvm/versions/node/v24.10.0/lib/node_modules/@anthropic-ai/claude-code/cli.js:422:5420)\n    at process.processTicksAndRejections (node:internal/process/task_queues:105:5)","timestamp":"2026-02-05T03:14:26.251Z"},{"error":"SyntaxError: Unexpected token 'A', \"API Error:\"... is not valid JSON\n    at JSON.parse (<anonymous>)\n    at file:///Users/alexanderriccio/.nvm/versions/node/v24.10.0/lib/node_modules/@anthropic-ai/claude-code/cli.js:67:810\n    at K (file:///Users/alexanderriccio/.nvm/versions/node/v24.10.0/lib/node_modules/@anthropic-ai/claude-code/cli.js:8:6811)\n    at v94 (file:///Users/alexanderriccio/.nvm/versions/node/v24.10.0/lib/node_modules/@anthropic-ai/claude-code/cli.js:1631:747)\n    at process.processTicksAndRejections (node:internal/process/task_queues:105:5)\n    at async file:///Users/alexanderriccio/.nvm/versions/node/v24.10.0/lib/node_modules/@anthropic-ai/claude-code/cli.js:3227:2144","timestamp":"2026-02-05T03:14:26.252Z"},{"error":"Error: 404 {\"type\":\"error\",\"error\":{\"type\":\"not_found_error\",\"message\":\"model: haiku\"},\"request_id\":\"req_011CXpCeyys84sVSyzmuM6jc\"}\n    at g4.generate (file:///Users/alexanderriccio/.nvm/versions/node/v24.10.0/lib/node_modules/@anthropic-ai/claude-code/cli.js:405:8768)\n    at _C.makeStatusError (file:///Users/alexanderriccio/.nvm/versions/node/v24.10.0/lib/node_modules/@anthropic-ai/claude-code/cli.js:422:2195)\n    at _C.makeRequest (file:///Users/alexanderriccio/.nvm/versions/node/v24.10.0/lib/node_modules/@anthropic-ai/claude-code/cli.js:422:5420)\n    at process.processTicksAndRejections (node:internal/process/task_queues:105:5)","timestamp":"2026-02-05T03:20:30.189Z"},{"error":"SyntaxError: Unexpected token 'A', \"API Error:\"... is not valid JSON\n    at JSON.parse (<anonymous>)\n    at file:///Users/alexanderriccio/.nvm/versions/node/v24.10.0/lib/node_modules/@anthropic-ai/claude-code/cli.js:67:810\n    at K (file:///Users/alexanderriccio/.nvm/versions/node/v24.10.0/lib/node_modules/@anthropic-ai/claude-code/cli.js:8:6811)\n    at v94 (file:///Users/alexanderriccio/.nvm/versions/node/v24.10.0/lib/node_modules/@anthropic-ai/claude-code/cli.js:1631:747)\n    at process.processTicksAndRejections (node:internal/process/task_queues:105:5)\n    at async file:///Users/alexanderriccio/.nvm/versions/node/v24.10.0/lib/node_modules/@anthropic-ai/claude-code/cli.js:3227:2144","timestamp":"2026-02-05T03:20:30.190Z"},{"error":"Error: 404 {\"type\":\"error\",\"error\":{\"type\":\"not_found_error\",\"message\":\"model: haiku\"},\"request_id\":\"req_011CXpCfAW8C1d3SxpsnNmVm\"}\n    at g4.generate (file:///Users/alexanderriccio/.nvm/versions/node/v24.10.0/lib/node_modules/@anthropic-ai/claude-code/cli.js:405:8768)\n    at _C.makeStatusError (file:///Users/alexanderriccio/.nvm/versions/node/v24.10.0/lib/node_modules/@anthropic-ai/claude-code/cli.js:422:2195)\n    at _C.makeRequest (file:///Users/alexanderriccio/.nvm/versions/node/v24.10.0/lib/node_modules/@anthropic-ai/claude-code/cli.js:422:5420)\n    at process.processTicksAndRejections (node:internal/process/task_queues:105:5)","timestamp":"2026-02-05T03:20:32.731Z"},{"error":"SyntaxError: Unexpected token 'A', \"API Error:\"... is not valid JSON\n    at JSON.parse (<anonymous>)\n    at file:///Users/alexanderriccio/.nvm/versions/node/v24.10.0/lib/node_modules/@anthropic-ai/claude-code/cli.js:67:810\n    at K (file:///Users/alexanderriccio/.nvm/versions/node/v24.10.0/lib/node_modules/@anthropic-ai/claude-code/cli.js:8:6811)\n    at v94 (file:///Users/alexanderriccio/.nvm/versions/node/v24.10.0/lib/node_modules/@anthropic-ai/claude-code/cli.js:1631:747)\n    at process.processTicksAndRejections (node:internal/process/task_queues:105:5)\n    at async file:///Users/alexanderriccio/.nvm/versions/node/v24.10.0/lib/node_modules/@anthropic-ai/claude-code/cli.js:3227:2144","timestamp":"2026-02-05T03:20:32.731Z"},{"error":"Error: 404 {\"type\":\"error\",\"error\":{\"type\":\"not_found_error\",\"message\":\"model: haiku\"},\"request_id\":\"req_011CXpCj6zQMjVRvJRzL4nnA\"}\n    at g4.generate (file:///Users/alexanderriccio/.nvm/versions/node/v24.10.0/lib/node_modules/@anthropic-ai…

Note: Content was truncated.

View original on GitHub ↗

9 Comments

github-actions[bot] · 6 months ago

Found 3 possible duplicate issues:

  1. https://github.com/anthropics/claude-code/issues/15635
  2. https://github.com/anthropics/claude-code/issues/16209
  3. https://github.com/anthropics/claude-code/issues/15191

This issue will be automatically closed as a duplicate in 3 days.

  • If your issue is a duplicate, please close it and 👍 the existing issue instead
  • To prevent auto-closure, add a comment or 👎 this comment

🤖 Generated with Claude Code

ariccio · 6 months ago

Uh, yeah, claude, I opened #15635, and this is one of the things that interacts with that issue to make it worse, but still separate.

amorriscode contributor · 6 months ago

Put up a PR for this that should go out on Monday's release.

ariccio · 6 months ago
Put up a PR for this that should go out on Monday's release.

Yay!

Notum · 6 months ago

I can confirm this bug and currently working with claude code as an agenting development is just impossible.
Agents context window is shared between agents and main claude code context:

● Agent "Create Certifications migration+model" completed
⎿ Context limit reached · /compact or /clear to continue

● Agent "Add CRS org settings migration" completed
⎿ Context limit reached · /compact or /clear to continue

● Agent "Create CRS models and migration" completed
⎿ Context limit reached · /compact or /clear to continue

● Agent "Add Authorization access matrix" completed
⎿ Context limit reached · /compact or /clear to continue

● Agent "Auth routes, controller, sidebar" completed
⎿ Context limit reached · /compact or /clear to continue

This I got in same moment.

ariccio · 6 months ago

@amorriscode which version were you targeting? I'm still seeing it 2.1.42. Feedback IDs: 369d792e-98a8-49e7-8bed-5e203cebd47e, b7f4aa8b-8d92-4ae0-8c10-fbb71640dc2b, 6f179ea9-d63b-45a5-86df-b98b773b8884.

For my own reference, this is the format-clean copy and paste prompt to reproduce.

I'm trying to replicate a bug that I often see in long running claude code sessions that compact multiple times. When we launch multiple parallel subagents that each load skills, after compaction, I often notice that the skills that the subagents loaded get "restored" into the main session context as if they were loaded by the main session!

I'd like you to launch a very large number  of subagents that do essentially nothing other than load skills - not loading ANY yourself - before exiting. We'll do this a few times, and then I'll run compact and see if they get "restored". I've run claude code in `--verbose --debug` mode, so later I may ask you to launch subagents to read and search the session jsonl file.
ariccio · 6 months ago

Okay, this does need to be addressed soon, there are certain workflows where this is costing me real money. It's a problem when it compacts ten times, with each new session starting at 80% context usage, it's spending most of the tokens on generating compaction summaries and not on getting work done.

ariccio · 6 months ago

Yay! Fixed in 2.1.45! Thanks @amorriscode

github-actions[bot] · 6 months ago

This issue has been automatically locked since it was closed and has not had any activity for 7 days. If you're experiencing a similar issue, please file a new issue and reference this one if it's relevant.