Communitygithub.com

上下文预算

审计Claude Code会话中所有加载组件的上下文窗口消耗情况,识别膨胀、冗余组件,并生成优先级的Token节省建议。当上下文窗口过快填满,需要找出哪些代理、技能、MCP服务器或规则正在消耗资源时使用。

上下文预算 是什么?

上下文预算技能用于审计Claude Code会话中所有加载组件的Token开销,帮助开发者回收上下文空间。它通过四个阶段工作:首先扫描代理、技能、规则和MCP服务器的文件并估算Token消耗;然后根据必要性对组件分类(始终需要、偶尔需要、很少需要);接着检测问题模式,如臃肿的代理描述、冗余组件、MCP服务器过度订阅等;最后生成带有优化建议的详细报告,按可节省的Token进行排序。该技能支持标准模式和详细模式,详细模式可提供逐文件、逐行的Token分解和重复内容比较。对于已集成多个代理、技能或MCP服务器的开发者特别有用,可通过移除冗余组件(尤其是MCP工具,每个工具模式约500 Token)有效降低上下文消耗,从而提升会话性能并避免Token限制。

兼容平台✓Claude Code~Codex CLI~Cursor
npx skills add https://github.com/affaan-m/everything-claude-code/tree/main/skills/context-budget

在你喜欢的 AI 中提问

打开一个已预加载此 Agent Skill 的新对话。

文档

上下文预算 是做什么的?

分析Claude Code会话中每个加载组件的Token开销,并呈现可操作的空间回收优化方案。

何时使用

  • 会话性能感觉迟钝或输出质量下降
  • 你最近添加了大量技能、代理或MCP服务器
  • 你想知道自己实际还有多少上下文空间
  • 计划添加更多组件,想知道是否有足够空间
  • 运行/context-budget命令(此技能为其提供支持)

工作原理

阶段 1:盘点

扫描所有组件目录并估算Token消耗:

代理 (agents/*.md)

  • 统计每个文件的行数和Token数(单词数 × 1.3)
  • 提取description前置元数据长度
  • 标记:文件 >200行(沉重)、描述 >30词(前置元数据臃肿)

技能 (skills/*/SKILL.md)

  • 统计每个SKILL.md的Token数
  • 标记:文件 >400行
  • 检查.agents/skills/中是否存在重复副本——跳过相同副本以避免重复计数

规则 (rules/**/*.md)

  • 统计每个文件的Token数
  • 标记:文件 >100行
  • 检测同一语言模块内规则文件之间的内容重叠

MCP服务器 (.mcp.json或活跃MCP配置)

  • 统计配置的服务器数量和总工具数量
  • 估计每个工具的模式开销约为500 Token
  • 标记:工具数量 >20的服务器、包装简单CLI命令(gh、git、npm、supabase、vercel)的服务器

CLAUDE.md(项目级别 + 用户级别)

  • 统计CLAUDE.md链中每个文件的Token数
  • 标记:合并总行数 >300行

阶段 2:分类

将每个组件归入一个类别:

类别标准操作
始终需要在CLAUDE.md中被引用、支持某个活跃命令、或与当前项目类型匹配保留
偶尔需要领域特定(如语言模式)、未在CLAUDE.md中被引用考虑按需激活
很少需要无命令引用、存在内容重叠、或无明显的项目匹配移除或延迟加载

阶段 3:检测问题

识别以下问题模式:

  • 臃肿的代理描述 —— 前置元数据中描述 >30词,每次任务工具调用都会加载
  • 沉重的代理 —— 文件 >200行,每次生成时膨胀任务工具上下文
  • 冗余组件 —— 技能重复代理逻辑,规则重复CLAUDE.md
  • MCP过度订阅 —— 超过10个服务器,或包装免费CLI工具的服务器
  • CLAUDE.md臃肿 —— 冗长的解释、过时的章节、应作为规则的指令

阶段 4:报告

生成上下文预算报告:

上下文预算报告
═══════════════════════════════════════

总估算开销:~XX,XXX Token
上下文模型:Claude Sonnet(200K窗口)
有效可用上下文:~XXX,XXX Token(XX%)

组件分解:
┌─────────────────┬────────┬───────────┐
│ 组件            │ 数量   │ Token数   │
├─────────────────┼────────┼───────────┤
│ 代理            │ N      │ ~X,XXX   │
│ 技能            │ N      │ ~X,XXX   │
│ 规则            │ N      │ ~X,XXX   │
│ MCP工具         │ N      │ ~XX,XXX  │
│ CLAUDE.md       │ N      │ ~X,XXX   │
└─────────────────┴────────┴───────────┘

警告:发现N个问题:
[按可节省Token排序]

前3个优化:
1. [操作] → 节省~X,XXX Token
2. [操作] → 节省~X,XXX Token
3. [操作] → 节省~X,XXX Token

潜在节省:~XX,XXX Token(当前开销的XX%)

在详细模式下,还会输出每个文件的Token计数、最重文件的逐行分解、重叠组件之间的具体冗余行、以及MCP工具列表(含每个工具的模式大小估算)。

示例

基本审计

用户:/context-budget
技能:扫描设置 → 16个代理(12,400 Token),28个技能(6,200),87个MCP工具(43,500),2个CLAUDE.md(1,200)
      标记:3个沉重代理,14个MCP服务器(3个可被CLI替代)
      最大节省:移除3个MCP服务器 → -27,500 Token(开销降低47%)

详细模式

用户:/context-budget --verbose
技能:完整报告 + 逐文件分解,显示planner.md(213行,1,840 Token),
      MCP工具列表(含每个工具大小),重复规则行并排显示

添加前检查

用户:我想再加5个MCP服务器,还有空间吗?
技能:当前开销33% → 添加5个服务器(~50个工具)将增加~25,000 Token → 推高到45%开销
      建议:先移除2个可被CLI替代的服务器,将开销控制在40%以下

最佳实践

  • Token估算:散文类使用单词数 × 1.3,代码密集型文件使用字符数 / 4
  • MCP是最大的杠杆:每个工具模式成本约500 Token;一个30工具的服务器比所有技能的总和还贵
  • 代理描述始终被加载:即使代理从未被调用,其描述字段也会出现在每次任务工具上下文中
  • 详细模式用于调试:当你需要精确定位导致开销的具体文件时使用,而非用于常规审计
  • 变更后审计:添加任何代理、技能或MCP服务器后运行,及早发现增长

Individual skills in this repo

This repo contains 20 individual skills — each has its own dedicated page.

accessibility

Design, implement, and audit inclusive digital products using WCAG 2.2 Level AA. Use when building or auditing UI that must meet WCAG 2.2 Level AA, or when reviewing a change for keyboard, contrast, or screen-reader support.

affaan-m/claude-api

Anthropic Claude API patterns for Python and TypeScript. Covers Messages API, streaming, tool use, vision, extended thinking, batches, prompt caching, and Claude Agent SDK. Use when building applications with the Claude API or Anthropic SDKs.

affaan-m/everything-claude-code

End-to-end marketing campaign planning and execution. Covers audience research, positioning, campaign angle definition, landing page copy, email sequences, social posts, ad copy, short-form video scripts, and content calendars. Use as the orchestration layer for multi-channel product launches. Use when planning or executing a multi-channel product launch, or producing landing page, email, social, or ad copy.

affaan-m/everything-claude-code

Development conventions and patterns for everything-claude-code. JavaScript project with conventional commits.

affaan-m/everything-claude-code-conventions

Development conventions and patterns for everything-claude-code. JavaScript project with conventional commits.

affaan-m/frontend-design

Create distinctive, production-grade frontend interfaces with high design quality. Use when the user asks to build web components, pages, or applications and the visual direction matters as much as the code quality.

affaan-m/gget

gget CLI and Python workflow for quick genomic database queries, sequence lookup, BLAST-style searches, enrichment checks, and reproducible bioinformatics evidence logs.

affaan-m/literature-review

Systematic literature-review workflow for academic, biomedical, technical, and scientific topics, including search planning, source screening, synthesis, citation checks, and evidence logging.

affaan-m/motion-ui

Production-ready UI motion system for React/Next.js. Use when implementing animations, transitions, or motion patterns.

affaan-m/project-guidelines-example

Example project-specific skill template based on a real production application.

affaan-m/pubmed-database

Direct PubMed and NCBI E-utilities search workflows for biomedical literature, MeSH queries, PMID lookup, citation retrieval, and API-backed literature monitoring.

affaan-m/scholar-evaluation

Structured scholarly-work evaluation for papers, proposals, literature reviews, methods sections, evidence quality, citation support, and research-writing feedback.

affaan-m/uspto-database

USPTO patent and trademark data workflow for official record lookup, PatentSearch queries, TSDR checks, assignment data, and reproducible IP research logs.

agent-architecture-audit

Full-stack diagnostic for agent and LLM applications. Audits the 12-layer agent stack for wrapper regression, memory pollution, tool discipline failures, hidden repair loops, and rendering corruption. Produces severity-ranked findings with code-first fixes. Essential for developers building agent applications, autonomous loops, or any LLM-powered feature. Use when an agent or LLM feature misbehaves and the failing layer is unknown, or before shipping an agent stack.

agent-eval

Head-to-head comparison of coding agents (Claude Code, Aider, Codex, etc.) on custom tasks with pass rate, cost, time, and consistency metrics. Use when choosing between coding agents, or when a change to an agent setup needs measured pass rate, cost, and time rather than an impression.

agent-harness-construction

Design and optimize AI agent action spaces, tool definitions, and observation formatting for higher completion rates. Use when defining or revising an agent

agentic-engineering

Operate as an agentic engineer using eval-first execution, decomposition, and cost-aware model routing. Use when planning or executing engineering work that agents will carry out end to end.

agentic-os

Build persistent multi-agent operating systems on Claude Code. Covers kernel architecture, specialist agents, slash commands, file-based memory, scheduled automation, and state management without external databases. Use when building a persistent multi-agent system on Claude Code with its own memory, commands, and scheduling.

agent-introspection-debugging

Structured self-debugging workflow for AI agent failures using capture, diagnosis, contained recovery, and introspection reports. Use when an agent run fails and you need a reproducible diagnosis instead of a retry.

agent-payment-x402

Add x402 payment execution to AI agents with per-task budgets, spending controls, and non-custodial wallets. Supports Base through agentwallet-sdk and X Layer through OKX Payments / OKX Agent Payments Protocol. Use when an agent must pay for something itself and needs per-task budgets, spending controls, and a non-custodial wallet.

相关技能