context-budget
审核Claude Code上下文窗口在代理、技能、MCP服务器和规则中的消耗情况。识别膨胀、冗余组件,并提供优先的令牌节省建议。
About this skill
This skill provides an in-depth analysis of the Claude Code AI agent's context window consumption. It systematically scans all loaded components—including agents, skills, rules, and MCP servers—to estimate their token usage. The skill identifies potential areas of inefficiency, such as bloated files (e.g., too many lines), redundant components (e.g., duplicate skills), overly verbose descriptions (preamble bloat), or overlapping rule sets. By flagging these issues and providing prioritized, actionable recommendations, 'context-budget' helps users optimize their agent's configuration, free up valuable context space, and ultimately improve session performance and output quality. It's an essential tool for maintaining an efficient and performant Claude Code environment, drawing from a comprehensive collection of best practices for Claude AI agents.
Best use case
Optimizing AI agent performance by effectively managing the context window; troubleshooting slow sessions or degraded output quality; understanding current context consumption and available capacity; planning for adding new components without hitting context limits.
审核Claude Code上下文窗口在代理、技能、MCP服务器和规则中的消耗情况。识别膨胀、冗余组件,并提供优先的令牌节省建议。
A detailed report on token consumption for each loaded component (agents, skills, rules, MCP servers), clear identification of inflated or redundant components with specific flags, and prioritized, actionable recommendations for saving tokens and freeing up context space, leading to improved agent performance and output quality.
Practical example
Example input
/context-budget
Example output
Context Budget Report for Claude Code Session: Overall Context Window Usage: 8,500 / 100,000 tokens (8.5%) Available Context Headroom: 91,500 tokens Components Analysis: Agents (`agents/*.md`): - core_coding_agent.md: 3,200 tokens (Heavy: >200 lines) - research_assistant.md: 750 tokens (Preamble Bloat: Description >30 words) Skills (`skills/*/SKILL.md`): - file_manager_skill: 1,100 tokens - code_refactor_skill: 400 tokens (Redundant: Duplicate of .agents/skills/code_refactor) Rules (`rules/**/*.md`): - python_best_practices.md: 580 tokens - error_handling_guidelines.md: 220 tokens (Overlap detected with general_coding_rules.md) MCP Servers (`.mcp.json` / active config): - dev_ops_server: 12 tools (Estimated 6,000 tokens for schema) - cli_utility_server: 3 tools (gh, git, npm) (Tagged: Wraps simple CLI commands) Optimization Recommendations (Prioritized): 1. **Refactor `core_coding_agent.md`**: Reduce file size by splitting into smaller, modular components to save ~1,500 tokens. 2. **Shorten `research_assistant.md` description**: Condense the preamble to under 30 words, saving ~100 tokens. 3. **Remove redundant `code_refactor_skill`**: Delete the duplicate skill from `skills/` to save ~400 tokens. 4. **Consolidate `error_handling_guidelines.md`**: Merge overlapping content with `general_coding_rules.md` to avoid redundancy and save ~150 tokens. 5. **Review `cli_utility_server`**: Consider if simple CLI commands like `gh` and `git` need dedicated MCP server wrapping; potentially remove or integrate differently to save schema tokens. *Note: Token counts are estimates based on heuristics (lines, word count, schema complexity).*
When to use this skill
- Session performance feels sluggish or output quality starts to decline.
- You've recently added numerous skills, agents, or MCP servers to your Claude Code environment.
- You need to understand the actual context headroom available to your agent.
- You plan to integrate more components and need to assess if there's sufficient context space.
When not to use this skill
- If you are not using Claude Code or an agent that manages its own context window.
- If your agent is consistently performing optimally and you are not concerned about context usage.
- If you are not adding or modifying agent components and do not perceive any performance issues related to context.
Installation
Claude Code / Cursor / Codex
Manual Installation
- Download SKILL.md from GitHub
- Place it in
.claude/skills/context-budget/SKILL.mdinside your project - Restart your AI agent — it will auto-discover the skill
How context-budget Compares
| Feature / Agent | context-budget | Standard Approach |
|---|---|---|
| Platform Support | Claude | Limited / Varies |
| Context Awareness | High | Baseline |
| Installation Complexity | easy | N/A |
Frequently Asked Questions
What does this skill do?
审核Claude Code上下文窗口在代理、技能、MCP服务器和规则中的消耗情况。识别膨胀、冗余组件,并提供优先的令牌节省建议。
Which AI agents support this skill?
This skill is designed for Claude.
How difficult is it to install?
The installation complexity is rated as easy. You can find the installation instructions above.
Where can I find the source code?
You can find the source code on GitHub using the link provided at the top of the page.
Related Guides
AI Agents for Coding
Browse AI agent skills for coding, debugging, testing, refactoring, code review, and developer workflows across Claude, Cursor, and Codex.
Best AI Skills for Claude
Explore the best AI skills for Claude and Claude Code across coding, research, workflow automation, documentation, and agent operations.
ChatGPT vs Claude for Agent Skills
Compare ChatGPT and Claude for AI agent skills across coding, writing, research, and reusable workflow execution.
SKILL.md Source
# 上下文预算
分析 Claude Code 会话中每个已加载组件的令牌开销,并提供可操作的优化建议以回收上下文空间。
## 使用时机
* 会话性能感觉迟缓或输出质量下降
* 你最近添加了许多技能、代理或 MCP 服务器
* 你想知道实际有多少上下文余量
* 计划添加更多组件,需要知道是否有空间
* 运行 `/context-budget` 命令(本技能为其提供支持)
## 工作原理
### 阶段 1:清单
扫描所有组件目录并估算令牌消耗:
**代理** (`agents/*.md`)
* 统计每个文件的行数和令牌数(单词数 × 1.3)
* 提取 `description` 前言长度
* 标记:文件 >200 行(繁重),描述 >30 词(臃肿的前言)
**技能** (`skills/*/SKILL.md`)
* 统计 SKILL.md 的令牌数
* 标记:文件 >400 行
* 检查 `.agents/skills/` 中的重复副本 — 跳过相同副本以避免重复计数
**规则** (`rules/**/*.md`)
* 统计每个文件的令牌数
* 标记:文件 >100 行
* 检测同一语言模块中规则文件之间的内容重叠
**MCP 服务器** (`.mcp.json` 或活动的 MCP 配置)
* 统计配置的服务器数量和工具总数
* 估算模式开销约为每个工具 500 令牌
* 标记:工具数 >20 的服务器,包装简单 CLI 命令的服务器 (`gh`, `git`, `npm`, `supabase`, `vercel`)
**CLAUDE.md**(项目级 + 用户级)
* 统计 CLAUDE.md 链中每个文件的令牌数
* 标记:合并总数 >300 行
### 阶段 2:分类
将每个组件归入一个类别:
| 类别 | 标准 | 操作 |
|--------|----------|--------|
| **始终需要** | 在 CLAUDE.md 中被引用,支持活动命令,或匹配当前项目类型 | 保留 |
| **有时需要** | 特定领域(例如语言模式),未在 CLAUDE.md 中引用 | 考虑按需激活 |
| **很少需要** | 无命令引用,内容重叠,或无明显的项目匹配 | 移除或延迟加载 |
### 阶段 3:检测问题
识别以下问题模式:
* **臃肿的代理描述** — 前言中描述 >30 词,会在每次任务工具调用时加载
* **繁重的代理** — 文件 >200 行,每次生成时都会增加任务工具的上下文
* **冗余组件** — 重复代理逻辑的技能,重复 CLAUDE.md 的规则
* **MCP 超额订阅** — >10 个服务器,或包装了可免费使用的 CLI 工具的服务器
* **CLAUDE.md 臃肿** — 冗长的解释、过时的部分、本应成为规则的指令
### 阶段 4:报告
生成上下文预算报告:
```
上下文预算报告
═══════════════════════════════════════
总预估开销:约 XX,XXX 个词元
上下文模型:Claude Sonnet (200K 窗口)
有效可用上下文:约 XXX,XXX 个词元 (XX%)
组件细分:
┌─────────────────┬────────┬───────────┐
│ 组件 │ 数量 │ 词元数 │
├─────────────────┼────────┼───────────┤
│ Agents │ N │ ~X,XXX │
│ Skills │ N │ ~X,XXX │
│ Rules │ N │ ~X,XXX │
│ MCP tools │ N │ ~XX,XXX │
│ CLAUDE.md │ N │ ~X,XXX │
└─────────────────┴────────┴───────────┘
WARNING: 发现的问题 (N):
[按可节省词元数排序]
前 3 项优化建议:
1. [action] → 节省约 X,XXX 个词元
2. [action] → 节省约 X,XXX 个词元
3. [action] → 节省约 X,XXX 个词元
潜在节省空间:约 XX,XXX 个词元 (占当前开销的 XX%)
```
在详细模式下,额外输出每个文件的令牌计数、最繁重文件的行级细分、重叠组件之间的具体冗余行,以及 MCP 工具列表和每个工具模式大小的估算。
## 示例
**基本审计**
```
/context-budget
技能:扫描设置 → 16个代理(12,400个令牌),28个技能(6,200),87个MCP工具(43,500),2个CLAUDE.md(1,200)
标记:3个重型代理,14个MCP服务器(3个可替换为CLI)
最高节省:移除3个MCP服务器 → -27,500个令牌(减少47%开销)
```
**详细模式**
```
/context-budget --verbose
技能:完整报告 + 按文件细目显示 planner.md(213 行,1,840 个令牌),
MCP 工具列表及每个工具的大小,重复规则行并排显示
```
**扩容前检查**
```
User: 我想再添加5个MCP服务器,有空间吗?
Skill: 当前开销33% → 添加5个服务器(约50个工具)会增加约25,000个tokens → 开销将升至45%
建议:先移除2个可用CLI替代的服务器以保持在40%以下
```
## 最佳实践
* **令牌估算**:对散文使用 `words × 1.3`,对代码密集型文件使用 `chars / 4`
* **MCP 是最大的杠杆**:每个工具模式约消耗 500 令牌;一个 30 个工具的服务器开销超过你所有技能的总和
* **代理描述始终加载**:即使代理从未被调用,其描述字段也存在于每个任务工具上下文中
* **详细模式用于调试**:需要精确定位导致开销的确切文件时使用,而非用于常规审计
* **变更后审计**:添加任何代理、技能或 MCP 服务器后运行,以便及早发现增量Related Skills
rules-distill
扫描技能以提取跨领域原则并将其提炼为规则——追加、修订或创建新的规则文件
strategic-compact
Suggests manual context compaction at logical intervals to preserve context through task phases rather than arbitrary auto-compaction.
context-management-context-save
Use when working with context management context save
token-budget-advisor
Offers the user an informed choice about how much response depth to consume before answering. Use this skill when the user explicitly wants to control response length, depth, or token budget. TRIGGER when: "token budget", "token count", "token usage", "token limit", "response length", "answer depth", "short version", "brief answer", "detailed answer", "exhaustive answer", "respuesta corta vs larga", "cuántos tokens", "ahorrar tokens", "responde al 50%", "dame la versión corta", "quiero controlar cuánto usas", or clear variants where the user is explicitly asking to control answer size or depth. DO NOT TRIGGER when: user has already specified a level in the current session (maintain it), the request is clearly a one-word answer, or "token" refers to auth/session/payment tokens rather than response size.
clawdnet
Register and manage AI agents on ClawdNet, the decentralized agent registry. Use when you need to register an agent, send heartbeats, update agent status, invoke other agents, or discover agents on the network.
SkillTree 主逻辑 🌳
---
workspace-surface-audit
Audit the active repo, MCP servers, plugins, connectors, env surfaces, and harness setup, then recommend the highest-value ECC-native skills, hooks, agents, and operator workflows. Use when the user wants help setting up Claude Code or understanding what capabilities are actually available in their environment.
ui-demo
Record polished UI demo videos using Playwright. Use when the user asks to create a demo, walkthrough, screen recording, or tutorial video of a web application. Produces WebM videos with visible cursor, natural pacing, and professional feel.
skill-comply
Visualize whether skills, rules, and agent definitions are actually followed — auto-generates scenarios at 3 prompt strictness levels, runs agents, classifies behavioral sequences, and reports compliance rates with full tool call timelines
santa-method
Multi-agent adversarial verification with convergence loop. Two independent review agents must both pass before output ships.
safety-guard
Use this skill to prevent destructive operations when working on production systems or running agents autonomously.
repo-scan
Cross-stack source code asset audit — classifies every file, detects embedded third-party libraries, and delivers actionable four-level verdicts per module with interactive HTML reports.