<?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: Abhishek solanki</title>
    <description>The latest articles on DEV Community by Abhishek solanki (@abhisolankii).</description>
    <link>https://dev.to/abhisolankii</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.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3840726%2Ffc95cbbc-4119-4a2a-a693-e0bc5d95c9ab.png</url>
      <title>DEV Community: Abhishek solanki</title>
      <link>https://dev.to/abhisolankii</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/abhisolankii"/>
    <language>en</language>
    <item>
      <title>🧠 Streaming LLM APIs Can Quietly Give Free Tokens</title>
      <dc:creator>Abhishek solanki</dc:creator>
      <pubDate>Tue, 24 Mar 2026 18:28:07 +0000</pubDate>
      <link>https://dev.to/abhisolankii/streaming-llm-apis-can-quietly-give-free-tokens-4lcl</link>
      <guid>https://dev.to/abhisolankii/streaming-llm-apis-can-quietly-give-free-tokens-4lcl</guid>
      <description>&lt;h2&gt;
  
  
  📌 The Problem
&lt;/h2&gt;

&lt;p&gt;Most OpenAI-compatible APIs send token usage only in the &lt;strong&gt;final chunk&lt;/strong&gt; of a stream.&lt;/p&gt;

&lt;p&gt;So if a user:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;refreshes&lt;/li&gt;
&lt;li&gt;closes the tab&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;👉 the stream stops&lt;br&gt;
👉 usage data never arrives&lt;/p&gt;

&lt;p&gt;But the user has already seen part of the response.&lt;/p&gt;




&lt;h2&gt;
  
  
  ⚠️ Why This Matters
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;❌ tokens not recorded&lt;/li&gt;
&lt;li&gt;❌ users get partial responses for free&lt;/li&gt;
&lt;li&gt;❌ can be abused&lt;/li&gt;
&lt;li&gt;❌ billing becomes inaccurate&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🚀 The Fix
&lt;/h2&gt;

&lt;p&gt;I added a fallback inside the streaming loop (&lt;code&gt;finally&lt;/code&gt; block).&lt;/p&gt;

&lt;p&gt;👉 If no usage data is received:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;calculate tokens manually using &lt;code&gt;tiktoken&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;count prompt + generated output
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;tokens_used&lt;/span&gt;&lt;span class="p"&gt;:&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;cl100k_base&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;prompt_tokens&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;sum&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="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;content&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]))&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;messages&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;output_tokens&lt;/span&gt; &lt;span class="o"&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="n"&gt;full_content&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="n"&gt;tokens_used&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;prompt_tokens&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;output_tokens&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  ✔️ Result
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;✔️ tokens tracked even if stream is interrupted&lt;/li&gt;
&lt;li&gt;✔️ no free token exploits&lt;/li&gt;
&lt;li&gt;✔️ accurate usage tracking&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  📌 Takeaway
&lt;/h2&gt;

&lt;p&gt;Streaming APIs don’t guarantee usage data.&lt;/p&gt;

&lt;p&gt;If you’re building anything with token limits or billing, don’t depend only on the final chunk.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>rag</category>
      <category>tokenusage</category>
      <category>multisourcerag</category>
    </item>
  </channel>
</rss>
