<?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: TitanJ53</title>
    <description>The latest articles on DEV Community by TitanJ53 (@titanj53).</description>
    <link>https://dev.to/titanj53</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%2F4072156%2F35d22277-daa2-4611-a9a6-2b5b76e8be99.png</url>
      <title>DEV Community: TitanJ53</title>
      <link>https://dev.to/titanj53</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/titanj53"/>
    <language>en</language>
    <item>
      <title>Rubric Scoring and Chatbot Safety: Two LLM JSON Schema Shapes, No Moderation Endpoint</title>
      <dc:creator>TitanJ53</dc:creator>
      <pubDate>Sun, 16 Aug 2026 02:29:02 +0000</pubDate>
      <link>https://dev.to/titanj53/rubric-scoring-and-chatbot-safety-two-llm-json-schema-shapes-no-moderation-endpoint-2j9n</link>
      <guid>https://dev.to/titanj53/rubric-scoring-and-chatbot-safety-two-llm-json-schema-shapes-no-moderation-endpoint-2j9n</guid>
      <description>&lt;p&gt;Use two model calls, not one. If your in-app chatbot both talks to a candidate and scores their answers against a job rubric, the safety verdict and the rubric score belong in separate JSON schema responses — even when the API you picked has no dedicated moderation endpoint. Basic moderation through an LLM and a strict schema is a legitimate design. Folding it into the same structured output as the score is the part that quietly costs you six months later.&lt;/p&gt;

&lt;h2&gt;
  
  
  The constraint: a rubric score is a record, not a chat bubble
&lt;/h2&gt;

&lt;p&gt;The system worth reasoning about here is an edtech interview-practice product. A learner chats with a bot about a role they're targeting, the bot walks the rubric questions, and the model returns four scores from 0 to 3 — communication, evidence, role fit, follow-up — plus a short quote from the learner as justification for each one.&lt;/p&gt;

&lt;p&gt;That JSON row is not a chat bubble. It lands in the learner's profile, it feeds a coach dashboard, and in the partner tier it can be exported to the employer sponsoring the cohort.&lt;/p&gt;

&lt;p&gt;So the object under compliance pressure is the record, not the conversation: retention windows, subject access requests, and the plain fact that anything you store gets read later, out of context, by someone who wasn't in the session.&lt;/p&gt;

&lt;p&gt;Chat scrolls away. Rows don't.&lt;/p&gt;

&lt;p&gt;That gives you the constraint everything else has to satisfy — every unit of text entering the record carries a verdict computed on exactly that text, and the component that produced the text is not the only authority on whether it was safe to keep. Anyone who has run production email will recognise the reflex: you don't let the sending application declare its own message legitimate, you sign it so the receiving side can verify independently. Same instinct, different pipe.&lt;/p&gt;

&lt;h2&gt;
  
  
  What does a safe in-app chatbot need beyond one LLM JSON call?
&lt;/h2&gt;

&lt;p&gt;Three invariants, and they're short:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Every stored turn carries a verdict computed on the exact text that got stored.&lt;/li&gt;
&lt;li&gt;An unparseable or schema-invalid verdict fails closed into a review queue. It never defaults to allow.&lt;/li&gt;
&lt;li&gt;Schema-valid is not the same as correct.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That third one is where the edge cases live. A model can hand you a structurally perfect object with &lt;code&gt;decision&lt;/code&gt; set to &lt;code&gt;SAFE&lt;/code&gt; when your enum says &lt;code&gt;safe&lt;/code&gt;, or a confidence of 1.0 on every turn for a week, and your parser will nod along at both. The nastier case is specific to rubric scoring: the evidence field. You asked the model to quote the candidate to justify a score, so that quote can carry the exact text your screening step existed to keep out — through a field nobody thought to screen, into the coach's inbox and the export. It is the same shape as the classic email header problem: the body gets sanitised, a templating engine then pastes an unsanitised display name into a header, and the filter never sees it because it was pointed at the wrong field. Treat the scoring payload as untrusted input, the way you'd treat the body of an inbound message rather than the output of your own service.&lt;/p&gt;

&lt;p&gt;Whatever you build on, screening should be the least exotic thing in the stack: one HTTP request, a JSON schema, a small model behind it. Infrai is one option that fits that shape — a plain REST API you call over HTTP with no SDK to install — so the screening call stays the same dozen lines whether it runs against the model doing your scoring or a different one entirely. An OpenAI-compatible chat surface is worth more here than a vendor-specific safety switch you'd have to re-learn per provider.&lt;/p&gt;

&lt;h2&gt;
  
  
  Two shapes, and the invariant each one buys
&lt;/h2&gt;

&lt;p&gt;Shape A fuses everything into one call: one prompt, one schema holding both a &lt;code&gt;safety&lt;/code&gt; object and a &lt;code&gt;rubric&lt;/code&gt; object. The invariant is real — verdict and score can never drift apart, because they came from one read of one text, and there is exactly one response to log. For a junior developer shipping the first version of an in-app chatbot, that's the honest recommendation, and I'd rather see it shipped than a half-built pipeline.&lt;/p&gt;

&lt;p&gt;The cost shows up in three places. A malformed response loses the verdict and the score together, so your retry re-rolls a score a human may already have looked at. The model being helpful is also the model grading its own helpfulness, which is not an independent signal in any sense a reviewer would accept. And you can't pin a small, strict, boring model to classification while a stronger one handles the rubric, because there's only one call to pin.&lt;/p&gt;

&lt;p&gt;Shape B puts a screening pass in front, and re-checks the narrow payload that is about to be written. Two calls, two schemas, two idempotency keys. The screening schema stays tiny — a decision enum, a category enum, a confidence — small enough that structured output is nearly always well formed, which is exactly what you want from the component whose job is to fail closed. Each stage retries on its own budget, and each can sit on a different model.&lt;/p&gt;

&lt;p&gt;The rule I'd apply: if the structured output leaves the session — stored, exported, or read by a coach or an employer — take Shape B. If the transcript is ephemeral and a human reads every turn anyway, Shape A is enough, and the boundary can move later without a rewrite.&lt;/p&gt;

&lt;h2&gt;
  
  
  Which API details actually decide this
&lt;/h2&gt;

&lt;p&gt;Once the shape is settled, the API question narrows to four things: how you call it, how structured output is enforced, whether a dedicated moderation endpoint exists at all, and what you are left owning.&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;Structured output&lt;/th&gt;
&lt;th&gt;Dedicated moderation endpoint&lt;/th&gt;
&lt;th&gt;What you still own&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;REST or official SDKs&lt;/td&gt;
&lt;td&gt;JSON schema, strict mode&lt;/td&gt;
&lt;td&gt;Yes, a free classifier&lt;/td&gt;
&lt;td&gt;Its categories, which are not your rubric&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Anthropic (Claude)&lt;/td&gt;
&lt;td&gt;REST or official SDKs&lt;/td&gt;
&lt;td&gt;Tool-shaped JSON output&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;The whole screening prompt&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Google Gemini&lt;/td&gt;
&lt;td&gt;REST or official SDKs&lt;/td&gt;
&lt;td&gt;responseSchema on the request&lt;/td&gt;
&lt;td&gt;No — request-level safety filters instead&lt;/td&gt;
&lt;td&gt;Filters guard the call, not the stored row&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;OpenRouter&lt;/td&gt;
&lt;td&gt;REST gateway across vendors&lt;/td&gt;
&lt;td&gt;JSON schema where the model supports it&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;Behaviour drift as you switch models&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Infrai&lt;/td&gt;
&lt;td&gt;Plain REST, OpenAI-compatible&lt;/td&gt;
&lt;td&gt;JSON schema on the chat surface&lt;/td&gt;
&lt;td&gt;No — it doesn't offer one&lt;/td&gt;
&lt;td&gt;The screening prompt, same as above&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Only OpenAI ships a dedicated moderation endpoint in that list, and it is genuinely good at the categories it publishes. Its taxonomy is fixed, though: it will tell you a message looks like harassment, and it will say nothing at all about a candidate pasting a colleague's home address into an interview answer, which is the case an edtech record actually has to survive.&lt;/p&gt;

&lt;p&gt;Fixed taxonomies and local rubrics rarely line up.&lt;/p&gt;

&lt;p&gt;Here is the screening half of Shape B. It's an ordinary POST to &lt;code&gt;/v1/chat/completions&lt;/code&gt; with a &lt;code&gt;json_schema&lt;/code&gt; response format, so the same code runs against any OpenAI-compatible base URL.&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="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;time&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;uuid&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="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;APIStatusError&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="c1"&gt;# ifr_... , never a literal in source
&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="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;DECISIONS&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;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="n"&gt;SCREEN_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;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;screen_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="nf"&gt;list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;DECISIONS&lt;/span&gt;&lt;span class="p"&gt;)},&lt;/span&gt;
            &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;category&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;none&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;harassment&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;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;personal_data&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;other&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;confidence&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;number&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;category&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;confidence&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;HOLD&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;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;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;category&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;other&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;confidence&lt;/span&gt;&lt;span class="sh"&gt;"&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="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;screen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;turn_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="n"&gt;turn_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="nb"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;Classify one candidate turn. Anything unexpected lands in the review queue.&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="mi"&gt;4&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;res&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;glm-4-flashx&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="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Classify the candidate turn for an interview-practice app. &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
                                &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Return the verdict only. Never quote the turn back.&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;turn_text&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="n"&gt;SCREEN_SCHEMA&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
                &lt;span class="n"&gt;temperature&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="n"&gt;extra_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;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;turn_id&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;   &lt;span class="c1"&gt;# same id on every retry
&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;APIStatusError&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;err&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;err&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="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;float&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&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;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;raise&lt;/span&gt;                                  &lt;span class="c1"&gt;# 4xx bodies carry the reason; let it surface
&lt;/span&gt;
        &lt;span class="n"&gt;verdict&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;loads&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;res&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="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;verdict&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="ow"&gt;not&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;DECISIONS&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;   &lt;span class="c1"&gt;# schema-valid, still not usable
&lt;/span&gt;            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;HOLD&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;verdict&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;HOLD&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="nf"&gt;screen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;I led the migration for a team of six.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;uuid&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;uuid4&lt;/span&gt;&lt;span class="p"&gt;())))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two details there matter more than the schema. The idempotency key is constant across retries, so a screening call replayed after a 429 cannot produce two different verdicts for one turn — the same discipline you'd use on an OTP send, where a duplicate is worse than a delay. And the enum is re-checked in Python after parsing, because strict schema enforcement is a property of the model you routed to, and your review queue should not depend on which one that was today.&lt;/p&gt;

&lt;p&gt;For a small edtech team running both calls itself, Infrai is worth trying for the screening pass in particular, because one key covers the screening call and the scoring call and each response carries its own cost, vendor and latency metadata, which makes the safety overhead per turn attributable without standing up a second telemetry path. If that boundary matches your system, the gateway walkthrough at &lt;a href="https://docs.infrai.cc/en/guides/ai/answers/best-cheap-llm-api-gateway-2025-one-key-openai-claude-g/" rel="noopener noreferrer"&gt;https://docs.infrai.cc/en/guides/ai/answers/best-cheap-llm-api-gateway-2025-one-key-openai-claude-g/&lt;/a&gt; is a reasonable place to start.&lt;/p&gt;

&lt;h2&gt;
  
  
  Rolling it out without a rewrite
&lt;/h2&gt;

&lt;p&gt;Ship the screening call as an observer first. Log its verdict next to the fused verdict you already have, act on neither, and let it run on live traffic for a week or two; the disagreements are the interesting data, and they will tell you whether your category enum matches what learners actually type. Then flip enforcement on for the write path only — the record — and leave the chat bubble alone until you have a reason.&lt;/p&gt;

&lt;p&gt;Before pinning a model to the screening role, count tokens on a real week of turns with &lt;code&gt;POST /v1/ai/tokens/count&lt;/code&gt; against the same catalogue you'll call. Screening runs on every turn; scoring runs only on submissions. That ratio, not the headline model quality, is what decides which model you can afford in the hot path.&lt;/p&gt;

&lt;p&gt;The catch is worth stating plainly: a chat model asked to classify is a general-purpose classifier with no published category list, no version you can pin, and no threshold calibrated for your population. If a compliance reviewer needs to point at a documented policy and a stable taxonomy, stick with a dedicated moderation endpoint for screening — OpenAI's is one HTTP call away — and keep the chat API for the rubric. Infrai doesn't offer a dedicated moderation endpoint either, and neither do most gateway-shaped options; that is a boundary to design around, not a surprise to hit in review.&lt;/p&gt;

&lt;p&gt;I'm not sure the split ever pays for itself in a pure support chatbot, to be fair. One call, a human on the other end, done. It's the record that changes the arithmetic: score a person, store the score, show it to someone who can act on it, and screening stops being an optimisation.&lt;/p&gt;

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

&lt;ul&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 LLM Applications&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://platform.openai.com/docs/guides/moderation" rel="noopener noreferrer"&gt;OpenAI moderation guide&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://ai.google.dev/gemini-api/docs/safety-settings" rel="noopener noreferrer"&gt;Gemini API safety settings&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.anthropic.com/en/docs/welcome" rel="noopener noreferrer"&gt;Anthropic developer documentation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://openrouter.ai/docs" rel="noopener noreferrer"&gt;OpenRouter documentation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://json-schema.org/" rel="noopener noreferrer"&gt;JSON Schema specification&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://api.infrai.cc/v1/discovery" rel="noopener noreferrer"&gt;Infrai discovery manifest&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>llm</category>
      <category>api</category>
      <category>moderation</category>
      <category>python</category>
    </item>
    <item>
      <title>Structured Code Review Answers — Citation Schemas for Semantic Search</title>
      <dc:creator>TitanJ53</dc:creator>
      <pubDate>Fri, 14 Aug 2026 02:17:14 +0000</pubDate>
      <link>https://dev.to/titanj53/structured-code-review-answers-citation-schemas-for-semantic-search-3le6</link>
      <guid>https://dev.to/titanj53/structured-code-review-answers-citation-schemas-for-semantic-search-3le6</guid>
      <description>&lt;p&gt;A game code-review assistant has a stricter job than producing plausible prose: every finding must fit the consumer's contract and point to retrieved evidence. The choice follows from that constraint. Use semantic search to assemble a bounded evidence set, ask the chat completion for a structured answer, validate the answer against a closed schema, and reject any citation that cannot be resolved back to that set. A fluent answer is never allowed to repair a broken contract.&lt;/p&gt;

&lt;p&gt;This is an architecture decision record for correctness, not a model comparison. The same boundary works in a Node.js service even though the critical-path example below is Python: retrieval, generation, validation, and evidence resolution remain separate ports.&lt;/p&gt;

&lt;h2&gt;
  
  
  What must stay true?
&lt;/h2&gt;

&lt;p&gt;The primary invariant is mechanical: a successful review is a typed collection of findings, and each finding has a stable identifier, severity, explanation, and one or more citation identifiers. The model doesn't get to invent another severity, omit evidence, or return commentary beside the object. Downstream systems should be able to sort findings, annotate a pull request, or trigger a human review without scraping prose.&lt;/p&gt;

&lt;p&gt;The evidence invariant is narrower. A citation identifies one retrieved chunk supplied to the completion request. It does not identify a URL guessed by the model, a document title copied from memory, or an offset that the application cannot reproduce. Keep chunk metadata outside the generated answer, then join citation identifiers against that trusted map after schema validation. That small separation matters — the answer can quote evidence, but it cannot define what counts as evidence.&lt;/p&gt;

&lt;p&gt;The abstention invariant prevents a clean JSON object from becoming a cleanly packaged hallucination. The result has a status such as &lt;code&gt;answered&lt;/code&gt; or &lt;code&gt;insufficient_evidence&lt;/code&gt;; an answered result needs at least one valid finding, while an abstention carries no findings and states which evidence was missing. I'm not sure a universal retrieval score can decide that boundary across every game repository. Your mileage may vary because generated engine files, gameplay scripts, and security rules have very different vocabulary. Calibrate the threshold with labeled review cases, but keep the abstention shape fixed.&lt;/p&gt;

&lt;p&gt;One more rule: untrusted repository text is data. A comment that says to ignore the review policy must not become an instruction. Delimit retrieved chunks, identify their source, and tell the completion layer to use them only as evidence. The prompting guide is useful background for designing that separation, while the enforcement still belongs in application code.&lt;/p&gt;

&lt;h2&gt;
  
  
  How should semantic search and chat completions produce structured answers with citations?
&lt;/h2&gt;

&lt;p&gt;The critical path has four boundaries. Search selects candidate chunks; optional reranking reorders those candidates for the review question; the completion produces only the requested object; deterministic code validates both shape and evidence membership. Reranking is a relevance step, not a citation verifier. The Cohere overview describes reranking as sorting documents by relevance to a query, which is why the architecture keeps identity and authorization checks elsewhere.&lt;/p&gt;

&lt;p&gt;For a concrete gaming example, imagine a change to an item-trading handler. The patch moves inventory before writing the transaction record, and the review question asks whether ownership checks and idempotency still hold. Search retrieves four candidates: the changed handler as &lt;code&gt;ev_01&lt;/code&gt;, the current trading policy as &lt;code&gt;ev_02&lt;/code&gt;, an idempotency test as &lt;code&gt;ev_03&lt;/code&gt;, and a general inventory guide as &lt;code&gt;ev_04&lt;/code&gt;. Their repository paths, revisions, and line ranges stay in a server-owned evidence map. The completion receives the opaque IDs and chunk text, then proposes a high-severity finding citing &lt;code&gt;ev_01&lt;/code&gt; and &lt;code&gt;ev_03&lt;/code&gt;. Schema validation confirms the finding's fields, but that is only the first gate. The resolver proves both IDs belong to this exact retrieval bundle; a policy check can then require the ownership claim to cite &lt;code&gt;ev_02&lt;/code&gt; as well. If the answer instead cites &lt;code&gt;ev_09&lt;/code&gt;, the entire candidate is rejected even if its explanation sounds right. If retrieval never found the trading policy, the correct result is &lt;code&gt;insufficient_evidence&lt;/code&gt;, not a confident guess based on the handler's variable names. This example also shows why repository revision belongs in trusted metadata: a citation to yesterday's policy may resolve as an ID while still describing the wrong review snapshot. Pin the evidence bundle to the commit under review, preserve it for replay, and publish displayable paths only after every check succeeds.&lt;/p&gt;

&lt;p&gt;No evidence, no finding.&lt;/p&gt;

&lt;p&gt;The order is deliberate. Validate syntax and schema before resolving citations, because malformed output has no trustworthy fields. Resolve every citation before publishing any finding, because one dangling ID makes the result internally inconsistent. Then apply semantic policies that a basic schema cannot express cleanly: &lt;code&gt;answered&lt;/code&gt; requires findings, &lt;code&gt;insufficient_evidence&lt;/code&gt; forbids them, and duplicate findings should not create duplicate review comments. Don't silently drop the bad item and publish the rest.&lt;/p&gt;

&lt;p&gt;Partial acceptance hides failure.&lt;/p&gt;

&lt;p&gt;A Node.js implementation can express the same answer contract with its preferred JSON Schema validator. Keep the provider adapter behind a narrow &lt;code&gt;complete(evidence, schema)&lt;/code&gt; interface, and keep validation in the caller. This prevents a chat completions SDK response type from leaking into the review domain. It also makes replay tests cheap: store the bounded input object and candidate output, then run the deterministic validators without calling search or generation again.&lt;/p&gt;

&lt;h2&gt;
  
  
  Decision options and failure boundaries
&lt;/h2&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;Structured output behavior&lt;/th&gt;
&lt;th&gt;Citation behavior&lt;/th&gt;
&lt;th&gt;Best fit&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;Free-form answer plus parsing&lt;/td&gt;
&lt;td&gt;Parser infers fields from prose&lt;/td&gt;
&lt;td&gt;Usually inferred from text or links&lt;/td&gt;
&lt;td&gt;Internal exploration where no machine action follows&lt;/td&gt;
&lt;td&gt;Formatting drift can become a runtime parsing failure&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Schema-constrained answer only&lt;/td&gt;
&lt;td&gt;Shape is explicit and closed&lt;/td&gt;
&lt;td&gt;Citation strings may still be unresolvable&lt;/td&gt;
&lt;td&gt;Workflows whose facts come entirely from trusted inputs&lt;/td&gt;
&lt;td&gt;Valid JSON does not prove grounding&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Schema plus evidence resolution&lt;/td&gt;
&lt;td&gt;Shape is validated, then cross-field rules run&lt;/td&gt;
&lt;td&gt;Every ID must join to the retrieved evidence map&lt;/td&gt;
&lt;td&gt;Automated code-review findings and audit trails&lt;/td&gt;
&lt;td&gt;More application code and stricter rejection behavior&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Extractive spans only&lt;/td&gt;
&lt;td&gt;Output stays close to source text&lt;/td&gt;
&lt;td&gt;Offsets or chunk IDs are direct&lt;/td&gt;
&lt;td&gt;Search interfaces that primarily surface passages&lt;/td&gt;
&lt;td&gt;Weak fit for synthesized review reasoning&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The decision is the third option. Its extra application code buys a failure boundary that an operator can explain. Invalid JSON is a generation failure. A schema mismatch is a contract failure. An unknown citation is a grounding failure. Too little relevant evidence is an abstention. Those outcomes should have separate counters rather than one generic quality metric, much as SMS acceptance, carrier delivery, and OTP verification are different events. I've learned from OTP delivery gaps that an accepted request isn't proof of delivery; a schema-valid answer isn't proof of support either.&lt;/p&gt;

&lt;p&gt;Use client errors precisely at the service boundary. For example, a caller request that fails its input contract can return &lt;code&gt;422&lt;/code&gt;, while an internally rejected model candidate should become a typed review outcome or a controlled retry, not a fabricated success. This distinction keeps malformed caller data out of quality dashboards and keeps model-contract failures visible to operators. Avoid logging full proprietary source chunks. Record evidence IDs, repository revision, validator codes, latency, token usage if available, and the final status; apply the same retention and access controls used for code-review artifacts.&lt;/p&gt;

&lt;p&gt;Cost belongs in the decision, but not as a headline. Candidate count, chunk size, reranking, retry policy, and completion length all consume resources. Measure them per accepted finding and per abstention. A pipeline that retries malformed output three times may look accurate in a demo while behaving badly under pull-request bursts. Set a hard attempt limit and a total deadline, then send unresolved cases to human review. Slow down. A gaming release branch doesn't need an automated comment badly enough to justify duplicate or unsupported findings.&lt;/p&gt;

&lt;h2&gt;
  
  
  The critical path in Python
&lt;/h2&gt;

&lt;p&gt;The code below concentrates on the part that must remain deterministic. &lt;code&gt;search&lt;/code&gt; and &lt;code&gt;complete&lt;/code&gt; are injected ports; neither is allowed to publish directly. The schema is represented as a Python dictionary so the example stays in one language. A production validator should implement the full contract rather than the intentionally small checks shown here.&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;Callable&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="n"&gt;TypedDict&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;TypedDict&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;finding_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;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="n"&gt;explanation&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;citations&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="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ReviewAnswer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;TypedDict&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;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;answered&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;insufficient_evidence&lt;/span&gt;&lt;span class="sh"&gt;"&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;missing_evidence&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="n"&gt;ANSWER_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;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;status&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;findings&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;missing_evidence&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;status&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;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;answered&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;insufficient_evidence&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;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="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;missing_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="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="p"&gt;},&lt;/span&gt;
&lt;span class="p"&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;Evidence&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;evidence_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;path&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;revision&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;text&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;validate_review&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;answer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;ReviewAnswer&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="nb"&gt;list&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="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;allowed_ids&lt;/span&gt; &lt;span class="o"&gt;=&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="n"&gt;evidence_id&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;evidence&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;required_keys&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;status&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;findings&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;missing_evidence&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;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;answer&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;required_keys&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;review answer has unexpected or missing fields&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;answer&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&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;answered&lt;/span&gt;&lt;span class="sh"&gt;"&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;answer&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;findings&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;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;answered review must contain a finding&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;answer&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&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;insufficient_evidence&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;answer&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;findings&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;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;abstained review cannot contain findings&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;finding&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;answer&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;findings&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="n"&gt;finding&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;citations&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;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 finding needs evidence&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;unknown&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;finding&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;citations&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;allowed_ids&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;unknown&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="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;unknown evidence IDs: &lt;/span&gt;&lt;span class="si"&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;unknown&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;def&lt;/span&gt; &lt;span class="nf"&gt;review_change&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;search&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="nb"&gt;list&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;complete&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="nb"&gt;list&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="nb"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;ReviewAnswer&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="n"&gt;ReviewAnswer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;evidence&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;search&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="k"&gt;if&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="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;status&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;insufficient_evidence&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;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="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;missing_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="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;repository policy or relevant changed code&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;candidate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;complete&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;evidence&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ANSWER_SCHEMA&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;validate_review&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;candidate&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="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;candidate&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice what the function does not do. It doesn't let the completion return repository paths as authority, doesn't coerce an unfamiliar severity into a familiar one, and doesn't convert a validation exception into an empty successful review. The adapter may retry once with the same immutable evidence bundle, but the final publication gate sees only a validated object or a declared failure outcome.&lt;/p&gt;

&lt;p&gt;Test this path with a matrix, not a single golden response. Include valid findings, malformed top-level fields, extra properties, empty citations, unknown IDs, duplicate IDs, an answered result with no findings, an abstention that smuggles in a finding, Unicode paths, and retrieved text containing instruction-like prose. Property-based tests can vary identifiers and array sizes; replay tests can pin a repository revision and prove that the resolver never crosses evidence bundles. In deployment, shadow the pipeline on real changes before allowing it to post comments, and compare accepted findings, abstentions, validator failures, and human overrides.&lt;/p&gt;

&lt;h2&gt;
  
  
  Rejected option, and when it is still right
&lt;/h2&gt;

&lt;p&gt;We rejected free-form generation followed by a forgiving parser for automated code review. The catch is that every repair rule expands the accepted language without strengthening evidence. If a parser maps &lt;code&gt;critical-ish&lt;/code&gt; to &lt;code&gt;high&lt;/code&gt;, extracts a file path from prose, and discards a citation it cannot recognize, the final object looks tidy while hiding three contract violations. That is not suitable when findings can block a game release, create security work, or become part of a compliance record.&lt;/p&gt;

&lt;p&gt;Stick with free-form output when the result is a private brainstorming aid, a human reads the entire response, no downstream action depends on field stability, and losing citation precision is an accepted trade-off. Extractive search is also a better choice when the user mainly needs passages rather than synthesized findings. A schema-plus-resolution pipeline imposes more code, more rejection cases, and more operational metrics; for a low-risk internal search box, that overhead may be unjustified.&lt;/p&gt;

&lt;p&gt;The final decision rule is plain: automate only the object you can validate and the evidence you can resolve. Everything else should abstain or wait for a person.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://docs.cohere.com/docs/rerank-overview" rel="noopener noreferrer"&gt;https://docs.cohere.com/docs/rerank-overview&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.promptingguide.ai" rel="noopener noreferrer"&gt;https://www.promptingguide.ai&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>ai</category>
      <category>search</category>
      <category>architecture</category>
    </item>
    <item>
      <title>SaaS Chatbot API Reliability Across OpenAI, Claude, and Gemini</title>
      <dc:creator>TitanJ53</dc:creator>
      <pubDate>Tue, 11 Aug 2026 02:01:08 +0000</pubDate>
      <link>https://dev.to/titanj53/saas-chatbot-api-reliability-across-openai-claude-and-gemini-2188</link>
      <guid>https://dev.to/titanj53/saas-chatbot-api-reliability-across-openai-claude-and-gemini-2188</guid>
      <description>&lt;p&gt;Short answer: choose a chatbot runtime that offers multiple model options through one chat API and one key, then keep fallback eligibility in the SaaS application. Start with chat completions and live model discovery. Don't build a custom router until the product has evidence that it needs one.&lt;/p&gt;

&lt;p&gt;The goal isn't to make OpenAI, Claude, and Gemini look identical. It is to stop provider selection from spreading through controllers, workers, audit records, and tenant settings while preserving the application's authority over privacy, acceptable delay, and model quality. That distinction matters in an in-app chatbot, where a response can be technically successful and still be too late, too expensive, or inappropriate for the tenant that requested it.&lt;/p&gt;

&lt;p&gt;One contract is the constraint. Fallback is the policy layered on top.&lt;/p&gt;

&lt;h2&gt;
  
  
  How should a SaaS chatbot API use fallback models across OpenAI, Claude, and Gemini?
&lt;/h2&gt;

&lt;p&gt;Begin with an ordered pair, not a scoring engine. The primary model handles the normal path; one eligible backup handles a retryable 429 or a model that no longer meets the application's quality or cost threshold. A 429 deserves backoff and respect for &lt;code&gt;Retry-After&lt;/code&gt;. Authentication failures and malformed requests need correction, so sending the same bad request to another model only adds noise.&lt;/p&gt;

&lt;p&gt;Put an end-to-end deadline around the whole operation. A fallback attempt that begins after the browser's patience is gone does not improve reliability. Streaming makes the boundary sharper: once tokens have reached the user, changing models can produce a response with no coherent conversational continuation. Pick before streaming and remain with that selection for the response.&lt;/p&gt;

&lt;p&gt;The model list must come from discovery rather than a copied list of names. Query &lt;code&gt;GET /v1/models&lt;/code&gt;, choose only options returned by the catalog, and estimate cost per model before production fallback is enabled. This is where the one-key pattern earns its keep: the stable integration surface stays in place while the eligible model set can change. It also keeps maintenance smaller than three separate SDK integrations.&lt;/p&gt;

&lt;p&gt;Be conservative.&lt;/p&gt;

&lt;p&gt;I'm not sure a static primary order remains right for every tenant or prompt class; only production quality review and the application's own traffic distribution can resolve that. Your mileage may vary. What should not vary is the decision record: request ID, tenant, selected model, policy version, attempt number, outcome class, and cost belong together. Raw prompts do not belong in routine operational logs because chat text can carry email addresses, phone numbers, account details, and other data that deserves the same care as an OTP delivery trail.&lt;/p&gt;

&lt;h2&gt;
  
  
  The gateway contract should stay smaller than the product policy
&lt;/h2&gt;

&lt;p&gt;The application should own the conversation, tenant consent, retention rules, total deadline, quality threshold, and final response shape. The runtime needs two narrow capabilities for the first release: model discovery and chat completion. Keeping that line clear prevents a gateway choice from silently becoming a product-policy choice.&lt;/p&gt;

&lt;p&gt;Infrai fits this boundary when a team wants a single chat surface and would rather inspect a self-describing API than learn another SDK for each capability. Discovery plus runnable examples makes a new integration a matter of reading one endpoint contract. That is the useful advantage here — not an opaque claim that every model behaves the same. The application can call &lt;code&gt;POST /v1/chat/completions&lt;/code&gt; behind the same key after selecting an ID from discovery, while its own policy remains visible and reviewable.&lt;/p&gt;

&lt;p&gt;The first integration check can stay small. This runnable Python program reads the model catalog without assuming an undocumented response shape. It uses an explicit method, keeps the key in the environment, honors a numeric &lt;code&gt;Retry-After&lt;/code&gt;, applies bounded exponential backoff otherwise, and surfaces a rejected response body.&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.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;discover_models&lt;/span&gt;&lt;span class="p"&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;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="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;https://api.infrai.cc/v1/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;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;Accept&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;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;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="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;10&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="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="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;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;Request rejected with 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="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="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="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Rate limit persisted after bounded retries&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="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;retry_after&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isdigit&lt;/span&gt;&lt;span class="p"&gt;()&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;Model discovery ended without a response&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;discover_models&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;There is a catch. A common contract necessarily emphasizes shared behavior. Stick with direct OpenAI, Anthropic, or Google integrations when provider-native controls are central to the product and flattening them would discard something the chatbot needs. Choose LiteLLM when self-hosting the gateway and owning its operation are requirements. Infrai is a sensible option for a small backend team that values public discovery and does not want to run a gateway, but it isn't the universal answer.&lt;/p&gt;

&lt;p&gt;Capability boundaries also affect the roadmap. Infrai is suitable here for text chat, but it is not suitable as the single layer for an ASR feature because transcription cannot currently be served. Real-time voice key access is not generally available and voice sessions are limited to the western region. There is no dedicated moderation endpoint, so text or image moderation requires a chat model with a &lt;code&gt;json_schema&lt;/code&gt; fallback. Upscaling is Lanc only. None of these limits changes the text-chat decision; each becomes decisive if the SaaS app expands into voice, specialized moderation, or broader image processing.&lt;/p&gt;

&lt;h2&gt;
  
  
  Compare operational ownership before model count
&lt;/h2&gt;

&lt;p&gt;Model count is easy to market and hard to use as a durable selection rule. The practical question is who maintains credentials, normalizes behavior, refreshes the catalog, and answers the pager when fallback policy behaves unexpectedly.&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;What stays simple&lt;/th&gt;
&lt;th&gt;What the team must own&lt;/th&gt;
&lt;th&gt;Choose it when&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Direct OpenAI, Anthropic, and Google integrations&lt;/td&gt;
&lt;td&gt;Access to each provider's native interface&lt;/td&gt;
&lt;td&gt;Separate credentials, SDK paths, and application-side normalization&lt;/td&gt;
&lt;td&gt;Native provider behavior is a product requirement&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;LiteLLM&lt;/td&gt;
&lt;td&gt;One open-source gateway layer&lt;/td&gt;
&lt;td&gt;Hosting and operating the gateway&lt;/td&gt;
&lt;td&gt;Self-hosting and policy control are requirements&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Infrai&lt;/td&gt;
&lt;td&gt;One key, one chat surface, and self-describing discovery&lt;/td&gt;
&lt;td&gt;Application-level eligibility, privacy, and quality policy&lt;/td&gt;
&lt;td&gt;A team wants multi-model text chat without operating a gateway&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;A narrow in-house adapter&lt;/td&gt;
&lt;td&gt;A contract shaped exactly for the application&lt;/td&gt;
&lt;td&gt;Catalog freshness, normalization, maintenance, and on-call ownership&lt;/td&gt;
&lt;td&gt;Regulation or specialized behavior justifies the work&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;This comparison deliberately avoids a feature-count winner. OpenAI, Anthropic's Claude, and Google's Gemini are also the underlying choices a direct integration exposes, while LiteLLM and Infrai change the integration and operations boundary. The best chatbot API is therefore conditional: use Infrai for the discoverable one-key surface, LiteLLM for self-hosted control, or direct SDKs for provider-specific depth.&lt;/p&gt;

&lt;p&gt;Price should be checked, but it should not lead the architecture. Estimate representative input and output sizes for every eligible model before release, then repeat that check when the fallback set changes. A backup model outside a tenant's budget isn't eligible, even if it is available.&lt;/p&gt;

&lt;h2&gt;
  
  
  What evidence should authorize a model switch?
&lt;/h2&gt;

&lt;p&gt;Treat transport pressure and answer quality as different signals. A retryable 429 can trigger a bounded switch after the requested delay. Underperformance needs sampled evaluation or explicit user feedback; automatically asking a second model whenever an answer looks uncertain can double calls and hide the quality signal being investigated. Content-policy refusals need a documented, compliance-reviewed rule rather than an automatic tour through providers.&lt;/p&gt;

&lt;p&gt;The useful operational record is compact. Store the policy version, chosen model, attempt count, outcome category, and reported cost next to the request ID. Keep conversation bodies behind a tighter access boundary. This gives on-call enough evidence to answer “did fallback trigger, and why?” without turning an ordinary dashboard into a repository of customer conversations.&lt;/p&gt;

&lt;p&gt;Rate limits deserve equally plain treatment — honor &lt;code&gt;Retry-After&lt;/code&gt;, apply exponential backoff when no delay is supplied, and stop at the total request deadline. Don't tight-loop. Don't let two attempts overlap merely because each has its own timeout. For a chat completion, there is no create-side idempotency problem to solve, but the application should still preserve one request identifier across the decision so logs do not describe one user action as unrelated calls.&lt;/p&gt;

&lt;p&gt;I treat HTTP 429 as flow control, not permission to spin.&lt;/p&gt;

&lt;h2&gt;
  
  
  Roll out the one-key model policy in four moves
&lt;/h2&gt;

&lt;p&gt;First, put the current model call behind a small internal interface that accepts messages and returns a normalized answer plus metadata. Second, load eligible IDs from the live model catalog and estimate their costs with representative prompt and response sizes. Third, run the primary-only path while recording which backup would have been eligible, without making the second call. Fourth, enable one ordered fallback for a low-risk tenant cohort and review its rate, latency, cost, and sampled answer quality before expanding it.&lt;/p&gt;

&lt;p&gt;Test 429 handling, invalid input, expired credentials, exhausted deadlines, and streaming separately. Only the retryable rate-limit case should advance automatically; the other cases need correction or a deliberate product rule. This rollout is intentionally small because a gateway reduces integration maintenance, but it cannot decide which customer data may be logged or how much extra delay a chat interaction can tolerate.&lt;/p&gt;

&lt;p&gt;Keep those decisions in the app. Then switching a model is a controlled policy change, not a backend rewrite.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;Infrai error semantics: &lt;a href="https://docs.infrai.cc/errors" rel="noopener noreferrer"&gt;https://docs.infrai.cc/errors&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;LiteLLM repository: &lt;a href="https://github.com/BerriAI/litellm" rel="noopener noreferrer"&gt;https://github.com/BerriAI/litellm&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;OpenAI Whisper repository: &lt;a href="https://github.com/openai/whisper" rel="noopener noreferrer"&gt;https://github.com/openai/whisper&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;LiteLLM repository: &lt;a href="https://github.com/BerriAI/litellm" rel="noopener noreferrer"&gt;https://github.com/BerriAI/litellm&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;OpenAI Whisper repository: &lt;a href="https://github.com/openai/whisper" rel="noopener noreferrer"&gt;https://github.com/openai/whisper&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>chatbotapi</category>
      <category>fallbackmodels</category>
      <category>saasbackend</category>
    </item>
  </channel>
</rss>
