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 分