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

@sohqureshi/tokenwise

v1.0.3

Published

Optimize JSON data for AI by reducing token usage

Readme

TokenWise

TokenWise is a lightweight utility for preparing JSON before sending it to AI models. It helps reduce payload noise, shrink token usage, and turn structured data into formats that are easier for LLMs to consume.

npm version downloads license stars Demo Support


🧠 What is tokenwise?

tokenwise is a lightweight utility that optimizes your data before sending it to AI models.

Raw JSON is:

  • ❌ verbose
  • ❌ token-heavy
  • ❌ expensive for LLMs

tokenwise helps you:

  • 📉 reduce token usage
  • ⚡ improve response speed
  • 💰 lower API costs

Installation

npm install @sohqureshi/tokenwise

Quick Usage

import ai, { compact, flatten, toNatural, toTOON } from "tokenwise";

const product = {
  product: {
    name: "Wireless Headphones",
    price: 79.99
  }
};

console.log(compact(product));
// {"product":{"name":"Wireless Headphones","price":79.99}}

console.log(flatten(product));
// {
//   "product.name": "Wireless Headphones",
//   "product.price": 79.99
// }

console.log(ai(product).compact().value());
// {"product":{"name":"Wireless Headphones","price":79.99}}

Core Functions

prune()

Removes fields you do not want to send to the model. By default it also removes null, undefined, and empty objects.

import { prune } from "tokenwise";

const input = {
  user: { name: "John", age: 28 },
  debug: true,
  internal: { apiKey: "secret" }
};

console.log(prune(input, ["debug", "internal"]));
// { user: { name: "John", age: 28 } }

compact()

Prunes empty/noisy values, then returns minified JSON.

compact({
  product: {
    name: "Wireless Headphones",
    price: 79.99
  }
});
// {"product":{"name":"Wireless Headphones","price":79.99}}

flatten()

Converts nested objects and arrays into one object with dot-notation keys.

flatten({
  policy: {
    claims: [
      { status: "approved", amount: 1200 },
      { status: "pending", amount: 500 }
    ]
  }
});
// {
//   "policy.claims.0.status": "approved",
//   "policy.claims.0.amount": 1200,
//   "policy.claims.1.status": "pending",
//   "policy.claims.1.amount": 500
// }

toNatural()

Converts JSON into readable sentences or numbered pointers.

toNatural([
  {
    user: {
      name: "Alice Johnson",
      email: "[email protected]",
      skills: ["Python", "JavaScript"]
    }
  }
]);
// 1. User Alice Johnson (email: [email protected], Having Python and JavaScript).

Medical and insurance-style data also becomes readable:

toNatural({
  policy: {
    holderName: "Carlos Rivera",
    policyNumber: "HLT-2048",
    claim: {
      status: "under review",
      requestedAmount: 64000
    }
  }
});
// policy: holder name Carlos Rivera, policy number HLT-2048, claim: status under review, requested amount 64000.

toTOON()

Converts JSON into a compact TOON-like text format. Arrays of objects become table-style rows.

toTOON({
  users: [
    { id: 1, name: "Ali" },
    { id: 2, name: "John" }
  ]
});
// users:
//   [2]{id,name}:
//     1,Ali
//     2,John

If later rows contain extra keys, the schema includes them:

toTOON({
  claims: [
    { id: "C-1", status: "approved" },
    { id: "C-2", amount: 500 }
  ]
});
// claims:
//   [2]{id,status,amount}:
//     C-1,approved,
//     C-2,,500

Tested Use Cases

TokenWise currently has coverage for:

  • Product JSON minification
  • Dot-notation flattening
  • Arrays and arrays of objects
  • Medical patient and appointment data
  • Insurance policy and claim data
  • Natural-language user pointers
  • TOON table formatting
  • Null, undefined, and empty values

Why It Helps

LLMs charge and reason over tokens. Sending raw JSON often includes repeated keys, unnecessary metadata, and formatting whitespace. TokenWise gives you multiple ways to reshape the same data depending on your prompt:

  • Use compact() when you need valid JSON with minimal whitespace.
  • Use flatten() when retrieval, search, or simple key-value context is better.
  • Use toNatural() when the model should read the data like human-friendly notes.
  • Use toTOON() when arrays of objects should be shorter than repeated JSON.

CLI

node demo.js

You can visualize token optimization results using planned CLI/Web visual tools.


🚀 Roadmap

  • [x] CLI support (Coming Soon)
  • [ ] NPM Support
  • [ ] Streaming support (GB+ data)
  • [ ] Schema-aware optimization
  • [ ] SaaS API

🛠 Comparison to Existing Tools (Future Section)

Highlight where tokenwise stands out, offering better compacting and token estimation features compared to other libraries.


🤝 Contributing

PRs are welcome,Feel free to open issues or submit PRs, Ideas are welcome for:

  • Better token compression strategies
  • Multi-model optimization
  • Streaming CLI support

note: Explore the CONTRIBUTING.md for more details for the contributons.


📄 License

MIT License


❤️ Support This Project

TokenWise is built to help developers reduce LLM cost and improve efficiency.

If it helps you, consider supporting its development:

Support


💡 Vision

Make AI cheaper and faster by optimizing data before it reaches the model.