npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

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.

Readme

agent-token-budget

Zero-dependency context window compressor, code signature folder, JSON minifier & BPE token estimator for AI coding agents and MCP.

npm version License: MIT


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:

  1. Context Window Exhaustion: 1,000-line implementation files and 500-item JSON objects quickly blow through context budgets and trigger truncation.
  2. 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/o200k and 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 2000

2. 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:

  1. estimate_tokens
    • Estimates token count for code or text in LLM contexts without external API calls.
  2. 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).
  3. minify_json
    • Compresses large JSON responses by trimming huge arrays and long strings while preserving structure.
    • Arguments: json (string | object), maxArrayItems (number), maxStringLength (number).
  4. 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