Agent skill
auto-shard
Automatically shard large CLAUDE.md files into smaller, organized shards. Detects files >40KB, identifies logical sections, classifies by criticality, creates shards in appropriate locations (rules/ or docs/), and validates integrity. Use when working with large documentation files or when Claude Code warns about context size.
Install this agent skill to your Project
npx add-skill https://github.com/ekson73/multi-agent-os/tree/main/commands/auto-shard
SKILL.md
Auto-Shard Skill
Version: 1.0.0 | Author: Claude-Code | Created: 2026-01-25
Purpose
Automatically shard large documentation files (especially CLAUDE.md) into smaller, organized components following the Claude Code context architecture.
When to Use
- Proactive: When a file exceeds 40KB
- Reactive: When Claude Code warns about context size
- Manual: When you want to reorganize documentation
- Hook-triggered: Automatically via
detect-large-file.shPreToolUse hook
Usage
/auto-shard <operation> [file_path] [options]
Operations
| Operation | Description | Example |
|---|---|---|
analyze |
Identify sections and structure | /auto-shard analyze ~/.claude/CLAUDE.md |
classify |
Classify sections by criticality | /auto-shard classify ~/.claude/CLAUDE.md |
generate |
Create shard files | /auto-shard generate ~/.claude/CLAUDE.md |
update-refs |
Update cross-references | /auto-shard update-refs ~/.claude/CLAUDE.md |
validate |
Verify integrity | /auto-shard validate ~/.claude/ |
full |
Run complete pipeline | /auto-shard full ~/.claude/CLAUDE.md |
status |
Show sharding status | /auto-shard status ~/.claude/ |
Routing Logic
┌─────────────────────────────────────────────────────────────────────┐
│ AUTO-SHARD ROUTER │
├─────────────────────────────────────────────────────────────────────┤
│ │
│ /auto-shard analyze <path> │
│ └── Load: operations/analyze.md │
│ └── Output: Section map with line counts │
│ │
│ /auto-shard classify <path> │
│ └── Load: operations/classify.md │
│ └── Output: Criticality classification per section │
│ │
│ /auto-shard generate <path> [--dry-run] │
│ └── Load: operations/generate.md │
│ └── Output: Shard files in rules/ or docs/ │
│ │
│ /auto-shard update-refs <path> │
│ └── Load: operations/update-refs.md │
│ └── Output: Updated links in main file │
│ │
│ /auto-shard validate <path> │
│ └── Load: operations/validate.md │
│ └── Output: Integrity report (SHA256 + links) │
│ │
│ /auto-shard full <path> [--dry-run] │
│ └── Pipeline: analyze → classify → generate → update-refs │
│ └── Recursive: Up to 3 levels if shards > 20KB │
│ │
│ /auto-shard status <path> │
│ └── Show: File sizes, shard count, integrity status │
│ │
└─────────────────────────────────────────────────────────────────────┘
Architecture
~/.claude/commands/auto-shard/
├── SKILL.md # This file (router)
├── operations/
│ ├── analyze.md # ShardAnalyzer
│ ├── classify.md # CriticalityClassifier
│ ├── generate.md # ShardGenerator
│ ├── update-refs.md # ReferenceUpdater
│ ├── validate.md # IntegrityValidator
│ └── recursive.md # RecursiveProcessor
└── scripts/
└── detect-large-file.sh # PreToolUse hook detector
Criticality Levels
| Level | Criteria | Destination | Auto-Load |
|---|---|---|---|
| CRITICAL | Core directives, must-follow rules | rules/ |
YES |
| HIGH | Important but can be on-demand | docs/ (top) |
NO |
| MEDIUM | Reference material | docs/ (nested) |
NO |
| LOW | Historical, examples, verbose | docs/archive/ |
NO |
Size Thresholds
| Threshold | Action |
|---|---|
| < 20KB | No action needed |
| 20-40KB | Optional sharding (suggest) |
| 40-80KB | Recommended sharding (warn) |
| > 80KB | Mandatory sharding (block) |
Output Structure
After sharding ~/.claude/CLAUDE.md:
~/.claude/
├── CLAUDE.md # Reduced (links to shards)
├── rules/
│ ├── core-directive.md # CRITICAL
│ ├── main-instructions.md # CRITICAL
│ └── pr-review-protocol.md # CRITICAL
└── docs/
├── git-worktree-protocol.md # HIGH
├── session-report-standard.md # HIGH
└── archive/
└── changelog-2025.md # LOW
Integration with Hook
The detect-large-file.sh hook triggers this skill automatically:
# Hook detects large file on Read/Write
# Emits JSON-RPC suggestion to stderr
# Claude Code sees suggestion and can invoke:
/auto-shard full ~/.claude/CLAUDE.md
Examples
Analyze a file
/auto-shard analyze ~/.claude/CLAUDE.md
Output:
## Section Analysis
| Section | Lines | Size | Heading Level |
|---------|-------|------|---------------|
| [C01] Core Directive | 45 | 2.1KB | H2 |
| [C02] Main Instructions | 120 | 5.8KB | H2 |
| ... | ... | ... | ... |
Total: 1,847 lines, 55KB
Recommended action: SHARD (>40KB threshold)
Full pipeline with dry-run
/auto-shard full ~/.claude/CLAUDE.md --dry-run
Output:
## Dry-Run: Would create 8 shards
| Shard | Destination | Size | Criticality |
|-------|-------------|------|-------------|
| core-directive.md | rules/ | 2.1KB | CRITICAL |
| main-instructions.md | rules/ | 5.8KB | CRITICAL |
| ...
Main file would reduce from 55KB to 12KB
No files modified (dry-run mode)
Validate integrity
/auto-shard validate ~/.claude/
Output:
## Integrity Report
| File | SHA256 | Links Valid | Status |
|------|--------|-------------|--------|
| CLAUDE.md | abc123... | 8/8 | OK |
| rules/core-directive.md | def456... | 2/2 | OK |
| ...
All 12 files validated. No broken links.
Error Handling
| Error | Recovery |
|---|---|
| Circular reference detected | Abort, report cycle |
| Shard destination conflict | Prompt for rename |
| Link target not found | Create stub or warn |
| SHA256 mismatch | Report drift, suggest resync |
Flags
| Flag | Description |
|---|---|
--dry-run |
Show what would happen without changes |
--force |
Override size thresholds |
--recursive |
Enable recursive sharding (default: 3 levels) |
--no-backup |
Skip backup creation |
--json |
Output in JSON format |
Instructions for Claude
When this skill is invoked:
- Parse the operation from arguments
- Load the appropriate operation file from
operations/ - Execute the operation following its specific instructions
- Return structured output (table format preferred)
- Suggest next action if pipeline is incomplete
On /auto-shard full:
Execute in sequence:
analyze- Build section mapclassify- Assign criticalitygenerate- Create shards (with backup)update-refs- Fix cross-referencesvalidate- Verify integrity- (If any shard > 20KB)
recursive- Re-shard large shards
On errors:
- Emit JSON-RPC error to stderr
- Provide actionable recovery instructions
- Never leave partial state (rollback on failure)
Skill: auto-shard v1.0.0 | Author: Claude-Code | 2026-01-25
Didn't find tool you were looking for?