<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Fraza365</title>
    <description>The latest articles on DEV Community by Fraza365 (@fraza365).</description>
    <link>https://dev.to/fraza365</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4250460%2Feb61f352-2154-4bb8-b92c-9068c5a32d9d.png</url>
      <title>DEV Community: Fraza365</title>
      <link>https://dev.to/fraza365</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/fraza365"/>
    <language>en</language>
    <item>
      <title>Your Claude token count is probably wrong — here's how to get the exact one</title>
      <dc:creator>Fraza365</dc:creator>
      <pubDate>Tue, 17 Nov 2026 19:19:17 +0000</pubDate>
      <link>https://dev.to/fraza365/your-claude-token-count-is-probably-wrong-heres-how-to-get-the-exact-one-39e3</link>
      <guid>https://dev.to/fraza365/your-claude-token-count-is-probably-wrong-heres-how-to-get-the-exact-one-39e3</guid>
      <description>&lt;p&gt;If you estimate Claude costs with a "token counter" website, there's a good chance the number is wrong — sometimes by a lot.&lt;/p&gt;

&lt;p&gt;Most online counters use &lt;strong&gt;OpenAI's tokenizer&lt;/strong&gt; (tiktoken / o200k_base) for every model, Claude included. That's understandable: Anthropic doesn't publish Claude's tokenizer. But Claude splits text differently, so those counts are estimates — and they're consistently low.&lt;/p&gt;

&lt;h2&gt;
  
  
  How different is it?
&lt;/h2&gt;

&lt;p&gt;Anthropic has an official endpoint, &lt;code&gt;POST /v1/messages/count_tokens&lt;/code&gt;, that returns the &lt;strong&gt;exact&lt;/strong&gt; number of input tokens for a given model. I ran the same texts through it and through GPT-5's tokenizer:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Text&lt;/th&gt;
&lt;th&gt;GPT-5&lt;/th&gt;
&lt;th&gt;Claude (Opus / Sonnet / Haiku 5.5)&lt;/th&gt;
&lt;th&gt;Difference&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;Hello world, how are you today?&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;8&lt;/td&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;+25%&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;A small JavaScript function&lt;/td&gt;
&lt;td&gt;23&lt;/td&gt;
&lt;td&gt;29&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;+26%&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Mixed French / Chinese / Arabic / Russian&lt;/td&gt;
&lt;td&gt;26&lt;/td&gt;
&lt;td&gt;33&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;+27%&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;A one-line SQL query&lt;/td&gt;
&lt;td&gt;50&lt;/td&gt;
&lt;td&gt;92&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;+84%&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Emoji and math symbols&lt;/td&gt;
&lt;td&gt;31&lt;/td&gt;
&lt;td&gt;29&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;−6%&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;em&gt;(Text-only counts; small samples, so treat them as indicative, not universal.)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Two takeaways:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;"Claude ≈ GPT + 10%" is not a safe rule of thumb.&lt;/strong&gt; Prose and code came out ~25% higher, and the SQL query nearly doubled.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;It depends on the content&lt;/strong&gt;, so the only reliable number is the one from Claude's own tokenizer.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Getting the exact count yourself
&lt;/h2&gt;

&lt;p&gt;The endpoint is free to call (it's rate-limited, and your account needs some credit for the API to respond at all).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;TypeScript&lt;/strong&gt; (&lt;code&gt;@anthropic-ai/sdk&lt;/code&gt;):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;Anthropic&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'@anthropic-ai/sdk'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Anthropic&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// reads ANTHROPIC_API_KEY&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;input_tokens&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;messages&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;countTokens&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'claude-opus-5-5'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;messages&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;role&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'user'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'Hello world, how are you today?'&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nb"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input_tokens&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Python&lt;/strong&gt; (&lt;code&gt;anthropic&lt;/code&gt;):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;anthropic&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;Anthropic&lt;/span&gt;

&lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Anthropic&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# reads ANTHROPIC_API_KEY&lt;/span&gt;
&lt;span class="n"&gt;resp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;messages&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;count_tokens&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"claude-opus-5-5"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;messages&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"role"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"user"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"content"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Hello world, how are you today?"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;resp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;input_tokens&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Gotcha 1: the message wrapper
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;count_tokens&lt;/code&gt; counts the whole &lt;em&gt;message&lt;/em&gt;, which includes a few tokens of framing around your text. If you want the tokens of the text alone (to compare with other tokenizers), measure the framing once and subtract it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;countText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;messages&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;countTokens&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;messages&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;role&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'user'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;input_tokens&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;overhead&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;count&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'a'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// "a" is a single token&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;count&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;overhead&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(Cache the overhead per model — it doesn't change.)&lt;/p&gt;

&lt;h3&gt;
  
  
  Gotcha 2: you don't need to count per model
&lt;/h3&gt;

&lt;p&gt;I expected different counts for Opus, Sonnet, Haiku and Fable. I tested English, mixed-language text, emoji, SQL and a 5,000-token source file: &lt;strong&gt;all current Claude models returned identical counts every time&lt;/strong&gt; — they share a tokenizer. One call covers all of them. (Older generations can differ, so re-check when a new model ships.)&lt;/p&gt;

&lt;h3&gt;
  
  
  Gotcha 3: don't put your API key in the browser
&lt;/h3&gt;

&lt;p&gt;If you build a counter for others, the key has to stay server-side. I used a tiny Cloudflare Worker that receives the text, calls &lt;code&gt;count_tokens&lt;/code&gt;, and returns only the numbers. Because it's a public endpoint, it also needs basic protection:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;an &lt;strong&gt;origin check&lt;/strong&gt;, so only your own pages can call it&lt;/li&gt;
&lt;li&gt;a &lt;strong&gt;per-IP rate limit&lt;/strong&gt; (Cloudflare's rate-limit binding works well)&lt;/li&gt;
&lt;li&gt;a &lt;strong&gt;cache keyed by a SHA-256 hash of the text&lt;/strong&gt; — identical text never hits the API twice, and you never store the text itself&lt;/li&gt;
&lt;li&gt;a &lt;strong&gt;back-off after a 429&lt;/strong&gt;, so a flood can't exhaust your account's rate limit&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What about GPT and Gemini?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;GPT-5.x&lt;/strong&gt;: OpenAI's &lt;code&gt;o200k_base&lt;/code&gt; tokenizer is public, so you can count exactly — even in the browser (the &lt;code&gt;gpt-tokenizer&lt;/code&gt; npm package).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;GPT-6 and Gemini&lt;/strong&gt;: their tokenizers aren't public (Gemini has a &lt;code&gt;countTokens&lt;/code&gt; API method), so any browser-side number is an estimate — and a good tool should say so.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  A free counter that does all this
&lt;/h2&gt;

&lt;p&gt;I wrapped the above into a free tool — paste text, see exact Claude and GPT-5 counts plus estimated cost across 17 models, with prices checked against the providers' pricing pages:&lt;/p&gt;

&lt;p&gt;👉 &lt;strong&gt;&lt;a href="https://boring-web.com/claude-token-counter" rel="noopener noreferrer"&gt;boring-web.com/claude-token-counter&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;No signup, and the text isn't stored. If you spot a wrong price or a missing model, let me know in the comments.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Have you seen big gaps between Claude and GPT token counts on your own data? I'd love to know which content types are the worst — SQL surprised me.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>ai</category>
      <category>claude</category>
      <category>llm</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
