Communitygithub.com

上下文预算

审计 Claude Code 会话中代理、技能、MCP 服务器和规则消耗的上下文令牌。识别臃肿、冗余的组件,并生成优先的令牌节省建议。当上下文窗口过快填满,需要找出占用资源的代理、技能、MCP 服务器或规则时使用。

上下文预算 是什么?

上下文预算技能用于分析和优化 Claude Code 会话中的令牌消耗。它会扫描所有加载的组件(包括代理、技能、规则、MCP 服务器和 CLAUDE.md 文件),估算每个组件占用的令牌数,并识别出臃肿或冗余的部分。该技能会生成一份详细的报告,按优先级列出可以节省令牌的优化建议,例如移除冗余的 MCP 服务器、精简过长的代理描述或规则文件。支持详细模式(`--verbose`)以显示每个文件的逐行分析。主要平台为 Claude Code。使用该技能可以显著释放上下文空间,提升会话性能和输出质量。

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

在你喜欢的 AI 中提问

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

文档

上下文预算 是做什么的?

分析 Claude Code 会话中每个已加载组件的令牌开销,并提供可操作的优化建议以回收上下文空间。

何时使用

  • 会话性能感觉缓慢或输出质量下降
  • 你最近添加了许多技能、代理或 MCP 服务器
  • 你想知道实际还有多少上下文空间
  • 计划添加更多组件,需要确认是否有足够空间
  • 运行 /context-budget 命令(此技能支持该命令)

工作原理

阶段 1:盘点

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

代理(agents/*.md)

  • 统计每个文件的行数和令牌数(单词数 × 1.3)
  • 提取 description frontmatter 的长度
  • 标记:文件 >200 行(重)、描述 >30 词(frontmatter 臃肿)

技能(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:检测问题

识别以下问题模式:

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

阶段 4:报告

生成上下文预算报告:

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

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

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

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

Top 3 优化建议:
1. [操作] → 节省 ~X,XXX 令牌
2. [操作] → 节省 ~X,XXX 令牌
3. [操作] → 节省 ~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 工具列表及每个工具大小、并排显示的重复规则行

扩展前检查

用户:我想添加 5 个更多 MCP 服务器,还有空间吗?
技能:当前开销 33% → 添加 5 个服务器(约 50 个工具)将增加约 25,000 令牌 → 推高到 45% 开销
       建议:先移除 2 个可被 CLI 替代的服务器,以保持开销低于 40%

最佳实践

  • 令牌估算:对于散文使用 单词数 × 1.3,对于代码密集型文件使用 字符数 / 4
  • MCP 是最大的杠杆:每个工具模式约成本 500 令牌;一个 30 工具的服务器比所有技能加起来还贵
  • 代理描述总是加载的:即使代理从未被调用,其描述字段也存在于每次 Task 工具上下文中
  • 详细模式用于调试:当你需要精确定位导致开销的具体文件时使用,不适合常规审计
  • 变更后审计:在添加任何代理、技能或 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/content-engine

Create platform-native content systems for X, LinkedIn, TikTok, YouTube, newsletters, and repurposed multi-platform campaigns. Use when the user wants social posts, threads, scripts, content calendars, or one source asset adapted cleanly across platforms.

affaan-m/ECC

Multi-platform content distribution across X, LinkedIn, Threads, and Bluesky. Adapts content per platform using content-engine patterns. Never posts identical content cross-platform. Use when the user wants to distribute content across social platforms.

affaan-m/fal-ai-media

Unified media generation via fal.ai MCP — image, video, and audio. Covers text-to-image (Nano Banana), text/image-to-video (Seedance, Kling, Veo 3), text-to-speech (CSM-1B), and video-to-audio (ThinkSound). Use when the user wants to generate images, videos, or audio with AI.

affaan-m/manim-video

日本語翻訳:このファイルは manim-video 用の日本語翻訳が必要です

affaan-m/remotion-video-creation

Remotion のベストプラクティス - React で動画を作成する。3D、アニメーション、音声、字幕、チャート、トランジションなどをカバーするドメイン固有の29のルール。

affaan-m/video-editing

AI-assisted video editing workflows for cutting, structuring, and augmenting real footage. Covers the full pipeline from raw capture through FFmpeg, Remotion, ElevenLabs, fal.ai, and final polish in Descript or CapCut. Use when the user wants to edit video, cut footage, create vlogs, or build video content.

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.

agent-self-evaluation

Use after completing any non-trivial task. The agent self-rates its output on 5 axes — accuracy, completeness, clarity, actionability, conciseness — with concrete evidence per criterion. Produces a structured 1-5 scorecard with specific improvement suggestions.

agent-sort

Build an evidence-backed ECC install plan for a specific repo by sorting skills, commands, rules, hooks, and extras into DAILY vs LIBRARY buckets using parallel repo-aware review passes. Use when ECC should be trimmed to what a project actually needs instead of loading the full bundle.

ai-first-engineering

Engineering operating model for teams where AI agents generate a large share of implementation output. Use when setting team process, review gates, or ownership rules for a codebase largely written by agents.

ai-regression-testing

Regression testing strategies for AI-assisted development. Sandbox-mode API testing without database dependencies, automated bug-check workflows, and patterns to catch AI blind spots where the same model writes and reviews code. Use when adding regression coverage to AI-assisted code, or when the same model both wrote and reviewed a change.

android-clean-architecture

Clean Architecture patterns for Android and Kotlin Multiplatform projects — module structure, dependency rules, UseCases, Repositories, and data layer patterns. Use when structuring modules, layers, or data flow in an Android or KMP project.

angular-developer

Generates Angular code and provides architectural guidance. Trigger when creating projects, components, or services, or for best practices on reactivity (signals, linkedSignal, resource), forms, dependency injection, routing, SSR, accessibility (ARIA), animations, styling (component styles, Tailwind CSS), testing, or CLI tooling.

相关技能