DEV Community

kirti kaushal
kirti kaushal

Posted on

I Wrote About Token Waste. Then I Built a Tool That Blocks It Before It Happens

By Kirti Kaushal

A few months ago I wrote about token optimization for GitHub Copilot and AI coding agents — the usual advice: narrow your scope, exclude generated files, don't send your whole repo for a one-line fix. Good advice. I still believe it.

I Wrote About Token Waste. Then I Built a Tool That Blocks It Before It Happens

But advice doesn't run automatically. You have to remember it, every time, on every prompt, forever. So I built the thing I actually wanted: something that checks for me, before anything gets sent, without me having to remember.

This is claude-cost-guard — a free Claude Code plugin that shows you the token count and cost, across Claude, GPT, and Gemini, before your prompt ever reaches the model. No LLM call to check it. No latency. Nothing sent anywhere unless you explicitly ask for a rewrite.

Why not just ask Claude to check my prompt?

Three reasons this needed to be a separate tool, not a habit of asking nicely:

  1. A free check has to actually be free. Asking Claude to critique your prompt costs a full round-trip — tokens spent just to find out you'd pasted a duplicate paragraph. A local check costs nothing and takes no time.
  2. No single model will tell you a competitor is cheaper. Cross-model cost comparison needs a neutral tool, not the model you're currently talking to.
  3. A habit you have to remember isn't a habit. A tool that fires on every single prompt, automatically, is the only version of this that actually works long-term.

How it works

claude-cost-guard hooks into Claude Code's UserPromptSubmit and UserPromptExpansion events — the same mechanism Claude Code itself uses to let plugins see a prompt before it's processed. On every prompt, slash-command, or skill invocation:

  • Token count and cost estimate — across six models, computed locally, instantly
  • Duplicate-paragraph detection — catches the classic "pasted the same section twice by accident"
  • Oversized-paste detection — flags a 2,000-line log dump when the actual question is one line
  • Missing-format detection — flags a "compare X and Y" request with no output format specified, which usually means a second round-trip to get the shape you actually wanted
  • Output-cost awareness — scans for explicit length signals ("brief," "comprehensive," "500 words") since output tokens are typically priced several times higher than input, and a flat guess undersells the real cost of a detailed request

It never blocks. This was a deliberate reversal partway through building it: an earlier version paused for confirmation on every single prompt, which meant every prompt cost a minimum of two submissions just to prove the point. A cost-saving tool that doubles your interactions to save you money is fighting itself. So the default is informational, always-visible, never in your way.

The one paid action — an optimized rewrite — only runs if you explicitly tag your prompt with #optimize. Never automatic, never silent spend.

What building it in public actually looked like

I want to be honest about this part, because it's the part most launch posts skip: most of the real bugs weren't found by writing more code — they were found by actually using it.

  • Unit tests passed on every function, in isolation, before this ever touched a real session. Good, but proved nothing about whether Claude Code would actually invoke the hook correctly.
  • The first live test found a false positive: claude plugin list, typed as an ordinary message, tripped the "missing output format" flag because it contains the word "list." Three words shouldn't read as a content request — fixed by requiring a minimum word count before that check fires at all.
  • A later session showed no output at all — the hook had silently failed. Debug logging traced it to a documented Claude Code issue where CLAUDE_PLUGIN_ROOT isn't always injected into the hook's subprocess environment. Fixed with a $PWD fallback, verified both with and without the variable set.
  • Stress-testing with a 1.6MB, 20,000-line paste (simulating someone dumping a full log file) ran in well under half a second — but surfaced a real UX bug: uncapped duplicate-paragraph flags would list every single repeat, unreadable past a handful. Capped at five, plus a summary count.
  • A direct question — "does output length also cost?" — led to reworking the cost estimate to actually respond to what the prompt asks for (brief vs. detailed vs. an explicit word count), instead of a flat guess applied identically to every prompt regardless of content.

None of these were hypothetical edge cases dreamed up in advance. Every one came from actually running the thing against something real.

Where this sits next to other tools

There's real, good work already happening in this space. claude-prompt-improver and similar plugins rewrite prompts for clarity using an LLM call on every fire — their own documentation is upfront that this costs 30–90 seconds of latency per prompt, which is exactly why they're opt-in by default. codeburn and Cost Guardian track running session cost after the fact, which is valuable for visibility but doesn't stop a wasteful prompt before it's sent. Token Guard audits the token cost of your repo's CLAUDE.md/.cursorrules files in CI — a real and useful adjacent problem, just a different one (repo hygiene, not per-prompt cost).

claude-cost-guard is specifically: free, instant, per-prompt, cross-model, before it runs. That's a narrow claim on purpose.

Try it

/plugin marketplace add kirti/claude-cost-guard
/plugin install claude-cost-guard
Enter fullscreen mode Exit fullscreen mode

Open source, MIT licensed: github.com/kirti/claude-cost-guard

If you find a real bug by actually using it — that's genuinely how every fix in this post got found. Open an issue.

Our Mission

Helping people learn, build, grow, and succeed through technology, education, and community.

“Knowledge becomes more valuable when it is shared.”

“Learn. Build. Share. Grow.”

❤️ Your support, likes, shares, and feedback motivate us to keep creating free educational content for everyone.

Thank you for being part of the journey! 🌱

Built with love by Kirti Kaushal — 15 years of engineering, finally building in public.

Kind is Cool 🙏 share your knowledge
Thank You for Reading ❤️
Thank you for spending your time with us and being part of our learning community.
🌟 Kind is Cool 📚 Share Your Knowledge
🚀 Help Others Grow

If you enjoyed this blog, tutorial, or Instagram post:

👍 Give it a Like
💬 Leave a Comment
🔄 Share it with someone who might find it helpful
⭐ Follow us for more content on AI, Coding, GitHub, Career Growth, and Continuous Learning

💻 GitHub: https://github.com/kirti

Top comments (0)