Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion skills/fleet-auditor/scripts/fleet.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,34 @@
init_sqlite_db,
migrate_add_columns,
estimate_tokens_from_file,
estimate_tokens_from_text,
)


def _estimate_skill_frontmatter_tokens(skill_md: Path) -> int:
"""Estimate tokens of a skill's YAML frontmatter only.

Claude Code loads only each skill's frontmatter (name + description)
into the session at startup. The SKILL.md body is loaded on demand
when the user invokes the skill via the Skill tool. Measuring the
full file over-counts overhead by ~10-20x.

Falls back to 100 tokens (the documented average) if frontmatter
cannot be parsed.
"""
try:
text = skill_md.read_text(encoding="utf-8", errors="replace")
except (OSError, PermissionError):
return 100
if not text.startswith("---"):
return 100
# Look for the closing --- after the opening one
end_idx = text.find("\n---", 4)
if end_idx == -1:
return 100
frontmatter = text[: end_idx + 4]
return estimate_tokens_from_text(frontmatter)

# ---------------------------------------------------------------------------
# Constants
# ---------------------------------------------------------------------------
Expand Down Expand Up @@ -549,6 +575,10 @@ def parse_config(self) -> dict:
}

# Skills
# Only the YAML frontmatter (name + description) is loaded into the
# session at startup. SKILL.md bodies load on demand when the user
# invokes the skill. Measuring the full file over-counts by ~10-20x
# and inflates skill_bloat findings. See issue #16.
skills_dir = CLAUDE_DIR / "skills"
if skills_dir.exists():
for sd in skills_dir.iterdir():
Expand All @@ -557,7 +587,7 @@ def parse_config(self) -> dict:
if skill_md.exists():
config["skills"].append({
"name": sd.name,
"tokens": estimate_tokens_from_file(skill_md),
"tokens": _estimate_skill_frontmatter_tokens(skill_md),
})

# CLAUDE.md
Expand Down
Loading