Back to articles
"Why you should count your tokens before calling the API"

"Why you should count your tokens before calling the API"

via Dev.to TutorialJamie Cole

You're passing the last 10 turns of a conversation into Claude. Feels reasonable. Each message looks short. You hit send and suddenly you've blown through 4000 tokens on a single request you thought would cost maybe 800. That's happened to me more than once. And the second time it happened, I started counting first. What even is a token? Roughly speaking, one token is about 0.75 words. So 1000 words is around 1300 tokens. The rule of thumb I use: take your word count, multiply by 1.3, and that's a workable estimate. Good enough for back-of-envelope maths. The problem is that rule of thumb breaks down in a few places. Code tokenises differently. JSON with lots of brackets and quotes eats tokens faster than plain text. System prompts that feel short can be surprisingly expensive. When approximate is fine vs when it isn't For quick personal projects, multiply by 1.3 and call it done. But if you're building something that passes dynamic context into every request, or you're pricing a produ

Continue reading on Dev.to Tutorial

Opens in a new tab

Read Full Article
2 views

Related Articles