[Bug] Subagent Skills Leak into Main Context After Compaction - extremely costly bug that breaks everything
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.
This issue has 9 comments on GitHub. Read the full discussion on GitHub ↗