Files
SuperClaude/superclaude/skills/pm/modules/token-counter.md
kazuki 2ec23b14e5 feat: implement lazy loading architecture with PM Agent Skills migration
## Changes

### Core Architecture
- Migrated PM Agent from always-loaded .md to on-demand Skills
- Implemented lazy loading: agents/modes no longer installed by default
- Only Skills and commands are installed (99.5% token reduction)

### Skills Structure
- Created `superclaude/skills/pm/` with modular architecture:
  - SKILL.md (87 tokens - description only)
  - implementation.md (16KB - full PM protocol)
  - modules/ (git-status, token-counter, pm-formatter)

### Installation System Updates
- Modified `slash_commands.py`:
  - Added Skills directory discovery
  - Skills-aware file installation (→ ~/.claude/skills/)
  - Custom validation for Skills paths
- Modified `agent_personas.py`: Skip installation (migrated to Skills)
- Modified `behavior_modes.py`: Skip installation (migrated to Skills)

### Security
- Updated path validation to allow ~/.claude/skills/ installation
- Maintained security checks for all other paths

## Performance

**Token Savings**:
- Before: 17,737 tokens (agents + modes always loaded)
- After: 87 tokens (Skills SKILL.md descriptions only)
- Reduction: 99.5% (17,650 tokens saved)

**Loading Behavior**:
- Startup: 0 tokens (PM Agent not loaded)
- `/sc:pm` invocation: ~2,500 tokens (full protocol loaded on-demand)
- Other agents/modes: Not loaded at all

## Benefits

1. **Zero-Footprint Startup**: SuperClaude no longer pollutes context
2. **On-Demand Loading**: Pay token cost only when actually using features
3. **Scalable**: Can migrate other agents to Skills incrementally
4. **Backward Compatible**: Source files remain for future migration

## Next Steps

- Test PM Skills in real Airis development workflow
- Migrate other high-value agents to Skills as needed
- Keep unused agents/modes in source (no installation overhead)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-21 05:17:53 +09:00

3.5 KiB
Raw Blame History

name, description, category
name description category
token-counter Dynamic token usage calculation from system notifications module

Token Counter Module

Purpose: Parse and format real-time token usage from system notifications

Input Source

System provides token notifications after each tool call:

<system_warning>Token usage: [used]/[total]; [remaining] remaining</system_warning>

Example:

Token usage: 57425/200000; 142575 remaining

Calculation Logic

Parse:
  used: Extract first number (57425)
  total: Extract second number (200000)
  remaining: Extract third number (142575)

Compute:
  percentage: (used / total) × 100
  # Example: (57425 / 200000) × 100 = 28.7125%

Format:
  percentage: Round to integer (28.7% → 28%)
  used: Round to K (57425 → 57K)
  total: Round to K (200000 → 200K)
  remaining: Round to K (142575 → 142K)

Output:
  "[%] ([used]K/[total]K) · [remaining]K avail"
  # Example: "28% (57K/200K) · 142K avail"

Formatting Rules

Number Rounding (K format)

Rules:
  < 1,000: Show as-is (e.g., 850 → 850)
  ≥ 1,000: Divide by 1000, round to integer (e.g., 57425 → 57K)

Examples:
  500 → 500
  1500 → 1K (not 2K)
  57425 → 57K
  142575 → 142K
  200000 → 200K

Percentage Rounding

Rules:
  Always round to nearest integer
  No decimal places

Examples:
  28.1% → 28%
  28.7% → 28%
  28.9% → 29%
  30.0% → 30%

Implementation Pattern

Step 1 - Wait for System Notification:
  Execute ANY tool call (Bash, Read, etc.)
  System automatically sends token notification

Step 2 - Extract Values:
  Parse notification text using regex or string split
  Extract: used, total, remaining

Step 3 - Calculate:
  percentage = (used / total) × 100
  Round percentage to integer

Step 4 - Format:
  Convert numbers to K format
  Construct output string

Step 5 - Display:
  🧠 [percentage]% ([used]K/[total]K) · [remaining]K avail

Usage in PM Command

Session Start Protocol (Step 3):
  1. Execute git status (triggers system notification)
  2. Wait for: <system_warning>Token usage: ...</system_warning>
  3. Apply token-counter module logic
  4. Format output: 🧠 [calculated values]
  5. Display to user

Anti-Patterns (FORBIDDEN)

❌ Static Values:
   🧠 30% (60K/200K) · 140K avail  # WRONG - hardcoded

❌ Guessing:
   🧠 ~25% (estimated)  # WRONG - no evidence

❌ Placeholder:
   🧠 [calculating...]  # WRONG - incomplete

✅ Dynamic Calculation:
   🧠 28% (57K/200K) · 142K avail  # CORRECT - real data

Validation

Self-Check Questions:
  ❓ Did I parse the actual system notification?
  ❓ Are the numbers from THIS session, not a template?
  ❓ Does the math check out? (used + remaining = total)
  ❓ Are percentages rounded correctly?
  ❓ Are K values formatted correctly?

Validation Formula:
  used + remaining should equal total
  Example: 57425 + 142575 = 200000 ✅

Edge Cases

No System Notification Yet:
  Action: Execute a tool call first (e.g., git status)
  Then: Parse the notification that appears

Multiple Notifications:
  Action: Use the MOST RECENT notification
  Reason: Token usage increases over time

Parse Failure:
  Fallback: "🧠 [calculating...] (execute a tool first)"
  Then: Retry after next tool call

Integration Points

Used by:

  • commands/pm.md - Session start protocol
  • agents/pm-agent.md - Status reporting
  • Any command requiring token awareness

Dependencies:

  • System-provided notifications (automatic)
  • No external tools required