Contextbudget

io.github.CloudCorpRecords由 GitHub 帳號 CloudCorpRecords 發布v0.1.0更新於 Oct 26, 2026

Token budget auditor for agent context: per-file estimates, top offenders, exclusion advice.

已驗證STDIO僅桌面開發者工具資料與分析

概覽

AI 產生的概覽

本機 MCP 伺服器,用來估算目錄樹的 token 成本,協助助理判斷它是否能放進代理上下文視窗。

可以做什麼
  • 估算目錄樹的 token 成本,提供逐檔案與逐目錄的合計、最大的檔案,以及與預算(預設 20 萬估算 token)比較的結論。
  • 取得被略過的檔案與目錄計數,以及過大檔案和預設略過目錄的發現項目。
  • 從回傳的 JSON 報告讀取依副檔名統計的 token 合計與最大的 10 個檔案。
適用情境
適合在把某個儲存庫或資料夾交給代理之前,決定要排除哪些內容,或粗略檢查目錄樹是否符合上下文預算。它只是粗略排序工具,並非精確的分詞計數。
執行需求
以本機 stdio 程序執行,可透過 npx(需 Node 20 或更新版本)或 Docker 映像啟動。無需驗證或 API 金鑰。選用環境變數 CONTEXTBUDGET_BUDGET 可設為正整數以變更預算,否則採用 20 萬。僅支援桌面端。
安裝前請注意
僅做唯讀估算:它會走訪並讀取指定路徑下的文字檔案,請只指向你願意被讀取的目錄。數值是啟發式估算(字元數除以四),不是真實分詞計數;二進位檔案、超過 1 MiB 的檔案、符號連結以及預設略過清單中的目錄都不計入。檔案名稱與目錄名稱也不列入估算。

安裝

在 SourceWeft 中

  1. 開啟 儀表板中的 Contextbudget,將其新增到工作區。
  2. 為需要使用其工具的對話啟用該服務。

Desktop only,透過 STDIO。 STDIO 服務會啟動本機處理程序,因此需要 SourceWeft 桌面主機。

其他 MCP 客戶端

參照 儲存庫 中的啟動說明。

README

contextbudget

contextbudget estimates the token cost of a directory tree for an agent context window. It walks a tree, estimates the cost of every text file, compares the total against a budget, and reports the largest files and the subtrees that were skipped, so you can decide what to exclude before a tree eats the whole window.

The estimator, honestly

The token numbers in this tool are a heuristic estimate, not a tokenizer count. contextbudget decodes each counted file as UTF-8 text and applies one deterministic rule: tokens = ceil(chars / 4), where chars is the length of the decoded text. Real tokenizers split text differently depending on the model, so a real tokenizer will give a different number for the same file, sometimes higher and sometimes lower. Use these numbers to compare files with each other, to rank the biggest offenders, and to check a tree against a rough budget. Do not use them as an exact prediction of what any specific model will charge or accept.

Install

sh
npx @reneturcios/contextbudget budget [path]

Or install it as a dependency of your own tooling and use the MCP server form below. The package requires Node 20 or newer.

CLI usage

sh
contextbudget budget [path] [--json]contextbudget mcpcontextbudget --version
  • budget [path]: check the tree at path, which defaults to the current directory. Prints a human readable report, or the full JSON report with --json.
  • mcp: run the MCP server over stdio.
  • --version: print the version.

Exit codes:

  • 0: the verdict is WITHIN-BUDGET.
  • 1: any other verdict, currently OVER-BUDGET.
  • 2: usage error, or the target does not exist.
  • 141: the reader closed the output pipe early (the shell visible equivalent of SIGPIPE).

Budget

The default budget is 200000 estimated tokens. Set the environment variable CONTEXTBUDGET_BUDGET to a positive integer to use a different budget:

sh
CONTEXTBUDGET_BUDGET=50000 contextbudget budget ./src

If the variable is unset, unparsable, or not positive, the default of 200000 applies.

Verdict rules

  • The verdict is WITHIN-BUDGET when the total estimated tokens are less than or equal to the budget, and OVER-BUDGET otherwise.
  • The walk skips these directories by default and never descends into them: node_modules, .git, dist, coverage, .next, vendor.
  • Files over 1 MiB (1048576 bytes) are skipped and not counted.
  • Binary files, detected by a NUL byte in the first 4 KiB, are skipped and not counted.
  • Symbolic links are not followed.

Findings:

  • One high finding, rule large-file, for each counted file whose estimated tokens are over 10 percent of the budget.
  • One medium finding, rule skipped-large, for each file skipped for exceeding the 1 MiB limit.
  • One info finding, rule skipped-dir, for each directory skipped by the default skip list.

Ordering is deterministic: files are ranked by estimated tokens descending, then path ascending, and directory entries are visited in sorted name order. Reports carry no timestamps.

Report shape

The JSON report has tool, version, target, verdict, counts, findings, and summary. The headline numbers live in counts:

  • files: files counted.
  • totalTokens: total estimated tokens across counted files.
  • totalChars: total characters across counted files.
  • budgetTokens: the budget in effect.
  • remainingTokens: budget minus total, negative when over budget.
  • skippedDirs, skippedBinary, skippedLarge: skip counts.
  • findings, findingsHigh, findingsMedium, findingsInfo, findingsLow: finding counts.
  • ext:<extension>:files and ext:<extension>:tokens: per extension totals, for example ext:.ts:tokens.
  • top:<relative path>: estimated tokens for each of the top 10 largest files.

MCP

The MCP server exposes one tool:

  • budget_path: takes a path string and returns the same JSON report as contextbudget budget <path> --json.

Run it with contextbudget mcp, or through the registry entry in server.json.

Limits

  • Text files only: binary files and files over 1 MiB are skipped, so their real cost is invisible to the report except as skip counts and, for large files, medium findings.
  • The estimator is a heuristic, as described above. It is not a tokenizer and does not know any specific model's vocabulary.
  • The skip list is fixed. There is no flag or config file for custom skips or custom budgets per directory in this version; the only budget control is CONTEXTBUDGET_BUDGET.
  • Counts reflect file contents only. File names, directory names, and any framing an agent harness adds around file contents are not estimated.

Development

sh
npm installnpm run buildnpm test

來源:README.md,提交 2caeca2

工具

1

budget_path

Estimate the token cost of a directory tree for an agent context window. Returns per-file and per-directory estimates using a deterministic character-based estimator (stated as an estimate, never an exact tokenizer count), the largest files, totals against a default 200k budget, and which subtrees to exclude. Verdict WITHIN-BUDGET or OVER-BUDGET.

輸入參數path必填

版本歷史

1
  1. v0.1.0最新Oct 25, 2026

評分

已滿足適用檢查項的 90%

該評分衡量此收錄條目的完整程度,依據註冊中心條目、端點與公開來源計算得出。它不是對伺服器本身的評價,也不表示伺服器安全或目前可用:可連通性是帶日期的檢查結果,評分也不包含安裝量或使用者評價。

  • 近 30 天內可連通25 分
  • 已宣告認證方式10 分

    請在註冊中心條目中宣告憑證請求標頭,或保持端點能夠回應探測,以便判定其認證方式。

  • 已列出工具必需15 分
  • 描述必需15 分
  • 圖示10 分
  • 文件15 分
  • 授權條款10 分