<?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: Byteforge</title>
    <description>The latest articles on DEV Community by Byteforge (@byteforgeboilerplates).</description>
    <link>https://dev.to/byteforgeboilerplates</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%2F4209701%2F2d67126d-c9bb-47fc-8d55-5f5fc76410bb.jpg</url>
      <title>DEV Community: Byteforge</title>
      <link>https://dev.to/byteforgeboilerplates</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/byteforgeboilerplates"/>
    <language>en</language>
    <item>
      <title>Pricing metered API data to the cent without losing a single fraction</title>
      <dc:creator>Byteforge</dc:creator>
      <pubDate>Tue, 27 Oct 2026 01:56:07 +0000</pubDate>
      <link>https://dev.to/byteforgeboilerplates/pricing-metered-api-data-to-the-cent-without-losing-a-single-fraction-37e8</link>
      <guid>https://dev.to/byteforgeboilerplates/pricing-metered-api-data-to-the-cent-without-losing-a-single-fraction-37e8</guid>
      <description>&lt;p&gt;Written for: developers building usage-based billing who want to see how one backend handles it.&lt;/p&gt;

&lt;p&gt;Hey, fellow developer. If you've ever built "pay for what you use" billing, you've probably hit the same wall I did: you charge per gigabyte, every call is a few kilobytes, and suddenly your rounding is eating money. This post walks through how the backend in this &lt;a href="https://byteforge.whop.site/" rel="noopener noreferrer"&gt;boilerplate&lt;/a&gt; prices metered API data so every cent is accounted for, with a short look at the code.&lt;/p&gt;

&lt;p&gt;I'll keep the code excerpts small. A stripped-down, runnable version of the pricing module with unit tests is in the &lt;a href="https://github.com/Byteforge-boilerplates/metered-pricing-sample" rel="noopener noreferrer"&gt;metered-pricing-sample&lt;/a&gt; repo, so you can try the math yourself. The excerpts below are from the production module.&lt;/p&gt;

&lt;h2&gt;
  
  
  The billable metric
&lt;/h2&gt;

&lt;p&gt;Before any math, you need to decide what you're counting. Here the metric is request body bytes plus response body bytes, and only for live-key calls that were answered with a status below 500. Test-key calls are free. Errors from our side don't cost the customer anything.&lt;/p&gt;

&lt;p&gt;The included allowance comes first. Each plan has a number of bytes that's covered by the subscription. Only the bytes beyond that allowance are priced by the overage tiers.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why rounding is the hard part
&lt;/h2&gt;

&lt;p&gt;Say overage costs 600 cents per GiB. A single API call might be 4 KB. Its cost is:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;4 KB × 600 cents / 1 GiB ≈ 0.0023 cents&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If you round each call on its own, you'll round every call to zero, and you'll never bill anything. If you round up each call, you'll overcharge by a huge margin. Neither approach is correct.&lt;/p&gt;

&lt;p&gt;The trick is to stop pricing each call in isolation. Price the cycle's total, then charge each call the difference. Look at this excerpt from &lt;code&gt;pricing.py&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;def&lt;/span&gt; &lt;span class="nf"&gt;cost_cents&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;billable_bytes&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;terms&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;PricingTerms&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;``f``: the cycle&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;s overage cost in cents at ``billable_bytes``, rounded half up once. A
    discount scales the exact numerator, so it is applied before the single rounding.&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
    &lt;span class="n"&gt;kept&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;terms&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;discount_percent&lt;/span&gt;
    &lt;span class="nf"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;overage_numerator&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;billable_bytes&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;terms&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;kept&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;GB&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;GB&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;debit_cents&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;settled_bytes&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;call_bytes&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;terms&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;PricingTerms&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;Cents to debit for a call of ``call_bytes`` settled at cumulative ``settled_bytes`` (&amp;gt;= 0).&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;cost_cents&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;settled_bytes&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;call_bytes&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;terms&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nf"&gt;cost_cents&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;settled_bytes&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;terms&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each call is debited &lt;code&gt;f(S + call) - f(S)&lt;/code&gt;, where &lt;code&gt;S&lt;/code&gt; is the cycle's running total. When you add up all the debits in a cycle, they telescope to &lt;code&gt;f(S_end)&lt;/code&gt;, so the total is exact, and the fractions of a cent carry forward instead of disappearing. The cycle's byte count is what moves; the cents follow from it.&lt;/p&gt;

&lt;h2&gt;
  
  
  A worked example
&lt;/h2&gt;

&lt;p&gt;Take a Standard-style setup: 50 GiB included, overage at 600 cents per GiB, no discount. Suppose a cycle has used exactly 51 GiB. The overage is 1 GiB:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The numerator is &lt;code&gt;1 GiB × 600 = 600·GiB&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Multiply by 100 (the &lt;code&gt;kept&lt;/code&gt; factor with no discount): &lt;code&gt;60000·GiB&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Add &lt;code&gt;50·GiB&lt;/code&gt; for the half-up rounding, then divide by &lt;code&gt;100·GiB&lt;/code&gt;: &lt;code&gt;60050 / 100 = 600&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So the cycle costs 600 cents, which is $6.00. That number is exact, because there's no fractional cent left over.&lt;/p&gt;

&lt;h2&gt;
  
  
  Tiers
&lt;/h2&gt;

&lt;p&gt;Tiers let you price the first chunk of overage at one rate and later chunks at a lower one. The loop in &lt;code&gt;overage_numerator&lt;/code&gt; walks through them:&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;for&lt;/span&gt; &lt;span class="n"&gt;tier&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;terms&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tiers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;overage&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;lower&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;
    &lt;span class="n"&gt;upper&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;overage&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;tier&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;up_to_bytes&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="nf"&gt;min&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;overage&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;tier&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;up_to_bytes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;total&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;upper&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;lower&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;tier&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cents_per_gb&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;tier&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;up_to_bytes&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;
    &lt;span class="n"&gt;lower&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;tier&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;up_to_bytes&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each tier only charges for the bytes that fall inside it. The last tier has no upper bound. The module also has a &lt;code&gt;validate_tiers&lt;/code&gt; function that rejects tier lists which aren't ordered correctly, so a bad tier list can be caught before it's used to bill anyone.&lt;/p&gt;

&lt;h2&gt;
  
  
  Keep the numbers in one place
&lt;/h2&gt;

&lt;p&gt;The useful habit here is that the cent-level math lives in one pure module. It has no database calls, no settings lookups, and no I/O, so you can test it with plain integers. Use integers for money too, never floats.&lt;/p&gt;

&lt;p&gt;If you want the whole flow (holds, prepaid balances, free trials, and arrears), the boilerplate includes it. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Get the full boilerplate: &lt;a href="https://byteforge.whop.site/" rel="noopener noreferrer"&gt;Byteforge&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>backend</category>
      <category>api</category>
      <category>tutorial</category>
      <category>saas</category>
    </item>
  </channel>
</rss>
