Menu

1 / 2

I built a client-side LLM token counter because I kept guessing at prompt costs

DEV Community·Weston G·7 months ago
#sSasL6Ag
#llm#webdev#tokens#gemini#system
Long article (917 words) — no AI summary yet.
Reading 0:00
15s threshold

Estimated read time: 4 minutes

I was building a RAG pipeline last month. Standard stuff — system prompt, some retrieved chunks, user message. Somewhere around the third iteration of tweaking the system prompt I realized I had absolutely no idea what I was spending per request.

The system prompt alone was a wall of text. Around 500 words. That's fine in isolation, but this thing was getting prepended to every single request , and I had no intuition for what that translates to in dollars.

So I opened the OpenAI Tokenizer, pasted the text, got a number. Then I had to open a calculator to multiply tokens × price. Then I wanted to compare to Claude's pricing. Then Gemini. By request five I was annoyed enough to just build the thing.

The concrete example that made it real

Here's a system prompt I actually had sitting in my codebase:

You are a helpful assistant for a B2B SaaS product. You help users understand their billing, navigate features, and troubleshoot common issues. Always be concise.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.