<?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: sanpao xiao</title>
    <description>The latest articles on DEV Community by sanpao xiao (@sanpao_xiao_f37a6e9589166).</description>
    <link>https://dev.to/sanpao_xiao_f37a6e9589166</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%2F4257402%2F00a80c65-1a8b-4f82-ba3b-d4b6586b6a82.png</url>
      <title>DEV Community: sanpao xiao</title>
      <link>https://dev.to/sanpao_xiao_f37a6e9589166</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sanpao_xiao_f37a6e9589166"/>
    <language>en</language>
    <item>
      <title>6 Mistakes I Made Billing an Async AI API (And How I'd Wire It Now)</title>
      <dc:creator>sanpao xiao</dc:creator>
      <pubDate>Mon, 23 Nov 2026 14:00:00 +0000</pubDate>
      <link>https://dev.to/sanpao_xiao_f37a6e9589166/6-mistakes-i-made-billing-an-async-ai-api-and-how-id-wire-it-now-f9e</link>
      <guid>https://dev.to/sanpao_xiao_f37a6e9589166/6-mistakes-i-made-billing-an-async-ai-api-and-how-id-wire-it-now-f9e</guid>
      <description>&lt;p&gt;&lt;em&gt;Disclosure: the code in this post comes out of a product I build. There's one link to it at the very end and nothing else to sell you. The lessons are provider-agnostic — they apply to any paid async job API.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This article was drafted with AI. The pipeline illustration was generated with AI.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Foohyafyn8dv083ipg6l2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Foohyafyn8dv083ipg6l2.png" alt="Async AI job billing pipeline showing validate, price, charge, submit without retry, poll with retries, artifact verification and idempotent refund" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Sync APIs are easy to charge for. You call, you get a result, you decrement a balance. Done.&lt;/p&gt;

&lt;p&gt;Async generation APIs — video, long audio, big renders — are not easy to charge for, and I learned that the expensive way. A job takes 30 to 90 seconds. You submit, you get a task ID, you poll. In between those two moments there are roughly six places where you can either give work away for free or, much worse, bill a user for something they never received.&lt;/p&gt;

&lt;p&gt;Here's every one I got wrong, and what the code looks like now.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. I clamped invalid input instead of rejecting it
&lt;/h2&gt;

&lt;p&gt;My first version was "helpful." A user asked for a 40-second clip on a model whose ceiling is 30? Clamp it to 30 and carry on. Asked for a resolution the model doesn't support? Snap to the nearest one.&lt;/p&gt;

&lt;p&gt;This is a reasonable instinct for a UI control and a catastrophic one for a billable parameter. The user asked for 40 seconds, got charged for 30 seconds, received 30 seconds, and filed a support ticket that was completely correct.&lt;/p&gt;

&lt;p&gt;Now validation throws, and the error says what the limit actually is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nb"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isInteger&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;duration&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;duration&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;spec&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;minDuration&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;duration&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;spec&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;maxDuration&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="sb"&gt;`${spec.label} duration must be an integer from ${spec.minDuration} to ${spec.maxDuration} seconds`&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The rule I'd write on the wall: &lt;strong&gt;never silently clamp an input that appears in the price.&lt;/strong&gt; Round a UI slider all you like. Never round the invoice.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. I retried a POST that had already cost money
&lt;/h2&gt;

&lt;p&gt;This one nearly got me.&lt;/p&gt;

&lt;p&gt;My HTTP wrapper had a generic "retry twice on network failure" policy, because of course it did. Every HTTP wrapper starts that way.&lt;/p&gt;

&lt;p&gt;Think about what that means for a job-submission endpoint. The request goes out, the provider creates the job and starts billing, and the response times out on the way back. My client sees a failure and helpfully submits it again. Now there are two jobs, the user is charged twice, and one of those jobs will complete into a void.&lt;/p&gt;

&lt;p&gt;A timeout on a POST is not a failure. It's an &lt;strong&gt;unknown&lt;/strong&gt;. The two states are indistinguishable from the client side, and retrying resolves the ambiguity in the most expensive possible direction.&lt;/p&gt;

&lt;p&gt;So submissions get no automatic retries at all:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// No automatic POST retries: an ambiguous timeout may already have created a billable task.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;GETs retry freely — polling a task status is idempotent and safe. Writes that create billable work get exactly one attempt, and an ambiguous outcome surfaces to the user as "we're not sure, check your activity log" rather than being papered over.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. I trusted the task ID in the response
&lt;/h2&gt;

&lt;p&gt;When you poll &lt;code&gt;/tasks/{id}&lt;/code&gt;, you assume you get task &lt;code&gt;{id}&lt;/code&gt; back. Mostly you do.&lt;/p&gt;

&lt;p&gt;But "mostly" is doing load-bearing work in that sentence, and the failure mode is nasty: a caching layer, a proxy, or a provider bug hands you a &lt;em&gt;different&lt;/em&gt; task's payload, and you cheerfully attach someone else's video to your user's job.&lt;/p&gt;

&lt;p&gt;One line fixes it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="n"&gt;taskId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Provider returned a mismatched task ID'&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;I now check this on every single poll. It has fired exactly once in production, which is one more time than "never," which is why it's still there.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. I believed "success" without checking for a result
&lt;/h2&gt;

&lt;p&gt;Status fields lie. Not maliciously — they're just eventually consistent. A provider can flip a job to &lt;code&gt;succeeded&lt;/code&gt; a moment before the output URL is actually populated and readable.&lt;/p&gt;

&lt;p&gt;My code marked the job complete, saved a row with an empty URL, and showed the user a broken player while their balance went down.&lt;/p&gt;

&lt;p&gt;Now &lt;code&gt;success&lt;/code&gt; is a claim that has to be corroborated by an artifact:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;taskStatus&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="no"&gt;SUCCESS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;urls&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;urls&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;!/^&lt;/span&gt;&lt;span class="n"&gt;https&lt;/span&gt;&lt;span class="o"&gt;?:&lt;/span&gt;\&lt;span class="o"&gt;/&lt;/span&gt;\&lt;span class="c1"&gt;//i.test(url))) {&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Task completed without a valid video; query again later'&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;// ...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Throwing here is correct and slightly counterintuitive: the job isn't failed, it's &lt;em&gt;not done yet&lt;/em&gt;. Treating a premature success as a retryable condition rather than a terminal one is the whole trick. The next poll, a second or two later, almost always has the URL.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. I served the provider's URL directly
&lt;/h2&gt;

&lt;p&gt;The output URL a generation API hands you is usually a signed URL on their storage, and it usually expires. Sometimes in 24 hours. Sometimes in one.&lt;/p&gt;

&lt;p&gt;I shipped that URL straight to the client and into the database. Users came back a week later to a dead link for something they'd paid for.&lt;/p&gt;

&lt;p&gt;Now every completed job copies its output into my own object storage before the job is marked done — and critically, &lt;strong&gt;a failed copy is a failed job&lt;/strong&gt;, not a successful one with a bad link:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;uploaded&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;saveFiles&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;urls&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;i&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="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;contentType&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'video/mp4'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sb"&gt;`video/${taskId}-${i}.mp4`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'video'&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;storageConfigs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;uploaded&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;uploaded&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="n"&gt;urls&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;uploaded&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;url&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;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Video is ready but saving failed; query the task again'&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Same pattern as the last one: an incomplete pipeline is a retryable state, never a terminal success. The provider's copy is still there on the next poll.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. I scattered model capabilities across conditionals
&lt;/h2&gt;

&lt;p&gt;The least dramatic mistake and the one that caused the most bugs over time.&lt;/p&gt;

&lt;p&gt;Each model in a family has different ceilings — maximum duration, available resolutions, which aspect ratios are legal. I encoded that knowledge as &lt;code&gt;if (model.includes('mini'))&lt;/code&gt; checks sprinkled across a validator, a pricing function, and three UI components. Adding a model meant finding all of them. I never found all of them.&lt;/p&gt;

&lt;p&gt;Now capabilities are one table, and everything else reads from it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;models&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="s1"&gt;'x-2.5-text-to-video'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;version&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'2.5'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mini&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;scene&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'text-to-video'&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="s1"&gt;'x-2.0-mini-text-to-video'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;version&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'2.0'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mini&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="n"&gt;scene&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'text-to-video'&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="c1"&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;// derived, in one place:&lt;/span&gt;
&lt;span class="n"&gt;maxDuration&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;spec&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;version&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s1"&gt;'2.5'&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="n"&gt;resolutions&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;spec&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;mini&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'480p'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'720p'&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="s1"&gt;'480p'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'720p'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'1080p'&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;The validator, the price calculator and the frontend all import the same module. A new model is one row. The pricing function and the UI cannot disagree about what's legal, because there's only one copy of the answer.&lt;/p&gt;

&lt;p&gt;That last part matters more than it sounds: when the frontend and the billing code disagree about whether a request is valid, you get charged-but-rejected requests, and those are the tickets that cost you a customer.&lt;/p&gt;

&lt;h2&gt;
  
  
  The shape I'd start with next time
&lt;/h2&gt;

&lt;p&gt;If I were wiring this from scratch tomorrow:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;One capabilities table&lt;/strong&gt;, imported by the validator, the pricer, and the UI.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Validate, then price, then charge, then submit&lt;/strong&gt; — in that order, and fail closed at every step.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No retries on billable writes.&lt;/strong&gt; Retry polls as much as you like.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Verify identity on every poll&lt;/strong&gt; (task ID match) and &lt;strong&gt;require an artifact&lt;/strong&gt; before accepting success.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Copy outputs to your own storage&lt;/strong&gt; as part of completion, not as a background nicety.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Make refunds idempotent&lt;/strong&gt;, because you will call them twice. A failed job that refunds twice is a worse bug than one that never refunds at all.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;None of this is clever. All of it is the difference between an async billing pipeline you can sleep through and one that wakes you up.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Bottom Line
&lt;/h2&gt;

&lt;p&gt;Async billing bugs are quiet. Nobody files a ticket for a job that silently succeeded on your dime, and the user who got double-charged by a retry assumes it was their fault for double-clicking. You find them by reading your own refund log and asking why each line is there.&lt;/p&gt;

&lt;p&gt;Validate before you charge, never retry a write that costs money, and treat every "done" from a provider as a claim that needs evidence.&lt;/p&gt;

&lt;p&gt;I learned all six of these shipping &lt;a href="https://seeddance3.org/" rel="noopener noreferrer"&gt;an AI video generator&lt;/a&gt;, where a wrong answer costs real money per request. If you're wiring up something similar and hit a seventh one I haven't listed, I genuinely want to hear about it in the comments.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>api</category>
      <category>architecture</category>
    </item>
  </channel>
</rss>
