<?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: iLostCount</title>
    <description>The latest articles on DEV Community by iLostCount (@ilostcount).</description>
    <link>https://dev.to/ilostcount</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%2F4110729%2F51ef02b9-09e5-44b0-8fe4-07b7100d1659.png</url>
      <title>DEV Community: iLostCount</title>
      <link>https://dev.to/ilostcount</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ilostcount"/>
    <language>en</language>
    <item>
      <title>How to count the tokens in an LLM prompt (and why the number matters)</title>
      <dc:creator>iLostCount</dc:creator>
      <pubDate>Sat, 05 Sep 2026 06:10:25 +0000</pubDate>
      <link>https://dev.to/ilostcount/how-to-count-the-tokens-in-an-llm-prompt-and-why-the-number-matters-4j1m</link>
      <guid>https://dev.to/ilostcount/how-to-count-the-tokens-in-an-llm-prompt-and-why-the-number-matters-4j1m</guid>
      <description>&lt;p&gt;If a prompt gets rejected as too long, or an API bill looks bigger than expected, the number that matters is tokens, not words. Here is the short version of how to check one.&lt;/p&gt;

&lt;h2&gt;
  
  
  What a token is
&lt;/h2&gt;

&lt;p&gt;Models do not read characters or words. They read tokens: chunks of text that a tokenizer splits your input into. For ordinary English prose, a useful rule of thumb is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;1 token is roughly 4 characters&lt;/li&gt;
&lt;li&gt;1 token is roughly 0.75 words&lt;/li&gt;
&lt;li&gt;1,000 words is roughly 1,300 tokens&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Those ratios shift. Code, JSON and long URLs tokenize worse than prose, because punctuation and odd substrings break into more pieces. Non-Latin scripts such as Arabic, Chinese and Japanese can cost several tokens per character in some tokenizers. Rules of thumb are fine for estimating and useless for a hard limit.&lt;/p&gt;

&lt;h2&gt;
  
  
  Counting in code
&lt;/h2&gt;

&lt;p&gt;For OpenAI models, &lt;code&gt;tiktoken&lt;/code&gt; gives an exact count:&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="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;tiktoken&lt;/span&gt;

&lt;span class="n"&gt;enc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;tiktoken&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get_encoding&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;o200k_base&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;enc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;encode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;your prompt here&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Anthropic and Google both expose token-counting endpoints in their APIs, so you can measure a prompt against the exact model you are calling instead of an approximation.&lt;/p&gt;

&lt;h2&gt;
  
  
  Counting without writing code
&lt;/h2&gt;

&lt;p&gt;Most of the time you are not writing a script. You pasted something into a chat window and you want to know whether it fits. That is what we built &lt;a href="https://ilostcount.com" rel="noopener noreferrer"&gt;iLostCount&lt;/a&gt; for: paste text, read the token, word and character counts as you type. No signup, and nothing is uploaded, since it runs in the page.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why the number matters
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Context window.&lt;/strong&gt; The window has to hold your system prompt, the conversation so far, any retrieved documents, and the answer. If the input fills the window, there is no room left for the output.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cost.&lt;/strong&gt; Input and output are both billed per token, so a prompt that re-sends a large document on every turn adds up quietly.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Truncation.&lt;/strong&gt; Some tools silently drop the oldest turns when you run over. That looks like the model forgetting, not like an error.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A cheap habit: count a big document before you paste it into a prompt. If a 40-page PDF turns into 30,000 tokens, you know to chunk or summarise it first.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Disclosure: this post is from the iLostCount project. The tool is free, and the source is public at &lt;a href="https://github.com/ahmad-almazeedi/token-counter" rel="noopener noreferrer"&gt;github.com/ahmad-almazeedi/token-counter&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

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