agent-token-budget
v1.0.0
Published
Zero-dependency context window compressor, code signature folder, JSON minifier & token estimator for AI coding agents and MCP.
Maintainers
Readme
agent-token-budget
Zero-dependency context window compressor, code signature folder, JSON minifier & BPE token estimator for AI coding agents and MCP.
Why agent-token-budget?
When AI coding agents (Claude Code, Cursor, Windsurf, Codex, Gemini) explore codebases, inspect large API responses, or read logs, they frequently encounter two major bottlenecks:
- Context Window Exhaustion: 1,000-line implementation files and 500-item JSON objects quickly blow through context budgets and trigger truncation.
- Loss of Critical Schema: Naive string truncation cuts off closing braces, types, and essential function signatures.
agent-token-budget solves this with zero external dependencies:
- Code Signature Folding: Collapses function/method bodies while keeping 100% of imports, types, interfaces, classes, and method signatures intact (saving 60% to 80% of tokens).
- Schema-Preserving JSON Minifier: Keeps object keys, schemas, and representative array samples while summarizing the rest.
- Accurate BPE Token Estimator: Subword token counter modeled after OpenAI
cl100k/o200kand modern LLM tokenizers (<1KB, ~0.05ms execution). - Line-Aware Budget Pruner: Trims text cleanly to stay within strict token limits without cutting words mid-character.
1. CLI Usage
# 1. Estimate tokens of any file
npx agent-token-budget count src/services/user.ts
# 2. Fold code bodies (TypeScript / JavaScript / Python) to save 70% tokens
npx agent-token-budget fold src/services/user.ts
# 3. Save folded code to a target file
npx agent-token-budget fold src/services/user.ts -o user.summary.ts
# 4. Minify large JSON payloads for prompt context
npx agent-token-budget minify-json response.json -n 2
# 5. Prune text or logs to strict token limits
npx agent-token-budget prune server.log --budget 20002. MCP Server Configuration
Add context-budget-mcp to your MCP configuration (~/.claude/settings.json, Cursor MCP settings, or Antigravity config):
{
"mcpServers": {
"token-budget": {
"command": "npx",
"args": ["-y", "agent-token-budget"]
}
}
}Available MCP Tools:
estimate_tokens- Estimates token count for code or text in LLM contexts without external API calls.
fold_code_signatures- Collapses function and method bodies to save 60-80% tokens while preserving interfaces, classes, and signatures.
- Arguments:
code(string),language(ts|js|py).
minify_json- Compresses large JSON responses by trimming huge arrays and long strings while preserving structure.
- Arguments:
json(string | object),maxArrayItems(number),maxStringLength(number).
prune_to_token_budget- Strictly trims large text or logs to stay within a maximum token limit without cutting mid-character.
- Arguments:
text(string),maxTokens(number).
3. Programmatic Usage (Node.js / TypeScript)
import {
estimateTokens,
foldJsTsCode,
foldPythonCode,
minifyJson,
pruneToTokenBudget,
} from 'agent-token-budget';
// 1. Fast Token Estimation
const tokens = estimateTokens('export async function authenticate() { ... }');
console.log('Estimated Tokens:', tokens);
// 2. Fold Code Signatures
const { code: foldedCode, foldedBlocks } = foldJsTsCode(largeSourceCode);
console.log(`Folded ${foldedBlocks} function bodies!`);
// 3. Minify Large JSON
const minified = minifyJson(hugeJsonObject, {
maxArrayItems: 3,
maxStringLength: 100,
});
// 4. Stay Within Budget
const { text: safePrompt, truncated } = pruneToTokenBudget(hugeLogOutput, 1500);License
MIT © OIYO Lab
