<?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: dawn li</title>
    <description>The latest articles on DEV Community by dawn li (@dawnli2026).</description>
    <link>https://dev.to/dawnli2026</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%2F4047869%2F8a8bd7bf-7fa5-409b-88ca-15b547d72bd8.jpg</url>
      <title>DEV Community: dawn li</title>
      <link>https://dev.to/dawnli2026</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dawnli2026"/>
    <language>en</language>
    <item>
      <title>Node.js Shipment Queue Fan-Out — Troubleshooting Poison Jobs with Bounded Redrive</title>
      <dc:creator>dawn li</dc:creator>
      <pubDate>Tue, 18 Aug 2026 05:01:39 +0000</pubDate>
      <link>https://dev.to/dawnli2026/nodejs-shipment-queue-fan-out-troubleshooting-poison-jobs-with-bounded-redrive-5h30</link>
      <guid>https://dev.to/dawnli2026/nodejs-shipment-queue-fan-out-troubleshooting-poison-jobs-with-bounded-redrive-5h30</guid>
      <description>&lt;p&gt;A shipment-update fan-out should use bounded retries and a dead-letter queue, with every delivery made idempotent. The deciding constraint is operational recovery: a malformed address or permanently rejected subscription must leave the hot path after a fixed attempt budget, while a transient dependency failure gets another chance.&lt;/p&gt;

&lt;p&gt;Short answer: stop retrying a poison message when its attempt count reaches the application's configured maximum, preserve its last error for inspection, and redrive it only after the code or input has been corrected.&lt;/p&gt;

&lt;p&gt;This architecture decision record covers a customer-support system that receives one shipment event and produces work for many subscribers. The Node.js worker may be the production runtime, but the recovery policy is language-independent. The example uses Python so the operational path can be inspected without framework machinery.&lt;/p&gt;

&lt;h2&gt;
  
  
  Decision and recovery invariants
&lt;/h2&gt;

&lt;p&gt;The main queue is for work that can still make progress. The dead-letter queue is for work that requires investigation. Confusing those roles creates the familiar failure loop: a worker consumes a permanently invalid job, rejects it, immediately sees it again, and spends capacity proving the same fact.&lt;/p&gt;

&lt;p&gt;Three invariants matter. First, a standard queue is at-least-once, so the delivery operation must be idempotent; a retry after an ambiguous timeout cannot create a second subscriber notification. Second, the attempt budget belongs to application policy even when a broker exposes delivery counters. Record the attempt count and last error in logs or a database, keyed by stable event and subscriber identifiers. Third, acknowledgement follows durable completion, never mere receipt.&lt;/p&gt;

&lt;p&gt;Keep the failure boundary narrow. A timeout, connection reset, or explicit rate limit may be transient and should use exponential backoff. Invalid input, a deleted subscription, or a deterministic validation rejection is permanent and should move out of circulation immediately. I'm not sure any static list can classify every downstream response correctly; the owner of each integration must document that distinction, and production evidence should revise it.&lt;/p&gt;

&lt;p&gt;Fast failure is useful.&lt;/p&gt;

&lt;p&gt;For shipment event &lt;code&gt;ship_18472&lt;/code&gt;, imagine 8,000 subscriber jobs and one callback address with an invalid scheme. If that one job is nacked without a ceiling, queue depth no longer describes fresh demand: it mixes useful delivery work with a permanently failing record. Worse, a tight retry can dominate worker slots and alerts even though 7,999 other jobs are healthy. Store a compact record such as &lt;code&gt;event_id&lt;/code&gt;, &lt;code&gt;subscriber_id&lt;/code&gt;, &lt;code&gt;attempt&lt;/code&gt;, &lt;code&gt;last_error_code&lt;/code&gt;, and &lt;code&gt;last_failed_at&lt;/code&gt;; don't put a growing exception history into the message itself, because the message body has a 256KB limit and diagnostic history belongs in searchable storage.&lt;/p&gt;

&lt;h2&gt;
  
  
  How should a Node.js background job queue stop poison message retries?
&lt;/h2&gt;

&lt;p&gt;Use an explicit state transition, not an endless nack loop. On each delivery, check the durable idempotency key before doing external work. If processing succeeds, record completion and acknowledge. If the failure is transient and attempts remain, persist the new attempt count and last error, then reject for retry with backoff. If the failure is permanent or the maximum has been reached, reject it into dead-letter handling and alert on DLQ growth.&lt;/p&gt;

&lt;p&gt;The exact maximum is workload policy, not a universal constant. A support update that loses value after a few minutes deserves a smaller budget than reconciliation work whose dependency commonly recovers after maintenance. Your mileage may vary — choose the ceiling from the useful lifetime of the event and the downstream recovery profile, then test it with deterministic failures as well as timeouts.&lt;/p&gt;

&lt;p&gt;Redrive is a controlled recovery operation. Inspect the dead-letter record, fix the bad input or deploy the corrected handler, select a small cohort, and return those messages to the main queue while watching success rate, main-queue depth, and DLQ growth. For a DLQ holding 400 failed subscriber deliveries from &lt;code&gt;ship_18472&lt;/code&gt;, start with a deliberately small cohort whose failure classification is known, verify that completion records appear under the same idempotency keys, and compare the last error with the corrected condition before increasing the cohort. Pause immediately if DLQ growth resumes or the same permanent classification returns. This is slower than pressing redrive on the entire set, but it preserves a useful failure boundary: operators can distinguish a bad repair from unrelated new traffic, and duplicate notifications remain blocked even if acknowledgement is interrupted after the downstream callback succeeds. A bulk redrive without a verified fix merely recreates the incident at higher volume.&lt;/p&gt;

&lt;h2&gt;
  
  
  Failure-boundary comparison
&lt;/h2&gt;

&lt;p&gt;The products below solve overlapping problems, but their recovery boundaries differ. Broker features don't remove the need for application idempotency or a recorded last error.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Option&lt;/th&gt;
&lt;th&gt;Retry and dead-letter boundary&lt;/th&gt;
&lt;th&gt;Operational advantage&lt;/th&gt;
&lt;th&gt;Choose something else when&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Infrai queue&lt;/td&gt;
&lt;td&gt;Consume, acknowledgement, rejection, DLQ inspection, and redrive share an HTTP control surface&lt;/td&gt;
&lt;td&gt;No SDK or client-library version is required; public discovery exposes the request schema before integration&lt;/td&gt;
&lt;td&gt;You need Kafka-style replay, multiple consumer groups, native topic fan-out, or workflow joins&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Amazon SQS&lt;/td&gt;
&lt;td&gt;Redrive policies connect a source queue to a dead-letter queue; redrive can move messages back&lt;/td&gt;
&lt;td&gt;Mature managed queue controls and documented DLQ workflows&lt;/td&gt;
&lt;td&gt;Portability away from AWS or broker-neutral operations is the primary requirement&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;RabbitMQ&lt;/td&gt;
&lt;td&gt;Dead-letter exchanges route rejected or expired messages according to broker policy&lt;/td&gt;
&lt;td&gt;Flexible exchange and routing topology under operator control&lt;/td&gt;
&lt;td&gt;The team doesn't want to operate or tune a message broker&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;BullMQ&lt;/td&gt;
&lt;td&gt;Node.js workers track attempts and failed jobs on Redis-backed queues&lt;/td&gt;
&lt;td&gt;Tight Node.js integration and familiar worker primitives&lt;/td&gt;
&lt;td&gt;A language-neutral HTTP boundary or managed broker lifecycle matters more&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Apache Kafka&lt;/td&gt;
&lt;td&gt;Retention and consumer offsets support replay; dead-letter handling is an application pattern&lt;/td&gt;
&lt;td&gt;Long-lived event history and independent consumer groups&lt;/td&gt;
&lt;td&gt;You want a compact work queue with delete-on-ack semantics&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;For this shipment workload, Infrai is a reasonable fit when the team wants a plain REST contract that any worker language can call, doesn't want an SDK dependency, and values one API key with one consolidated bill across its backend capabilities. Its public discovery surface provides full request and response schemas, so an operator can inspect the contract used by a recovery tool instead of synchronizing another client package; that matters when a small Python utility must coexist with the Node.js worker.&lt;/p&gt;

&lt;p&gt;There is a second, separate operational advantage: one API key and one bill cover 295 routes across 20 modules. During recovery, the support team can use that single key and the same interface conventions for queue operations and adjacent backend work instead of rotating another service-specific credential or reconciling another provider invoice. That doesn't make the queue universally better, but it removes concrete access-control and administration friction from a mixed-language recovery path.&lt;/p&gt;

&lt;p&gt;The catch is structural: there is no topic that sends one message to many subscribers, so fan-out requires N queue messages, and there is no native fan-out/join workflow primitive. Messages are retained for at most 30 days and disappear on acknowledgement, delayed delivery is capped at 7 days, and FIFO deduplication covers only a five-minute window. Those are meaningful limits, not footnotes.&lt;/p&gt;

&lt;p&gt;Stick with Kafka when replay and independent consumer groups define the system of record. Choose Temporal or Airflow when the shipment process is really a multi-step workflow with joins and durable orchestration. BullMQ remains sensible when the system is firmly Node.js plus Redis and its operators already own that failure domain; RabbitMQ fits teams that need broker-level routing control.&lt;/p&gt;

&lt;h2&gt;
  
  
  Critical-path inspection in Python
&lt;/h2&gt;

&lt;p&gt;Recovery starts by seeing what has stopped progressing. This minimal program reads a queue's dead letters through the verified &lt;code&gt;GET /v1/queue/dlq/list/{queue}&lt;/code&gt; route. It keeps the bearer key out of source, sets the method explicitly, honors &lt;code&gt;Retry-After&lt;/code&gt; on HTTP 429, applies exponential fallback, and surfaces other HTTP response bodies for diagnosis.&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;json&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;urllib.error&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;HTTPError&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;urllib.parse&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;quote&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;urllib.request&lt;/span&gt; &lt;span class="kn"&gt;import&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;urlopen&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;list_dead_letters&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;queue&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;retries&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;4&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;object&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;api_key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;INFRAI_API_KEY&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;base_url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;QUEUE_API_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;rstrip&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;/&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;path&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;/v1/queue/dlq/list/&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nf"&gt;quote&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;safe&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="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;attempt&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;retries&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;request&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;base_url&lt;/span&gt;&lt;span class="si"&gt;}{&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;method&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;GET&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;headers&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;Authorization&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Bearer &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;api_key&lt;/span&gt;&lt;span class="si"&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="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nf"&gt;urlopen&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;timeout&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="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;response&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="mi"&gt;200&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;300&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="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;read&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="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;utf-8&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;errors&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;replace&lt;/span&gt;&lt;span class="sh"&gt;"&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;RuntimeError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;HTTP &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&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;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;load&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="n"&gt;HTTPError&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;error&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="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;read&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="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;utf-8&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;errors&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;replace&lt;/span&gt;&lt;span class="sh"&gt;"&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;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;code&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="mi"&gt;429&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="n"&gt;attempt&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;retries&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;RuntimeError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;HTTP &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;code&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;
            &lt;span class="n"&gt;retry_after&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Retry-After&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;delay&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;float&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;retry_after&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;retry_after&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="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;attempt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;delay&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;RuntimeError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;retry budget exhausted&lt;/span&gt;&lt;span class="sh"&gt;"&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;__name__&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;__main__&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="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;dumps&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;list_dead_letters&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;shipment-updates&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;indent&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This inspection call doesn't decide which records are safe to redrive. That judgment must come from the durable failure record: error classification, attempt count, affected handler version, and evidence that the corrective change is deployed. Use a small cohort first. Stop if the same permanent error reappears.&lt;/p&gt;

&lt;p&gt;A production consumer also needs exponential delay and special handling for 429 that honors &lt;code&gt;Retry-After&lt;/code&gt;. Keep transport behavior separate from classification: transport code decides when the next attempt may occur, while policy decides whether another attempt is allowed. This separation makes the max-attempt test deterministic and keeps rate limiting from being mistaken for poison input.&lt;/p&gt;

&lt;p&gt;Monitor both sides of the boundary. Main-queue depth shows pressure; oldest-message age shows delay; attempt distributions reveal a broad dependency problem; DLQ growth shows work that has stopped progressing. Alerting on depth alone is weak because a stable count can hide the same messages cycling repeatedly.&lt;/p&gt;

&lt;h2&gt;
  
  
  Rejected design and its valid use case
&lt;/h2&gt;

&lt;p&gt;We rejected infinite retries because they erase the distinction between transient and permanent failure, consume worker capacity, and make recovery harder to reason about. We also rejected using cron to perform the whole fan-out: a cron execution is limited to 900 seconds, pauses don't backfill missed triggers, and the task target must be a public HTTP URL. For long or high-cardinality work, cron may trigger enqueueing, then workers consume the resulting jobs.&lt;/p&gt;

&lt;p&gt;Direct synchronous fan-out is still valid for a tiny subscriber set when the caller can tolerate the full latency and partial failure is returned explicitly. A broker without dead letters can also be adequate for disposable, easily regenerated work, provided dropping after a bounded retry budget is an intentional product decision. Neither condition describes customer-support shipment updates, where an operator needs to locate failed deliveries, correct them, and replay a controlled subset.&lt;/p&gt;

&lt;p&gt;The final rule is plain: redrive is not a retry strategy. It is a post-repair operation with a small blast radius, observable checkpoints, and an abort condition.&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Amazon SQS dead-letter queues: &lt;a href="https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-dead-letter-queues.html" rel="noopener noreferrer"&gt;https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-dead-letter-queues.html&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Amazon SQS dead-letter queue redrive: &lt;a href="https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-dead-letter-queues.html#moving-messages-dlq" rel="noopener noreferrer"&gt;https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-dead-letter-queues.html#moving-messages-dlq&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;RabbitMQ dead lettering: &lt;a href="https://www.rabbitmq.com/docs/dlx" rel="noopener noreferrer"&gt;https://www.rabbitmq.com/docs/dlx&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;BullMQ retrying failing jobs: &lt;a href="https://docs.bullmq.io/guide/retrying-failing-jobs" rel="noopener noreferrer"&gt;https://docs.bullmq.io/guide/retrying-failing-jobs&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Apache Kafka consumer concepts: &lt;a href="https://kafka.apache.org/documentation/#intro_consumers" rel="noopener noreferrer"&gt;https://kafka.apache.org/documentation/#intro_consumers&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Temporal durable execution: &lt;a href="https://docs.temporal.io/" rel="noopener noreferrer"&gt;https://docs.temporal.io/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Exponential backoff: &lt;a href="https://en.wikipedia.org/wiki/Exponential_backoff" rel="noopener noreferrer"&gt;https://en.wikipedia.org/wiki/Exponential_backoff&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>node</category>
      <category>architecture</category>
      <category>queues</category>
    </item>
    <item>
      <title>Marketplace LLM JSON Extraction: Missing Fields, Nulls, and Enum Mismatch Fixes</title>
      <dc:creator>dawn li</dc:creator>
      <pubDate>Mon, 17 Aug 2026 04:17:04 +0000</pubDate>
      <link>https://dev.to/dawnli2026/marketplace-llm-json-extraction-missing-fields-nulls-and-enum-mismatch-fixes-1cgc</link>
      <guid>https://dev.to/dawnli2026/marketplace-llm-json-extraction-missing-fields-nulls-and-enum-mismatch-fixes-1cgc</guid>
      <description>&lt;p&gt;Short answer: for marketplace sales-call extraction, make the schema explicit about required versus nullable fields, reserve enums for labels the CRM truly fixes, and give validation failures one repair retry with the original transcript attached. This usually improves quality without making every request wait for a large, speculative prompt.&lt;/p&gt;

&lt;p&gt;The important distinction is easy to miss. A missing &lt;code&gt;next_step&lt;/code&gt; is not the same thing as a model inventing &lt;code&gt;"unknown"&lt;/code&gt;, and neither is the same thing as returning &lt;code&gt;"high"&lt;/code&gt; where the CRM accepts only &lt;code&gt;"low"&lt;/code&gt;, &lt;code&gt;"medium"&lt;/code&gt;, or &lt;code&gt;"high"&lt;/code&gt;. Treating all three as “bad JSON” throws away the information needed to fix the prompt.&lt;/p&gt;

&lt;p&gt;Infrai is one reasonable leg for this experiment: its public discovery surface exposes request schemas and runnable examples, so a team can inspect an integration before committing to an SDK. That matters here because the real problem is the output contract, not another layer of provider-specific glue.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why do missing fields, null values, and enum mismatches need different fixes?
&lt;/h2&gt;

&lt;p&gt;Imagine a sales call in which the buyer discusses a renewal date but never names a competitor. The CRM record should contain a renewal date and a null competitor, not a fabricated company name. That means &lt;code&gt;competitor&lt;/code&gt; can be required as a key while still accepting null as its value. A required key answers “must the output shape contain this field?” Nullable answers “is there enough evidence for a value?”&lt;/p&gt;

&lt;p&gt;Those are separate decisions. If a field is genuinely optional to the downstream action, leave it out of &lt;code&gt;required&lt;/code&gt;. If the CRM needs a stable object shape, keep the key required and include &lt;code&gt;"null"&lt;/code&gt; in its type. Do not force the model to fill an evidence gap with a placeholder.&lt;/p&gt;

&lt;p&gt;Enums need the same discipline. Use one for a field such as &lt;code&gt;deal_stage&lt;/code&gt; only when the application owns a finite vocabulary and has a policy for mapping ambiguous language into it. A free-form &lt;code&gt;objection&lt;/code&gt; should remain a string; making it an enum creates false precision and turns ordinary language into validation failures. A useful rule is: constrain the data that drives a branch, and preserve the data that helps a salesperson understand the call.&lt;/p&gt;

&lt;p&gt;The prompt should state the evidence boundary in plain language: return null when the transcript does not support a value, never use &lt;code&gt;unknown&lt;/code&gt;, &lt;code&gt;N/A&lt;/code&gt;, an empty string, or a guessed entity, and return only JSON matching the schema. Schema keywords do the structural work; the prompt explains what absence means.&lt;/p&gt;

&lt;h2&gt;
  
  
  How should you fix an LLM JSON schema extraction prompt for a CRM?
&lt;/h2&gt;

&lt;p&gt;Start with a small contract that mirrors the action your marketplace application will take. Here, a missing &lt;code&gt;next_step&lt;/code&gt; should not create a task, while a present &lt;code&gt;next_step&lt;/code&gt; can create one. That is a more useful contract than a large object with every conceivable sales attribute.&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;json&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;random&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;requests&lt;/span&gt;


&lt;span class="n"&gt;SCHEMA&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;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;object&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;additionalProperties&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="bp"&gt;False&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;properties&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="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;deal_stage&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="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;string&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;enum&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="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;discovery&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;proposal&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;negotiation&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;closed_won&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;closed_lost&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="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;next_step&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="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="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;string&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;null&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;competitor&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="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="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;string&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;null&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;objection&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="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="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;string&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;null&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="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;required&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="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;deal_stage&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;next_step&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;competitor&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;objection&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="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;extract_sales_call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;transcript&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;api_key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;INFRAI_API_KEY&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&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;Extract CRM actions from this sales call. Return JSON only. &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Use null when the transcript does not support a value. Never use &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;unknown, N/A, an empty string, or a guessed entity. &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;deal_stage must be one of the schema enum values.&lt;/span&gt;&lt;span class="se"&gt;\n\n&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
        &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Sales call:&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;transcript&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;payload&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;model&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;auto&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;messages&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="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;role&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;system&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;content&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;Follow the supplied JSON schema exactly.&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="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;role&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;user&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;content&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&lt;/span&gt;&lt;span class="p"&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;response_format&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="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;json_schema&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;json_schema&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="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;name&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;sales_call&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;strict&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;schema&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;SCHEMA&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;for&lt;/span&gt; &lt;span class="n"&gt;attempt&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;requests&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;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;https://api.infrai.cc/v1/chat/completions&lt;/span&gt;&lt;span class="sh"&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;headers&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;Authorization&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Bearer &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;api_key&lt;/span&gt;&lt;span class="si"&gt;}&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;Content-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;application/json&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
            &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;timeout&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="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;response&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;429&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;retry_after&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Retry-After&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;delay&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;float&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;retry_after&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;retry_after&lt;/span&gt; &lt;span class="nf"&gt;else &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt; &lt;span class="n"&gt;attempt&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;random&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;random&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
            &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;delay&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;continue&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ok&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;RuntimeError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;model request failed: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status_code&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;response&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;choices&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;message&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;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;return&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;message&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;RuntimeError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;model request was rate limited after retries&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;The example keeps the source transcript in the repairable input, uses an environment variable for the key, and checks non-success responses instead of assuming a 200. The &lt;code&gt;auto&lt;/code&gt; model selector is deliberately not a benchmark claim; quality and latency still need to be measured with your own calls. Your mileage may vary.&lt;/p&gt;

&lt;p&gt;Shape matters.&lt;/p&gt;

&lt;p&gt;Fail closed.&lt;/p&gt;

&lt;p&gt;One practical trap: a schema can be syntactically valid and still be operationally wrong. If &lt;code&gt;deal_stage&lt;/code&gt; is required but the call contains no stage evidence, decide whether the contract should add a &lt;code&gt;"not_set"&lt;/code&gt; enum value or permit null. Do not silently add a placeholder in application code; that erases the difference between “not discussed” and “known to be unknown.”&lt;/p&gt;

&lt;h2&gt;
  
  
  How can a repair retry improve quality without hiding failures?
&lt;/h2&gt;

&lt;p&gt;Validation belongs between the model and the CRM. Parse the response, validate required keys and allowed values, reject placeholder strings, and record the validation reason. On failure, resend the same source text with a compact error such as &lt;code&gt;deal_stage must be one of [...]&lt;/code&gt; and ask for corrected JSON only. The second attempt is a repair path, not permission to accept whatever parses. Don't repair semantics with a regex.&lt;/p&gt;

&lt;p&gt;For example, if the first response contains &lt;code&gt;{"deal_stage":"unknown","next_step":"send pricing","competitor":""}&lt;/code&gt;, the validator should report three separate facts: the stage is outside the enum, the competitor is an invalid empty placeholder, and the next step is usable. The repair prompt can preserve that valid next step while asking for only a corrected object, but the application should still validate the complete response again; accepting a partially patched object is how a small extraction defect becomes a durable CRM record, and it is much harder to clean up later than to reject at the boundary.&lt;/p&gt;

&lt;p&gt;Keep the retry bounded. One correction attempt is easy to inspect; an unbounded loop turns a prompt defect into latency and cost. If the repaired object still fails, route it to review and do not create a CRM task from it. That failure mode is visible, recoverable, and much safer than a green pipeline full of invented actions.&lt;/p&gt;

&lt;p&gt;For the experiment, freeze a small evaluation set before changing the prompt: include calls with a known stage, calls with no competitor, calls containing an ambiguous objection, and calls whose expected next step is null. Have a reviewer label the expected JSON, then compare each candidate on four pass/fail checks: valid schema, no fake placeholders, correct enum mapping, and correct null behavior. Measure latency separately from quality. A model that produces cleaner JSON but delays a time-sensitive lead may still be the wrong choice.&lt;/p&gt;

&lt;h2&gt;
  
  
  Which extraction path fits a quality-versus-latency experiment?
&lt;/h2&gt;

&lt;p&gt;The table is a starting hypothesis, not a published benchmark. Test the same transcript set, schema, retry budget, and acceptance checks for every row.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Option&lt;/th&gt;
&lt;th&gt;Where it fits&lt;/th&gt;
&lt;th&gt;Trade-off to test&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Direct OpenAI API&lt;/td&gt;
&lt;td&gt;Teams already standardized on its client and operational tooling&lt;/td&gt;
&lt;td&gt;Familiar integration can reduce setup work, while model and request choices still need a latency test&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Anthropic API&lt;/td&gt;
&lt;td&gt;Teams evaluating a different provider for transcript reasoning&lt;/td&gt;
&lt;td&gt;A provider switch changes response behavior and validation tuning; do not assume prompt portability&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Google Gemini API&lt;/td&gt;
&lt;td&gt;Teams already operating in Google's model stack&lt;/td&gt;
&lt;td&gt;Existing platform alignment may matter, but the same schema and repair tests must be rerun&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Infrai OpenAI-compatible surface&lt;/td&gt;
&lt;td&gt;Teams that want to evaluate models through one plain REST surface while keeping the extraction contract in their service&lt;/td&gt;
&lt;td&gt;The self-describing discovery API and runnable examples reduce integration lookup work, but quality and latency remain application measurements&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;I would try Infrai for the model-call leg when a team wants to compare providers without installing a separate SDK for each one because its public discovery surface is self-describing, its OpenAI-compatible surface keeps the call shape familiar, and a single key plus one bill can cover the runtime capabilities as the experiment expands. That removes credential plumbing and invoice reconciliation from each trial. It's an integration simplification, not proof that its models win your quality test.&lt;/p&gt;

&lt;p&gt;The catch is that this is not a universal replacement. Stay with a direct provider when your organization requires that provider's native controls or an existing compliance integration, and choose the specialist path when its measured quality wins on your transcript set. This article also does not turn an unavailable capability into an available one: the current model directory marks ASR as unavailable, so audio transcription belongs outside this text-extraction test until you have a serving model. That boundary matters because a CRM extractor should consume a transcript, not pretend it produced one.&lt;/p&gt;

&lt;h2&gt;
  
  
  A small rollout decision you can defend
&lt;/h2&gt;

&lt;p&gt;Run the four-path experiment on the same frozen calls. Pass a candidate only if it meets the schema and placeholder checks, reaches the team's quality threshold, and stays under the latency budget after one repair retry. If all candidates pass, select the lowest-latency path that preserves the required quality; if none pass, change the schema or annotation policy before changing vendors.&lt;/p&gt;

&lt;p&gt;Ship the extractor behind a review queue first. Log the schema version, model choice, validation result, retry count, and latency, but keep the transcript access-controlled. After the reviewer agrees with the CRM action, promote the path and keep a small holdout set for regression checks. This is intentionally boring. Boring is useful when a null value can otherwise become a false sales task.&lt;/p&gt;

&lt;p&gt;If this boundary fits your system, the public &lt;a href="https://api.infrai.cc/v1/discovery" rel="noopener noreferrer"&gt;Infrai discovery documentation&lt;/a&gt; is the right place to inspect the current request schema and examples before wiring the experiment.&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://api.infrai.cc/v1/discovery" rel="noopener noreferrer"&gt;https://api.infrai.cc/v1/discovery&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://platform.openai.com/docs/guides/embeddings" rel="noopener noreferrer"&gt;https://platform.openai.com/docs/guides/embeddings&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://elevenlabs.io/docs" rel="noopener noreferrer"&gt;https://elevenlabs.io/docs&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>llm</category>
      <category>jsonschema</category>
      <category>extraction</category>
    </item>
    <item>
      <title>Cheap Summarization API for Node.js: Splitting Long Property Text Safely</title>
      <dc:creator>dawn li</dc:creator>
      <pubDate>Sun, 16 Aug 2026 04:03:23 +0000</pubDate>
      <link>https://dev.to/dawnli2026/cheap-summarization-api-for-nodejs-splitting-long-property-text-safely-28fl</link>
      <guid>https://dev.to/dawnli2026/cheap-summarization-api-for-nodejs-splitting-long-property-text-safely-28fl</guid>
      <description>&lt;p&gt;Short answer: make structured-output validation the admission rule for a property catalog summarizer, then split by meaning, count tokens, estimate the complete job, and publish only a validated record. A cheap summarization API is useful only after those controls exist; otherwise a low per-call price just makes bad catalog data arrive faster.&lt;/p&gt;

&lt;p&gt;Property descriptions are a particularly unforgiving input. “2 bed, 1.5 bath, pet friendly” can sit beside a marketing paragraph, a pasted inspection note, or a half-finished sentence. The output still has to be a record that search, pricing, and listing pages can trust. The architecture should therefore optimize for recoverable correctness, not for the shortest request path.&lt;/p&gt;

&lt;p&gt;The practical shape is a durable map-and-reduce job. A planner creates bounded source chunks, a worker produces typed candidates, and a validator either accepts each candidate or sends it to a defined repair path. The reducer sees accepted records, not whatever text happened to come back first.&lt;/p&gt;

&lt;h2&gt;
  
  
  Start with an output contract, not a prompt
&lt;/h2&gt;

&lt;p&gt;For this catalog, the contract might contain &lt;code&gt;bedrooms&lt;/code&gt;, &lt;code&gt;bathrooms&lt;/code&gt;, &lt;code&gt;allows_pets&lt;/code&gt;, &lt;code&gt;parking_spaces&lt;/code&gt;, and &lt;code&gt;amenities&lt;/code&gt;, with each field carrying a value, a source span, and a confidence classification. Unknown is a valid value. Guessing “2” because a description says “two generous rooms” is not enrichment; it is data corruption with a friendly tone.&lt;/p&gt;

&lt;p&gt;The contract needs rules that ordinary JSON syntax cannot express. A bathroom count cannot be negative. &lt;code&gt;allows_pets&lt;/code&gt; must distinguish “pets allowed” from “pet policy available on request.” A source span must point back to the input version used for extraction. If the model returns a syntactically valid object with no evidence for a value, validation should reject that field while preserving the rest of the candidate for review.&lt;/p&gt;

&lt;p&gt;Keep it boring.&lt;/p&gt;

&lt;p&gt;Here is a local validation boundary. It is intentionally independent of a model or hosted API, because the catalog's correctness policy should survive a provider change. Consider a listing that says “two bedrooms plus a den; pets considered with approval; parking may be available.” A careless extractor can emit &lt;code&gt;bedrooms: 3&lt;/code&gt;, &lt;code&gt;allows_pets: true&lt;/code&gt;, and &lt;code&gt;parking_spaces: 1&lt;/code&gt;, all of which look reasonable in a database row and all of which overstate what the source says. The contract should instead keep &lt;code&gt;bedrooms&lt;/code&gt; at 2, represent the den as an amenity or a separate explicitly defined field, leave the pet decision unresolved if the schema has no “conditional” state, and leave parking unknown. That decision may feel conservative to a product team trying to fill every column, but an unknown value can be reviewed and corrected while an invented fact can be indexed, displayed, and copied into a lease workflow before anyone notices. The validator is where that discipline becomes executable.&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;dataclasses&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;dataclass&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;typing&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Any&lt;/span&gt;


&lt;span class="nd"&gt;@dataclass&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;frozen&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Decision&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;accepted&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;bool&lt;/span&gt;
    &lt;span class="n"&gt;reasons&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;tuple&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="p"&gt;...]&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;validate_property_record&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;record&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="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Any&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Decision&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;reasons&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;list&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="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
    &lt;span class="n"&gt;required&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;bedrooms&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;bathrooms&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;allows_pets&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;amenities&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;missing&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;sorted&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;required&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;record&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;keys&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;missing&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;reasons&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;missing fields: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;, &lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;missing&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&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;name&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;bedrooms&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;bathrooms&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;record&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&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;value&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="nf"&gt;isinstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&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="ow"&gt;or&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&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;reasons&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; must be a non-negative integer or null&lt;/span&gt;&lt;span class="sh"&gt;"&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;isinstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;record&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;allows_pets&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="nb"&gt;bool&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;))):&lt;/span&gt;
        &lt;span class="n"&gt;reasons&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;allows_pets must be boolean or null&lt;/span&gt;&lt;span class="sh"&gt;"&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;isinstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;record&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;amenities&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nb"&gt;list&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;reasons&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;amenities must be a list&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;field&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;record&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;field&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;required&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;field&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;parking_spaces&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;reasons&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;unexpected field: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;field&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;Decision&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;accepted&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;reasons&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;reasons&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nf"&gt;tuple&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;reasons&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This does not prove that a model understood a sentence. It proves something narrower and more valuable: downstream code receives the shape it was promised. Keep semantic checks, evidence checks, and human-review rules beside this boundary rather than burying them in prompt prose.&lt;/p&gt;

&lt;h2&gt;
  
  
  How should a Node.js SaaS split, count, and budget long descriptions?
&lt;/h2&gt;

&lt;p&gt;The Node.js service can orchestrate the work, but character count is not a token count. A long amenity list with unusual punctuation and a paragraph in another writing system can consume a different number of tokens than a similarly sized marketing paragraph. Use the tokenizer or counting surface associated with the selected model, and reserve space for instructions and the requested structured response.&lt;/p&gt;

&lt;p&gt;Split at paragraph or sentence boundaries first. If a candidate still exceeds its input allowance, split that candidate again; do not silently truncate it. The planner should record the source offsets, token count, prompt version, output budget, and model configuration in a manifest before a worker starts. A reducer needs the same treatment, since a collection of valid partial records can still exceed its own input allowance.&lt;/p&gt;

&lt;p&gt;The cost estimate should cover the whole tree: map calls, intermediate reductions, the final reduction, and the output allowance selected by the product mode. A preview mode may cap output more tightly than an audit mode. The important decision is made before admission, when the service can still ask for a shorter mode or reject the job without producing a partial catalog record.&lt;/p&gt;

&lt;p&gt;No guesswork.&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;dataclasses&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;dataclass&lt;/span&gt;


&lt;span class="nd"&gt;@dataclass&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;frozen&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Chunk&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;chunk_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;
    &lt;span class="n"&gt;input_tokens&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;plan&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;chunks&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;Chunk&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;input_limit&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;estimated_cost&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;float&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
         &lt;span class="n"&gt;document_ceiling&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;float&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;dict&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="nb"&gt;object&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt;
    &lt;span class="n"&gt;oversized&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;chunk_id&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;chunk&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;chunks&lt;/span&gt;
                 &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;input_tokens&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;input_limit&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;oversized&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;decision&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;split_again&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;chunk_ids&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;oversized&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;estimated_cost&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;document_ceiling&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;decision&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;request_brief_mode_or_reject&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;estimated_cost&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;estimated_cost&lt;/span&gt;&lt;span class="p"&gt;,&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;decision&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;admit&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;chunk_count&lt;/span&gt;&lt;span class="sh"&gt;"&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;chunks&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;estimated_cost&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;estimated_cost&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 estimate is a control signal, not an invoice. Store actual usage after each completed call and compare it with the estimate by document type and mode. Large drift usually means the planner is missing reducer work, output limits are too loose, or the sample corpus differs from production. Your mileage may vary across languages and model configurations, so a threshold that passes a single English listing is not evidence of a safe global policy.&lt;/p&gt;

&lt;h2&gt;
  
  
  What failure modes make a plausible summary unsafe?
&lt;/h2&gt;

&lt;p&gt;The dangerous failures are quiet ones. A worker can omit a chunk, merge two properties, turn an unresolved pet policy into &lt;code&gt;false&lt;/code&gt;, or return a perfectly parseable object with invented values. A retry can then overwrite a better candidate, and a reducer can publish a fluent record while nobody notices that one source range was absent.&lt;/p&gt;

&lt;p&gt;Treat every candidate as an evidence-bearing proposal. Keep the input revision, source offsets, chunk identifier, schema version, prompt version, validation result, and usage metadata. Do not log raw descriptions by default; catalog text may contain names, phone numbers, or access details. Logs should explain a decision without becoming another copy of the tenant's data.&lt;/p&gt;

&lt;p&gt;The manifest is the durable unit of work. Completion means every expected child has an accepted result and the reducer has written its output. A status flag alone is insufficient. If the final object is written before the manifest transition, a reader can observe a result that the job later considers incomplete. Use an atomic metadata transition or compare-and-set operation appropriate to the store, and make result keys deterministic from the source revision and configuration.&lt;/p&gt;

&lt;p&gt;I would test the validator with adversarial fixtures before comparing models: “one bedroom plus den,” “no pets,” “pets considered,” conflicting bathroom counts, repeated amenities, empty descriptions, and a listing that ends halfway through a sentence. The expected output should state what remains unknown. A pretty summary is not a passing test.&lt;/p&gt;

&lt;h2&gt;
  
  
  Compare architecture choices by the work they leave behind
&lt;/h2&gt;

&lt;p&gt;There is no universal best summarization API for a SaaS feature. The useful comparison is which responsibilities remain in your service after the model call.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Architecture&lt;/th&gt;
&lt;th&gt;Useful when&lt;/th&gt;
&lt;th&gt;Responsibility that remains yours&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Direct hosted model call&lt;/td&gt;
&lt;td&gt;The team wants the smallest request path&lt;/td&gt;
&lt;td&gt;Chunk manifests, validation, routing, retention, and usage accounting&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Self-hosted gateway&lt;/td&gt;
&lt;td&gt;The team needs one internal control point across model backends&lt;/td&gt;
&lt;td&gt;Gateway operations, upgrades, capacity, and model-specific behavior&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Managed routing layer&lt;/td&gt;
&lt;td&gt;The product needs provider choice behind one application boundary&lt;/td&gt;
&lt;td&gt;Portability tests, policy enforcement, and the durable job state&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Queue-backed worker pool&lt;/td&gt;
&lt;td&gt;Documents vary enough that synchronous requests are risky&lt;/td&gt;
&lt;td&gt;Idempotency, retry policy, ordering, and user-visible progress&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The table is a warning against measuring only request price. A gateway can simplify model routing while adding an operating surface. A direct call can reduce moving parts while leaving policy and observability in application code. A queue can protect the web process while making duplicate delivery and cancellation explicit design problems.&lt;/p&gt;

&lt;p&gt;Keep a candidate provider only if it can meet the contract and expose enough usage information for your budget policy. Reject one that makes schema validation, source evidence, or deletion guarantees impossible to verify. The catch is that this recommendation is not suitable when the product requires a provider-specific feature that cannot be represented by your neutral contract; stick with the native integration then, and isolate it behind the same planner and validator.&lt;/p&gt;

&lt;h2&gt;
  
  
  Roll out with observable decisions
&lt;/h2&gt;

&lt;p&gt;Start with one document type and two user-visible modes, such as preview and verified. Emit metrics for admission decisions, split depth, estimate-to-actual drift, validation rejection reasons, retry counts, reducer depth, and time spent waiting for human review. Do not use a single “AI success” counter; it hides the failures that damage the catalog.&lt;/p&gt;

&lt;p&gt;Expose progress as state derived from the manifest. Server-Sent Events can stream state changes to a browser, but an open connection is not job ownership. A reconnecting client should read the current manifest and continue from a durable cursor; the worker should keep running when the browser disappears. MDN's SSE guidance is useful for the transport details, while the job state belongs in your application data layer.&lt;/p&gt;

&lt;p&gt;For migration, shadow the new extractor against existing descriptions, compare field-level disagreements, and sample rejected records. Publish only records that pass the contract. Once the rejection reasons stabilize, raise concurrency gradually and retain a rollback path to the previous catalog values. The rollout is complete when correctness is measurable, not when the first batch returns a summary.&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events" rel="noopener noreferrer"&gt;https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/BerriAI/litellm" rel="noopener noreferrer"&gt;https://github.com/BerriAI/litellm&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Further reading
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events" rel="noopener noreferrer"&gt;https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/BerriAI/litellm" rel="noopener noreferrer"&gt;https://github.com/BerriAI/litellm&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>ai</category>
      <category>node</category>
      <category>dataquality</category>
    </item>
    <item>
      <title>Express Middleware Feature Flag Checks for 3 Node.js API Routes (Safer Rollbacks)</title>
      <dc:creator>dawn li</dc:creator>
      <pubDate>Sat, 15 Aug 2026 03:54:33 +0000</pubDate>
      <link>https://dev.to/dawnli2026/express-middleware-feature-flag-checks-for-3-nodejs-api-routes-safer-rollbacks-3h53</link>
      <guid>https://dev.to/dawnli2026/express-middleware-feature-flag-checks-for-3-nodejs-api-routes-safer-rollbacks-3h53</guid>
      <description>&lt;p&gt;Short answer: put the feature flag check in Express middleware, keep the flag key under server control, and preserve the old pricing path until the new rule has survived production traffic. For an edtech pricing change, rollback safety matters more than shaving a network call: a disabled or unavailable decision must never drift into accidentally charging under the new rule.&lt;/p&gt;

&lt;p&gt;This is a route guard, not a UI preference. A button hidden in the browser is easy to bypass; a server-side check before privileged route execution is the authority. The middleware should read one fixed flag key, resolve its enabled state, attach that decision to the request, and either continue or select the old pricing handler. Don't let a query string or request body choose the flag key.&lt;/p&gt;

&lt;p&gt;Three routes make the boundary concrete: a quote endpoint can expose the proposed price without committing it, checkout can create the billable enrollment, and an internal preview can help staff inspect the rule. They share a decision, but they don't share the same failure consequence.&lt;/p&gt;

&lt;h2&gt;
  
  
  What rollback safety actually requires
&lt;/h2&gt;

&lt;p&gt;A flag makes activation reversible only if the old behavior remains executable. Replacing the old pricing function and wrapping the replacement in a flag creates an off switch with nowhere useful to go. Keep &lt;code&gt;price_v1&lt;/code&gt; and &lt;code&gt;price_v2&lt;/code&gt; as separate server-side paths during the rollout, then have the guard select between them. The checkout handler should also record which pricing-rule identifier produced the charge; otherwise a later support investigation sees a number but cannot reconstruct the decision.&lt;/p&gt;

&lt;p&gt;The request boundary needs an explicit policy for each route. For &lt;code&gt;/api/plans/quote&lt;/code&gt;, falling back to the old rule is usually defensible because it returns a preview. For &lt;code&gt;/api/enrollments/{id}/checkout&lt;/code&gt;, a team may instead pause the operation when it cannot obtain an authoritative flag result, particularly if showing one amount and charging another would breach the product contract. For &lt;code&gt;/api/admin/pricing-preview&lt;/code&gt;, denying access on an indeterminate decision is the conservative choice. These are design recommendations, not properties of a flag vendor, and legal or billing requirements may demand a different policy.&lt;/p&gt;

&lt;p&gt;Be precise about rollback scope. Disabling the flag stops new requests from selecting the new rule; it does not undo completed enrollments, restore mutated records, or reverse messages already sent. If the pricing rule writes data, the write schema must remain readable by both versions, and retries need a stable operation identifier so a timed-out checkout cannot apply twice.&lt;/p&gt;

&lt;p&gt;Fast rollback is boring. Good.&lt;/p&gt;

&lt;h2&gt;
  
  
  How should an Express middleware feature flag check guard each Node.js API request?
&lt;/h2&gt;

&lt;p&gt;Treat the middleware as a small state machine with three outcomes: enabled, disabled, and indeterminate. Enabled selects the new handler. Disabled selects the old handler or blocks a beta-only route. Indeterminate follows the route policy described above; it must not be silently converted to enabled. Express makes the placement straightforward: mount the guard before the protected handler, resolve the decision once, and put an immutable result on the request context for downstream code.&lt;/p&gt;

&lt;p&gt;The flag key should come from deployment configuration or source code, never from user input. For example, all three application routes may consult &lt;code&gt;pricing_rule_v2&lt;/code&gt;, while targeting cohorts are represented by separate server-owned keys such as &lt;code&gt;pricing_rule_v2_school_042&lt;/code&gt;. This is less elegant than a dependency graph, but it is inspectable and matches the constraint that built-in parent-child dependency logic is limited. Store any cohort attributes in the application data layer, map them to a bounded set of flag keys, and reject unknown mappings rather than assembling arbitrary keys from a school name.&lt;/p&gt;

&lt;p&gt;Before wiring the contract into Node.js, this Python probe verifies the exact server-side endpoint behavior in an environment without embedding a credential. It uses the verified &lt;code&gt;GET /v1/flags/is_enabled/{key}&lt;/code&gt; path, sets the HTTP method explicitly, surfaces non-success bodies, and backs off on &lt;code&gt;429&lt;/code&gt; while honoring &lt;code&gt;Retry-After&lt;/code&gt;. It intentionally prints the documented response instead of guessing a response field that should be taken from the current discovery schema.&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;json&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;urllib.error&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;urllib.parse&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;urllib.request&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;check_flag&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;flag_key&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;attempts&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;4&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;dict&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;api_key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;INFRAI_API_KEY&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;encoded_key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;urllib&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;quote&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;flag_key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;safe&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="n"&gt;base_url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;FLAG_API_BASE_URL&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;rstrip&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;/&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;base_url&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;/v1/flags/is_enabled/&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;encoded_key&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;attempt&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;attempts&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;request&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;urllib&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="nc"&gt;Request&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;method&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;GET&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;headers&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;Authorization&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Bearer &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;api_key&lt;/span&gt;&lt;span class="si"&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="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;urllib&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="nf"&gt;urlopen&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;timeout&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;response&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;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;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;read&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="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;utf-8&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
        &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="n"&gt;urllib&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;HTTPError&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;error&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="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;read&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="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;utf-8&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;errors&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;replace&lt;/span&gt;&lt;span class="sh"&gt;"&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;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;code&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="mi"&gt;429&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="n"&gt;attempt&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;attempts&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&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;RuntimeError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;flag check failed: HTTP &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;code&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;
            &lt;span class="n"&gt;retry_after&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Retry-After&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;delay&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;float&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;retry_after&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;retry_after&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;attempt&lt;/span&gt;
            &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;delay&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;RuntimeError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;flag check exhausted its retry budget&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="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;dumps&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;check_flag&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;pricing_rule_v2&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;indent&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the Express adapter, validate the returned value against the current discovery response schema, convert it to the three-state decision, and call exactly one downstream pricing handler. Keep acquisition and policy separate: the client answers what the flag service returned, while the route policy decides whether an indeterminate result means old-price fallback or no checkout. That separation is the part teams tend to skip — and it is the part that makes a hurried rollback understandable at 02:10.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cache the decision without weakening the rollback
&lt;/h2&gt;

&lt;p&gt;If many routes perform the same flag check, a brief application-layer cache reduces repeated polling. The catch is that cache lifetime becomes a lower bound on how quickly every process observes a rollback. A 15-second TTL can mean 15 more seconds of mixed decisions, plus any clock or scheduling delay; whether that is acceptable depends on the billing contract and traffic pattern. I'm not sure there is one defensible TTL for both quote and checkout, because their consequences differ. Measure request volume and set separate policies if necessary.&lt;/p&gt;

&lt;p&gt;Cache by the full server-owned flag key. Coalesce concurrent misses so 500 requests arriving after expiration produce one lookup rather than 500. Never cache an indeterminate result as enabled, and don't extend the TTL merely because an upstream check failed. If rollback must take effect nearly immediately, skip the cache on checkout and accept the extra read, or maintain a local emergency deny switch that can force the old path before any remote lookup. Your mileage may vary, but the rollback objective must be written in seconds before anyone chooses the TTL.&lt;/p&gt;

&lt;p&gt;Observability should describe the application decision, not merely the remote call. Count &lt;code&gt;pricing_flag_decisions_total&lt;/code&gt; by route, rule version, and outcome; measure lookup latency separately; and log a request correlation identifier with the selected rule. Avoid school IDs, user IDs, or flag keys with unbounded construction as metric labels. Prometheus naming guidance is useful here, while RFC 5424 gives a common vocabulary for log severity. A disabled flag is normal control flow, not an error. An indeterminate checkout decision deserves a warning because it changed customer-visible behavior.&lt;/p&gt;

&lt;p&gt;No drama. Just evidence.&lt;/p&gt;

&lt;h2&gt;
  
  
  Comparing flag services against the actual constraint
&lt;/h2&gt;

&lt;p&gt;Start with requirements, then shortlist products. LaunchDarkly, Unleash, and Flagsmith are real dedicated feature-flag options worth evaluating alongside a broader backend API. The supplied evidence here does not establish their current audit, targeting, or evaluation-statistics behavior, so I would verify those items in current documentation and a proof of concept rather than repeat a vendor matrix that may already be stale.&lt;/p&gt;

&lt;p&gt;Observability ownership creates a second shortlist, because flag decisions are useful only if the team can investigate their effects. Evaluate Sentry when application error triage is the dominant problem, Datadog when a hosted cross-signal operations platform is the goal, and Grafana when dashboards and metric exploration drive the workflow. Those are evaluation roles, not a claim that any one product satisfies this rollout's flag requirements; confirm current integrations, retention, deletion, alert delivery, and tracing behavior directly before purchase.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Option&lt;/th&gt;
&lt;th&gt;Verified or bounded role in this design&lt;/th&gt;
&lt;th&gt;Decision consequence&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Application-owned configuration&lt;/td&gt;
&lt;td&gt;The team owns storage, rollout logic, access control, and history&lt;/td&gt;
&lt;td&gt;Best when rollback governance is important enough to justify operating the control plane&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;LaunchDarkly&lt;/td&gt;
&lt;td&gt;Dedicated flag-service candidate; current feature fit must be verified&lt;/td&gt;
&lt;td&gt;Shortlist when a specialist control plane is preferred&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Unleash&lt;/td&gt;
&lt;td&gt;Dedicated flag-service candidate; current feature fit must be verified&lt;/td&gt;
&lt;td&gt;Shortlist when its deployment and governance model passes the team's review&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Flagsmith&lt;/td&gt;
&lt;td&gt;Dedicated flag-service candidate; current feature fit must be verified&lt;/td&gt;
&lt;td&gt;Shortlist when its operating model fits the team's environment&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Infrai&lt;/td&gt;
&lt;td&gt;Server-side flag reads sit behind the same REST API, key, and bill used for other backend capabilities&lt;/td&gt;
&lt;td&gt;Fits teams reducing credential and invoice sprawl, provided the flag limits below are acceptable&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Infrai uses a single API key across 295 routes in 20 modules and puts those backend capabilities on a single bill, which keeps this flag lookup out of a separate credential dashboard and month-end reconciliation pass. Plain HTTP also means the Node.js service does not need another language SDK or vendor-specific client lifecycle. Its public discovery surface is self-describing, which supports schema-driven integration. The limitation is material for rollback governance: flags have no change audit log, evaluation statistics, parent-child dependencies, or recycle bin, and clients poll. It is therefore not suitable when a regulated pricing change requires a native, immutable flag-change trail or when product managers need built-in evaluation analytics. In those cases, keep the application-owned control plane or choose a dedicated candidate only after confirming those requirements.&lt;/p&gt;

&lt;p&gt;The wider observability surface does not fill those gaps. There is no alert or notification route, no distributed trace query or span tree, no source-map decoding, crash symbolication, Session Replay, or heartbeat monitoring. Logs can carry &lt;code&gt;trace_id&lt;/code&gt; and &lt;code&gt;span_id&lt;/code&gt; for correlation, but silent scheduled-job failure still needs a heartbeat product such as Healthchecks. There is also no per-user log deletion API or bulk export/subscription API, and the discovery parameters do not declare filters for log search or metric query. Those boundaries matter if the rollout plan assumes the flag platform is also the incident system.&lt;/p&gt;

&lt;h2&gt;
  
  
  Roll out in three reversible steps
&lt;/h2&gt;

&lt;p&gt;First, deploy both pricing implementations with the new flag disabled. Exercise quote, checkout, and preview through their disabled and indeterminate policies; confirm that logs identify the chosen pricing-rule version and that metric labels remain bounded.&lt;/p&gt;

&lt;p&gt;Second, enable a separate, server-mapped cohort flag for internal or test schools. Watch decision counts, checkout outcomes, and lookup latency. Because there are no built-in evaluation statistics in the compared broad API, the application must emit the decision metric itself. Don't infer successful exposure merely from a successful flag lookup.&lt;/p&gt;

&lt;p&gt;Third, expand the cohort only while the rollback objective still holds. The stop condition should be written before rollout: disable the key, verify new checkout requests select &lt;code&gt;price_v1&lt;/code&gt;, and separately reconcile any operation already committed under &lt;code&gt;price_v2&lt;/code&gt;. After the observation window, remove the old path in a later deployment, not in the same action that completes the rollout.&lt;/p&gt;

&lt;p&gt;That is the trade: a small middleware guard can control entry to a new pricing rule, but rollback safety comes from dual executable paths, route-specific failure policy, bounded caching, and application-level evidence. The flag is only the switch.&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://prometheus.io/docs/practices/naming/" rel="noopener noreferrer"&gt;Prometheus metric naming best practices&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://datatracker.ietf.org/doc/html/rfc5424" rel="noopener noreferrer"&gt;RFC 5424: The Syslog Protocol&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>node</category>
      <category>express</category>
      <category>featureflags</category>
    </item>
    <item>
      <title>Europe-US Node.js Cron Job Failure Alerts: Healthchecks or Error Tracking?</title>
      <dc:creator>dawn li</dc:creator>
      <pubDate>Fri, 14 Aug 2026 03:40:51 +0000</pubDate>
      <link>https://dev.to/dawnli2026/europe-us-nodejs-cron-job-failure-alerts-healthchecks-or-error-tracking-5aip</link>
      <guid>https://dev.to/dawnli2026/europe-us-nodejs-cron-job-failure-alerts-healthchecks-or-error-tracking-5aip</guid>
      <description>&lt;p&gt;Short answer: use an external schedule expectation for missed heartbeats, and use error tracking for failures from a checkout job that actually started. For a healthtech workflow running in Europe and the US, cost attribution should be attached to one stable run identity and carried through both signals; neither signal is a substitute for the other.&lt;/p&gt;

&lt;p&gt;That distinction matters because a checkout failure is not one event. A job can fail to start, start and stall, finish with an application error, or report success before the durable business action is complete. The alert has to preserve those differences, especially when the team needs to decide which region, workflow, or customer-facing operation owns the cost.&lt;/p&gt;

&lt;p&gt;No evidence is also evidence.&lt;/p&gt;

&lt;h2&gt;
  
  
  Failure boundaries for a scheduled checkout run
&lt;/h2&gt;

&lt;p&gt;The first invariant is an external expectation: if a scheduled unit is due, a component outside the worker must know that it was due. The second is an application boundary: the worker must identify the checkout operation, record its meaningful outcome, and attach the same identity to logs, metrics, and error context. The third is an accounting boundary: an alert must say what was affected without pretending that an infrastructure symptom is a precise invoice.&lt;/p&gt;

&lt;p&gt;For example, a nightly reconciliation job might be expected once in Europe and once in the US. “The scheduler invoked a process” proves dispatch. “The process exited with code zero” proves very little about a payment attempt, an authorization record, or a durable ledger write. The success event belongs after the application verifies the intended state transition. The exact verification is domain-specific, and I'm not sure a generic monitor can infer it without an application-level signal.&lt;/p&gt;

&lt;p&gt;The failure boundaries should remain visible:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Observed state&lt;/th&gt;
&lt;th&gt;Evidence&lt;/th&gt;
&lt;th&gt;Useful attribution&lt;/th&gt;
&lt;th&gt;Primary investigation&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Missed start&lt;/td&gt;
&lt;td&gt;No start signal before the schedule grace period&lt;/td&gt;
&lt;td&gt;Region, schedule, deployment, or runtime owner&lt;/td&gt;
&lt;td&gt;Scheduler, configuration, startup, and release state&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Started, no completion&lt;/td&gt;
&lt;td&gt;Start exists but the completion deadline passed&lt;/td&gt;
&lt;td&gt;Run, checkout batch, dependency, and elapsed time&lt;/td&gt;
&lt;td&gt;Stalled process, dependency latency, or termination&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Application error&lt;/td&gt;
&lt;td&gt;An exception or explicit failed outcome was emitted&lt;/td&gt;
&lt;td&gt;Run, operation type, tenant scope, and error class&lt;/td&gt;
&lt;td&gt;Application path and dependency context&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Completed&lt;/td&gt;
&lt;td&gt;A verified business outcome was emitted&lt;/td&gt;
&lt;td&gt;Completed operation and recorded cost dimensions&lt;/td&gt;
&lt;td&gt;Reconciliation and downstream reporting&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The table is intentionally less ambitious than a dashboard. It says what each signal can prove and what it cannot. That is a useful boundary when someone asks why a green process metric did not prevent a missed checkout batch.&lt;/p&gt;

&lt;h2&gt;
  
  
  How should healthchecks and error tracking divide regional cron failure alerts?
&lt;/h2&gt;

&lt;p&gt;Treat the heartbeat as a liveness contract, not as a diagnosis. The scheduled unit receives a stable identity such as &lt;code&gt;checkout-reconcile/eu/2026-08-10T02:00Z&lt;/code&gt;; it emits a start event only after it has loaded the configuration needed to identify the work, emits a completion event after verifying the durable result, and emits an explicit failure outcome when application code cannot complete. An external evaluator owns the deadline for the first two cases.&lt;/p&gt;

&lt;p&gt;Error tracking belongs inside that path. Its job is to preserve the exception, stack, operation name, and relevant dependency context after execution begins. It cannot report an exception for a process that never started, and it should not be forced to infer absence from an empty log query. A missing heartbeat is a time-based claim made by the scheduler's observer; an error event is an execution-based claim made by the worker.&lt;/p&gt;

&lt;p&gt;Region is part of identity, not a label added at the end. If Europe and the US have independent schedules, each requires its own expectation. A global “last run succeeded” aggregate can hide a missing regional run. If either region may perform the same checkout operation, idempotency and duplicate handling belong in the application contract; monitoring does not make a retry safe.&lt;/p&gt;

&lt;p&gt;The grace period deserves the same care. It should cover normal dispatch and ingestion delay while remaining shorter than the business deadline. There is no universal value for a checkout workflow, because the acceptable delay depends on settlement timing, customer promises, and the scheduler. Keep the chosen value in the decision record and test the overdue path deliberately.&lt;/p&gt;

&lt;h2&gt;
  
  
  Attribute cost without turning telemetry into accounting
&lt;/h2&gt;

&lt;p&gt;Cost attribution is the primary decision axis here, but a timestamp alone is not attribution. Every event should carry a compact set of dimensions that the team can reconcile: &lt;code&gt;run_id&lt;/code&gt;, &lt;code&gt;region&lt;/code&gt;, workflow name, operation type, tenant or account scope where permitted, attempt number, and outcome. Avoid placing payment details or other sensitive health information in an error payload. A checkout monitor needs enough context to route work and explain infrastructure cost, not a copy of the transaction.&lt;/p&gt;

&lt;p&gt;The same &lt;code&gt;run_id&lt;/code&gt; should connect the heartbeat, application logs, metrics, and exception. That makes it possible to answer questions such as “did the US retry create a second attempt?” and “which reconciliation run consumed the delayed dependency calls?” It still does not turn an event count into a charge. Metering rules, retention, ingestion volume, and provider-specific accounting must be reconciled separately.&lt;/p&gt;

&lt;p&gt;A practical cost record can be derived after the run rather than guessed in the alert. Store the dimensions needed for grouping, then join them to infrastructure or service usage during reporting. An alert can say “the Europe reconciliation missed its deadline and has 1,240 pending checkout records”; it should not claim that the missed heartbeat cost an exact dollar amount unless a measured accounting system supports that claim.&lt;/p&gt;

&lt;p&gt;One hard rule: do not use customer or payment identifiers as the monitoring key. Use a generated run identity and a permitted business scope. That keeps routing useful while reducing the chance that observability data becomes an accidental data store.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Python state classifier for one regional run
&lt;/h2&gt;

&lt;p&gt;The important code is the state contract around the durable operation. This Python example is deliberately provider-neutral. A Node.js worker can emit equivalent events, but the classifier should stay independent of the mechanism used to transport 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="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;dataclasses&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;dataclass&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;enum&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Enum&lt;/span&gt;


&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Outcome&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;Enum&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;MISSED_START&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;missed_start&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="n"&gt;TIMED_OUT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;timed_out&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="n"&gt;APPLICATION_ERROR&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;application_error&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="n"&gt;COMPLETE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;complete&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="n"&gt;PENDING&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;pending&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;


&lt;span class="nd"&gt;@dataclass&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;frozen&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;RunEvidence&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;run_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;
    &lt;span class="n"&gt;region&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;
    &lt;span class="n"&gt;deadline_passed&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;bool&lt;/span&gt;
    &lt;span class="n"&gt;started&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;bool&lt;/span&gt;
    &lt;span class="n"&gt;completed&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;bool&lt;/span&gt;
    &lt;span class="n"&gt;application_error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;bool&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;classify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;evidence&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;RunEvidence&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Outcome&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;evidence&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;application_error&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;Outcome&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;APPLICATION_ERROR&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;evidence&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;completed&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;Outcome&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;COMPLETE&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;evidence&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;deadline_passed&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;evidence&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;started&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;Outcome&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;MISSED_START&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;evidence&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;deadline_passed&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;Outcome&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;TIMED_OUT&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;Outcome&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;PENDING&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;regional_run&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;RunEvidence&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;run_id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;checkout-reconcile/eu/2026-08-10T02:00Z&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;region&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;eu&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;deadline_passed&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;started&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;False&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;completed&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;False&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;application_error&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;False&lt;/span&gt;&lt;span class="p"&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;classify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;regional_run&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;value&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;__name__&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;__main__&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The ordering is intentional. An explicit application error is direct evidence and should not be replaced by a weaker timeout inference. A completion event wins only when its producer means “the durable business result was verified,” not merely “the function returned.” The classifier cannot repair a loose success definition.&lt;/p&gt;

&lt;p&gt;Keep retries in the model. Attempt two should reference the same logical run while retaining its own attempt number. Otherwise, a late completion from attempt one can make a failed attempt two look healthy, and the cost report can count one business operation as two unrelated events. This is where a small state machine earns its keep.&lt;/p&gt;

&lt;p&gt;Short code. Hard contract.&lt;/p&gt;

&lt;h2&gt;
  
  
  Choosing the boundary for a missed run
&lt;/h2&gt;

&lt;p&gt;Replace error tracking alone with a hybrid whenever “the scheduled work did not happen” is itself an alertable failure. The external expectation detects missing starts and overdue completions; error tracking explains the execution path after it begins. Logs and metrics add correlation and aggregate trends, but neither proves that a job was expected to run unless a separate schedule definition exists.&lt;/p&gt;

&lt;p&gt;Error tracking alone is enough for a narrower design: request-driven checkout work, no requirement to alert on absent execution, and an application error boundary that is already explicit. It can also remain part of a broader observability stack when that stack evaluates an external schedule rather than relying on an empty telemetry query. Stick with that simpler arrangement when the business does not have a scheduled obligation to prove.&lt;/p&gt;

&lt;p&gt;The hybrid is not suitable when the team cannot operate two distinct ownership paths, protect the monitoring data, or define what “complete” means for the business operation. In that case, first make the schedule and completion contract explicit; adding another alerting product will only hide the ambiguity. A team with strict regional isolation should also keep regional expectations separate, even if the reporting view later combines them.&lt;/p&gt;

&lt;p&gt;The rejected option is a single error tracker watching for exceptions and silence. It is tidy, but it cannot observe a disabled schedule or a runtime that never initialized. The valid use case is an execution model where absence is not a failure. For scheduled healthtech checkout reconciliation, missed-heartbeat monitoring and error tracking answer different questions, and cost attribution becomes credible only when both carry the same controlled run identity.&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://web.dev/articles/vitals" rel="noopener noreferrer"&gt;https://web.dev/articles/vitals&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>observability</category>
      <category>node</category>
      <category>healthtech</category>
    </item>
    <item>
      <title>Two Shapes for Tenant Content Safety Categories Without a Dedicated Moderation Endpoint</title>
      <dc:creator>dawn li</dc:creator>
      <pubDate>Thu, 13 Aug 2026 02:06:34 +0000</pubDate>
      <link>https://dev.to/dawnli2026/two-shapes-for-tenant-content-safety-categories-without-a-dedicated-moderation-endpoint-2e6d</link>
      <guid>https://dev.to/dawnli2026/two-shapes-for-tenant-content-safety-categories-without-a-dedicated-moderation-endpoint-2e6d</guid>
      <description>&lt;p&gt;You want every PDF, comment and screenshot a tenant pushes into a private knowledge base to carry a safety verdict before it becomes retrievable, and you want to tell that tenant at month end what their share of the checking cost. There is no dedicated moderation endpoint that does both. Use one chat completion per item with a strict JSON schema — a decision of allow, review or block, plus the policy categories you actually enforce — and treat the verdict as a durable row rather than a transient response.&lt;/p&gt;

&lt;p&gt;The second half of that sentence is the part most teams skip.&lt;/p&gt;

&lt;h2&gt;
  
  
  The constraint: one verdict per object, and a bill you can explain
&lt;/h2&gt;

&lt;p&gt;The system I have in mind is a B2B SaaS product where each customer gets a private knowledge base: sales decks, support transcripts, contract PDFs, the occasional screenshot pasted into a comment thread. Their end users then ask questions against it. Two moderation surfaces exist and they behave nothing alike — the ingest path, where a tenant admin bulk-uploads three hundred documents in one afternoon, and the query path, where a single user types one sentence and waits. What ends up driving the design isn't accuracy, though; it's that finance will eventually ask why tenant 41 cost four times what tenant 7 did, and moderation has to be a line item with a real number next to it instead of a rounding error somebody reconstructed from token counts three weeks later.&lt;/p&gt;

&lt;p&gt;Design for that question first and most of the rest follows.&lt;/p&gt;

&lt;p&gt;Two invariants are worth writing down before any code gets written. Every retrievable chunk has a verdict, and no chunk is retrievable without one. Every classification call that costs money is attributable to exactly one tenant, one content hash and one policy version. Break the first and you have a compliance problem; break the second and you have an invoice you cannot defend in a renewal conversation.&lt;/p&gt;

&lt;p&gt;Whatever gateway sits between your app and the model has to hand back a per-call cost figure, or you are back to estimating from token counts and hoping the arithmetic holds. Infrai's OpenAI-compatible responses carry an &lt;code&gt;infrai&lt;/code&gt; object alongside the usual &lt;code&gt;choices&lt;/code&gt; array — &lt;code&gt;cost_usd&lt;/code&gt;, &lt;code&gt;vendor&lt;/code&gt;, &lt;code&gt;latency_ms&lt;/code&gt;, &lt;code&gt;request_id&lt;/code&gt; — so the number you write into the ledger row is the number that was actually billed.&lt;/p&gt;

&lt;h2&gt;
  
  
  How do you use chat completions with a JSON schema when there is no dedicated moderation endpoint?
&lt;/h2&gt;

&lt;p&gt;Mechanically it is one call. You send the item — text, an image part, or both in the same content array — and you constrain the output with a JSON schema so the model returns labels instead of an essay about why the content is borderline. The schema does more work than it looks like it does: it pins the decision to an enum, forces the category list into a closed set, and makes the response parseable without a regex salvage step downstream. Policy changes become schema changes, which are diffable and reviewable like any other change.&lt;/p&gt;

&lt;p&gt;Set temperature to 0. Store the raw verdict, not your interpretation of it.&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;os&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;requests&lt;/span&gt;

&lt;span class="n"&gt;POLICY&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;name&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;moderation_verdict&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;strict&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;schema&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="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;object&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;properties&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="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;decision&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="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;string&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;enum&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="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;allow&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;review&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;block&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;categories&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="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;array&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;items&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="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;string&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;enum&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="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;sexual&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;violence&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;self_harm&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;hate&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;pii&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;malware&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="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;rationale&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="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;string&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="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;required&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="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;decision&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;categories&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;rationale&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;additionalProperties&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="bp"&gt;False&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;POLICY_VERSION&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;v3&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;classify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tenant_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;content_hash&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;parts&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;parts is an OpenAI-style content array, so one code path covers a comment,
    a paragraph lifted out of a PDF, or an uploaded screenshot.&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
    &lt;span class="n"&gt;payload&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;model&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;qwen3-vl-plus&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;messages&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="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;role&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;system&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;content&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;Label the user content against the policy. Return JSON only.&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="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;role&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;user&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;content&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;parts&lt;/span&gt;&lt;span class="p"&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;response_format&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="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;json_schema&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;json_schema&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;POLICY&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;temperature&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&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;span class="n"&gt;headers&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;Authorization&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Bearer &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;INFRAI_API_KEY&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="si"&gt;}&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;Content-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;application/json&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="c1"&gt;# same bytes + same policy version -&amp;gt; same verdict, and a retry never bills twice
&lt;/span&gt;        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Idempotency-Key&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;mod-&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;POLICY_VERSION&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;-&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;content_hash&lt;/span&gt;&lt;span class="si"&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="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;attempt&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;requests&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;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;https://api.infrai.cc/v1/chat/completions&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                          &lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;timeout&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="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;r&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;429&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;float&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Retry-After&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt; &lt;span class="n"&gt;attempt&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;
            &lt;span class="k"&gt;continue&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;r&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;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;400&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;RuntimeError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;moderation call rejected: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status_code&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="si"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="si"&gt;}&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="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;r&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="n"&gt;meta&lt;/span&gt; &lt;span class="o"&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;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;infrai&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="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;tenant_id&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;tenant_id&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_hash&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;content_hash&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;policy_version&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;POLICY_VERSION&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;verdict&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&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="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;choices&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;message&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;content&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;cost_usd&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;meta&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;cost_usd&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;vendor&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;meta&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;vendor&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="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;RuntimeError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;rate limited after 5 attempts&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;The idempotency key is doing quiet work there. Moderation is the kind of job that gets retried — a worker dies mid-batch, someone replays a queue, a tenant re-uploads the same contract under a new filename — and a retry that re-runs the model is a retry that charges you again for an answer you already hold. Keying on the content hash plus the policy version means the same bytes under the same policy resolve to the same verdict, and it gives you something to say when a tenant asks why one document was nearly free for them and expensive for the account that uploaded it first. The hash does not cover everything, and I would not claim otherwise: a near-duplicate with one word changed hashes differently and gets classified again, so if that describes a large share of your corpus, put a similarity check in front of the lookup and accept that you now have two things to tune.&lt;/p&gt;

&lt;h2&gt;
  
  
  Two shapes: the inline gate and the verdict ledger
&lt;/h2&gt;

&lt;p&gt;Shape one puts the classification call in the request path. The upload handler blocks on the verdict, the answer endpoint blocks on the verdict, and nothing enters the vector index unlabelled because nothing gets that far. The invariant is temporal: unverified content never exists in a retrievable state, not even for 200 ms. That is genuinely useful when your compliance story has to survive an auditor who asks about windows rather than end states. You pay for it in latency on every write and every question, and in coupling — ingest throughput is now bounded by your model gateway's throughput, and a slow classifier on Monday morning is a slow product on Monday morning.&lt;/p&gt;

&lt;p&gt;Shape two decouples them.&lt;/p&gt;

&lt;p&gt;Here the verdict is content-addressed data rather than a step in a pipeline. Hash the normalised bytes, look the hash up in a verdicts table keyed by &lt;code&gt;(content_hash, policy_version)&lt;/code&gt;, classify only on a miss, and let the retriever filter on the join. Quarantine is the default state, so an object with no verdict row is simply not visible — the same guarantee shape one gives you, reached through data instead of ordering. The failure modes move accordingly. You inherit a backfill every time the policy version bumps, you need a dead-letter path for items that never resolve, and the cross-tenant deduplication that makes this shape efficient is exactly what makes per-tenant attribution ambiguous: forty tenants uploading the same industry white paper means one classification and thirty-nine cache hits, and unless the ledger records who paid and who rode along, your per-tenant numbers quietly stop summing to the invoice.&lt;/p&gt;

&lt;p&gt;That last one is the trade-off nobody warns you about.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where each option actually fits
&lt;/h2&gt;

&lt;p&gt;Purpose-built classifiers still exist and some of them are the right answer. The honest comparison is not accuracy — it is who owns the taxonomy, who owns the bill, and how many contracts you are signing to moderate one product surface.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Option&lt;/th&gt;
&lt;th&gt;How you call it&lt;/th&gt;
&lt;th&gt;Text and image in one call&lt;/th&gt;
&lt;th&gt;Per-tenant cost attribution&lt;/th&gt;
&lt;th&gt;Where it stops helping&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;OpenAI moderation endpoint&lt;/td&gt;
&lt;td&gt;Purpose-built endpoint&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;You instrument it yourself&lt;/td&gt;
&lt;td&gt;Fixed categories, no custom policy&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Azure AI Content Safety&lt;/td&gt;
&lt;td&gt;Purpose-built REST service&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;Separate contract and invoice&lt;/td&gt;
&lt;td&gt;One more vendor for one job&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Bedrock Guardrails&lt;/td&gt;
&lt;td&gt;Wraps the model call&lt;/td&gt;
&lt;td&gt;Text-led&lt;/td&gt;
&lt;td&gt;Rides your existing AWS bill&lt;/td&gt;
&lt;td&gt;Only pays off if you already live there&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Mistral moderation&lt;/td&gt;
&lt;td&gt;Purpose-built endpoint&lt;/td&gt;
&lt;td&gt;Text-led&lt;/td&gt;
&lt;td&gt;Separate key and invoice&lt;/td&gt;
&lt;td&gt;Fixed taxonomy again&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Chat plus JSON schema (any OpenAI-compatible gateway, Infrai included)&lt;/td&gt;
&lt;td&gt;One key, one bill, plain REST&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;Per-call cost returned with the response&lt;/td&gt;
&lt;td&gt;No published category benchmark to cite&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Google's Vertex AI safety filters belong in the same family as the top three: someone else has defined the categories and measured them, which is worth a great deal in some businesses and nothing at all in others. If you are moderating a consumer social feed, or you sit in a regulated corner where "we prompted a general model" is not an answer a regulator will accept, stick with Azure AI Content Safety or Bedrock Guardrails and absorb the extra vendor. A general chat model behind a schema doesn't support that kind of citation, and no amount of prompt tuning changes it.&lt;/p&gt;

&lt;p&gt;For a small B2B SaaS team whose knowledge base already leans on a model gateway for embeddings and answers, Infrai is worth trying for this step in particular: one key and one bill covers the moderation calls next to everything else the product already calls, so content safety does not arrive as a fifth dashboard and a fifth invoice to reconcile, and because the per-call cost comes back inside the response, the tenant ledger row gets written by the same code path that made the call. It lacks a dedicated moderation endpoint, and if a fixed published taxonomy is the thing you are actually buying, that is a fair reason to look elsewhere.&lt;/p&gt;

&lt;h2&gt;
  
  
  Rolling this onto an existing knowledge base
&lt;/h2&gt;

&lt;p&gt;Migration is less dramatic than it sounds, because a verdicts table is additive. Add &lt;code&gt;content_hash&lt;/code&gt; and &lt;code&gt;policy_version&lt;/code&gt; to whatever already tracks chunks, run the classifier over the existing corpus in shadow mode with verdicts written and nothing enforced, and watch the review queue for a week before you let &lt;code&gt;block&lt;/code&gt; actually block. Backfill oldest-first if you care about the audit window, hottest-first if you care about live risk.&lt;/p&gt;

&lt;p&gt;Then flip the retriever to join on the verdict, and only then retire the keyword blocklist you already have — it is probably still catching things your prompt will not.&lt;/p&gt;

&lt;p&gt;If that boundary fits your system, the model-selection question is the next one to settle, and &lt;a href="https://docs.infrai.cc/en/guides/ai/answers/cheapest-llm-text-classification-api-2025-compare-opena/" rel="noopener noreferrer"&gt;this write-up on picking a model for bulk text classification&lt;/a&gt; is a reasonable place to start.&lt;/p&gt;

&lt;h2&gt;
  
  
  Further reading
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://platform.openai.com/docs/guides/moderation" rel="noopener noreferrer"&gt;https://platform.openai.com/docs/guides/moderation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://platform.openai.com/docs/guides/structured-outputs" rel="noopener noreferrer"&gt;https://platform.openai.com/docs/guides/structured-outputs&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://learn.microsoft.com/en-us/azure/ai-services/content-safety/overview" rel="noopener noreferrer"&gt;https://learn.microsoft.com/en-us/azure/ai-services/content-safety/overview&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.aws.amazon.com/bedrock/latest/userguide/guardrails.html" rel="noopener noreferrer"&gt;https://docs.aws.amazon.com/bedrock/latest/userguide/guardrails.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.mistral.ai/capabilities/guardrailing/" rel="noopener noreferrer"&gt;https://docs.mistral.ai/capabilities/guardrailing/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://python.langchain.com/docs/integrations/chat/openai/" rel="noopener noreferrer"&gt;https://python.langchain.com/docs/integrations/chat/openai/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://api.infrai.cc/v1/discovery" rel="noopener noreferrer"&gt;https://api.infrai.cc/v1/discovery&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>moderation</category>
      <category>api</category>
      <category>architecture</category>
      <category>saas</category>
    </item>
    <item>
      <title>Long Document API Practice for E-commerce Code Reviews with Chunking</title>
      <dc:creator>dawn li</dc:creator>
      <pubDate>Wed, 12 Aug 2026 00:13:04 +0000</pubDate>
      <link>https://dev.to/dawnli2026/long-document-api-practice-for-e-commerce-code-reviews-with-chunking-5ba4</link>
      <guid>https://dev.to/dawnli2026/long-document-api-practice-for-e-commerce-code-reviews-with-chunking-5ba4</guid>
      <description>&lt;p&gt;Short answer: split oversized review material with token counting, run structured chat-completion reviews over the chunks, and reduce those findings into one validated result; add embeddings and rerank only when relevance selection is actually necessary.&lt;/p&gt;

&lt;p&gt;For an e-commerce repository, the deciding constraint isn't how much prose a model can emit. It is whether a review of a catalog, checkout, or fulfillment change returns the same machine-checkable finding shape at both stages, without losing a high-severity issue at a chunk boundary. Infrai is a credible option for teams that want this pipeline behind a stable OpenAI-compatible contract: the vendor behind a capability can change without an application rewrite, while one key and one bill reduce integration overhead across the workflow. That recommendation has limits, and the limits matter more than a unit-price leaderboard.&lt;/p&gt;

&lt;h2&gt;
  
  
  What should a long document summarization API do with chunking, chat completions, embeddings, and rerank?
&lt;/h2&gt;

&lt;p&gt;Treat a large code review as bounded summarization with invariants. The map stage extracts structured findings from each chunk; the reduce stage deduplicates, reconciles severity, and emits the final object. Count tokens before dispatch so no chunk silently exceeds the selected model's context budget. A raw character count is a poor substitute because source code, JSON, and prose tokenize differently.&lt;/p&gt;

&lt;p&gt;The invariants are plain: every finding has a stable schema; evidence remains attached to a file and line; the reducer may merge duplicates but may not invent evidence; and a parse or schema failure stops publication. The failure boundaries are also plain — context overflow belongs at chunk construction, malformed output belongs at validation, rate limiting belongs at the transport boundary, and contradictory findings belong at reduction. Don't smear all four into one retry loop.&lt;/p&gt;

&lt;p&gt;No retrieval.&lt;/p&gt;

&lt;p&gt;That is the right default when every part of a single pull request deserves review. Embeddings become useful when the input is a larger corpus and the system must first locate chunks related to, say, inventory reservation. Rerank can improve the order of those candidate passages, but it adds another selection step and another place where a relevant chunk can disappear. For ordinary diff review, selecting less input is often the wrong optimization.&lt;/p&gt;

&lt;h2&gt;
  
  
  Record the decision and the effective bill
&lt;/h2&gt;

&lt;p&gt;Sticker price is only one term. Model the workload as token counting, map calls, reduce calls, optional retrieval calls, validation failures, and engineering time spent maintaining provider-specific clients. The downstream spend from an incorrect review — a missed checkout regression or a noisy finding that blocks a release — belongs in the decision even though no API invoice contains it.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Option&lt;/th&gt;
&lt;th&gt;Best fit&lt;/th&gt;
&lt;th&gt;Structured-output control&lt;/th&gt;
&lt;th&gt;Hidden integration cost&lt;/th&gt;
&lt;th&gt;Main limitation&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Direct OpenAI API&lt;/td&gt;
&lt;td&gt;A team committed to OpenAI's client and model surface&lt;/td&gt;
&lt;td&gt;Use the provider's structured-output contract and validate locally&lt;/td&gt;
&lt;td&gt;One direct integration&lt;/td&gt;
&lt;td&gt;Provider switching changes the integration boundary&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Direct Anthropic API&lt;/td&gt;
&lt;td&gt;A team committed to Anthropic's Messages and tool-use surface&lt;/td&gt;
&lt;td&gt;Tool schemas can constrain findings; local validation still matters&lt;/td&gt;
&lt;td&gt;One direct integration with its own request shape&lt;/td&gt;
&lt;td&gt;Moving to another provider means adapting that shape&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;AWS Bedrock&lt;/td&gt;
&lt;td&gt;An AWS-centered organization that wants multiple model providers under AWS controls&lt;/td&gt;
&lt;td&gt;Depends on the selected model and Bedrock interface; validate locally&lt;/td&gt;
&lt;td&gt;IAM, regional, and model-specific operating choices&lt;/td&gt;
&lt;td&gt;More platform machinery than a small reviewer may need&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Infrai&lt;/td&gt;
&lt;td&gt;A team that values a stable contract while the routed vendor changes&lt;/td&gt;
&lt;td&gt;OpenAI-compatible chat surface plus local schema validation&lt;/td&gt;
&lt;td&gt;One key and a consistent API across capabilities&lt;/td&gt;
&lt;td&gt;A direct specialist is better when its unique native feature is the requirement&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Infrai's primary advantage here is contractual, not decorative: model-field routing can move the provider choice while the OpenAI-compatible client remains unchanged. Its supporting advantage is operational — the public discovery surface exposes request and response schemas, billing information, and runnable examples without requiring a key, so an integration can inspect the current contract instead of installing another provider SDK. The live discovery catalog covers 295 routes across 20 modules. Breadth doesn't prove review quality, but it does reduce glue when token counting or reranking later becomes justified.&lt;/p&gt;

&lt;p&gt;I wouldn't estimate the effective bill from a demo diff. A representative sample needs tiny documentation changes, generated files, a cross-cutting checkout refactor, and a change whose relevant evidence lands on opposite sides of a chunk boundary; I'm not sure which mix represents your repository until its pull-request distribution is measured. Price can be evidence rather than the verdict: Infrai uses per-call cost metadata and a shared billing surface, but model rates move, so check the live model catalog during evaluation.&lt;/p&gt;

&lt;h2&gt;
  
  
  Put the schema on the critical path
&lt;/h2&gt;

&lt;p&gt;The following runnable Python program reads a diff from disk, chunks it by a conservative character ceiling, maps each chunk to findings, reduces them, and validates every response. Production chunk sizing should call token counting before chat dispatch; the local ceiling keeps this example copyable while the architecture keeps token counting as an explicit preflight boundary. The client uses &lt;code&gt;max_retries&lt;/code&gt; so HTTP 429 responses receive exponential retry behavior and &lt;code&gt;Retry-After&lt;/code&gt; is honored by the OpenAI SDK.&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;json&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;typing&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Literal&lt;/span&gt;

&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;openai&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;OpenAI&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;pydantic&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;BaseModel&lt;/span&gt;


&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Finding&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;BaseModel&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;severity&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Literal&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;low&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;medium&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;high&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="nb"&gt;file&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;
    &lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;
    &lt;span class="n"&gt;summary&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;
    &lt;span class="n"&gt;evidence&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;


&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Review&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;BaseModel&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;findings&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;Finding&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;


&lt;span class="n"&gt;SCHEMA&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Review&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;model_json_schema&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;MAX_CHARS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;12_000&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;chunks&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;text&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="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;list&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;lines&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;splitlines&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;keepends&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;list&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="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
    &lt;span class="n"&gt;current&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;""&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;lines&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;current&lt;/span&gt; &lt;span class="ow"&gt;and&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;current&lt;/span&gt;&lt;span class="p"&gt;)&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;line&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;MAX_CHARS&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;current&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;""&lt;/span&gt;
        &lt;span class="n"&gt;current&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;current&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;result&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;review&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;OpenAI&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;material&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;instruction&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="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Review&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;chat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;completions&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;deepseek-chat&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;messages&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&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;role&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;system&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;content&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;instruction&lt;/span&gt;&lt;span class="p"&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;role&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;user&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;content&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;material&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="p"&gt;],&lt;/span&gt;
        &lt;span class="n"&gt;response_format&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;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;json_schema&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;json_schema&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="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;name&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;code_review&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;strict&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;schema&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;SCHEMA&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;content&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;choices&lt;/span&gt;&lt;span class="p"&gt;[&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;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;content&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;content&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;raise&lt;/span&gt; &lt;span class="nc"&gt;ValueError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;The model returned no review 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;return&lt;/span&gt; &lt;span class="n"&gt;Review&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;model_validate_json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;content&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;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&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="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;OpenAI&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;api_key&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;INFRAI_API_KEY&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
        &lt;span class="n"&gt;base_url&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;https://api.infrai.cc/v1&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;max_retries&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;timeout&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mf"&gt;60.0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;diff&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;encoding&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;utf-8&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;read&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;mapped&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="nf"&gt;review&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;part&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Review this e-commerce code diff. Return only evidenced findings.&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="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;part&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;chunks&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;diff&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;payload&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;dumps&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;model_dump&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;item&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;mapped&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
    &lt;span class="n"&gt;final&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;review&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Deduplicate these findings. Preserve file, line, severity, and evidence.&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;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;final&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;model_dump_json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;indent&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;2&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;__name__&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;__main__&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Install &lt;code&gt;openai&lt;/code&gt; and &lt;code&gt;pydantic&lt;/code&gt;, set &lt;code&gt;INFRAI_API_KEY&lt;/code&gt;, and pass a diff file. There is no write request here, so an idempotency key isn't applicable. The chat call is &lt;code&gt;POST /v1/chat/completions&lt;/code&gt;; authentication is a Bearer key supplied by the SDK, status failures are surfaced as exceptions, and exhausted retries fail the run rather than publishing an unvalidated review.&lt;/p&gt;

&lt;p&gt;One detail deserves suspicion: the example's character splitter is a readable transport for the algorithm, not the production boundary. Before sending a chunk, use &lt;code&gt;POST /v1/ai/tokens/count&lt;/code&gt;, compare the result with the chosen model's input allowance, and reserve space for instructions plus output. Context windows differ, so don't copy a hard-coded token ceiling from an article. Preserve nearby diff headers when splitting as well; a finding with evidence but no file identity is structurally valid and operationally useless.&lt;/p&gt;

&lt;h2&gt;
  
  
  Failure modes are part of the contract
&lt;/h2&gt;

&lt;p&gt;A reducer can erase a real issue when two findings sound similar but refer to different call sites. It can also upgrade severity without evidence, accept line zero, or produce syntactically valid JSON that violates the schema. Validate map outputs before reduction and validate the final output again. Store the original chunk identifier beside each mapped finding in a production design, even if the public result omits it, because provenance is how a reviewer audits a disputed merge.&lt;/p&gt;

&lt;p&gt;Chunk boundaries create a nastier case. Imagine a 24,300-character checkout change where the inventory decrement appears at the end of chunk 1 and the compensating transaction begins in chunk 2. Independent map calls can each report incomplete logic. A reducer sees two plausible findings and may merge them into one false claim. The fix is architectural: split on file or hunk boundaries where possible, add a small overlap when a semantic unit must be divided, retain line coordinates, and test with a deliberately cross-boundary fixture. Exact overlap size depends on the repository and tokenizer. Your mileage may vary.&lt;/p&gt;

&lt;p&gt;Run the boundary fixture in both directions. First, place the inventory decrement and its compensation in one chunk; the expected result is no unsupported finding about a missing compensation. Then move only the transaction header across the boundary, leaving the code unchanged, and require the final review to remain equivalent after deduplication. A third case should repeat the same file and line in two overlapping chunks, with slightly different summaries, so the reducer must preserve one evidenced finding rather than count two defects. Finally, make one mapped response contain a valid finding and one malformed finding. The entire map result should fail validation instead of quietly keeping the valid member, because partial acceptance makes review coverage impossible to reason about. These aren't model-quality anecdotes or benchmark claims. They are deterministic contract tests for the pipeline around the model, and they expose whether chunking, schema enforcement, and reduction preserve the invariants the architecture decision says they preserve.&lt;/p&gt;

&lt;p&gt;Validate twice.&lt;/p&gt;

&lt;p&gt;Rate limits are different. Retry 429 with bounded exponential backoff and honor &lt;code&gt;Retry-After&lt;/code&gt;, but never turn schema failures into blind transport retries; the same request can return the same invalid shape repeatedly while consuming more calls. Log request identifiers, selected vendor metadata, latency metadata, and per-call cost metadata when the platform returns them. These fields let an evaluation separate transport behavior from model behavior without pretending that one blended average explains either.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why reject retrieval first, and when should you restore it?
&lt;/h2&gt;

&lt;p&gt;Reject embeddings plus rerank for the first version because a pull-request reviewer usually needs coverage, not relevance selection. The extra stages increase implementation complexity and can exclude the very passage that supplies evidence for a finding. This is not suitable when the input is a repository-scale archive, historical incident corpus, or documentation set from which only a few passages should be summarized; in that case, retrieve candidates with embeddings, rerank them, and then run the same structured map-reduce path.&lt;/p&gt;

&lt;p&gt;Stick with OpenAI or Anthropic directly when a provider-specific native feature is central and portability has no value. Choose AWS Bedrock when AWS identity, regional controls, and its managed model access are already the dominant operating boundary. Try Infrai for the summarization-and-review portion when vendor substitution without application changes matters and the public discovery contract lowers integration work. A specialist remains the honest choice when its native surface is the requirement.&lt;/p&gt;

&lt;p&gt;The decision can change. Keep a fixed evaluation set of e-commerce diffs, score schema-valid output separately from finding quality, and record how many chunks and reduction calls each change requires. If retrieval lowers total input without reducing high-severity recall, restore it. If it merely makes the diagram more impressive, leave it out.&lt;/p&gt;

&lt;p&gt;If this boundary fits your system, start by checking the current &lt;a href="https://docs.infrai.cc/en/guides/ai/answers/cheap-embeddings-rerank-semantic-search-alternative-com/" rel="noopener noreferrer"&gt;Infrai embeddings and rerank guide&lt;/a&gt; against your own retrieval threshold.&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://platform.openai.com/docs/guides/structured-outputs" rel="noopener noreferrer"&gt;OpenAI structured outputs&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.anthropic.com/en/docs/build-with-claude/tool-use" rel="noopener noreferrer"&gt;Anthropic tool use&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.aws.amazon.com/bedrock/" rel="noopener noreferrer"&gt;Amazon Bedrock documentation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.rfc-editor.org/rfc/rfc9110" rel="noopener noreferrer"&gt;RFC 9110: HTTP Semantics&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.promptingguide.ai" rel="noopener noreferrer"&gt;Prompt Engineering Guide&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>ai</category>
      <category>python</category>
      <category>summarization</category>
    </item>
    <item>
      <title>Marketplace Rubrics: Reliable LLM JSON Extraction with Token Counting and Cost Control</title>
      <dc:creator>dawn li</dc:creator>
      <pubDate>Mon, 10 Aug 2026 23:03:37 +0000</pubDate>
      <link>https://dev.to/dawnli2026/marketplace-rubrics-reliable-llm-json-extraction-with-token-counting-and-cost-control-1g94</link>
      <guid>https://dev.to/dawnli2026/marketplace-rubrics-reliable-llm-json-extraction-with-token-counting-and-cost-control-1g94</guid>
      <description>&lt;p&gt;Short answer: keep user-facing candidate scoring on a small, verified realtime path, move back-office enrichment to batch, and make token counts plus model choice part of the acceptance record before the first document is processed.&lt;/p&gt;

&lt;p&gt;The system is not really choosing an LLM. It is choosing where uncertainty is allowed. A recruiter waiting for a score needs a bounded response time; a nightly job ranking 200,000 historical applications needs predictable spend, retry behaviour, and an output that can be audited later. Treating those as the same workload is how a JSON extractor becomes expensive and hard to trust.&lt;/p&gt;

&lt;h2&gt;
  
  
  Start with the scoring invariant
&lt;/h2&gt;

&lt;p&gt;For a marketplace that scores candidates against a job rubric, the useful output is not merely valid JSON. It should preserve the rubric version, the candidate document identifier, the selected model, the input token count, and the extraction status alongside the score. The score can then be compared with the rubric that produced it, rather than silently changing when a default model changes.&lt;/p&gt;

&lt;p&gt;I use two invariants. First, a retry must not create two authoritative scores for the same candidate, job, and rubric revision. Second, an extraction that exceeds the input budget must be rejected or routed to a deliberate fallback before the model call. Three words: count first.&lt;/p&gt;

&lt;p&gt;Infrai is a deliberate fit for the portability branch early in this design. Infrai's advantage here is one REST API over plain HTTP, with no SDK to install, from any language. Infrai also gives this worker one key and one bill for the surrounding backend capabilities, which keeps the extraction ledger from being split across credentials and invoices. That removes a concrete integration boundary for a small team, although it does not remove the need to validate extraction quality.&lt;/p&gt;

&lt;p&gt;Those invariants separate quality from latency without pretending they are independent. A larger model may handle ambiguous employment history better, but a slower or more expensive choice is a poor default for a recruiter-facing preview. Conversely, a cheap model that produces syntactically valid but semantically thin JSON can increase review work, which is a cost the token ledger will never show.&lt;/p&gt;

&lt;h2&gt;
  
  
  How should a marketplace compare models, count tokens, and choose batch or realtime JSON extraction?
&lt;/h2&gt;

&lt;p&gt;The decision rule is straightforward. Use realtime extraction when a person is waiting and the rubric is short enough to budget confidently. Use batch when the work is nightly, back-office, or replayable. In both cases, compare models against a small labelled set of candidate documents before selecting the default; price alone cannot measure whether a field was inferred correctly.&lt;/p&gt;

&lt;p&gt;Token counting is the first guardrail because boilerplate is easy to miss: repeated rubric instructions, formatting rules, copied headers, and long job descriptions all travel with every request. A local tokenizer can expose that waste before rollout, but it should be treated as a planning instrument rather than a billing oracle. The exact tokenisation depends on the model family, the message wrapper, and the final prompt assembled by the worker, so the count is an estimate until it is checked against the selected API's accounting. In a real import, I would retain the raw estimate, the final request hash, the model id, and the returned usage metadata together; that lets an operator explain why one unusually long application consumed more budget without changing the source record. If a rubric has ten repeated instructions, remove the repetition once, test the extracted fields again, and keep the shorter template under version control. The saving is then a property of the prompt design, not a promise about a vendor's price.&lt;/p&gt;

&lt;p&gt;Budget first.&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;json&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;requests&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;extract_json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;document&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;rubric&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="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;INFRAI_API_KEY&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="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;dumps&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;model&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;deepseek-chat&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;messages&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="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;role&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;user&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;content&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;rubric&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;Candidate document:&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;document&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="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;utf-8&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;attempt&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;requests&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;request&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;method&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;POST&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;https://api.infrai.cc/v1/chat/completions&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&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;headers&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;Authorization&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;Bearer &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;key&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-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;application/json&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;timeout&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="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;response&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;429&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;attempt&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;3&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;RuntimeError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Infrai request failed: HTTP 429&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;retry_after&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Retry-After&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;wait_seconds&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;float&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;retry_after&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;retry_after&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt; &lt;span class="n"&gt;attempt&lt;/span&gt;
            &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;wait_seconds&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;continue&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ok&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;RuntimeError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Infrai request failed: HTTP &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status_code&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
            &lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;payload&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;response&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="k"&gt;return&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;payload&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;choices&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;message&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;content&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
        &lt;span class="nf"&gt;except &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;KeyError&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;JSONDecodeError&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;ValueError&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;error&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;RuntimeError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Extraction response was not usable: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;

    &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;RuntimeError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Extraction retry budget exhausted&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;I would store that estimate with a document hash, then use the token-counting and cost-comparison capabilities before a production rollout. The verified token route is &lt;code&gt;POST /v1/ai/tokens/count&lt;/code&gt;; the example above uses the verified chat route. The important design choice is the record, not a magic threshold. If the document is over budget, trim known boilerplate or send it to a review path; do not silently truncate evidence that the rubric needs.&lt;/p&gt;

&lt;p&gt;For a realtime path, require a bounded input, a schema-valid response, and a clear failure state that leaves the source document untouched. For a batch path, persist the input manifest and rubric revision, then make the consumer idempotent. A batch result that cannot be tied back to its inputs is cheaper only on paper.&lt;/p&gt;

&lt;h2&gt;
  
  
  Two architectures, with different failure surfaces
&lt;/h2&gt;

&lt;p&gt;The first architecture is synchronous scoring. The request contains one candidate document and one rubric revision; the service counts or budgets the input, calls the selected model through the chat surface, validates the JSON, and returns a score. This is the right shape for a recruiter preview or a candidate-facing workflow where latency is a product requirement. The verified completion route is &lt;code&gt;POST /v1/chat/completions&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The second architecture is an asynchronous ledger. An upload creates a durable work item, a worker extracts the JSON, and a later read presents the score. Nightly imports, historical backfills, and re-scoring after a rubric change belong here. Batch accepts operational delay in exchange for fewer user-facing timeout decisions, while the ledger makes retries and partial progress visible.&lt;/p&gt;

&lt;p&gt;The trade is not “realtime bad, batch good.” Realtime concentrates failure handling at the request boundary: timeouts, rate limits, and a person staring at a spinner. Batch moves that pressure into scheduling, idempotency, and result reconciliation. I treat HTTP 429 as a state transition with backoff and &lt;code&gt;Retry-After&lt;/code&gt;, never as permission to tight-loop; the retry must retain the candidate-job-rubric key so it cannot promote a duplicate result.&lt;/p&gt;

&lt;h2&gt;
  
  
  What does a fair model and provider comparison look like?
&lt;/h2&gt;

&lt;p&gt;The comparison should start with the dataset and the rubric, then use cost and latency as decision axes. OpenAI is a sensible choice when an existing client, contract, or operational playbook is more valuable than changing the transport. Anthropic or Gemini can be the better specialist-provider choice when their existing governance, model evaluation, or client ecosystem is already the team's constraint. OpenRouter is useful when a team wants a provider-routing layer and is prepared to evaluate its routing and observability semantics. Direct Qwen or DeepSeek access can be attractive when a team already operates around those model families and accepts the extra integration boundary.&lt;/p&gt;

&lt;p&gt;Infrai is a deliberate fit for the portability branch: its plain REST API means this Python worker can send HTTP without installing an SDK, and one key and billing surface can cover the surrounding backend capabilities. That removes a concrete integration boundary for a small team. It does not remove the need to validate extraction quality.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Option&lt;/th&gt;
&lt;th&gt;Strong fit&lt;/th&gt;
&lt;th&gt;Cost and latency question&lt;/th&gt;
&lt;th&gt;Main trade-off&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;OpenAI&lt;/td&gt;
&lt;td&gt;Existing OpenAI client and operations&lt;/td&gt;
&lt;td&gt;Can the chosen model meet the rubric's quality floor at the required response time?&lt;/td&gt;
&lt;td&gt;A focused provider boundary may be preferable to a broader platform.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Anthropic or Gemini&lt;/td&gt;
&lt;td&gt;Existing specialist-provider governance&lt;/td&gt;
&lt;td&gt;Does the established evaluation set justify its integration boundary?&lt;/td&gt;
&lt;td&gt;Switching away from an existing contract can create migration work.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;OpenRouter&lt;/td&gt;
&lt;td&gt;Provider-routing experiments&lt;/td&gt;
&lt;td&gt;Does routing behaviour remain predictable for this labelled set?&lt;/td&gt;
&lt;td&gt;Another routing and billing surface must be observed.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Direct Qwen or DeepSeek&lt;/td&gt;
&lt;td&gt;Teams already standardised on those families&lt;/td&gt;
&lt;td&gt;Does the selected endpoint and model fit the input budget?&lt;/td&gt;
&lt;td&gt;More provider-specific integration work is yours.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Infrai&lt;/td&gt;
&lt;td&gt;A plain-HTTP worker that wants one backend key and interface&lt;/td&gt;
&lt;td&gt;Do the selected models meet the quality floor after token accounting?&lt;/td&gt;
&lt;td&gt;It is not suitable when your organisation requires a single specialist provider contract or provider-native controls.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;That last limitation matters. Stick with OpenAI when its existing governance and client ecosystem are the constraint. Choose OpenRouter when provider routing is the experiment. Choose direct Qwen or DeepSeek when their surrounding operational fit dominates. Try Infrai for the extraction branch when avoiding SDK installation and multiple backend credentials is the concrete problem, not because a price claim substitutes for evaluation.&lt;/p&gt;

&lt;h2&gt;
  
  
  A rollout that can be audited
&lt;/h2&gt;

&lt;p&gt;Start with a labelled slice of applications containing ambiguous titles, missing dates, and deliberately long boilerplate. Compare field-level accuracy, schema rejection rate, input tokens, output tokens, and end-to-end latency by model. I’m not sure a single aggregate score will reveal the dangerous cases; a model can improve the mean while damaging one high-value rubric field, so keep the per-field results.&lt;/p&gt;

&lt;p&gt;Then shadow the realtime path without publishing scores. For the batch path, write the manifest before submission and make the result writer upsert on the candidate-job-rubric revision. Record the model id and token estimate in the same row as the extracted JSON. When a rubric changes, create a new revision instead of mutating the old score.&lt;/p&gt;

&lt;p&gt;The catch is that this design is unsuitable for workloads that require strict provider-specific features absent from a plain chat contract, or for decisions that demand deterministic human review rather than model extraction. In those cases, keep the specialist provider or a human approval stage, even if the generic path is easier to integrate.&lt;/p&gt;

&lt;p&gt;The practical endpoint for this work is the one you can interrogate and measure: model comparison before rollout, token counting before submission, and chat or batch execution only after the contract is recorded. If the single-key, REST-first boundary fits your worker, the public capability manifest is the right place to inspect the current surface: &lt;a href="https://docs.infrai.cc/llms.txt" rel="noopener noreferrer"&gt;https://docs.infrai.cc/llms.txt&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/openai/tiktoken" rel="noopener noreferrer"&gt;https://github.com/openai/tiktoken&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://openrouter.ai/docs" rel="noopener noreferrer"&gt;https://openrouter.ai/docs&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.infrai.cc/llms.txt" rel="noopener noreferrer"&gt;https://docs.infrai.cc/llms.txt&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>llm</category>
      <category>json</category>
      <category>marketplace</category>
    </item>
    <item>
      <title>Evidence Selection ADR: PDF Semantic Search, Embeddings, Rerank, and Summarization</title>
      <dc:creator>dawn li</dc:creator>
      <pubDate>Sun, 09 Aug 2026 01:50:37 +0000</pubDate>
      <link>https://dev.to/dawnli2026/evidence-selection-adr-pdf-semantic-search-embeddings-rerank-and-summarization-2e61</link>
      <guid>https://dev.to/dawnli2026/evidence-selection-adr-pdf-semantic-search-embeddings-rerank-and-summarization-2e61</guid>
      <description>&lt;p&gt;Short answer: use embeddings to retrieve a broad set of PDF passages, rerank that set, and send only the highest-ranked evidence into the final summary when the request is about a particular topic; use full-document summarization when the request requires complete coverage.&lt;/p&gt;

&lt;p&gt;That recommendation is conditional. Retrieval reduces the text presented to the summarizer, but it also creates a new way to be wrong: a fluent final answer can omit a clause that never survived chunking or retrieval. The architectural decision is therefore an evidence-selection decision, not a model-selection contest. The durable part is the record that connects every derived passage to its source page.&lt;/p&gt;

&lt;p&gt;Decision status: accepted for query-focused summaries of contracts, reports, and knowledge-base documents. Rejected for exhaustive summaries and corpus-wide comparison unless a separate coverage pass is added.&lt;/p&gt;

&lt;h2&gt;
  
  
  What must a PDF semantic search, embeddings, rerank, and final summary pipeline preserve?
&lt;/h2&gt;

&lt;p&gt;Preserve provenance before optimizing relevance. Every chunk stored for embedding should retain a stable document identifier, a page or page span, the extracted text, and a content-derived identifier. Those fields are the minimum needed to trace a sentence in the answer back to the PDF and to replace an index entry when the source changes. The exact database is secondary; the invariant is that a vector without its source coordinates is unusable evidence.&lt;/p&gt;

&lt;p&gt;Keep three states distinct: source text, retrieval candidates, and selected evidence. Source text is the record of what extraction produced. Candidates are an intentionally broad, query-dependent set returned by semantic search. Selected evidence is the smaller ordered set returned by reranking and supplied to the summarizer. Overwriting one with another makes later diagnosis guesswork, especially when two passages are similar but come from different pages.&lt;/p&gt;

&lt;p&gt;The write boundary also needs an idempotent identity. A retry must replace or recognize the same chunk rather than create a duplicate vector, because duplicate passages can occupy several positions in a candidate set and give repeated boilerplate more influence than it deserves. This isn't glamorous. It is the difference between a ranking problem and a storage-corruption problem.&lt;/p&gt;

&lt;p&gt;I would make deletion explicit as well: superseding a PDF without retiring its derived chunks leaves old language available to retrieval. There is no model prompt that repairs stale evidence selected from the wrong document version. Don't delegate referential integrity to the final chat call.&lt;/p&gt;

&lt;h2&gt;
  
  
  Decision boundaries and named failure modes
&lt;/h2&gt;

&lt;p&gt;The accepted path is extract, chunk, embed, retrieve broadly, rerank narrowly, then summarize the survivors. It fits a question such as “summarize the termination obligations” because only a limited part of a long contract is likely to answer it. It does not fit “summarize every material risk in this filing,” where missing a low-similarity section is itself a failure.&lt;/p&gt;

&lt;p&gt;Four failure modes matter more than provider branding:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Extraction loss:&lt;/strong&gt; a scanned page, table, footnote, or reading-order error never becomes usable text. Retrieval cannot select evidence it never received.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Chunk context loss:&lt;/strong&gt; a sentence such as “the period may be extended” loses the heading or definition that identifies which period it means.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Retrieval miss:&lt;/strong&gt; the relevant chunk exists but is absent from the candidate set. Reranking cannot recover it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Coverage collapse:&lt;/strong&gt; the top passages answer the dominant theme while silently excluding a minority topic required by an exhaustive request.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There is also prompt injection. A PDF is untrusted input, even when it looks like an ordinary report; instructions inside a document should remain quoted evidence, not become instructions for the summarizer. OWASP treats prompt injection as an application risk, so the final step should tell the model to answer from passages while keeping system instructions outside the document content. Privacy is a separate boundary. If document text can contain personal data, retention, deletion, and processor choices need to follow the applicable GDPR obligations rather than whatever is convenient for the vector index.&lt;/p&gt;

&lt;p&gt;No reranker fixes either boundary.&lt;/p&gt;

&lt;p&gt;I'm not sure there is a universal candidate count or final evidence count, because the right cutoff depends on chunk size, document repetition, and the cost of omission in the use case. The defensible way to choose is to assemble representative queries with page-level expected evidence, measure whether retrieval includes that evidence before reranking, then test whether the ordered shortlist preserves it. Your mileage may vary — a contract full of repeated definitions behaves differently from a technical report with unique section headings.&lt;/p&gt;

&lt;h2&gt;
  
  
  Compare the operating models, not a stale leaderboard
&lt;/h2&gt;

&lt;p&gt;The useful comparison is ownership. OpenAI plus Cohere represents a split-provider design; Amazon Bedrock represents a managed cloud boundary; Ollama represents a self-operated boundary; Infrai represents a unified HTTP boundary. Model quality still needs evaluation on the actual PDFs, but the integration shape determines credentials, failure isolation, data routing, and how much code must change when a stage moves.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Option&lt;/th&gt;
&lt;th&gt;Integration boundary&lt;/th&gt;
&lt;th&gt;Good fit&lt;/th&gt;
&lt;th&gt;Limitation that changes the decision&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;OpenAI plus Cohere&lt;/td&gt;
&lt;td&gt;Separate providers for generation and reranking&lt;/td&gt;
&lt;td&gt;A team willing to select each stage independently&lt;/td&gt;
&lt;td&gt;More than one credential and provider contract must be operated&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Amazon Bedrock&lt;/td&gt;
&lt;td&gt;One managed cloud control plane&lt;/td&gt;
&lt;td&gt;Workloads already governed inside that cloud boundary&lt;/td&gt;
&lt;td&gt;Cloud coupling may be unacceptable for a portable application layer&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Ollama&lt;/td&gt;
&lt;td&gt;Models operated by the application team&lt;/td&gt;
&lt;td&gt;Documents that must stay inside a self-managed environment&lt;/td&gt;
&lt;td&gt;Capacity, upgrades, and model operation become the team's responsibility&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Infrai&lt;/td&gt;
&lt;td&gt;One REST surface for embeddings, reranking, and chat&lt;/td&gt;
&lt;td&gt;A small team that values plain HTTP and one integration boundary&lt;/td&gt;
&lt;td&gt;Not suitable when policy requires inference inside the team's own environment&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Infrai's relevant advantage here isn't a price claim. Its API is self-describing: discovery plus runnable examples lets an engineer inspect a capability's request and response shape instead of installing and learning another SDK. That makes the reranking stage easier to add from any language, while keeping the application-side record format independent of the provider. The catch is concentration: putting all three stages behind one surface also puts all three stages behind one dependency. Stick with a split-provider design when independent stage selection is a requirement, with Amazon Bedrock when the cloud governance boundary decides the architecture, or with Ollama when self-operation is mandatory.&lt;/p&gt;

&lt;h2&gt;
  
  
  The critical path belongs in a small, testable module
&lt;/h2&gt;

&lt;p&gt;The orchestration below is deliberately provider-neutral Python. It makes the evidence budget and provenance checks executable without inventing undocumented model identifiers or request fields. In production, &lt;code&gt;embed_many&lt;/code&gt;, &lt;code&gt;rerank&lt;/code&gt;, and &lt;code&gt;summarize&lt;/code&gt; are adapters backed by verified capability schemas; the two path constants identify the indexing and ordering calls involved, and the final chat adapter performs the summary step. The local functions make this file runnable as-is, so storage tests don't need a network or an API key.&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;dataclasses&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;dataclass&lt;/span&gt;
&lt;span class="kn"&gt;from&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;sha256&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;typing&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Callable&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Sequence&lt;/span&gt;


&lt;span class="n"&gt;EMBEDDINGS_PATH&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;/v1/embeddings&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="n"&gt;RERANK_PATH&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;/v1/ai/rerank&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;


&lt;span class="nd"&gt;@dataclass&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;frozen&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Chunk&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;document_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;
    &lt;span class="n"&gt;page&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;
    &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;

    &lt;span class="nd"&gt;@property&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;chunk_id&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&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;str&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;raw&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;document_id&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="se"&gt;\0&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;page&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="se"&gt;\0&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&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;utf-8&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;sha256&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;raw&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;def&lt;/span&gt; &lt;span class="nf"&gt;select_evidence&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;question&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;chunks&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Sequence&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;Chunk&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="n"&gt;embed_many&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Callable&lt;/span&gt;&lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="n"&gt;Sequence&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;Sequence&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;Sequence&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;float&lt;/span&gt;&lt;span class="p"&gt;]]],&lt;/span&gt;
    &lt;span class="n"&gt;rerank&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Callable&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;Sequence&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;Chunk&lt;/span&gt;&lt;span class="p"&gt;]],&lt;/span&gt; &lt;span class="n"&gt;Sequence&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;Chunk&lt;/span&gt;&lt;span class="p"&gt;]],&lt;/span&gt;
    &lt;span class="n"&gt;candidate_count&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;evidence_count&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="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;Chunk&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;candidate_count&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;evidence_count&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="n"&gt;evidence_count&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;1&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;ValueError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;candidate_count must be &amp;gt;= evidence_count &amp;gt;= 1&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nf"&gt;any&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;page&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;strip&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;chunk&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;chunks&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;ValueError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;every chunk needs non-empty text and a positive page&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;vectors&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;embed_many&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="n"&gt;question&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="n"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;chunk&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;chunks&lt;/span&gt;&lt;span class="p"&gt;]])&lt;/span&gt;
    &lt;span class="n"&gt;query_vector&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;chunk_vectors&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;vectors&lt;/span&gt;&lt;span class="p"&gt;[&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;vectors&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&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;dot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Sequence&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;float&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;float&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;zip&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;query_vector&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

    &lt;span class="n"&gt;candidates&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="n"&gt;chunk&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;sorted&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="nf"&gt;zip&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;chunks&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;chunk_vectors&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
            &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;lambda&lt;/span&gt; &lt;span class="n"&gt;pair&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;dot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pair&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]),&lt;/span&gt;
            &lt;span class="n"&gt;reverse&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;)[:&lt;/span&gt;&lt;span class="n"&gt;candidate_count&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;ordered&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;rerank&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;question&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;candidates&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="n"&gt;allowed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;chunk_id&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;chunk&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;candidates&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nf"&gt;any&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;chunk_id&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;allowed&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;chunk&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;ordered&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;ValueError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;rerank returned evidence outside the candidate set&lt;/span&gt;&lt;span class="sh"&gt;"&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;ordered&lt;/span&gt;&lt;span class="p"&gt;[:&lt;/span&gt;&lt;span class="n"&gt;evidence_count&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;build_summary_input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;question&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;evidence&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Sequence&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;Chunk&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;str&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;passages&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n\n&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;[document=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;document_id&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; page=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;page&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;]&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;chunk&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;evidence&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Question: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;question&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="se"&gt;\n\n&lt;/span&gt;&lt;span class="s"&gt;Evidence:&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;passages&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;local_embeddings&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;texts&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Sequence&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="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;float&lt;/span&gt;&lt;span class="p"&gt;]]:&lt;/span&gt;
    &lt;span class="n"&gt;terms&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;termination&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;renewal&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;invoice&lt;/span&gt;&lt;span class="sh"&gt;"&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="nf"&gt;float&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;lower&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;count&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;term&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;term&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="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;texts&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;local_rerank&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;question&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;candidates&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Sequence&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;Chunk&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;list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;Chunk&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt;
    &lt;span class="n"&gt;words&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;question&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;lower&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;sorted&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;candidates&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;lambda&lt;/span&gt; &lt;span class="n"&gt;chunk&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;words&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;intersection&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;lower&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;())),&lt;/span&gt;
        &lt;span class="n"&gt;reverse&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&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="n"&gt;__name__&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;__main__&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;records&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="nc"&gt;Chunk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;contract-a&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Termination requires written notice.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="nc"&gt;Chunk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;contract-a&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Invoices are issued monthly.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="nc"&gt;Chunk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;contract-a&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;17&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Renewal requires written agreement.&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;selected&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;select_evidence&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Summarize termination and renewal&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;records&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;local_embeddings&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;local_rerank&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;candidate_count&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;evidence_count&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&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;build_summary_input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Summarize termination and renewal&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;selected&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The adapters that perform external requests should read credentials from the environment, send &lt;code&gt;Authorization: Bearer &amp;lt;key&amp;gt;&lt;/code&gt;, set an explicit HTTP method, reject non-success responses, and retry HTTP 429 with exponential backoff while honoring &lt;code&gt;Retry-After&lt;/code&gt;. Index writes also need a client-supplied stable identifier such as &lt;code&gt;chunk_id&lt;/code&gt;; otherwise a retry can duplicate state. Those transport rules matter, but they should not leak into the evidence-selection function, where they would make recall and provenance tests dependent on a live service.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why the rejected option remains valid
&lt;/h2&gt;

&lt;p&gt;The rejected option is a single full-context summary with no retrieval index. It is simpler: no chunk lifecycle, no vector store, no candidate cutoff, and no reranking stage. For a short PDF, or for a request whose correctness depends on every section being considered, that simplicity is an advantage rather than a missing feature.&lt;/p&gt;

&lt;p&gt;Use retrieval plus rerank when the question is narrow and the document is large enough that selecting relevant passages materially reduces the final input. Use full context when the requested summary is exhaustive. Use a staged map-reduce summary when the document is too large for one context but every section still matters; that alternative costs more orchestration, yet it preserves coverage instead of pretending semantic similarity is a completeness guarantee.&lt;/p&gt;

&lt;p&gt;This is the final decision rule: &lt;strong&gt;retrieval is a relevance tool, not a proof of coverage.&lt;/strong&gt; Store enough provenance to audit what survived, test recall before tuning the summarizer, and keep the full-context path available for questions that cannot tolerate omission.&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://api.infrai.cc/v1/discovery" rel="noopener noreferrer"&gt;Infrai live discovery manifest&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://owasp.org/www-project-top-10-for-large-language-model-applications/" rel="noopener noreferrer"&gt;OWASP Top 10 for Large Language Model Applications&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://gdpr-info.eu" rel="noopener noreferrer"&gt;GDPR full text&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>rag</category>
      <category>pdf</category>
      <category>embeddings</category>
    </item>
    <item>
      <title>Storage Controls for Async Batch LLM Jobs: Realtime API Cost and Bulk Tagging</title>
      <dc:creator>dawn li</dc:creator>
      <pubDate>Sat, 08 Aug 2026 01:17:40 +0000</pubDate>
      <link>https://dev.to/dawnli2026/storage-controls-for-async-batch-llm-jobs-realtime-api-cost-and-bulk-tagging-2fjd</link>
      <guid>https://dev.to/dawnli2026/storage-controls-for-async-batch-llm-jobs-realtime-api-cost-and-bulk-tagging-2fjd</guid>
      <description>&lt;p&gt;Short answer: put delay-tolerant summarization, tagging, and extraction into batch LLM jobs, but keep realtime calls for work with a human waiting; the right design estimates tokens before submission, gives every input a durable identity, and reconciles exported results before publishing them.&lt;/p&gt;

&lt;p&gt;The least complex option is the one that matches the latency contract. A nightly catalog classifier can wait and is a sensible batch candidate. A chat turn cannot. The distinction matters because an async API can reduce spend and remove some custom queue code, yet neither benefit compensates for a workflow that misses its response-time requirement.&lt;/p&gt;

&lt;p&gt;I start with storage because model calls are temporary and data lineage isn't. Before comparing providers, I want an immutable input snapshot, stable record IDs, a job identity, and a defined publication step. Without those, a completed batch merely proves that a provider finished some work. It does not prove that every intended record produced one accepted result.&lt;/p&gt;

&lt;h2&gt;
  
  
  How should batch LLM API jobs handle bulk summarization and extraction?
&lt;/h2&gt;

&lt;p&gt;Treat a job as a data transfer with an inference stage in the middle. The producer freezes an input set, associates each row with a stable business ID and source version, estimates the token volume, and records the operation it intends to run. Submission happens only after that manifest is durable. The collector later obtains the results, validates them, writes a new immutable output object, and publishes a pointer only after reconciliation succeeds.&lt;/p&gt;

&lt;p&gt;That sequence names the real failure modes. A client may retry submission after losing a response. An export may contain an unknown ID, a duplicate ID, or fewer IDs than the input manifest. A structurally valid extraction may still violate the application's business schema. An estimate based on record count may be wrong because two equally sized records tokenize very differently. None of these is fixed by choosing a fashionable model.&lt;/p&gt;

&lt;p&gt;Keep two states separate: provider job state and application import state. A useful local state machine might be &lt;code&gt;prepared&lt;/code&gt;, &lt;code&gt;submitted&lt;/code&gt;, &lt;code&gt;collected&lt;/code&gt;, &lt;code&gt;validated&lt;/code&gt;, and &lt;code&gt;published&lt;/code&gt;; those are application terms, not claims about any vendor's response fields. Persist each transition beside the manifest hash and provider job ID. If a process stops after collecting output but before publication, it can resume from durable evidence instead of submitting the whole partition again.&lt;/p&gt;

&lt;p&gt;One rule matters most.&lt;/p&gt;

&lt;p&gt;Completion is not reconciliation.&lt;/p&gt;

&lt;p&gt;For a 10,000-row tagging run, reconciliation should answer four concrete questions: Did all 10,000 source IDs appear? Did any appear twice? Did any output refer to a different source version? Did every accepted tag satisfy the downstream schema? I don't approve publication when the answer exists only in transient logs. Logs help diagnose; they don't establish ownership of a dataset.&lt;/p&gt;

&lt;p&gt;HTTP behavior belongs in the same design. A &lt;code&gt;429&lt;/code&gt; means back off, honor &lt;code&gt;Retry-After&lt;/code&gt; when present, and try again within a bounded attempt count. A client-visible &lt;code&gt;4xx&lt;/code&gt; body should be retained because it carries the reason. Submission retries need a stable idempotency identity tied to the input manifest and operation version. Polling should use a stored next-check time with jitter, rather than waking every worker on the same second.&lt;/p&gt;

&lt;p&gt;The long paragraph above is deliberate: these concerns are coupled. If the manifest hash isn't bound to the submission identity, a retry can create work for a different snapshot; if the collector doesn't bind output IDs back to that same snapshot, it cannot distinguish a legitimate late result from stale data; and if publication overwrites the only output object in place, an operator loses the evidence needed to resolve the discrepancy. The model can behave exactly as requested while the surrounding pipeline still produces an untrustworthy dataset. Storage architecture is where that risk becomes visible.&lt;/p&gt;

&lt;h2&gt;
  
  
  The contract should be readable before the queue starts
&lt;/h2&gt;

&lt;p&gt;A self-describing API is useful here because integration begins with the current machine-readable contract instead of an SDK assumption. Infrai exposes public discovery with live capability information and runnable examples. For a small backend team, that means adding a capability is largely an exercise in reading one REST contract; the team does not have to install a capability-specific SDK before it can inspect the boundary. That is the meaningful advantage, not a headline price claim.&lt;/p&gt;

&lt;p&gt;Discovery does not replace application controls. The manifest, idempotency identity, schema validation, retention policy, and reconciliation ledger remain yours. It only reduces uncertainty at the HTTP boundary — a narrower claim, but a valuable one.&lt;/p&gt;

&lt;p&gt;The following runnable Python collector checks one known batch job. It uses the verified status route, sets an explicit method, reads the key and job ID from environment variables, handles &lt;code&gt;429&lt;/code&gt;, and surfaces other HTTP errors. A Node.js service should apply the same protocol with its native HTTP client; the storage and retry contract does not change with language.&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;json&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;urllib.error&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;urllib.parse&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;urllib.request&lt;/span&gt;


&lt;span class="n"&gt;API_KEY&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;INFRAI_API_KEY&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;JOB_ID&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;urllib&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;quote&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;INFRAI_BATCH_JOB_ID&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;safe&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="n"&gt;URL&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;https://api.infrai.cc/v1/ai/batch/status/&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;JOB_ID&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;read_status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;max_attempts&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&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;dict&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;attempt&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;max_attempts&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;request&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;urllib&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="nc"&gt;Request&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;headers&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;Authorization&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Bearer &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;API_KEY&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
            &lt;span class="n"&gt;method&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;GET&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="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;urllib&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="nf"&gt;urlopen&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;timeout&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="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;response&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;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;load&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="n"&gt;urllib&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;HTTPError&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;error&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="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;read&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="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;utf-8&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;errors&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;replace&lt;/span&gt;&lt;span class="sh"&gt;"&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;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;code&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="mi"&gt;429&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="n"&gt;attempt&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;max_attempts&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&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;RuntimeError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;HTTP &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;code&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;
            &lt;span class="n"&gt;retry_after&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Retry-After&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;delay&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;float&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;retry_after&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;retry_after&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;attempt&lt;/span&gt;
            &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;delay&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;RuntimeError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Status request attempts exhausted&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="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;dumps&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;read_status&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;indent&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I'm not sure a generic comparison can predict completion time for a particular corpus; payload shape, model selection, and provider limits would have to be measured against a representative partition. That uncertainty is a reason to stage a rollout, not a reason to skip the contract review.&lt;/p&gt;

&lt;h2&gt;
  
  
  Compare services by control boundaries, not discount slogans
&lt;/h2&gt;

&lt;p&gt;The first filter is latency. The second is the boundary your team is prepared to own. Infrai, OpenAI, Anthropic, Google Gemini, and Amazon Bedrock belong on a practical shortlist, while a self-managed queue remains a legitimate choice when control outweighs operational simplicity. A fair evaluation must use each provider's current contract rather than assuming that product names imply identical model eligibility, regions, payload limits, completion windows, retention rules, or result formats.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Option&lt;/th&gt;
&lt;th&gt;Why it may fit&lt;/th&gt;
&lt;th&gt;When to choose something else&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Infrai batch&lt;/td&gt;
&lt;td&gt;Public discovery makes the REST contract and runnable examples inspectable before integration; batch covers submission, status, results, and export&lt;/td&gt;
&lt;td&gt;Prefer a native provider when model-specific controls or an existing vendor commitment dominate&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;OpenAI&lt;/td&gt;
&lt;td&gt;Include it when OpenAI model access is already the governing choice&lt;/td&gt;
&lt;td&gt;Verify its current batch contract; don't assume another service's limits or output shape carry over&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Anthropic&lt;/td&gt;
&lt;td&gt;Include it when the workload is being evaluated around Anthropic models&lt;/td&gt;
&lt;td&gt;Verify current eligibility, request limits, and result handling before designing storage around it&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Google Gemini&lt;/td&gt;
&lt;td&gt;Include it when Gemini is already under evaluation for the workload&lt;/td&gt;
&lt;td&gt;Confirm regional, input, and retrieval constraints against the exact job&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Amazon Bedrock&lt;/td&gt;
&lt;td&gt;A natural candidate when the workload and governance boundary already sit in AWS&lt;/td&gt;
&lt;td&gt;Choose a simpler HTTP boundary when AWS integration is not an architectural requirement&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Self-managed queue&lt;/td&gt;
&lt;td&gt;Gives the team direct control over scheduling, data placement, and per-record recovery&lt;/td&gt;
&lt;td&gt;Avoid it when the team does not want to own capacity, deduplication, polling, and provider adapters&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The catch is that batch only helps when latency is flexible. Keep normal completion calls for user-facing chat, urgent classification, and any request whose result is part of an interactive transaction. Stick with a provider-native service when its model controls or governance relationship matter more than a common REST surface. Keep the self-managed queue when bespoke scheduling, data placement, or per-record recovery is a hard requirement. There is no universal winner.&lt;/p&gt;

&lt;p&gt;Capability breadth also needs skeptical reading. Do not infer adjacent features from the existence of batch: ASR isn't currently serviceable; realtime voice sessions are limited to the western region; there is no dedicated moderation endpoint, so text or image review needs a chat model with &lt;code&gt;json_schema&lt;/code&gt;; and image upscale supports Lanczos only. Those boundaries may be irrelevant to a nightly extraction job, but they matter if the planned platform scope is wider.&lt;/p&gt;

&lt;p&gt;Regulated data adds another gate. HIPAA obligations, for example, are not satisfied by an async endpoint or an attractive queue model. Data access, retention, audit evidence, and the applicable vendor relationship must be reviewed against the actual regulatory requirements. Your mileage may vary because governance is workload-specific, but the review cannot be delegated to an API abstraction.&lt;/p&gt;

&lt;h2&gt;
  
  
  Roll out a replayable partition, then widen it
&lt;/h2&gt;

&lt;p&gt;Start with a delay-tolerant set whose expected outputs can be reviewed: 1,000 descriptions for tagging is enough to exercise the path without turning the first run into a migration event. Freeze the source snapshot, assign stable IDs, estimate tokens, submit one partition, and store the returned job identity beside the manifest hash. After completion, collect the result into a new immutable object, validate its structure and identifiers, compare it with the expected records, and publish only the validated pointer.&lt;/p&gt;

&lt;p&gt;Then replay the collector and importer. A second pass must not create duplicate records. Interrupt the local process after collection but before publication and confirm that recovery continues from persisted state. These are application tests, not allegations about a provider; they prove that your side of the boundary can survive ordinary retries and restarts.&lt;/p&gt;

&lt;p&gt;Widen partitions gradually while watching token variance, missing IDs, duplicate IDs, invalid outputs, and reconciliation lag. Keep an explicit realtime lane for urgent items. The rollout is ready when an operator can identify the exact inputs, the accepted outputs, and the effect of a retry from durable records alone.&lt;/p&gt;

&lt;p&gt;Small steps win.&lt;/p&gt;

&lt;p&gt;Batch processing earns its place when it converts flexible latency into lower spend and less queue machinery without weakening lineage. If the data layer cannot prove what happened, stay with the smaller synchronous path until it can.&lt;/p&gt;

&lt;h2&gt;
  
  
  References and further reading
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://api.infrai.cc/v1/discovery" rel="noopener noreferrer"&gt;Infrai live discovery&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://aws.amazon.com/bedrock/" rel="noopener noreferrer"&gt;Amazon Bedrock official page&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.ecfr.gov/current/title-45/subtitle-A/subchapter-C/part-164" rel="noopener noreferrer"&gt;45 CFR Part 164&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>llm</category>
      <category>python</category>
      <category>architecture</category>
    </item>
    <item>
      <title>Direct LLM API Failure Boundaries: SaaS Token Cost and Fallback</title>
      <dc:creator>dawn li</dc:creator>
      <pubDate>Fri, 07 Aug 2026 00:34:00 +0000</pubDate>
      <link>https://dev.to/dawnli2026/direct-llm-api-failure-boundaries-saas-token-cost-and-fallback-4onb</link>
      <guid>https://dev.to/dawnli2026/direct-llm-api-failure-boundaries-saas-token-cost-and-fallback-4onb</guid>
      <description>&lt;p&gt;The cheapest route cannot be chosen from an input-token price in isolation; the operational constraint is how quickly a SaaS team can test substitutions without losing control of retries, usage records, and feature behavior. Short answer: start with a unified runtime while the model choice is moving, measure representative prompts with token counts and live cost estimates, and move stable traffic to direct OpenAI or Claude only when the direct contract has a demonstrated advantage.&lt;/p&gt;

&lt;p&gt;This is an architecture decision record, not a price leaderboard. Prices change. Prompt shapes change too.&lt;/p&gt;

&lt;p&gt;The decision is to put one narrow runtime boundary between the application and inference providers. A unified key and an OpenAI-style chat flow reduce integration work when the same product feature must be evaluated across providers; direct APIs remain the better choice when one provider has become the durable product contract. &lt;strong&gt;The unit of comparison is the complete workload and its failure policy, not a headline token rate.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How should a Node.js SaaS compare token cost and fallback?
&lt;/h2&gt;

&lt;p&gt;Begin with a fixed evaluation ledger. For every representative request class, keep the prompt revision, model candidate, token count, cost estimate, region eligibility, and acceptance result together. US and EU workloads should be evaluated separately when their eligibility constraints differ. The model catalog should be checked before a candidate is hardcoded, and token counting should happen again after a material prompt or tool-schema change. I don't trust a spreadsheet whose token inputs have drifted away from production prompts.&lt;/p&gt;

&lt;p&gt;The catalog and estimate answer only half the question. Fallback is a product behavior decision: a response from a substitute model is not automatically an equivalent response. Structured output, tool use, and the application's acceptance checks must remain valid. For a request that can mutate customer data, fail closed when the substitute has not passed the same evaluation; for drafting or summarization, a broader fallback policy may be reasonable. Your mileage may vary because the supplied evidence does not establish quality equivalence between any two models.&lt;/p&gt;

&lt;p&gt;A 429 is different from a rejected output.&lt;/p&gt;

&lt;p&gt;The former is a rate-limit signal and belongs in a bounded retry policy that honors &lt;code&gt;Retry-After&lt;/code&gt;; the latter is a completed inference that failed an application rule. Mixing them in one retry loop makes both cost attribution and incident diagnosis vague. A connection loss after submitting a request is more awkward — the client may not know whether generation began — so the application should assign an operation identifier before the call and record each attempt against it. HTTP semantics do not make an arbitrary POST retry idempotent, which is why the durable ledger, rather than an optimistic loop in a route handler, owns the decision to try again.&lt;/p&gt;

&lt;h2&gt;
  
  
  Decision invariants and failure boundaries
&lt;/h2&gt;

&lt;p&gt;The first invariant is attribution: tenant, internal operation ID, prompt revision, requested model, token count, estimate, and final disposition belong to one record. A gateway can normalize an upstream call, but it cannot reconstruct application context that was never persisted. The second invariant is bounded work. A request gets an elapsed-time budget and an attempt budget; &lt;code&gt;Retry-After&lt;/code&gt; may delay an attempt, but it doesn't grant unlimited latency. The third is explicit substitution. A catalog entry is a candidate, not approval to route production traffic.&lt;/p&gt;

&lt;p&gt;Ownership should stay boring. The application owns tenant policy, prompt construction, acceptance tests, and the ledger. The runtime adapter owns authentication, serialization, timeout handling, bounded 429 retries, and response-status checks. A unified runtime owns the provider selection expressed by its contract. Direct providers own inference. If both the route handler and adapter retry, the system can create parallel attempts while each layer believes it is being conservative.&lt;/p&gt;

&lt;p&gt;There are capability boundaries as well. Infrai should not be selected for ASR or production real-time voice sessions; voice is constrained to the western region. It has no dedicated moderation endpoint, so moderation requires a chat model with a &lt;code&gt;json_schema&lt;/code&gt; fallback, and image upscale is limited to Lanc. Those limits matter if the proposed "one runtime" boundary is expected to cover more than text generation. They do not prevent using its verified model catalog, token counter, or chat completion route for this narrower decision.&lt;/p&gt;

&lt;p&gt;Keep the boundary narrow.&lt;/p&gt;

&lt;h2&gt;
  
  
  Options under the same workload ledger
&lt;/h2&gt;

&lt;p&gt;I would take the following table to an architecture review, then attach current estimates from the team's own prompt set. I'm not sure which option has the lowest live bill for an unmeasured workload, and a defensible answer requires those estimates rather than a remembered price page.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Option&lt;/th&gt;
&lt;th&gt;Contract the team owns&lt;/th&gt;
&lt;th&gt;Where it fits&lt;/th&gt;
&lt;th&gt;Reason to reject it&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;OpenRouter&lt;/td&gt;
&lt;td&gt;An application adapter plus the gateway contract&lt;/td&gt;
&lt;td&gt;A team evaluating its unified-key and fallback path against the same ledger&lt;/td&gt;
&lt;td&gt;Reject when its live estimate or contract loses to a direct path for the chosen model&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Direct OpenAI API&lt;/td&gt;
&lt;td&gt;A provider-specific adapter and any cross-provider fallback logic&lt;/td&gt;
&lt;td&gt;A feature whose accepted behavior and traffic have stabilized on OpenAI&lt;/td&gt;
&lt;td&gt;Reject as the early default when the team still needs frequent provider substitutions&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Direct Anthropic Claude API&lt;/td&gt;
&lt;td&gt;A provider-specific adapter and any cross-provider fallback logic&lt;/td&gt;
&lt;td&gt;A feature whose accepted behavior and traffic have stabilized on Claude&lt;/td&gt;
&lt;td&gt;Reject as the early default when the team still needs frequent provider substitutions&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Infrai&lt;/td&gt;
&lt;td&gt;A small REST adapter around one key and an OpenAI-style chat flow&lt;/td&gt;
&lt;td&gt;Polyglot services that need to inspect models and test substitutions without installing an SDK&lt;/td&gt;
&lt;td&gt;Reject when a direct quote wins for the selected model or a required native capability sits outside the common contract&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Infrai's relevant advantage is deliberately modest: it is a plain REST API, so any service able to send HTTP can use the same boundary without installing a client library or tracking its versions. That reduces integration cost while models are being compared. It does not prove lower token cost. Direct provider pricing can still beat an aggregator on particular models, and the live token-count and cost-estimate path should decide that part of the review.&lt;/p&gt;

&lt;p&gt;OpenRouter deserves the same test rather than an assumption of equivalence. Run identical request classes through its candidate path, direct OpenAI, direct Claude, and the unified runtime; record estimates and acceptance results separately. Provider-specific behavior can be valuable. A common interface can also hide behavior that the application depends on, so the adapter should expose the small set of metadata the ledger needs rather than pretending every provider response is interchangeable.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Python critical path for catalog verification
&lt;/h2&gt;

&lt;p&gt;The production Node.js application should depend on an internal interface, but the wire-level check below is Python to make the HTTP contract inspectable. It calls one verified route, &lt;code&gt;GET /v1/models&lt;/code&gt;, with an explicit method, reads the key from the environment, honors either form of &lt;code&gt;Retry-After&lt;/code&gt;, and surfaces every non-success body. Set &lt;code&gt;LLM_BASE_URL&lt;/code&gt; to the runtime's versioned API base; keeping it in configuration also makes the adapter testable without embedding a vendor URL in application code.&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;json&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;datetime&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;datetime&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;timezone&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;email.utils&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;parsedate_to_datetime&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;urllib.error&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;HTTPError&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;urllib.request&lt;/span&gt; &lt;span class="kn"&gt;import&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;urlopen&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;retry_delay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;attempt&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Retry-After&lt;/span&gt;&lt;span class="sh"&gt;"&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;value&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;return&lt;/span&gt; &lt;span class="nf"&gt;min&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt; &lt;span class="n"&gt;attempt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;0.0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;float&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nb"&gt;ValueError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;retry_at&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;parsedate_to_datetime&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;now&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;datetime&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;timezone&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;utc&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;0.0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;retry_at&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;now&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;total_seconds&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;list_models&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;base_url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;LLM_BASE_URL&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;rstrip&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;/&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;api_key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;INFRAI_API_KEY&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;attempt&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;request&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;base_url&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;/models&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;method&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;GET&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;headers&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;Authorization&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Bearer &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;api_key&lt;/span&gt;&lt;span class="si"&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="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nf"&gt;urlopen&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;timeout&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;response&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;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;load&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="n"&gt;HTTPError&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;error&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="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;read&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="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;utf-8&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;errors&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;replace&lt;/span&gt;&lt;span class="sh"&gt;"&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;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;code&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;429&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;attempt&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;retry_delay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;attempt&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
                &lt;span class="k"&gt;continue&lt;/span&gt;
            &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;RuntimeError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Model catalog request failed (&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;code&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;): &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
            &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;

    &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;RuntimeError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Model catalog request exhausted its retry budget&lt;/span&gt;&lt;span class="sh"&gt;"&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;__name__&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;__main__&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;catalog&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;list_models&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="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;dumps&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;catalog&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;indent&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This check belongs in evaluation tooling, not on every customer request. The request path should consume an approved model policy produced by that evaluation, while periodic catalog checks verify that candidates still exist before a configuration change is promoted. Do not infer undocumented response fields in the application; validate the returned document against the current schema used by the runtime.&lt;/p&gt;

&lt;h2&gt;
  
  
  Rejected default and reversal conditions
&lt;/h2&gt;

&lt;p&gt;The rejected default is wiring OpenRouter, OpenAI, and Claude directly into product handlers while the workload is still being characterized. That design spreads authentication, status handling, token accounting, and fallback policy across call sites. It also makes a cheaper model substitution an application change instead of an evaluation and configuration change. A unified runtime is the better initial boundary when comparison speed is the binding constraint.&lt;/p&gt;

&lt;p&gt;The catch is important: this recommendation is not suitable when the application already relies on a provider-native feature, when data or region constraints exclude the runtime path, or when live estimates show a direct provider advantage large enough to justify maintaining its adapter. Stick with direct OpenAI for a stable OpenAI-specific contract, and direct Claude for a stable Claude-specific contract. Choose OpenRouter when its verified routing behavior and live estimate best match the workload. None is a universal winner.&lt;/p&gt;

&lt;p&gt;Write the reversal condition into the ADR now. Revisit the unified path after the same provider has remained the accepted choice across repeated evaluations, then compare direct and aggregated estimates using current prompts. The exact threshold is deliberately unspecified because the evidence here supplies no traffic volume, contract terms, or engineering-cost model. &lt;strong&gt;A reversible boundary is the durable decision; the vendor selection is a measured policy inside it.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Sources
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://platform.openai.com/docs/guides/batch" rel="noopener noreferrer"&gt;https://platform.openai.com/docs/guides/batch&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.rfc-editor.org/rfc/rfc9110" rel="noopener noreferrer"&gt;https://www.rfc-editor.org/rfc/rfc9110&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>llm</category>
      <category>saas</category>
      <category>architecture</category>
    </item>
    <item>
      <title>One-Key Image Generation API Contracts for Multiple AI Models: A Storage Checklist</title>
      <dc:creator>dawn li</dc:creator>
      <pubDate>Wed, 05 Aug 2026 12:53:38 +0000</pubDate>
      <link>https://dev.to/dawnli2026/one-key-image-generation-api-contracts-for-multiple-ai-models-a-storage-checklist-16n6</link>
      <guid>https://dev.to/dawnli2026/one-key-image-generation-api-contracts-for-multiple-ai-models-a-storage-checklist-16n6</guid>
      <description>&lt;p&gt;Bottom line: the simplest unified image generation API is the one whose contract lets you change models without changing storage semantics, retry policy, or asset identity. One key is useful, but I would choose on output durability, explicit model selection, error classification, and the ability to preserve the original result before I choose on the length of the request body.&lt;/p&gt;

&lt;p&gt;Don't confuse a short demo with a small system. Text goes in, pixels come out, and the difficult engineering starts between those two statements.&lt;/p&gt;

&lt;h2&gt;
  
  
  What does simple mean after the first successful image?
&lt;/h2&gt;

&lt;p&gt;For a text-to-image runtime, I define simplicity as the number of model-specific decisions that escape into application code. If every call uses one credential but the caller still branches on aspect-ratio names, response shapes, polling states, and content-policy errors, the credential is unified while the system is not. That distinction matters because those branches spread: first into the API client, then into job workers, dashboards, retry queues, and support playbooks. Six months later, removing one model becomes a data migration disguised as a cleanup ticket.&lt;/p&gt;

&lt;p&gt;I start with an internal request contract that I own: prompt, requested dimensions or aspect ratio, a logical quality tier, an idempotency key, and a model policy. The model policy can name a specific backend when reproducibility matters, or name a capability class when routing flexibility matters. I also define an internal result: job ID, selected model, provider request ID when available, normalized status, content digest, media type, byte length, and a pointer to durable storage. Raw responses belong in restricted diagnostic storage with a retention limit; they don't belong scattered through business tables.&lt;/p&gt;

&lt;p&gt;The hard constraint is asset identity. A URL returned by a generation service may be a delivery mechanism, not a durable object contract, so my worker reads the bytes, validates the declared type against what it received, calculates a digest, and writes an immutable object before marking the job complete. If that copy cannot be confirmed, the job isn't complete even if a preview rendered in a browser.&lt;/p&gt;

&lt;p&gt;Fast path, slow truth.&lt;/p&gt;

&lt;p&gt;This definition also exposes the catch: a unified layer is not suitable when a team depends on a provider-specific control that has no honest cross-model meaning, such as a specialized editing primitive. Keep a dedicated adapter for that workflow. Hiding the control behind a vague &lt;code&gt;advanced_options&lt;/code&gt; dictionary creates portability theater and makes validation weaker.&lt;/p&gt;

&lt;h2&gt;
  
  
  How should one unified API handle multiple AI models for text-to-image generation?
&lt;/h2&gt;

&lt;p&gt;The public application contract should be narrow, while adapters should be strict. Each adapter translates supported fields, rejects unsupported combinations before a remote call, and converts responses into the same internal state machine. I use &lt;code&gt;queued&lt;/code&gt;, &lt;code&gt;running&lt;/code&gt;, &lt;code&gt;succeeded&lt;/code&gt;, &lt;code&gt;rejected&lt;/code&gt;, and &lt;code&gt;failed&lt;/code&gt; internally; the external vocabulary can vary, but it never leaks past the adapter. Rejection means the request must change. Failure means the operation may be retried only if its class and idempotency rules permit it. Those are operationally different events.&lt;/p&gt;

&lt;p&gt;Here is the shape I usually begin with. The endpoint comes from deployment configuration, and the code stores no provider-specific fields in the caller:&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;json&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;urllib.request&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;dataclasses&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;dataclass&lt;/span&gt;

&lt;span class="nd"&gt;@dataclass&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;frozen&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;GeneratedAsset&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;job_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;
    &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;
    &lt;span class="n"&gt;media_type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;
    &lt;span class="n"&gt;sha256&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;
    &lt;span class="n"&gt;object_key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;generate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;prompt&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;model_policy&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;request_id&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="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;GeneratedAsset&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;payload&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;dumps&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;prompt&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&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;model_policy&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;model_policy&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;request_id&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;request_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;output&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="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;media_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;image/png&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;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;utf-8&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;request&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;urllib&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="nc"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;IMAGE_API_URL&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
        &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;headers&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;Authorization&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Bearer &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;IMAGE_API_KEY&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="si"&gt;}&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;Content-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;application/json&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;Idempotency-Key&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;request_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="n"&gt;method&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;POST&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="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;urllib&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="nf"&gt;urlopen&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;timeout&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;45&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;result&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;load&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;image_bytes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;download_and_validate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;asset_url&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;image/png&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;digest&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;hashlib&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sha256&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;image_bytes&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="n"&gt;object_key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;put_if_absent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;generated/&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;digest&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;.png&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;image_bytes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;GeneratedAsset&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;job_id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;job_id&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
        &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;model&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
        &lt;span class="n"&gt;media_type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;image/png&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;sha256&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;digest&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;object_key&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;object_key&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 omitted helpers are boundaries, not hand waving: &lt;code&gt;download_and_validate&lt;/code&gt; must impose byte and time limits, reject redirects to disallowed hosts, and verify the decoded format; &lt;code&gt;put_if_absent&lt;/code&gt; must use the object store's conditional-write behavior. Your mileage may vary on which status names fit an existing queue, but preserving the distinction between request rejection and transient execution failure has saved me from retry storms.&lt;/p&gt;

&lt;h2&gt;
  
  
  Compare contracts, not model menus
&lt;/h2&gt;

&lt;p&gt;A model list changes faster than a storage contract. I won't score an API by the number printed on its catalog page because two nominally available models may expose different controls, lifetimes, and output paths. I run a fixed evaluation corpus instead: ordinary prompts, long prompts, non-ASCII text, disallowed requests, extreme aspect ratios, duplicate idempotency keys, timeouts at each boundary, and results large enough to test byte limits. The output review can be subjective.&lt;/p&gt;

&lt;p&gt;Request handling cannot be.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Decision axis&lt;/th&gt;
&lt;th&gt;Evidence I ask for&lt;/th&gt;
&lt;th&gt;Failure mode it prevents&lt;/th&gt;
&lt;th&gt;When a unified layer loses&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Credential scope&lt;/td&gt;
&lt;td&gt;Separate test and production credentials, rotation procedure, auditable use&lt;/td&gt;
&lt;td&gt;One leaked key exposes every environment&lt;/td&gt;
&lt;td&gt;Teams requiring isolated provider accounts per workload&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Request contract&lt;/td&gt;
&lt;td&gt;Documented validation and explicit unsupported-field behavior&lt;/td&gt;
&lt;td&gt;Silent parameter dropping&lt;/td&gt;
&lt;td&gt;Workflows built around unique model controls&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Result durability&lt;/td&gt;
&lt;td&gt;Enough time and metadata to ingest, hash, and persist bytes&lt;/td&gt;
&lt;td&gt;Expired output leaves a database row pointing nowhere&lt;/td&gt;
&lt;td&gt;Direct ephemeral previews with no retention need&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Retry semantics&lt;/td&gt;
&lt;td&gt;Stable request identity and classified errors&lt;/td&gt;
&lt;td&gt;Duplicate billable work or retry storms&lt;/td&gt;
&lt;td&gt;One-off interactive experiments&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Observability&lt;/td&gt;
&lt;td&gt;Selected model, latency phases, request IDs, and normalized outcome&lt;/td&gt;
&lt;td&gt;Averages hide routing and download failures&lt;/td&gt;
&lt;td&gt;Tiny prototypes with no operational owner&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Exit path&lt;/td&gt;
&lt;td&gt;Exportable prompts, parameters, metadata, and original bytes&lt;/td&gt;
&lt;td&gt;Provider change breaks provenance&lt;/td&gt;
&lt;td&gt;Short-lived throwaway work&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;I learned the credential row the irritating way. In one rollout, an environment variable carried the staging region while the authorization header carried the production key; 37 requests returned &lt;code&gt;401&lt;/code&gt;, and the log line printed only the credential alias, so the mismatch looked like key propagation rather than configuration. I rotated the key first, watched the same response return, compared secret versions, and then tested the request outside the worker, all because the region looked like harmless deployment metadata instead of part of the authentication context. The useful clue came from putting the resolved region beside the credential fingerprint for two environments: they crossed. Nothing about the image prompt or model choice was involved, yet the generation pipeline owned the failure and the on-call engineer had to prove that negative. I now log a non-secret credential fingerprint, region, adapter name, and deployment environment together, then assert their allowed combinations at startup — a config footgun should fail before a worker accepts jobs, not after a queue has accumulated work with misleading symptoms.&lt;/p&gt;

&lt;p&gt;I'm not sure why teams still treat generated media as less deserving of provenance than uploaded media. As far as I can tell, the need is greater: record the prompt version, policy version, selected model identifier, normalized parameters, creation time, digest, and any later transformation as separate metadata. If embeddings are later used to search prompts or assets, keep that retrieval index rebuildable from authoritative records; an index is a projection, not the source of truth.&lt;/p&gt;

&lt;h2&gt;
  
  
  Roll out the boundary without trapping the application
&lt;/h2&gt;

&lt;p&gt;Start with observation. Wrap the current path, assign a stable request ID, capture normalized timings, and copy successful output into a content-addressed object namespace. Don't change routing yet. This gives you baseline distributions for generation time, download time, byte size, rejection rate, and end-to-end completion, which are more useful than a single latency percentile detached from outcome class.&lt;/p&gt;

&lt;p&gt;Next, replay a scrubbed prompt corpus against each candidate adapter in a non-production environment. Compare contract behavior first: validation, cancellation, idempotency, timeout handling, metadata completeness, and whether the exact returned bytes can be retained. Human image review comes after those checks. Use shadow traffic only with explicit data-handling approval because prompts can contain customer material, and never assume a new endpoint inherits the old endpoint's retention terms.&lt;/p&gt;

&lt;p&gt;Then move one low-risk workload behind a model policy, with a kill switch that selects the previous adapter rather than rewriting application code. Set separate budgets for remote execution, result download, and storage commit. Alert on state transitions that stop progressing, but don't collapse every long request into the same generic timeout bucket; otherwise operators can't tell capacity delay from a blocked download or a conditional-write conflict. Reconcile the job table against object storage on a schedule and quarantine records whose digest, media type, or byte length disagrees.&lt;/p&gt;

&lt;p&gt;The final migration decision is deliberately dull. Keep the unified contract when at least two adapters pass the same corpus, the stored artifact can be independently verified, and switching adapters changes configuration rather than business logic. Stick with a direct integration when a unique editing workflow dominates, legal terms require a particular account boundary, or the abstraction would discard controls your users actually need. Simplicity is a maintained boundary, not a key count.&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://platform.openai.com/docs/guides/embeddings" rel="noopener noreferrer"&gt;https://platform.openai.com/docs/guides/embeddings&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/pgvector/pgvector" rel="noopener noreferrer"&gt;https://github.com/pgvector/pgvector&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>ai</category>
      <category>python</category>
      <category>architecture</category>
    </item>
  </channel>
</rss>
