Counts how often each skill runs, lists the ones that never trigger, and hands a weak description to skill-creator

A Claude Code mod that shows how often each of your skills runs, which never trigger, which reach Claude without their description, and hands a weak description to skill-creator. Part of claude-plugins.
/skill-stats: a table of your skills: uses found in past transcripts, uses since the mod loaded, last seen, listing tokens, and whether Claude sees the description or only the name. /skill-stats all adds every plugin and built in skill./skill-fix <skill>: puts a skill-creator request for that skill's description in the prompt box, with the facts above. It never sends it."Your skills" are the ones in a skills folder, plus the plugin skills from the marketplaces you name.
| Setting | Default | What it does |
|---|---|---|
config_dirs | this account | Comma separated Claude Code config dirs whose transcripts count, for several accounts on one machine |
own_marketplaces | empty | Comma separated marketplace names whose plugin skills count as yours |
source_dirs | empty | Comma separated folders where your skills' source lives, so /skill-fix points at the file to edit and not the installed copy |
projects/*.jsonl under your config directory, or the folders in config_dirs) and those files' dates, the skills listed in this session with their size, and the marketplaces in your settings./skill-stats; /skill-fix fills the prompt box with a skill-creator request and never sends it.tool.call) or a typed skill command (prompt.submit, checked against $.command.list) counts a skill as used and passes on unchanged. It does not hook skill.prompt, which Claude Code's own cc-plugin-sec-default skips on Team and Enterprise accounts, so skills preloaded into a subagent are not counted. command.run answers its own /skill-stats and /skill-fix commands and no other.Needs Claude Code 2.1.287 or later (mods). Tested on 2.1.289.
/plugin marketplace add mutlumehmet/claude-plugins
/plugin install skill-stats@mehmetmutlu
A mod runs inside Claude Code with your permissions. Read the code before you install any mod. Settings are asked for at install, or later with /plugin configure skill-stats@mehmetmutlu.
cleanupPeriodDays.Made by Mehmet Mutlu, part of claude-plugins. More of his work at mehmetmutlu.dev.
hooks/register.js 258 lines1// skill-stats: how often each skill runs, which never trigger, and a one key
2// hand off to skill-creator for a weak description.
3//
4// Two sources of use, kept apart:
5// - history: Skill tool calls and /name commands found in the transcripts of the
6// config dirs the config_dirs setting names (default: this account's), under
7// <dir>/projects/**/*.jsonl. Covers the past, but Claude Code deletes
8// transcripts after cleanupPeriodDays.
9// "Your" skills are the ones from a skills folder, plus the plugin skills from the
10// marketplaces the own_marketplaces setting names.
11// - live: every Skill tool call and typed skill command from now on, kept in
12// $.store, which is per account and survives transcript cleanup.
13
14// Settings, read once in register
15let configDirs = []
16let ownMarketplaces = []
17let sourceDirs = []
18function list(v) {
19 return String(v ?? '').split(',').map((x) => x.trim()).filter(Boolean)
20}
21// GREP is the same pattern for grep -E, which has no \/ escape
22const GREP = '"name":"Skill","input":\\{"skill":"[^"]+"|<command-name>/[^<]+</command-name>'
23const USE = /"name":"Skill","input":\{"skill":"([^"]+)"|<command-name>\/([^<\s]+)<\/command-name>/
24const NOT_MINE = ['plugin', 'built-in', 'mcp']
25// A listing entry this small carries the name and no description. Measured on
26// 2.1.289: entries with a description are 47 tokens and up, bare ones 4 to 13.
27const NAME_ONLY_TOKENS = 20
28
29export function register(on, options) {
30 configDirs = list(options?.config_dirs)
31 ownMarketplaces = list(options?.own_marketplaces)
32 sourceDirs = list(options?.source_dirs)
33 on('session.start', async ($, e, next) => {
34 await $.command.register({
35 name: 'skill-stats',
36 description: 'Skill use counts and the skills that never trigger (add "all" for plugin skills)',
37 })
38 await $.command.register({
39 name: 'skill-fix',
40 description: 'Put a skill-creator request for one skill\'s description in the prompt box',
41 argumentHint: '<skill>',
42 })
43 return next(e)
44 })
45
46 // Live count: a Skill tool call or a typed skill command. Not skill.prompt: a
47 // managed plugin above every user plugin (cc-plugin-sec-default, on Team and
48 // Enterprise accounts) skips it and says so in the transcript. Skills preloaded
49 // into a subagent are not counted.
50 on('tool.call', async ($, e, next) => {
51 const ran = await next(e)
52 if (e.tool === 'Skill' && ran.deny === undefined && !ran.isError) await countUse($, e.skill)
53 return ran
54 })
55
56 on('prompt.submit', async ($, e, next) => {
57 const ran = await next(e)
58 if (e.command && (await isSkillCommand($, e.command.name))) await countUse($, e.command.name)
59 return ran
60 })
61
62 on('command.run', { command: 'skill-stats' }, async ($, e) => {
63 const all = e.args.trim() === 'all'
64 const rows = await collect($, all)
65 return { text: render(rows.list, rows.listing, all) }
66 })
67
68 on('command.run', { command: 'skill-fix' }, async ($, e) => {
69 const name = e.args.trim()
70 if (!name) return { text: 'Usage: /skill-fix <skill>. /skill-stats lists the candidates.' }
71 const rows = await collect($, true)
72 const row = rows.list.find((r) => r.name === name || r.key === name)
73 if (!row) return { text: 'No skill named ' + name + ' in this session.' }
74 const source = await findSource($, row.name)
75 const filled = await $.prompt.fill({ text: fixPrompt(row, rows.listing, source) })
76 return { text: filled.isFilled ? 'The skill-creator request is in the prompt box. Read it, then press Enter.' : fixPrompt(row, rows.listing, source) }
77 })
78}
79
80async function countUse($, name) {
81 const live = { ...((await $.store.get('live')) ?? {}) }
82 const prev = live[name] ?? { n: 0, last: 0 }
83 live[name] = { n: prev.n + 1, last: await $.clock.now() }
84 await $.store.set('live', live)
85}
86
87// A typed command reaches prompt.submit when it asks the model: a skill, a
88// plugin's or the user's markdown command, or a built-in like /review, which
89// is no skill
90async function isSkillCommand($, name) {
91 try {
92 const info = (await $.command.list()).find((c) => c.name === name)
93 return info !== undefined && (info.source === 'plugin' || info.source === 'user')
94 } catch {
95 return false
96 }
97}
98
99// Every skill this session has, with its history and live counts
100async function collect($, all) {
101 const usage = await $.session.usage({ breakdown: 'summary' })
102 const skills = usage.context.breakdown?.skills
103 const listing = skills
104 ? { total: skills.totalSkills, included: skills.includedSkills, tokens: skills.tokens }
105 : null
106 const listed = skills?.skillFrontmatter ?? []
107
108 const history = await scanHistory($)
109 const live = (await $.store.get('live')) ?? {}
110 const marketplaceOf = await pluginMarketplaces($)
111 const mine = (s) => !NOT_MINE.includes(s.source) ||
112 (s.source === 'plugin' && ownMarketplaces.includes(marketplaceOf.get(s.pluginName) ?? ''))
113
114 const rows = listed
115 .filter((s) => all || mine(s))
116 .map((s) => {
117 // A plugin skill is called as plugin:name, a user skill by its bare name
118 const key = s.pluginName && !s.name.includes(':') ? s.pluginName + ':' + s.name : s.name
119 // Synced claude.ai skills are called with a prefix (anthropic-skills:pdf), so add up every key ending in :name too
120 const past = sum(history, key, s.name)
121 const l = sum(new Map(Object.entries(live)), key, s.name)
122 return { name: s.name, key, source: s.source, tokens: s.tokens, nameOnly: s.tokens < NAME_ONLY_TOKENS, uses: past.n, liveUses: l.n, last: Math.max(past.last, l.last) }
123 })
124 .sort((a, b) => b.uses + b.liveUses - (a.uses + a.liveUses) || a.name.localeCompare(b.name))
125 return { list: rows, listing }
126}
127
128// skill name -> { n, last }, last being the newest transcript file it appears in
129async function scanHistory($) {
130 const home = await $.env.get('HOME')
131 const own = (await $.env.get('CLAUDE_CONFIG_DIR')) || home + '/.claude'
132 const dirs = (configDirs.length ? configDirs : [own]).map((d) => d.replace(/^~(?=\/|$)/, home).replace(/\/+$/, '') + '/projects')
133 const grep = await $.process.run(
134 ['grep', '-roE', '--include=*.jsonl', GREP, ...dirs],
135 { timeoutMs: 60000 },
136 )
137 // grep exits 1 when nothing matched, 2 on an error such as a missing dir
138 const found = new Map()
139 const files = new Set()
140 for (const line of grep.stdout.split('\n')) {
141 const at = line.indexOf('.jsonl:')
142 if (at < 0) continue
143 const file = line.slice(0, at + 6)
144 const m = USE.exec(line.slice(at + 7))
145 if (!m) continue
146 const name = m[1] ?? m[2]
147 const entry = found.get(name) ?? { n: 0, files: new Set() }
148 entry.n += 1
149 entry.files.add(file)
150 found.set(name, entry)
151 files.add(file)
152 }
153
154 const mtime = new Map()
155 if (files.size) {
156 const stat = await $.process.run(['stat', '-f', '%m %N', ...files])
157 for (const line of stat.stdout.split('\n')) {
158 const sp = line.indexOf(' ')
159 if (sp > 0) mtime.set(line.slice(sp + 1), Number(line.slice(0, sp)) * 1000)
160 }
161 }
162
163 const out = new Map()
164 for (const [name, entry] of found) {
165 let last = 0
166 for (const f of entry.files) last = Math.max(last, mtime.get(f) ?? 0)
167 out.set(name, { n: entry.n, last })
168 }
169 return out
170}
171
172function sum(map, key, name) {
173 let n = 0, last = 0
174 for (const [k, v] of map) {
175 if (k === key || k === name || (!name.includes(':') && k.endsWith(':' + name))) {
176 n += v.n
177 last = Math.max(last, v.last)
178 }
179 }
180 return { n, last }
181}
182
183function day(ms) {
184 return ms ? new Date(ms).toISOString().slice(0, 10) : 'never'
185}
186
187function render(list, listing, all) {
188 const lines = []
189 if (listing) {
190 lines.push('Skill listing: ' + listing.included + ' of ' + listing.total + ' skills fit its budget (' + listing.tokens + ' tokens).')
191 if (listing.included < listing.total) {
192 lines.push('Skills past the budget are listed without their description, so Claude rarely picks them. Fewer plugin skills helps more than a better description there.')
193 }
194 lines.push('')
195 }
196 const width = Math.max(5, ...list.map((r) => r.key.length))
197 lines.push('skill'.padEnd(width) + ' history live last seen tokens listed as source')
198 for (const r of list) {
199 lines.push(
200 r.key.padEnd(width) + ' ' + String(r.uses).padStart(7) + ' ' + String(r.liveUses).padStart(4) +
201 ' ' + day(r.last).padEnd(10) + ' ' + String(r.tokens).padStart(6) + ' ' + (r.nameOnly ? 'name only' : 'full ') + ' ' + r.source,
202 )
203 }
204 const never = list.filter((r) => r.uses + r.liveUses === 0).map((r) => r.key)
205 lines.push('')
206 lines.push(never.length ? 'Never triggered (' + never.length + '): ' + never.join(', ') : 'Every listed skill has triggered at least once.')
207 const bare = list.filter((r) => r.nameOnly).map((r) => r.key)
208 if (bare.length) lines.push('Listed by name only, no description (' + bare.length + '): ' + bare.join(', ') + '. Claude can only match these by name.')
209 lines.push('History comes from the transcripts in ' + (configDirs.length ? configDirs.join(', ') : 'this account') + ', which Claude Code deletes after cleanupPeriodDays. Live counts start when this mod loads.')
210 if (never.length) lines.push('/skill-fix <skill> puts a skill-creator request for one of them in the prompt box.')
211 if (!all) lines.push('/skill-stats all adds plugin and built-in skills.')
212 return lines.join('\n')
213}
214
215// plugin name -> marketplace, from the enabled plugins in the merged settings
216async function pluginMarketplaces($) {
217 const out = new Map()
218 try {
219 const settings = await $.settings.read()
220 for (const id of Object.keys(settings?.enabledPlugins ?? {})) {
221 const at = id.lastIndexOf('@')
222 if (at > 0) out.set(id.slice(0, at), id.slice(at + 1))
223 }
224 } catch {
225 // No settings: only skills from a skills folder count as yours
226 }
227 return out
228}
229
230// The SKILL.md to edit, found under the source_dirs setting (an installed plugin is a cached copy)
231async function findSource($, name) {
232 if (!sourceDirs.length || !/^[\w.-]+$/.test(name)) return null
233 const home = await $.env.get('HOME')
234 const roots = sourceDirs.map((d) => d.replace(/^~(?=\/|$)/, home))
235 const found = await $.process.run(['find', ...roots, '-path', '*/skills/' + name + '/SKILL.md', '-not', '-path', '*/node_modules/*'])
236 return found.stdout.split('\n').find(Boolean) ?? null
237}
238
239function fixPrompt(row, listing, source) {
240 const facts = [
241 'uses in transcripts: ' + row.uses,
242 'live uses: ' + row.liveUses,
243 'last seen: ' + day(row.last),
244 'listing tokens: ' + row.tokens + (row.nameOnly ? ' (listed by name only, the description does not reach Claude; find out why before rewriting it)' : ''),
245 'source: ' + row.source,
246 ]
247 if (listing && listing.included < listing.total) {
248 facts.push('the skill listing holds ' + listing.included + ' of ' + listing.total + ' skills, so some are listed without a description')
249 }
250 return (
251 '/anthropic-skills:skill-creator Improve the description of the ' + row.key +
252 ' skill so it triggers when it should. ' + facts.join('; ') +
253 '. Read its SKILL.md first' +
254 (source ? ' (the source to edit is ' + source + '; the installed plugin is a cached copy, so after the edit bump that plugin\'s version and push)' : '') +
255 ', propose the new description, and wait for my yes before writing it.'
256 )
257}
258