<?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: Luyu Fang</title>
    <description>The latest articles on DEV Community by Luyu Fang (@rffanlab).</description>
    <link>https://dev.to/rffanlab</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%2F4034686%2Fc9c41d15-62e3-4f90-89b7-9493530d8000.png</url>
      <title>DEV Community: Luyu Fang</title>
      <link>https://dev.to/rffanlab</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rffanlab"/>
    <language>en</language>
    <item>
      <title>Your FastAPI Webhook Returned 200. Why Did the Order Run Twice?</title>
      <dc:creator>Luyu Fang</dc:creator>
      <pubDate>Thu, 23 Jul 2026 06:30:40 +0000</pubDate>
      <link>https://dev.to/rffanlab/your-fastapi-webhook-returned-200-why-did-the-order-run-twice-49h6</link>
      <guid>https://dev.to/rffanlab/your-fastapi-webhook-returned-200-why-did-the-order-run-twice-49h6</guid>
      <description>&lt;p&gt;Your webhook endpoint returned &lt;code&gt;200 OK&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The provider still delivered the event again.&lt;/p&gt;

&lt;p&gt;That is not necessarily a provider bug. Most webhook systems promise &lt;strong&gt;at-least-once delivery&lt;/strong&gt;, not exactly-once delivery. A timeout, a lost response, a process restart, or a manual replay can send the same logical event more than once.&lt;/p&gt;

&lt;p&gt;The dangerous implementation looks harmless:&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="nd"&gt;@app.post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;/webhooks/provider&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;receive&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="nf"&gt;create_order&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;ok&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the same event arrives twice, &lt;code&gt;create_order()&lt;/code&gt; runs twice. Replace "order" with invoice, email, provisioning job, CRM record, or AI-agent action and the failure becomes expensive quickly.&lt;/p&gt;

&lt;p&gt;Here is the small pattern I use to make the HTTP boundary safer.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Verify the signature against the raw body
&lt;/h2&gt;

&lt;p&gt;Do not parse and re-serialize the payload before signature verification. Whitespace, key order, and encoding can change the byte sequence.&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;hashlib&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;hmac&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;valid_signature&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;supplied&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;secret&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;bytes&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;bool&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;expected&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;hmac&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;secret&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;hashlib&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sha256&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;hexdigest&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;hmac&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;compare_digest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;expected&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;supplied&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Use &lt;code&gt;hmac.compare_digest()&lt;/code&gt; rather than a normal string comparison.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Treat the provider event ID as a database key
&lt;/h2&gt;

&lt;p&gt;An in-memory set is not enough. It disappears on restart and does not coordinate multiple workers.&lt;/p&gt;

&lt;p&gt;Create a durable inbox table with a unique event ID:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;webhook_inbox&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;event_id&lt;/span&gt; &lt;span class="nb"&gt;TEXT&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;event_type&lt;/span&gt; &lt;span class="nb"&gt;TEXT&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;payload&lt;/span&gt; &lt;span class="nb"&gt;TEXT&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="k"&gt;state&lt;/span&gt; &lt;span class="nb"&gt;TEXT&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="s1"&gt;'accepted'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;attempts&lt;/span&gt; &lt;span class="nb"&gt;INTEGER&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;last_error&lt;/span&gt; &lt;span class="nb"&gt;TEXT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;accepted_at&lt;/span&gt; &lt;span class="nb"&gt;TIMESTAMP&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="k"&gt;CURRENT_TIMESTAMP&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The uniqueness constraint is the important part. Two concurrent requests can both pass a separate "does this ID exist?" query. Let the database perform the reservation atomically.&lt;/p&gt;

&lt;p&gt;For a minimal SQLite example:&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="n"&gt;cursor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;
    INSERT OR IGNORE INTO webhook_inbox(event_id, event_type, payload)
    VALUES (?, ?, ?)
    &lt;/span&gt;&lt;span class="sh"&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;event_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;event_type&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;decode&lt;/span&gt;&lt;span class="p"&gt;()),&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;commit&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="n"&gt;duplicate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;rowcount&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In PostgreSQL, use &lt;code&gt;INSERT ... ON CONFLICT DO NOTHING&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Acknowledge durable acceptance, not completed business work
&lt;/h2&gt;

&lt;p&gt;Keep the HTTP path short:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;read the raw body;&lt;/li&gt;
&lt;li&gt;verify the signature;&lt;/li&gt;
&lt;li&gt;validate the minimum envelope;&lt;/li&gt;
&lt;li&gt;persist the inbox record;&lt;/li&gt;
&lt;li&gt;return &lt;code&gt;202 Accepted&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Do not call three downstream APIs before responding. Slow processing increases the chance that the provider times out and retries.&lt;/p&gt;

&lt;p&gt;The endpoint can look like this:&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;from&lt;/span&gt; &lt;span class="n"&gt;fastapi&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;FastAPI&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Header&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;HTTPException&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt;

&lt;span class="n"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;FastAPI&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="nd"&gt;@app.post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;/webhooks/provider&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;status_code&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;HTTP_202_ACCEPTED&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;receive_webhook&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;x_signature&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Header&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;default&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&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;body&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;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;body&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="nf"&gt;valid_signature&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;x_signature&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;secret&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;HTTPException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;status_code&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;401&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;detail&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Invalid signature&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;event&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;loads&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;cursor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;INSERT OR IGNORE INTO webhook_inbox(event_id,event_type,payload) VALUES(?,?,?)&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;id&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]),&lt;/span&gt; &lt;span class="nf"&gt;str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;type&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]),&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;decode&lt;/span&gt;&lt;span class="p"&gt;()),&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;commit&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;accepted&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;duplicate&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;rowcount&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&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 worker processes accepted rows separately. A crash after the database commit does not lose the event.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Test the duplicate directly
&lt;/h2&gt;

&lt;p&gt;Do not wait for production retries to discover whether idempotency works.&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;test_duplicate_delivery_has_one_durable_acceptance&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;event&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;id&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;evt_duplicate_1&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;type&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;order.paid&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;first&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;signed_post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;second&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;signed_post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="n"&gt;first&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status_code&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;202&lt;/span&gt;
    &lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="n"&gt;first&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;()[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;duplicate&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="bp"&gt;False&lt;/span&gt;
    &lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="n"&gt;second&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status_code&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;202&lt;/span&gt;
    &lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="n"&gt;second&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;()[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;duplicate&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Also test:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;an invalid signature;&lt;/li&gt;
&lt;li&gt;two concurrent deliveries of the same ID;&lt;/li&gt;
&lt;li&gt;a worker crash after inbox persistence;&lt;/li&gt;
&lt;li&gt;a downstream timeout;&lt;/li&gt;
&lt;li&gt;events delivered in reverse order;&lt;/li&gt;
&lt;li&gt;a stale event trying to overwrite newer state.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  5. Duplicate delivery is not the only problem
&lt;/h2&gt;

&lt;p&gt;Events can arrive out of order. A delayed &lt;code&gt;subscription.updated&lt;/code&gt; event may arrive after &lt;code&gt;subscription.cancelled&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Store the provider timestamp or sequence number and define allowed state transitions. An older event should become a no-op rather than silently restoring stale state.&lt;/p&gt;

&lt;p&gt;Retries also need a terminal state. Record attempt count, last error, and the next retry time. After the configured limit, move the event to a dead-letter state that can be inspected and replayed intentionally.&lt;/p&gt;

&lt;h2&gt;
  
  
  The practical rule
&lt;/h2&gt;

&lt;p&gt;Your webhook should create &lt;strong&gt;one durable inbox record&lt;/strong&gt;, not perform one irreversible business action inside the HTTP request.&lt;/p&gt;

&lt;p&gt;That single design choice makes duplicates, crashes, retries, and debugging much easier to reason about.&lt;/p&gt;

&lt;p&gt;I published the complete runnable FastAPI receiver, tests, production checklist, and reusable review prompt here:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://codez.win/guides/fastapi-webhook-idempotency?utm_source=devto&amp;amp;utm_medium=article&amp;amp;utm_campaign=webhook_20260723" rel="noopener noreferrer"&gt;https://codez.win/guides/fastapi-webhook-idempotency?utm_source=devto&amp;amp;utm_medium=article&amp;amp;utm_campaign=webhook_20260723&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What is the most expensive duplicate webhook failure you have seen: double billing, duplicate emails, or repeated provisioning?&lt;/p&gt;

</description>
      <category>fastapi</category>
      <category>webdev</category>
      <category>python</category>
      <category>programming</category>
    </item>
    <item>
      <title>A Verifiable AI Agent Workflow: 7 Checks Before You Add Autonomy</title>
      <dc:creator>Luyu Fang</dc:creator>
      <pubDate>Mon, 20 Jul 2026 06:22:53 +0000</pubDate>
      <link>https://dev.to/rffanlab/a-verifiable-ai-agent-workflow-7-checks-before-you-add-autonomy-3jho</link>
      <guid>https://dev.to/rffanlab/a-verifiable-ai-agent-workflow-7-checks-before-you-add-autonomy-3jho</guid>
      <description>&lt;p&gt;Most agent projects fail before the model does.&lt;/p&gt;

&lt;p&gt;The failure usually starts with an unclear outcome, an overpowered tool, or a demo that has no repeatable pass/fail test. The agent looks impressive once, then becomes impossible to trust in production.&lt;/p&gt;

&lt;p&gt;Here is the checklist I now use before adding more autonomy.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Define one verifiable outcome
&lt;/h2&gt;

&lt;p&gt;Avoid goals such as "help the user with support."&lt;/p&gt;

&lt;p&gt;Use an outcome a human can inspect:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Given a support ticket and the account history, produce a draft reply with cited evidence and route it to a human reviewer.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A good outcome has a clear input, output, owner, and acceptance test.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Separate three kinds of work
&lt;/h2&gt;

&lt;p&gt;Every step belongs in one of these buckets:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Deterministic work&lt;/strong&gt;: API calls, schema validation, database reads, file transforms.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Model judgment&lt;/strong&gt;: classification, summarization, ranking, drafting.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Human approval&lt;/strong&gt;: sending messages, spending money, changing permissions, or deleting data.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This separation makes failures easier to locate. It also prevents a model from being asked to do work that ordinary code can do more reliably.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Give every tool a typed contract
&lt;/h2&gt;

&lt;p&gt;An agent tool should look like an API, not a vague capability.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"get_order"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"input"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"order_id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"string"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"output"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"status"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"enum"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"items"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"array"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"refund_eligible"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"boolean"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"errors"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="s2"&gt;"ORDER_NOT_FOUND"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="s2"&gt;"UPSTREAM_TIMEOUT"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Define timeouts, retry rules, idempotency, and the exact errors the model may receive. Do not return an unbounded blob and hope the model interprets it correctly.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Make approval gates explicit
&lt;/h2&gt;

&lt;p&gt;A human approval gate is not a weakness. It is a product feature when the consequence is expensive or irreversible.&lt;/p&gt;

&lt;p&gt;Useful approval gates include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;sending an external email&lt;/li&gt;
&lt;li&gt;issuing a refund&lt;/li&gt;
&lt;li&gt;publishing content&lt;/li&gt;
&lt;li&gt;changing account access&lt;/li&gt;
&lt;li&gt;running a destructive command&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The agent can prepare the action and explain its evidence. The human approves the final side effect.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Design failure cases before the happy path
&lt;/h2&gt;

&lt;p&gt;Write at least five realistic failures before implementation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;required data is missing&lt;/li&gt;
&lt;li&gt;two sources contradict each other&lt;/li&gt;
&lt;li&gt;the tool times out&lt;/li&gt;
&lt;li&gt;the model returns invalid structured output&lt;/li&gt;
&lt;li&gt;the requested action exceeds the user's permission&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For every failure, define whether the system retries, asks a question, escalates, or stops.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Build pass/fail evaluations
&lt;/h2&gt;

&lt;p&gt;"Looks good" is not an evaluation.&lt;/p&gt;

&lt;p&gt;Create a small suite with cases such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;known-good input produces the expected action&lt;/li&gt;
&lt;li&gt;missing evidence prevents the action&lt;/li&gt;
&lt;li&gt;an injected instruction inside retrieved content is ignored&lt;/li&gt;
&lt;li&gt;a repeated request does not create a duplicate side effect&lt;/li&gt;
&lt;li&gt;a timeout produces a recoverable state&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Start with 10 carefully chosen cases. Run them after every prompt, tool, or model change.&lt;/p&gt;

&lt;h2&gt;
  
  
  7. Ship the smallest observable workflow
&lt;/h2&gt;

&lt;p&gt;The first milestone should fit into one day:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;one input type&lt;/li&gt;
&lt;li&gt;one model decision&lt;/li&gt;
&lt;li&gt;one read-only tool&lt;/li&gt;
&lt;li&gt;one structured output&lt;/li&gt;
&lt;li&gt;one approval screen&lt;/li&gt;
&lt;li&gt;one event log&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Do not begin with a swarm of agents. Add another agent only when it removes a measured bottleneck.&lt;/p&gt;

&lt;h2&gt;
  
  
  A reusable planning prompt
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Turn the AI agent idea below into a verifiable MVP.

Return:
1. one measurable user outcome
2. inputs and outputs
3. deterministic steps, model-judgment steps, and human approval gates
4. a typed contract for every tool
5. five realistic failure cases
6. pass/fail evaluation cases
7. the smallest milestone testable in one day

Do not add autonomy unless it is required for the outcome.

Agent idea:
[Describe the user, task, data, tools, and constraints]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The goal is not maximum autonomy. The goal is a workflow that can earn trust one observable step at a time.&lt;/p&gt;

&lt;p&gt;I keep the copyable prompt and related workflow examples in this &lt;a href="https://codez.win/guides/ai-agent-workflows?utm_source=devto&amp;amp;utm_medium=article&amp;amp;utm_campaign=verifiable-agent-workflows" rel="noopener noreferrer"&gt;AI agent workflow guide&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>programming</category>
      <category>agents</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Your AI Agent MVP Does Not Need More Autonomy</title>
      <dc:creator>Luyu Fang</dc:creator>
      <pubDate>Sat, 18 Jul 2026 04:22:24 +0000</pubDate>
      <link>https://dev.to/rffanlab/your-ai-agent-mvp-does-not-need-more-autonomy-26oe</link>
      <guid>https://dev.to/rffanlab/your-ai-agent-mvp-does-not-need-more-autonomy-26oe</guid>
      <description>&lt;p&gt;Most AI agent MVPs start with the wrong question:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;How much can we make autonomous?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A better first question is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;What is the smallest useful outcome a human can verify?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The difference matters. An autonomous demo can look impressive while hiding unreliable decisions, unclear permissions, and failure states that nobody has tested. A narrow, reviewable workflow is less dramatic, but it can become a real product.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Draw the boundary before choosing tools
&lt;/h2&gt;

&lt;p&gt;Split the workflow into three kinds of work:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Deterministic steps&lt;/strong&gt;: validation, parsing, database reads, calculations, and format conversion.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Model judgment&lt;/strong&gt;: classification, summarization, ranking, and drafting where uncertainty is expected.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Human approval&lt;/strong&gt;: sending messages, changing production data, spending money, or publishing externally.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This boundary tells you where an LLM is useful and where ordinary code is safer. It also prevents the agent from quietly gaining permissions just because a demo needs to look seamless.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Give every tool a typed contract
&lt;/h2&gt;

&lt;p&gt;An agent tool should not be described as “search the system” or “update the record.” Define its inputs, outputs, timeouts, permission checks, and failure responses.&lt;/p&gt;

&lt;p&gt;For example, a lead-research tool can return:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the public source URL;&lt;/li&gt;
&lt;li&gt;extracted facts;&lt;/li&gt;
&lt;li&gt;confidence for each fact;&lt;/li&gt;
&lt;li&gt;missing fields;&lt;/li&gt;
&lt;li&gt;a structured error when the page cannot be read.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The model can then reason over evidence instead of inventing a successful result. Typed contracts also make tool calls testable without invoking the full agent.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Test failures before adding autonomy
&lt;/h2&gt;

&lt;p&gt;Five evaluation cases are usually more valuable than five more tools:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;a normal request with complete data;&lt;/li&gt;
&lt;li&gt;missing or contradictory input;&lt;/li&gt;
&lt;li&gt;a tool timeout;&lt;/li&gt;
&lt;li&gt;a low-confidence model response;&lt;/li&gt;
&lt;li&gt;a request that needs permission the agent does not have.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Each case needs an observable pass/fail rule. “The answer looks good” is not a rule. “The agent cites the source, marks the missing field, and does not call the write tool” is.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Keep the first external action reviewable
&lt;/h2&gt;

&lt;p&gt;For an early release, prefer read-only tools. Let the agent prepare a draft, proposed database change, or command plan, then require a human to approve the final external action.&lt;/p&gt;

&lt;p&gt;This is not a permanent limitation. It is how you collect evidence about where the system is reliable enough to automate next.&lt;/p&gt;

&lt;h2&gt;
  
  
  A practical MVP sequence
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Choose one narrow outcome.&lt;/li&gt;
&lt;li&gt;Write the expected input and output.&lt;/li&gt;
&lt;li&gt;Separate deterministic code from model judgment.&lt;/li&gt;
&lt;li&gt;Define typed tools and permission boundaries.&lt;/li&gt;
&lt;li&gt;Add five realistic evaluation cases.&lt;/li&gt;
&lt;li&gt;Keep the final high-impact action behind approval.&lt;/li&gt;
&lt;li&gt;Record failures and only automate the stable parts.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The goal of an agent MVP is not to imitate a fully autonomous employee. It is to prove that one workflow can produce repeatable value without hiding uncertainty.&lt;/p&gt;

&lt;p&gt;I turned this sequence into a reusable, editor-verified workflow on Codez Win:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://codez.win/guides/ai-agent-workflows?utm_source=devto&amp;amp;utm_medium=article&amp;amp;utm_campaign=guide_launch_20260718" rel="noopener noreferrer"&gt;https://codez.win/guides/ai-agent-workflows?utm_source=devto&amp;amp;utm_medium=article&amp;amp;utm_campaign=guide_launch_20260718&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What is the smallest agent workflow you have seen deliver repeatable value in production?&lt;/p&gt;

</description>
      <category>ai</category>
      <category>agents</category>
      <category>productivity</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
