skill-token-budget
Installation
Review the source and dependencies with a prompt. A package is available to download.
Install with an AI assistant
Copy the prompt so your assistant can review the pinned revision and dependencies first.
View promptHide prompt
Check whether SkillPI Skill Finder is installed for your current agent. If it is missing, install and enable it using the official instructions at https://github.com/Findskills/skillpi. Then use Skill Finder to locate and install "skill-token-budget" from this exact SkillPI detail page: https://skillpi.com/skills/deepskill-8a0c74d2eec447a309926baa070a84fc000cbc76e3c5c1b41f547155ea4aba66. Verify the upstream source and version against https://skillhub.cn/skills/user_58fad8c3/skill-token-budget before installing. If they do not match, stop and report the mismatch. Report the installed path and source version.
Source and setup
skillhub.cn
Setup guidance pending
See the original source for supported tools, dependencies, and permissions.
Installation test pending
Skill token-budget measurer and lazy-load simulator: attach a “token budget + truncation strategy” to any skill to quantify the measured token savings before and after loading. Supports static budgets, dynamic budgets derived from remaining session tokens, and configurable target budget ratios (--budget-ratio). Intended for slimming skills, measuring loading cost, producing token_budget.json, validating lazy-loading value, and context-engineering verification. Trigger phrases include token budget, skill lazy-loading, token measurement, etc.
Introduction source: third_party · user_58fad8c3 · 1.0.4 Reference
SkillPi interpretation
- Tasks
- Translation pending; see source documentation.
- Software, accounts and paid services
- "$PY" "$SKILL/scripts/measure_token_budget.py" --skill-dir "$TARGET" --budget 6000 --strategy truncate_in_file ```
- Testing
- Not tested
Browse files
Frontmatter
name: skill-token-budget
agent_created: true
description: 技能 token 预算测量与懒加载模拟器。给任意大技能加上「token 预算 + 截断策略」配置,量化加载前后的 token 差值(实测节省,非预设);支持静态写死预算、按会话剩余 token 自动推算的动态预算、以及可配置的目标预算比例(--budget-ratio)。当用户想给技能做 token 瘦身、测技能加载成本、配 token_budget.json、验证懒加载价值、做技能上下文工程时使用。触发词:token 预算、技能懒加载、token 测量、预算截断、技能瘦身、上下文工程、token budget、技能 token 估算、measure token。技能 Token 预算器(Skill Token Budget)
给大技能配 token_budget.json、量化「加载前后 token 差值」、验证懒加载价值。
本技能自包含:脚本位于 scripts/measure_token_budget.py,纯 Python 标准库实现,无需任何第三方依赖、无需建 venv、无需 pip install。与 skill-inventory、skill-orchestrator 同款形态。
何时用
- 某个技能
.md文件很多(如mc-integrated有 35 张方法卡),怀疑加载时 token 太重。 - 想给技能加「token 预算 + 跳过低频模块 / 文件内截断」策略,但先要用数据证明值得做。
- 想把预算从「写死数字」升级为「按会话剩余 token 自动推算」(动态预算)。
调用方式(直接用托管 Python)
PY="C:/Users/13301/.workbuddy/binaries/python/versions/3.13.12/python.exe"
SKILL="C:/Users/13301/.workbuddy/skills/skill-token-budget"
TARGET="C:/Users/13301/.workbuddy/skills/mc-integrated" # 被测技能目录
# 1) probe:先看全量 token(无预算)
"$PY" "$SKILL/scripts/measure_token_budget.py" --skill-dir "$TARGET" --probe
# 2) 正式测量:读被测技能目录里的 token_budget.json(推荐,先给它写好配置)
"$PY" "$SKILL/scripts/measure_token_budget.py" --skill-dir "$TARGET" --out ./TOKEN_REPORT.md
# 3) 动态预算:按会话剩余 token 自动推算(覆盖写死数字)
"$PY" "$SKILL/scripts/measure_token_budget.py" --skill-dir "$TARGET" --session-remaining 20000 --out ./TOKEN_DYN.md
# 4) 手动覆盖预算/策略(不依赖 json 配置)
"$PY" "$SKILL/scripts/measure_token_budget.py" --skill-dir "$TARGET" --budget 6000 --strategy truncate_in_file
参数
| 参数 | 默认值 | 说明 |
|---|---|---|
--skill-dir |
必填 | 被测技能目录 |
--probe |
关 | 只看全量 token 排行,不做预算 |
--budget |
读 json 的 total_budget_tokens |
静态预算上限(token),命令行覆盖配置 |
--strategy |
读 json 的 strategy |
skip_low_priority / truncate_in_file |
--session-remaining |
读环境变量 SESSION_REMAINING_TOKENS |
【动态预算】会话剩余 token,提供后预算自动推算 |
--ratio |
读 json 的 dynamic.ratio(默认 0.5) |
动态预算下技能占剩余 token 的比例上限 |
--out |
无 | 报告 md 输出路径 |
被测技能需提供的配置:token_budget.json(放在被测技能目录根)
{
"total_budget_tokens": 12000,
"dynamic": {"enabled": true, "ratio": 0.5},
"strategy": "skip_low_priority",
"priority": [
"SKILL.md",
"references/cards/cluster1-strategy-external.md",
"references/cards/cluster2-strategy-internal.md"
]
}
priority[0]视为核心(如 SKILL.md 路由引擎),动态预算下永远完整可载,不被截断。- 优先级越靠前越先加载;超预算后整文件跳过(skip)或按剩余比例截断(truncate)。
- 未在
priority表的文件排最后,触发时再 Read。
两种截断策略
| 策略 | 行为 | 适合 |
|---|---|---|
skip_low_priority |
按优先级累计,超预算的整文件跳过(真·懒加载) | 模块间独立、按需再加载 |
truncate_in_file |
超预算文件按剩余预算比例截取前 N 字符 | 单文件内含多卡、须保留头部 |
动态预算公式
预算 = min(全量, max(会话剩余 × ratio, 核心))
- 会话充裕 → 预算 ≥ 全量 → 全载(零浪费)
- 会话紧张 → 自动收紧到「核心 + 高频」,避免写死预算的两种浪费(小会话硬塞 / 大会话误截)
- 核心(路由引擎)始终保底,技能不失效
关键实现坑(已修复,备忘)
- token 是确定性启发式估算(CJK 每字~1;英文词每 4 字符~1;其他每 4 字符~1),用于测相对差值,非 tiktoken 精确计数。
- Windows 路径
\U:命令里渲染OUT时务必转正斜杠,否则python -c触发unicodeescape报错。 - 动态预算缺省:不传
--session-remaining且没环境变量时回退静态配置,不会崩。 - Windows 控制台:stdout 加
reconfigure(utf-8, errors="replace")防 GBK 编码炸。
产出
- 控制台:预算内/全量 token、节省比例、已加载/跳过/截断清单。
--out时:报告 md(含[TOC]、结论、预算推算、文件 token 排行、已加载/跳过/截断明细、方法说明)。- 被测技能目录新增
token_budget.json(由使用者编写,本技能只读取并量化)。
批量模式(一键扫多个大技能)
对一批技能批量出 token 报告,支持自动生成配置并验证节省。脚本 scripts/batch_token_report.py(同样纯 stdlib,subprocess 调 measure_token_budget.py)。
PY="C:/Users/13301/.workbuddy/binaries/python/versions/3.13.12/python.exe"
SKILL="C:/Users/13301/.workbuddy/skills/skill-token-budget"
# 扫指定技能 + 自动纳入全量>8000 的大技能;无配置者自动生成 token_budget.json 并测节省
"$PY" "$SKILL/scripts/batch_token_report.py" \
--only industry-research-pro,material-deep-analysis \
--auto-min 8000 --auto-config --out ./TOKEN_BATCH_REPORT.md
参数:
| 参数 | 默认 | 说明 |
|------|------|------|
| --only | industry-research-pro,material-deep-analysis | 逗号分隔技能名列表 |
| --auto-min | 8000 | 全量 token 超此值自动纳入(0=关) |
| --auto-config | 关 | 对无 token_budget.json 的技能,按「SKILL.md 必载 + 其余按 token 降序」自动生成配置并测节省 |
| --budget-ratio | 0.63 | 目标预算比例(假设值,可配):自动生成配置时 预算=全量×ratio。改这个值验证不同瘦身力度(如 0.5=目标砍一半),实测节省仍以报告「节省%」为准 |
| --all-skills | 关 | 关闭「仅自制技能」安全过滤,处理所有技能(含官方/市场)。默认仅处理自制技能 |
| --skills-root | ~/.workbuddy/skills | 技能根目录 |
| --out | ./token_batch_report_<日期>.md | 汇总 md |
安全默认(self-only,重要):批量模式默认仅处理 自制技能(SKILL.md frontmatter 含 agent_created: true)。官方/市场技能(如 ppt-generator-skill、excel-auto-zh 等)默认被跳过,绝不向其中写入 token_budget.json。若确实要处理非自制技能,显式加 --all-skills。
行为:有配置→测节省(existing);--auto-config 且无配置→自动生成后测(auto-generated);否则仅 probe 体量(probe only);非自制且未加 --all-skills→跳过(skipped)。汇总表按全量 token 降序,含模式/全量/预算内/节省%/已载跳过;明细展开已载跳过清单与建议。
⚠️ 目标预算 vs 实测节省(不要循环论证):自动配置的预算 = 全量 × --budget-ratio(默认 0.63)是人为设定的目标假设,不是"验证出来的 37% 规律"。报告里的「节省%」是实测值——由 skip_low_priority 整文件跳过粒度决定,随技能文件分布而变,通常 ≥ (1-ratio),不会恰好等于 37%。想比较不同瘦身力度就改 --budget-ratio(如 --budget-ratio 0.5)再看实测节省怎么变。生成的 token_budget.json 会记录本次用的 budget_ratio_assumed,便于事后区分"这是预设目标"还是"这是实测结果"。
衔接
- 配合
skill-inventory:先治理后编排——台账理清资产后,对大技能做 token 瘦身。 - 配合
skill-orchestrator:DAG 编排的out产物若需二次处理,可用本技能评估编排脚本自身加载成本。 - 与下一篇 Skills 工程文章互证:Token 预算 / 上下文动态修剪 / 脚本增强结构化。
Current listing version
1.0.4
Safety and assessment details
Quality assessment
Out of 100 · Assessment report from SkillPi LAB.
Highlights
- 解决大技能 token 加载成本高、上下文工程优化的实际痛点
- 支持静态/动态预算、多种截断策略,适用场景广泛
- 提供批量模式,可一键扫描多个技能并自动生成配置
Highlights
- 触发词覆盖全面且边界清晰(token 预算、技能懒加载、token 测量等)
- 无与其他 Skill 明显重叠或歧义
Areas to improve
-
description 重复 Reported severity: Minor
frontmatter 和 skill_body 开头均包含完整 description,存在冗余
Recommendation: 保留一处即可,建议移除 skill_body 开头的重复 description
-
缺少显式 'When to Use' 章节标题 Reported severity: Minor
内容已涵盖使用场景,但未使用标准标题 'When to Use'
Recommendation: 将 '## 何时用' 改为 '## When to Use' 以符合规范
Highlights
- 文档结构完整,含调用方式、参数表、配置示例、策略对比、公式说明
- 提供端到端调用示例和批量模式说明
- frontmatter 完整,单文件长度合理
Highlights
- 纯 Python 标准库实现,无需第三方依赖,部署简单
- 覆盖主要异常处理(路径转义、编码、缺省回退)
- 批量模式有安全默认(仅处理自制技能),防止误操作
The arithmetic mean of the first four original scores, rounded to a whole number.
Static text checks
100 / 100 · skillpi-20261123-no-r3-v1
No current rules matched the text.
This score reflects rule matches in the text and is provided for reference before use. It excludes attachments and runtime behavior.
Permissions and checks before use
Automatically summarized from this main document. Matches may be examples or defensive advice; they are not actual permission declarations. Attachments, external dependencies and runtime behavior are not covered.
-
Install or fetch external dependencies
Use official sources and pin versions. Check install scripts in an isolated environment. This document check does not cover code downloaded later.
Mentioned in main document · lines 11
Security rating and access scope
safe · Static evidence review; not a runtime safety guarantee.
"$PY" "$SKILL/scripts/measure_token_budget.py" --skill-dir "$TARGET" --budget 6000 --strategy truncate_in_file ```
Before use, confirm and authorize actual file access, external services and network destinations in your host. This review grants no runtime permissions.
User discussion
No verified reviews yet.