Dev.to AI πŸ€– Ai πŸ‘ 0 πŸ“– 2 min read

Estimating AI prompt cost locally in a Chrome extension, with no API key

Most people only find out what a prompt cost after the bill arrives. By then the prompt is long gone and there is no way to tell whether it was the 40 word version or the 400 word one. Token Wise is a Chromium extension

Most people only find out what a prompt cost after the bill arrives. By then the prompt is long gone and there is no way to tell whether it was the 40 word version or the 400 word one.

Token Wise is a Chromium extension that puts the estimate back at the moment of typing. This post is about the parts that were interesting to build, not a launch pitch.

Why local estimation

The first decision was whether to call a tokenizer endpoint. That would have been easy and wrong. It means an API key, a server, a privacy policy covering prompt text, and a round trip on every keystroke pause. Instead the extension approximates token counts in the browser and applies published per model rates, so the number appears while typing with nothing leaving the tab.

The approximation is not exact, and it does not need to be. If a prompt is heading for 900 tokens against 120, the user needs to know it is expensive, not that it is 913 rather than 906.

Where the count is shown

A small badge sits above the existing prompt box on the AI chat sites people already use. It is deliberately not a separate app: no new interface to learn, no prompts to copy across. Colour coding is coarse on purpose, low, medium or high, because a precise figure invites arguments about the last two percent and hides the point.

The rewrite

Showing a cost is half the job. The other half is making the cheaper version take one click. Optimize sends the prompt over HTTPS to a rewriting service and returns a shorter, clearer version, then shows the original and the rewrite side by side with the token difference before anything is sent. Four presets cover Balanced, Shorter, Clearer and More specific, and a keyboard shortcut does the same without leaving the keyboard.

Two details matter for trust:

  • The sent prompt is not stored or logged.
  • There is a local only mode that keeps everything on the device and never makes the call at all.

What the extension does not do

It cannot see your actual provider bill. It shows an estimate from published rates, not the amount you will be charged, because there is no billing access and no OAuth handshake with the model vendors. That is a real limitation and worth stating plainly rather than burying in a tooltip.

What I would do differently

Build the history panel earlier. Having the last hundred prompts with CSV export turned out to be more useful than expected, for spotting the handful of habitual prompts that eat most of the budget, and it is the kind of thing that is easy to defer and hard to retrofit.

If you are working on anything similar, the local estimate plus published rates combination is worth trying before reaching for an API. It is cheaper, it is faster, and it removes the entire class of conversations about where prompt text is stored.

Token Wise is free to try: chromewebstore.google.com/detail/token-wise/akmhikhlfebjbipfbpadfaibemlkpclc

πŸ“° Read the original article on Dev.to AI

Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.