Communitygithub.com

コンテキスト予算

Claude Code のコンテキストウィンドウ消費量を、エージェント、スキル、MCPサーバー、ルールにわたって監査します。肥大化、重複コンポーネントを特定し、優先順位付きのトークン削減推奨を生成します。コンテキストウィンドウが急速に満たされ、どのコンポーネントが消費しているかを特定する必要がある場合に使用します。

コンテキスト予算 とは?

このスキルは、Claude Code セッション内でロードされたすべてのコンポーネント(エージェント、スキル、ルール、MCPサーバー、CLAUDE.md)を監査し、コンテキストウィンドウの消費量を可視化します。各コンポーネントのトークン使用量を推定し、肥大化や重複などの問題を検出し、実用的な最適化の推奨事項を優先順位付きで提供します。これにより、開発者はパフォーマンス低下の原因を特定し、コンテキストスペースを解放するための具体的なアクション(不要なMCPサーバーの削除や重複ルールの統合など)を実行できます。監査結果は、トークン節約量でランク付けされたレポートとして表示され、通常モードと詳細(verbose)モードの両方をサポートします。このスキルは、Claude Code エージェントの /context-budget コマンドで呼び出され、パフォーマンスのチューニングや新しいコンポーネント追加前の余地確認に役立ちます。

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

お気に入りのAIに質問する

このエージェントスキルを事前に読み込んだ状態で新しいチャットを開きます。

ドキュメント

コンテキスト予算 は何をしますか?

Claude Code セッション内のすべてのロード済みコンポーネントにおけるトークンオーバーヘッドを分析し、コンテキスト領域を再利用するための実用的な最適化を提示します。

使用するタイミング

  • セッションのパフォーマンスが低下したり、出力品質が低下している場合
  • 最近、多数のスキル、エージェント、または MCP サーバーを追加した場合
  • 実際にどの程度のコンテキストヘッドルームがあるかを把握したい場合
  • さらにコンポーネントを追加する予定があり、その余地があるかどうかを確認したい場合
  • /context-budget コマンドを実行した場合(このスキルがバックエンドとして機能します)

動作の仕組み

フェーズ 1: インベントリ

すべてのコンポーネントディレクトリをスキャンし、トークン消費量を推定します。

エージェント (agents/*.md)

  • ファイルごとの行数とトークン数をカウント(単語数 × 1.3)
  • フロントマターの description の長さを抽出
  • フラグ: ファイル >200行(重い)、description >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: 問題検出

以下の問題パターンを特定します:

  • 肥大化したエージェント説明文 — フロントマター内の description が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件):
[トークン節約量でランク付け]

上位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 ツールリスト、重複ルール行のサイドバイサイド表示

追加前の空き確認

ユーザー: MCP サーバーをあと5つ追加したいが、余裕はあるか?
スキル: 現在のオーバーヘッド 33% → 5サーバー(約50ツール)追加で約25,000トークン増加 → オーバーヘッドが 45% に
       推奨: まず CLI で代替可能なサーバーを2つ削除し、40% 未満を維持

ベストプラクティス

  • トークン見積もり: 散文には 単語数 × 1.3 を使用し、コードが多いファイルには 文字数 / 4 を使用
  • MCP が最大のレバレッジポイント: 各ツールスキーマは約500トークン。30ツールのサーバーは、すべてのスキルを合わせたよりもコストがかかる
  • エージェントの説明文は常にロードされる: エージェントが呼び出されなくても、その description フィールドはすべての 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/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.

関連スキル