<?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: Prasanth Pattathil</title>
    <description>The latest articles on DEV Community by Prasanth Pattathil (@prasanth_pattathil).</description>
    <link>https://dev.to/prasanth_pattathil</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%2F4213898%2F913ede6a-590f-4db0-95a8-5bede9fc1c37.png</url>
      <title>DEV Community: Prasanth Pattathil</title>
      <link>https://dev.to/prasanth_pattathil</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/prasanth_pattathil"/>
    <language>en</language>
    <item>
      <title>An LLM Gateway That Cut the Bill: Meter First, Then Route</title>
      <dc:creator>Prasanth Pattathil</dc:creator>
      <pubDate>Mon, 09 Nov 2026 12:01:02 +0000</pubDate>
      <link>https://dev.to/prasanth_pattathil/an-llm-gateway-that-cut-the-bill-meter-first-then-route-4b0f</link>
      <guid>https://dev.to/prasanth_pattathil/an-llm-gateway-that-cut-the-bill-meter-first-then-route-4b0f</guid>
      <description>&lt;p&gt;&lt;em&gt;A design case study: why a growing model bill is usually a routing and caching problem, and how one small gateway made it visible and then smaller.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fb705i9ggydrjcqscxeo4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fb705i9ggydrjcqscxeo4.png" width="799" height="420"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Part of the AI Architecture Casebook: real-world AI problems, redesigned one decision at a time.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;About this case study.&lt;/em&gt;&lt;/strong&gt; &lt;em&gt;This is a composite scenario. The apps, teams and numbers are illustrative. It shows a design and how to measure it. The numbers are planning figures, not benchmark results.&lt;/em&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  The problem
&lt;/h3&gt;

&lt;p&gt;A mid-sized company has nine internal apps that call Gemini models. A claims summariser, a support-ticket classifier, a contract Q&amp;amp;A tool, a nightly enrichment job, two agents, and a few smaller tools. Each team set up its own access: some use a service account, two use API keys, one shares a key with another team.&lt;/p&gt;

&lt;p&gt;In January the monthly model bill was about $11,000. In September it was about $48,000.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F4zlymujk61hufcjjoal3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F4zlymujk61hufcjjoal3.png" width="800" height="537"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;The bill quadrupled in nine months, and nobody could say which app caused it.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Finance asked a simple question: &lt;em&gt;which team is spending this, and on what?&lt;/em&gt; Nobody could answer. Every call landed in the same Google Cloud project. The bill showed models and token counts, not apps or features.&lt;/p&gt;

&lt;p&gt;Two answers came up straight away:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;“Switch everyone to the cheapest model.”&lt;/strong&gt; That would cut the bill and break the two use cases that really need a stronger model.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;“Negotiate a discount.”&lt;/strong&gt; A discount on waste is still waste.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Both skip the first step. The real problem was simpler:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;You can’t cut a bill you can’t attribute.&lt;/strong&gt; Meter every call first. Then route each use case to the cheapest model that passes its evaluation, and cache what repeats.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3&gt;
  
  
  What the fix had to satisfy
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fxlgfv4a9izggo6mbwa0j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fxlgfv4a9izggo6mbwa0j.png" width="800" height="570"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;The first two rows cannot bend.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The first row matters most. A cheaper model that quietly gives worse answers is not a saving; it is a cost moved somewhere harder to see.&lt;/p&gt;
&lt;h3&gt;
  
  
  The design in one picture
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fexegzmd5oczkyihkusdq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fexegzmd5oczkyihkusdq.png" width="799" height="477"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Every model call goes through one gateway, which meters, limits, routes and caches.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The gateway is a small service on Cloud Run. Apps call it instead of calling the model directly. It looks up the caller’s &lt;strong&gt;use case&lt;/strong&gt; , checks the budget, applies caching, calls the model the use case is configured for, and writes a usage record to BigQuery.&lt;/p&gt;

&lt;p&gt;Models are reached through Google’s Gemini Enterprise Agent Platform, formerly Vertex AI. The rename happened in April 2026, and the aiplatform.googleapis.com endpoint stays the same.&lt;/p&gt;

&lt;p&gt;Six decisions carry the design.&lt;/p&gt;
&lt;h3&gt;
  
  
  Decision 1: One door to the models
&lt;/h3&gt;

&lt;p&gt;The first step is boring and does most of the work: &lt;strong&gt;every model call goes through the gateway, and nothing else can call the models.&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Only the gateway’s service account has permission to call models in the project. Teams’ own service accounts lose it, and the API keys are revoked.&lt;/li&gt;
&lt;li&gt;Each app calls the gateway with its own identity (an ID token from its service account). The gateway never trusts a team name written in the request body.&lt;/li&gt;
&lt;li&gt;Each request names a &lt;strong&gt;use case&lt;/strong&gt; , such as claims-summary or nightly-enrichment. A use case is the unit of ownership, budget and routing. The gateway checks that the caller is allowed to use it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We looked at four ways to build the door:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F5qsuuym7a29cej8vm667.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F5qsuuym7a29cej8vm667.png" width="800" height="419"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Four ways to put a door in front of the models.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;If you already run Apigee, its LLM token policies (token quotas per app and prompt-size limits) and its semantic-cache support cover a lot of this design, and that is a reasonable choice. We didn’t run Apigee, and the gateway needed to do only five things, so we kept our own on Cloud Run. It is a few hundred lines, and it’s the most-tested code in the system.&lt;/p&gt;
&lt;h3&gt;
  
  
  Decision 2: Meter every call before changing anything
&lt;/h3&gt;

&lt;p&gt;For the first two weeks the gateway changed nothing. It passed every call through and wrote one record per call:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csvs"&gt;&lt;code&gt;&lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;caller&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;use_case&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;owner_team&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mode&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;online&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;batch&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="n"&gt;input_tokens&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cached_tokens&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;output_tokens&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="n"&gt;latency_ms&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;retry_of&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="n"&gt;cache_layer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cost_usd&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;price_version&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Cost is computed from a &lt;strong&gt;versioned price table&lt;/strong&gt; in the gateway’s config, so a record always says which prices it used. It won’t match the invoice to the cent, and it doesn’t need to. It has to be close enough to rank use cases, and the monthly total is reconciled against the billing export.&lt;/p&gt;

&lt;p&gt;Two weeks of records answered finance’s question:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Flfw65c2avu0r3g4ykgdy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Flfw65c2avu0r3g4ykgdy.png" width="800" height="398"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Where the September bill went, by cause.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;None of the five causes was “the model is too expensive”. They were:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;The strongest model for everything.&lt;/strong&gt; Every app defaulted to a Pro-class model, including classification and short extraction, where a Flash-class model passed the same tests.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The same long prompt, sent again and again.&lt;/strong&gt; The contract Q&amp;amp;A tool sent a 7,000-token policy pack with every question, but it put the user’s question first, so the shared part never formed a reusable prefix.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A nightly job paying on-demand prices.&lt;/strong&gt; The enrichment job had no deadline tighter than “by morning”, yet it called the online API row by row.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Retries and one runaway.&lt;/strong&gt; One weekend, an agent retried a failing tool call in a loop for 31 hours.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Duplicate work.&lt;/strong&gt; Three apps summarised the same incoming documents.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;
  
  
  Decision 3: Route by use case, not by request
&lt;/h3&gt;

&lt;p&gt;The obvious fix for cause 1 is a “smart router”: a small model that reads each request and picks a model. We didn’t do that. A per-request router adds a model call to every request, and it makes quality depend on a classifier nobody evaluates.&lt;/p&gt;

&lt;p&gt;Instead, &lt;strong&gt;each use case is configured with a model tier&lt;/strong&gt; , and the tier only changes when the evaluation says so.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fvnv6tuzz32esytdsatbu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fvnv6tuzz32esytdsatbu.png" width="800" height="406"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Each use case has a tier. Moving it to a cheaper one needs a passing evaluation.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The config lives in git, next to the gateway:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;claims-summary&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;owner&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;claims-team&lt;/span&gt;
  &lt;span class="na"&gt;tier&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;flash&lt;/span&gt;
  &lt;span class="na"&gt;max_output_tokens&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;800&lt;/span&gt;
  &lt;span class="na"&gt;budget_usd_per_day&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;120&lt;/span&gt;
  &lt;span class="na"&gt;on_budget&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;alert&lt;/span&gt;
&lt;span class="na"&gt;contract-qa&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;owner&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;legal-ops&lt;/span&gt;
  &lt;span class="na"&gt;tier&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;pro&lt;/span&gt;
  &lt;span class="na"&gt;budget_usd_per_day&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;300&lt;/span&gt;
  &lt;span class="na"&gt;on_budget&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;alert&lt;/span&gt;
&lt;span class="na"&gt;nightly-enrichment&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;owner&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;data-team&lt;/span&gt;
  &lt;span class="na"&gt;tier&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;flash-lite&lt;/span&gt;
  &lt;span class="na"&gt;mode&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;batch&lt;/span&gt;
  &lt;span class="na"&gt;budget_usd_per_day&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt;
  &lt;span class="na"&gt;on_budget&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;stop&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Tiers are names, not model IDs. The gateway maps flash to a specific, pinned model version. Upgrading a tier is a config change, and it goes through the same evaluation gate as a prompt change. &lt;em&gt;That gate is Case 05.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;To move a use case down a tier, its owner runs its golden set (a few dozen real requests with expected results) against both tiers. If the cheaper tier is within the agreed margin, the config changes. In this scenario, six of the nine use cases moved down. Contract Q&amp;amp;A and one agent stayed on Pro.&lt;/p&gt;

&lt;h3&gt;
  
  
  Decision 4: Cache in layers, and never across users
&lt;/h3&gt;

&lt;p&gt;Caching is the second-largest saving, and the easiest one to get wrong. We use three layers, from safest to riskiest.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fdu7sznn52d7moj3rgdub.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fdu7sznn52d7moj3rgdub.png" width="800" height="351"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Three layers of caching, from safest to riskiest.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Layer 1: prompt prefix caching.&lt;/strong&gt; Gemini models on the platform reuse a repeated prompt prefix and bill the cached tokens at a reduced rate. This is implicit caching, and it only helps if the stable part comes first. So the gateway requires prompts in a fixed order: system instructions, then shared documents, then the user’s input. For the contract Q&amp;amp;A tool, moving the question to the end turned most of each prompt into a cached prefix. For a large document used all day, an explicit cache with a time-to-live is worth it too.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Layer 2: an exact-response cache.&lt;/strong&gt; For use cases that are deterministic (temperature 0, same input gives the same output), the gateway caches the response under a hash of the model version, the full prompt, the parameters and the &lt;strong&gt;data version&lt;/strong&gt; of anything the prompt was built from. The same document summarised by three apps now costs one call. The data version is what keeps this safe: when the document changes, the key changes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Layer 3: a semantic cache, off by default.&lt;/strong&gt; A semantic cache returns a stored answer for a question that is &lt;em&gt;similar&lt;/em&gt; to an earlier one. It saves more, and it can return the wrong answer with complete confidence. We allow it only for public, non-personal content, such as an internal FAQ. &lt;strong&gt;It is never allowed for anything filtered by user permissions.&lt;/strong&gt; Two users with different access can ask the same question and deserve different answers. That is the rule from Case 01: access is decided by data, and a cache must not become a way around it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Batch, for work that can wait.&lt;/strong&gt; The nightly enrichment job now submits one batch job instead of 40,000 online calls. Batch inference is billed at a discount and doesn’t use the online quota the interactive apps depend on.&lt;/p&gt;

&lt;h3&gt;
  
  
  Decision 5: Budgets that stop runaways, not users
&lt;/h3&gt;

&lt;p&gt;Every use case has a daily budget. What happens when it is reached depends on who is calling.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fglisya1f73hmudp6ph4d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fglisya1f73hmudp6ph4d.png" width="800" height="400"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;What happens at the budget depends on who is calling.&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Interactive apps alert; they don’t stop.&lt;/strong&gt; Cutting off a claims handler mid-shift costs more than the tokens. The owner is paged at 80% and 100%.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Agents and batch jobs stop.&lt;/strong&gt; Nobody is waiting on them in real time, and they are where runaways happen.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Retries have their own budget.&lt;/strong&gt; The gateway allows two retries with backoff. If the same use case sends the same failing request ten times in a minute, the gateway opens a circuit breaker for that use case and pages the owner.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The 31-hour weekend loop would have been stopped within a minute by the circuit breaker, and at the latest by the daily budget.&lt;/p&gt;

&lt;h3&gt;
  
  
  Decision 6: Keep the gateway boring
&lt;/h3&gt;

&lt;p&gt;The gateway sits on the path of every AI feature in the company, so it has to be dull and reliable.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;It never rewrites prompts.&lt;/strong&gt; It may reject a request (too large, wrong order, over budget), but it never edits one. Teams must be able to trust that what they send is what the model sees.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No automatic model fallback.&lt;/strong&gt; If a model is unavailable, the gateway returns an error. A fallback is allowed only if it is listed in the use case’s config and passed that use case’s evaluation. Silent fallbacks are silent quality changes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Metering never blocks a request.&lt;/strong&gt; Usage records go through Pub/Sub to BigQuery. If BigQuery is slow, requests still flow and records catch up.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Budget checks fail open for interactive use cases, closed for agents and batch.&lt;/strong&gt; If the budget counter store is unavailable, an interactive call goes through and is logged; an agent call waits.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Stateless and regional.&lt;/strong&gt; Several instances, a minimum instance count so there are no cold starts, and timeouts set per use case.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  A sketch of the gateway
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;handle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;caller&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;uc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;POLICIES&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;use_case&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="c1"&gt;# from git&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;caller&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;uc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;callers&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;Forbidden&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;use_case&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="nf"&gt;check_prompt_order&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;uc&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;budget&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;check&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;uc&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# stops agents and batch&lt;/span&gt;

    &lt;span class="n"&gt;key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;uc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;exact_cache&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="nf"&gt;cache_key&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;uc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;req&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;hit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cache&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;key&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;hit&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="nf"&gt;record&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;uc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;hit&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;layer&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"exact"&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;hit&lt;/span&gt;

    &lt;span class="n"&gt;resp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;call_model&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="no"&gt;TIERS&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;uc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tier&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;# pinned version&lt;/span&gt;
        &lt;span class="n"&gt;prompt&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;req&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="n"&gt;max_output_tokens&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;uc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;max_output&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="n"&gt;uc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;timeout_s&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="n"&gt;uc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;max_retries&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;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;resp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ttl&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;uc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cache_ttl&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;budget&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;uc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;resp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;usage&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;record&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;uc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;resp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;layer&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&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="n"&gt;resp&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;cache_key&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;uc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;req&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="nf"&gt;sha256&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="p"&gt;{&lt;/span&gt;
        &lt;span class="s2"&gt;"model"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;TIERS&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;uc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tier&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s2"&gt;"prompt"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;req&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="s2"&gt;"params"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s2"&gt;"data_version"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data_version&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;sort_keys&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;True&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="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;hexdigest&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;Notice what’s missing: no line decides which model to use by reading the request. The use case decides, and the evaluation decides the use case.&lt;/p&gt;

&lt;h3&gt;
  
  
  What it saved
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fl2qn5ido042cmsdnpncp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fl2qn5ido042cmsdnpncp.png" width="800" height="441"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;From about $48,000 to about $20,000 a month. Most of it came from routing, not discounts.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The biggest saving came from the least technical change: moving six use cases to a cheaper tier after their evaluations passed. Caching came second. Stopping the runaway was small in a normal month and large in a bad one.&lt;/p&gt;

&lt;h3&gt;
  
  
  What can still go wrong
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F7a3vcnlq2ddrh30wej79.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F7a3vcnlq2ddrh30wej79.png" width="800" height="444"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;What can still go wrong, and how you would catch it.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The number to watch is &lt;strong&gt;cost per successful task&lt;/strong&gt; , per use case, not the total bill. The total goes up when usage grows, which is fine. Cost per task going up means something changed: a longer prompt, a tier change, a cache that stopped hitting.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;At 10x&lt;/strong&gt; , I’d expect these to change first:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;With many more use cases, the policy file needs an owner and a review process of its own, not just a pull request.&lt;/li&gt;
&lt;li&gt;Provisioned throughput becomes worth pricing for the two or three steadiest interactive use cases.&lt;/li&gt;
&lt;li&gt;If most traffic becomes agents calling tools, Agent Platform’s Agent Gateway is worth evaluating for agent-to-tool traffic. This gateway still covers agent-to-model calls.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  What we would do differently
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Turn on metering first, on day one of the first AI app.&lt;/strong&gt; Attribution is cheap to add early and painful to reconstruct later.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Make “use case” a required field from the start.&lt;/strong&gt; Retrofitting it meant reading nine codebases.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Write the golden set before choosing the model.&lt;/strong&gt; Every tier change in this case waited on an owner writing tests.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Key takeaways
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;You can’t cut a bill you can’t attribute.&lt;/strong&gt; Put one door in front of the models and meter every call.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Route by use case, not by request.&lt;/strong&gt; Each use case gets a tier, and the tier changes only when its evaluation passes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Put the stable part of the prompt first.&lt;/strong&gt; Prefix caching only works if there is a prefix.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cache exact responses with a data version, and never cache across users.&lt;/strong&gt; A semantic cache must not bypass permissions.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Stop runaways, not users.&lt;/strong&gt; Agents and batch jobs hit a hard budget; interactive apps alert.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Keep the gateway boring.&lt;/strong&gt; It never rewrites prompts and never silently falls back.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;em&gt;Next in the series: Case 05, evaluation as a release gate (AWS). The design and decision records for this case are in the&lt;/em&gt; &lt;a href="https://github.com/prasanth2006z/ai-architecture-casebook/tree/main/cases/04-llm-gateway" rel="noopener noreferrer"&gt;&lt;em&gt;series repository&lt;/em&gt;&lt;/a&gt;&lt;em&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;How would you have handled it? I’d like to hear other approaches in the comments.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Google Cloud details reflect public documentation as of October 2026. Check current docs and pricing before you build.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>googlecloudplatform</category>
      <category>generativeaitools</category>
      <category>llm</category>
      <category>architecture</category>
    </item>
    <item>
      <title>Keeping a Team Knowledge Assistant in Sync: Index on Change, Retrieve on Question</title>
      <dc:creator>Prasanth Pattathil</dc:creator>
      <pubDate>Mon, 02 Nov 2026 12:01:02 +0000</pubDate>
      <link>https://dev.to/prasanth_pattathil/keeping-a-team-knowledge-assistant-in-sync-index-on-change-retrieve-on-question-1e7</link>
      <guid>https://dev.to/prasanth_pattathil/keeping-a-team-knowledge-assistant-in-sync-index-on-change-retrieve-on-question-1e7</guid>
      <description>&lt;p&gt;&lt;em&gt;A design case study: why a hand-built context goes stale, and how to keep an assistant current with code, docs and schema automatically.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fjygv1ni44p7e5n0ipwio.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fjygv1ni44p7e5n0ipwio.png" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;About this case study.&lt;/strong&gt; This is a composite scenario. The system, tables and numbers are illustrative. It shows a design and how to measure it, not benchmark results.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  The setup
&lt;/h3&gt;

&lt;p&gt;ClaimFlow is the claims backend of a mid-sized insurer. It takes a submitted claim, checks it against the policy, tracks documents, sets reserves and moves the claim from submitted to paid or rejected.&lt;/p&gt;

&lt;p&gt;Three teams work on the claims journey, and our team owns only one part:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Claims Portal&lt;/strong&gt; (the UI) is owned by the portal team.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Claims Intake&lt;/strong&gt; (preprocessing) is owned by the integration team. It cleans and enriches claims before they reach us.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;ClaimFlow&lt;/strong&gt; (the backend) is ours: the business rules, the claim lifecycle and the claims database.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;What we know about ClaimFlow lives in three places:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Bitbucket&lt;/strong&gt; : the service code and the database migrations.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Confluence&lt;/strong&gt; : feature specs written by the business team, plus runbooks and design notes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The database&lt;/strong&gt; : about 80 tables (claims, claim items, policies, reserves, payments, documents, status history, reference data) with foreign keys, status codes and some conventions nobody wrote down.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;A typical feature touches all three teams: a new screen in the Portal, a new validation in Intake, new rules in ClaimFlow, and new columns or tables in the database.&lt;/p&gt;

&lt;p&gt;To help new team members, we built a chat assistant. People ask it how a process works, how tables connect, where logic lives in the code, and, most of all, for &lt;strong&gt;SQL to troubleshoot&lt;/strong&gt; a claim that is stuck.&lt;/p&gt;

&lt;p&gt;The assistant answers from &lt;strong&gt;one large context document&lt;/strong&gt;. Someone collects Confluence pages, table definitions and code snippets, asks ChatGPT to summarise them, and pastes the result in. When something changes, someone does it again.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F2ugo040eh7d50jlxziu7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F2ugo040eh7d50jlxziu7.png" width="800" height="484"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Today: three teams, three sources, and one hand-built context that someone has to remember to update.&lt;/em&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  What goes wrong
&lt;/h3&gt;

&lt;p&gt;Here is a typical week.&lt;/p&gt;

&lt;p&gt;On Tuesday, feature &lt;strong&gt;CLM-482, partial approval&lt;/strong&gt; , ships. The business spec in Confluence is updated. A migration splits claim_items.amount into claimed_amount and approved_amount, and adds a new status, PARTIALLY_APPROVED. The code goes to production.&lt;/p&gt;

&lt;p&gt;On Thursday, a new team member is asked why some claims look stuck. They ask the assistant for SQL. The assistant still has last month’s context.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fzj1x67pnv4zwdkpadbmv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fzj1x67pnv4zwdkpadbmv.png" width="800" height="395"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Between the release and the next manual refresh, every answer comes from the old version.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Two things can happen, and the second is worse:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Loud failure.&lt;/strong&gt; The SQL uses amount, which no longer exists. The query fails. Annoying, but at least everyone can see it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Silent failure.&lt;/strong&gt; The SQL filters on the statuses the assistant knows about. It runs fine and returns rows, but misses every PARTIALLY_APPROVED claim. The person troubleshooting now has a wrong answer that looks right.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The stale context is not just a maintenance chore. It is a design problem:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;It goes stale by default.&lt;/strong&gt; Freshness depends on someone remembering.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;It doesn’t fit.&lt;/strong&gt; One document can’t hold 80 tables, the codebase and every spec. Summarising to make it fit drops exactly the details people ask about.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;It blends the sources.&lt;/strong&gt; The spec says what was intended, the code says what happens, and the schema says what exists. A summary mixes them, so nobody can tell which one a sentence came from.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;It can’t be checked.&lt;/strong&gt; Answers carry no links back to a page, file or table.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So the rule for this design is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Index on change, retrieve on question.&lt;/strong&gt; Keep the sources as the truth. Keep an index that updates when they change. Fetch what each question needs, and cite it.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3&gt;
  
  
  The design in one picture
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fv26ooqctxswh0ln4pqn8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fv26ooqctxswh0ln4pqn8.png" width="800" height="667"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Changes trigger sync jobs; the assistant retrieves from the indexes and cites what it used.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This case runs on Google Cloud. Webhooks and schedulers start sync jobs on Cloud Run, Pub/Sub decouples them, and Cloud SQL for PostgreSQL with pgvector holds the indexes and the schema catalog. A Gemini model on Gemini Enterprise Agent Platform (formerly Vertex AI) answers through a small set of tools. The same shape works on any cloud.&lt;/p&gt;

&lt;p&gt;Six decisions shape it.&lt;/p&gt;
&lt;h3&gt;
  
  
  Decision 1: Retrieve from indexed sources, not a hand-built summary
&lt;/h3&gt;

&lt;p&gt;We looked at three options:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fv4o2z7sm1x5sjr97zfsq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fv4o2z7sm1x5sjr97zfsq.png" width="800" height="369"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Three ways to give the assistant its knowledge.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;A managed connector is a reasonable choice for the Confluence part. Gemini Enterprise, for example, has a Confluence Cloud connector with full and incremental syncs. But our questions mix all three sources (“which table stores the field the spec calls &lt;em&gt;approved amount&lt;/em&gt;, and where is it set in code?”), and the schema needs more than text search. So we keep our own indexes for code and schema, and keep Confluence in the same store so one answer can cite all three.&lt;/p&gt;

&lt;p&gt;The important change is not the tool. It is that &lt;strong&gt;the assistant no longer reads a copy written by a person.&lt;/strong&gt; It reads the sources, through an index that a machine keeps current.&lt;/p&gt;
&lt;h3&gt;
  
  
  Decision 2: Sync on change, reconcile on schedule
&lt;/h3&gt;

&lt;p&gt;Each source tells us about changes in a different way, so each gets its own trigger.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fnbj2sz2fxsdqbs3zc107.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fnbj2sz2fxsdqbs3zc107.png" width="800" height="458"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;One trigger per source, plus a nightly reconcile that catches anything a trigger missed.&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Bitbucket.&lt;/strong&gt; The assistant should describe the code that is &lt;strong&gt;running in production&lt;/strong&gt; , not whatever was merged last. So the deploy pipeline moves a prod tag to the commit it deploys. A webhook on repo:push fires when that tag moves and calls a Cloud Run endpoint. The endpoint checks the HMAC signature in the X-Hub-Signature header, then publishes a change event with the old and new commit.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Confluence.&lt;/strong&gt; Every 15 minutes, a job runs a CQL query such as space = CLM and lastmodified &amp;gt;= "2026-10-18 09:00" and re-indexes what changed. Confluence Cloud can send webhooks too, but polling is simpler to run, and 15 minutes is fresh enough for this use.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Database schema.&lt;/strong&gt; If the deployed change touches the migrations folder, a job reads the live schema (tables, columns, keys, comments) from a &lt;strong&gt;read replica&lt;/strong&gt; , and reference tables such as status codes, and rebuilds the schema catalog.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Nightly reconcile.&lt;/strong&gt; Once a night, everything is listed in full and compared with the index. This catches missed webhooks, deleted pages and permission changes.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Three rules make the sync safe to run again and again:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Idempotent.&lt;/strong&gt; Each item is stored with a content hash. Unchanged content is skipped.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Deletes are real.&lt;/strong&gt; A deleted page or removed file must disappear from the index. Stale facts are worse than missing ones.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Everything is versioned.&lt;/strong&gt; Each chunk carries its source version: commit SHA, page version or schema snapshot time.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;
  
  
  Decision 3: Each source keeps its own shape
&lt;/h3&gt;

&lt;p&gt;Prose, code and schema are not the same kind of thing, so we don’t index them the same way.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F2y8ocfm8t98hl2eol3vc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F2y8ocfm8t98hl2eol3vc.png" width="800" height="426"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Each source is stored in the form its questions need.&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Confluence&lt;/strong&gt; is chunked by section, with the page title, space, labels and feature key added to each chunk. This is the same approach Case 01 used for HR policies.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Code&lt;/strong&gt; is chunked by function, class and HTTP route, not by line count, so a chunk is a unit someone would actually ask about. We also generate a short summary per module. This is allowed because it is &lt;strong&gt;derived and versioned&lt;/strong&gt; : it is rebuilt by the pipeline whenever that module changes, never by hand.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Schema&lt;/strong&gt; is not embedded as text at all. It is a &lt;strong&gt;catalog&lt;/strong&gt; of tables, columns, types, foreign keys, comments and allowed status values. The assistant reaches it through tools: describe_table, find_join_path (a shortest path over foreign keys) and list_status_values.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The schema catalog holds &lt;strong&gt;structure, not customer data&lt;/strong&gt;. Claims tables contain personal and medical details. Only reference tables, such as status codes, contribute values.&lt;/p&gt;
&lt;h3&gt;
  
  
  Decision 4: When the sources disagree, say so
&lt;/h3&gt;

&lt;p&gt;Each source is the authority on a different question:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fow5mnfluai40empx3e79.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fow5mnfluai40empx3e79.png" width="800" height="459"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Each source answers a different question. A good answer says which one it used.&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Schema&lt;/strong&gt; : what exists.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Code in production&lt;/strong&gt; : what actually happens.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Confluence&lt;/strong&gt; : what was intended.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If the spec says partial approvals need a second reviewer but the code doesn’t check for one, the assistant should not pick one and hide the other. It should answer with both, cite both, and say they disagree.&lt;/p&gt;

&lt;p&gt;The nightly reconcile also produces a &lt;strong&gt;drift report&lt;/strong&gt; for the team: status values in the database that no Confluence page mentions, columns with no comment, and pages that refer to columns that no longer exist. The assistant’s sync job doubles as a documentation check.&lt;/p&gt;
&lt;h3&gt;
  
  
  Decision 5: Check SQL against today’s schema, and never run it
&lt;/h3&gt;

&lt;p&gt;Generated SQL is the most useful output and the riskiest. So it passes a guard before anyone sees it:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fps6e5zb0r3z7spa98c62.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fps6e5zb0r3z7spa98c62.png" width="799" height="445"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Every generated query is checked against the current schema. A person decides whether to run it.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The core of the guard is short. It parses the query, rejects anything that is not a single read-only statement, and checks every table and column against the catalog built from the live 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="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;sqlglot&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;sqlglot&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;exp&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;sqlglot&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;errors&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;SqlglotError&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;sqlglot&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;optimizer&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;qualify&lt;/span&gt;

&lt;span class="no"&gt;WRITES&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;exp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Insert&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;exp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Update&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="n"&gt;exp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Delete&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;exp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Drop&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="n"&gt;exp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Create&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;exp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Alter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="n"&gt;exp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Merge&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;exp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Command&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;check_sql&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sql&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="p"&gt;:&lt;/span&gt;
    &lt;span class="s2"&gt;"""catalog: {table: {column: type}}
    built from the live schema."""&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;stmts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sqlglot&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;sql&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;read&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"postgres"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nc"&gt;SqlglotError&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;e&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="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"does not parse: {e}"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stmts&lt;/span&gt;&lt;span class="p"&gt;)&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;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"one statement only"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;tree&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;stmts&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="c1"&gt;# 1. Read-only: no writes anywhere.&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="nb"&gt;isinstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tree&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;exp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Query&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="s2"&gt;"SELECT only"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nb"&gt;any&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tree&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find_all&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="no"&gt;WRITES&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;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"SELECT only"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

    &lt;span class="c1"&gt;# 2. Every table must exist now.&lt;/span&gt;
    &lt;span class="n"&gt;ctes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;alias&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;
            &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;tree&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find_all&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;exp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;CTE&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;t&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;tree&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find_all&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;exp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Table&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;catalog&lt;/span&gt;
                &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;ctes&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="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"no table {t.name}"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

    &lt;span class="c1"&gt;# 3. Every column must exist now.&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;qualify&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;qualify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;tree&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;catalog&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;dialect&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"postgres"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;validate_qualify_columns&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nc"&gt;SqlglotError&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;e&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="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"unknown column: {e}"&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="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the guard returns a problem, the model gets the error and tries again, up to a limit. A query that passes is also run through EXPLAIN on the read replica, under a read-only role with a statement timeout, to catch type errors and very expensive plans.&lt;/p&gt;

&lt;p&gt;The guard catches the &lt;strong&gt;loud&lt;/strong&gt; failure from the CLM-482 story: amount no longer exists. The &lt;strong&gt;silent&lt;/strong&gt; one, the missing status, is caught differently. The assistant gets status values from list_status_values, so the new PARTIALLY_APPROVED status appears as soon as the reference table changes.&lt;/p&gt;

&lt;p&gt;The query is then &lt;strong&gt;shown, not run&lt;/strong&gt;. A person reads it and runs it against the read replica.&lt;/p&gt;

&lt;h3&gt;
  
  
  Decision 6: Show freshness, and test after every sync
&lt;/h3&gt;

&lt;p&gt;Every answer ends with where it came from and how current that source is:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fskqak3dw9tdy8wdqqi3q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fskqak3dw9tdy8wdqqi3q.png" width="800" height="459"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Every answer carries its sources and their versions, so a reader can judge how far to trust it.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;After each sync, a set of about 50 real questions from new joiners and past incidents runs against the new index. Each question has the source it should cite and, for SQL questions, the tables it must use. If the pass rate drops sharply, the new index is not switched on and the team is alerted. This is the same release-gate idea Case 05 covers in detail.&lt;/p&gt;

&lt;p&gt;We track four numbers:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fjrw9i9gbrfd46yllkp12.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fjrw9i9gbrfd46yllkp12.png" width="799" height="370"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;What we watch.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Two boundaries
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Permissions.&lt;/strong&gt; Some Confluence pages are restricted. Their restrictions are stored with each chunk and applied as a filter at retrieval time, so the assistant never shows a user something they couldn’t open themselves. This is the same rule as Case 01: access is decided by data, not by the model.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Other teams’ systems.&lt;/strong&gt; The Portal and Intake are not ours, and we can’t keep their internals current. We index only their &lt;strong&gt;published contracts&lt;/strong&gt; : API specs, event schemas and the tables we share. Questions about their internals get a pointer to the owning team.&lt;/p&gt;

&lt;h3&gt;
  
  
  Rollout
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fzhoirjf4b4g768tvj2v9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fzhoirjf4b4g768tvj2v9.png" width="800" height="332"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Start with the part that prevents wrong SQL; retire the hand-built context only when the new one wins.&lt;/em&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Schema catalog and SQL guard first.&lt;/strong&gt; This is where stale knowledge does the most damage, and it needs no embeddings.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Confluence next&lt;/strong&gt; , with section chunking and the 15-minute sync.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Code last&lt;/strong&gt; , chunked by function, class and route, re-indexed on each production deploy.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Compare, then switch.&lt;/strong&gt; Run the golden questions against both the old context and the new indexes. Retire the hand-built context when the new system wins on citations and SQL validity.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  What we would do differently
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Collect the golden questions on day one.&lt;/strong&gt; Every question a new joiner asks in chat is free test data.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Ask the business team for one habit: the feature key in the page title.&lt;/strong&gt; It is the cheapest way to link a spec to its commits and migrations.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Comment the schema.&lt;/strong&gt; Column comments are the highest-value documentation in this system, because the assistant reads them on every SQL question.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Key takeaways
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;A hand-built context is a copy, and copies go stale.&lt;/strong&gt; Index the sources instead.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Sync on change, reconcile on schedule,&lt;/strong&gt; and treat deletes as seriously as updates.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Give each source its own shape:&lt;/strong&gt; sections for docs, functions for code, a catalog for schema.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;When sources disagree, show both.&lt;/strong&gt; Spec, code and schema answer different questions.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Check generated SQL against today’s schema, and never run it automatically.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Put the version on every answer, and test after every sync.&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;em&gt;Next in the series: Case 04, an LLM gateway that cut the bill (GCP). The design and decision records for this case are in the&lt;/em&gt; &lt;a href="https://github.com/prasanth2006z/ai-architecture-casebook/tree/main/cases/03-knowledge-assistant-sync" rel="noopener noreferrer"&gt;&lt;em&gt;series repository&lt;/em&gt;&lt;/a&gt;&lt;em&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Google Cloud and Atlassian details reflect public documentation as of October 2026. Check current docs before you build.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>artificialintelligen</category>
      <category>retrievalaugmentedge</category>
      <category>architecture</category>
      <category>developerproductivit</category>
    </item>
    <item>
      <title>Invoice Extraction with Human Review: Route on Checks, Not on Confidence</title>
      <dc:creator>Prasanth Pattathil</dc:creator>
      <pubDate>Mon, 26 Oct 2026 11:01:02 +0000</pubDate>
      <link>https://dev.to/prasanth_pattathil/invoice-extraction-with-human-review-route-on-checks-not-on-confidence-1non</link>
      <guid>https://dev.to/prasanth_pattathil/invoice-extraction-with-human-review-route-on-checks-not-on-confidence-1non</guid>
      <description>&lt;p&gt;A design case study: why a confidence score can’t decide which invoices get paid, and what to route on instead.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fmy6e9p0ik9ttxc9bnon1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fmy6e9p0ik9ttxc9bnon1.png" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;About this case study.&lt;/strong&gt; This is a composite scenario built on synthetic invoices. It shows a design and how to measure it. The numbers are planning figures, not benchmark results.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  The problem
&lt;/h3&gt;

&lt;p&gt;A mid-sized distributor gets about 6,000 supplier invoices a month. They arrive as PDFs, scans and phone photos in more than one language. Three accounts-payable clerks type each one into the ERP: vendor, invoice number, dates, line items, tax, total, and the purchase order it belongs to.&lt;/p&gt;

&lt;p&gt;The ask from finance was simple: “Let the AI read the invoices, and have people check only the ones it is unsure about.”&lt;/p&gt;

&lt;p&gt;That sentence hides the hard part. &lt;strong&gt;How does the system know when it is unsure?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fttiqlc4nchj8x7d0z1ua.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fttiqlc4nchj8x7d0z1ua.png" width="800" height="370"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Today every field is typed by hand. The goal splits invoices into clean and unsure, but who decides which is which?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The obvious answer is the model’s confidence score: auto-approve above 90%, send to a person below it. That is the design this post argues against.&lt;/p&gt;
&lt;h3&gt;
  
  
  Why confidence alone is the wrong gate
&lt;/h3&gt;

&lt;p&gt;Extraction services return a confidence number for each field. Amazon Textract’s AnalyzeExpense, for example, gives separate scores for the label it found (“Total:”) and the value next to it (“$55.64”). Those scores are useful, but they answer a narrow question: &lt;em&gt;how sure am I that I read these characters correctly?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fq6wtuwzbweufp3be7sl3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fq6wtuwzbweufp3be7sl3.png" width="800" height="408"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;A total read with 99.9% confidence on an invoice that should never be paid.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;They do not answer the questions finance cares about:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Do the line items add up to the total?&lt;/li&gt;
&lt;li&gt;Is this a vendor we actually do business with?&lt;/li&gt;
&lt;li&gt;Does it match an open purchase order?&lt;/li&gt;
&lt;li&gt;Have we already paid this invoice?&lt;/li&gt;
&lt;li&gt;Are the bank details the same ones we have on file?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A model can read a total of $12,480.00 with 99.9% confidence on an invoice that is a duplicate, that belongs to no purchase order, or that asks us to pay a new bank account. High confidence, wrong outcome. The worst errors in accounts payable are not misread digits. They are perfectly read invoices that should never be paid.&lt;/p&gt;

&lt;p&gt;So the rule for this system is the same one Post 1 used for permissions:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Model output is a claim. Checks against our own data are the evidence.&lt;/strong&gt; The model extracts; deterministic checks decide.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3&gt;
  
  
  The design in one picture
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F6lwpubndrc1sltert60t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F6lwpubndrc1sltert60t.png" width="799" height="534"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Extract, validate, route: three outcomes, a 2% audit, and every correction stored.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Five decisions shape this design.&lt;/p&gt;
&lt;h3&gt;
  
  
  Decision 1: The extractor is replaceable; the routing is the product
&lt;/h3&gt;

&lt;p&gt;There are two reasonable extraction options on AWS today:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fjc20i75un863fdngnbru.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fjc20i75un863fdngnbru.png" width="799" height="330"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Textract vs. Bedrock Data Automation.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fnzdp1fxl631kzuhugf78.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fnzdp1fxl631kzuhugf78.png" width="800" height="357"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;The extractor sits behind an adapter. The checks, routing and review loop stay the same.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;We start with AnalyzeExpense and put it behind a small interface that returns one normalized Invoice object: fields, values, confidence and page geometry. If BDA does better on our golden set later, we swap the extractor and change nothing downstream.&lt;/p&gt;

&lt;p&gt;That is the point of this decision. Extraction quality will keep improving and vendors will keep changing. The checks, the routing and the review loop are where our business rules live, so they should not be tied to one extractor.&lt;/p&gt;
&lt;h3&gt;
  
  
  Decision 2: Validate with deterministic checks
&lt;/h3&gt;

&lt;p&gt;After extraction, every invoice runs the same checks. None of them uses a model.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fjg35h8rn2d4lj1kcr8uh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fjg35h8rn2d4lj1kcr8uh.png" width="800" height="512"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Two checks stop an invoice outright; the rest send it to review with a reason.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Two rules are worth pointing out.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Match vendors by tax ID, not name.&lt;/strong&gt; “ACME Ltd”, “Acme Limited” and “ACME LTD.” are the same company to a person and three companies to a string comparison. Names are for display; IDs are for joins.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Never update bank details from an invoice.&lt;/strong&gt; Changed bank details on an invoice are the most common sign of payment fraud. If the invoice disagrees with the vendor master, the invoice goes to a separate queue that only the vendor-management team can clear, and they confirm by calling a phone number already on file. No confidence score can turn this off.&lt;/p&gt;
&lt;h3&gt;
  
  
  Decision 3: Thresholds per field, set by the cost of being wrong
&lt;/h3&gt;

&lt;p&gt;One global threshold treats a misread invoice date the same as a misread total. Those mistakes do not cost the same.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F3gcygxaf7zut132r1mtq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F3gcygxaf7zut132r1mtq.png" width="800" height="361"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Higher cost of error, higher bar to skip review.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;These starting values are not magic. They are tuned on the golden set (Decision 5), and the target is set per field: &lt;em&gt;what is the most auto-approval we can get while keeping this field’s escaped error rate under X?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Arithmetic checks also give a second signal that confidence cannot. If the total was read with 99% confidence but the line items do not add up, one of the numbers is wrong, and confidence did not catch it.&lt;/p&gt;
&lt;h3&gt;
  
  
  Decision 4: Build the review queue; show reviewers only what is flagged
&lt;/h3&gt;
&lt;h4&gt;
  
  
  Why not Amazon A2I
&lt;/h4&gt;

&lt;p&gt;Amazon Augmented AI (A2I) used to be the default answer for “human review on AWS.” In June 2026 AWS announced that A2I, along with SageMaker Ground Truth, is moving to maintenance and stopped accepting new customers on July 30, 2026. A new system should not be built on it.&lt;/p&gt;

&lt;p&gt;The review loop we need is small enough to own:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Step Functions with a task token.&lt;/strong&gt; The workflow pauses at the review step with .waitForTaskToken. The review UI calls SendTaskSuccess with the corrected invoice, and the workflow continues to the ERP. No polling and no extra state machine.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;DynamoDB&lt;/strong&gt; holds the queue: invoice ID, reasons for review, assigned reviewer, SLA timestamp.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A small web UI&lt;/strong&gt; behind Cognito shows the PDF with bounding boxes &lt;strong&gt;only around the flagged fields&lt;/strong&gt; , plus the reason (“line items sum to 11,980.00; total says 12,480.00”).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fwq3sy9agivzpjd8bo7xq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fwq3sy9agivzpjd8bo7xq.png" width="800" height="547"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Left: the workflow pauses on a task token until a reviewer finishes. Right: the reviewer sees only the flagged fields, with the reason.&lt;/em&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  Design the screen for speed, not completeness
&lt;/h4&gt;

&lt;p&gt;The first review screen showed all 25 fields. Reviewers re-checked every one, so reviewing took almost as long as typing. Showing only the flagged fields, with the reason and the highlighted region, turns a review into a 20-second decision instead of a three-minute re-key.&lt;/p&gt;

&lt;p&gt;Two rules keep this honest:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Reviewers must touch every flagged field.&lt;/strong&gt; Each flagged field needs an explicit “confirm” or a correction, so a reviewer can’t approve the whole invoice in one click.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Hard stops never go to the normal queue.&lt;/strong&gt; Duplicate and bank-detail cases have their own queue, their own role, and a four-eyes rule.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;
  
  
  Decision 5: Measure what escapes, not just what gets reviewed
&lt;/h3&gt;

&lt;p&gt;This is the decision most teams skip, and it is the one that matters most.&lt;/p&gt;

&lt;p&gt;If you only look at invoices that went to review, you learn how often reviewers correct the model. You learn &lt;strong&gt;nothing&lt;/strong&gt; about the invoices that were auto-approved, and those are the ones that turn into wrong payments.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fc6fce9lsit8xd8issvgn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fc6fce9lsit8xd8issvgn.png" width="800" height="522"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;The random audit is the only window into invoices nobody looked at.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;So we measure four things:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F8hii4oxailf759h0ydt7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F8hii4oxailf759h0ydt7.png" width="800" height="316"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Four numbers to watch. The escaped error rate is the one that matters most.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;2% random audit&lt;/strong&gt; is the key. A small random sample of auto-approved invoices goes to review anyway, without telling the reviewer it was auto-approved. That is the only unbiased estimate of the escaped error rate. Without it, a rising straight-through rate looks like progress even if it comes from errors slipping through.&lt;/p&gt;

&lt;p&gt;Every correction is stored as (invoice, field, extracted value, corrected value, reason). That store becomes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the &lt;strong&gt;golden set&lt;/strong&gt; for comparing extractors (Textract vs BDA) and tuning thresholds;&lt;/li&gt;
&lt;li&gt;a &lt;strong&gt;vendor watch list&lt;/strong&gt; : vendors whose invoices are corrected often get a stricter threshold or a vendor-specific blueprint;&lt;/li&gt;
&lt;li&gt;the &lt;strong&gt;release gate&lt;/strong&gt; for any change to the extractor or thresholds (more on that in Post 4).&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  A sketch of the router
&lt;/h3&gt;

&lt;p&gt;The routing logic is short, and it should be. It is the most important code in the system, so it needs to be easy to read and test.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;dataclasses&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;dataclass&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;enum&lt;/span&gt; &lt;span class="nf"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;Enum&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Route&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Enum&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="no"&gt;AUTO_APPROVE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"auto_approve"&lt;/span&gt;
    &lt;span class="no"&gt;REVIEW&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"review"&lt;/span&gt;
    &lt;span class="no"&gt;HARD_STOP&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"hard_stop"&lt;/span&gt;

&lt;span class="no"&gt;HARD_STOP_CHECKS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"duplicate"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"bank_details"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nd"&gt;@dataclass&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;CheckResult&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="kt"&gt;str&lt;/span&gt;
    &lt;span class="n"&gt;passed&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt;
    &lt;span class="n"&gt;reason&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;
    &lt;span class="n"&gt;field&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;route&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;checks&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;auto_limit&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;audit_sample&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;failed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;checks&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;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;passed&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

    &lt;span class="c1"&gt;# 1. Fraud and duplicates: nothing overrides these.&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nb"&gt;any&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="no"&gt;HARD_STOP_CHECKS&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;failed&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="nc"&gt;Route&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;HARD_STOP&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;failed&lt;/span&gt;

    &lt;span class="c1"&gt;# 2. Any failed check: review, with the reasons.&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;failed&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;Route&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;REVIEW&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;failed&lt;/span&gt;

    &lt;span class="c1"&gt;# 3. Large invoices always get a person.&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;auto_limit&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;why&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;CheckResult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"amount_limit"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;False&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                          &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"over {auto_limit}"&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;Route&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;REVIEW&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;why&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

    &lt;span class="c1"&gt;# 4. Random audit of clean invoices.&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;audit_sample&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;why&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;CheckResult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"audit_sample"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;False&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                          &lt;span class="s2"&gt;"random audit"&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;Route&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;REVIEW&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;why&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;Route&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;AUTO_APPROVE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fr3h9zi176v47sk2qh6m6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fr3h9zi176v47sk2qh6m6.png" width="800" height="711"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;The router, top to bottom. Confidence never short-cuts the checks.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Notice what is missing: there is no line that says “if confidence is high, skip the checks.” Confidence is only one of the checks (per field), never a way around the others.&lt;/p&gt;

&lt;h3&gt;
  
  
  What we would expect, and how we would know
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fsjqhkb2ez92ua7t3mgxq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fsjqhkb2ez92ua7t3mgxq.png" width="800" height="320"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Each stage opens only when the data from the previous one says it is safe.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;For a rollout like this, a reasonable plan is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Weeks 1–4, shadow mode.&lt;/strong&gt; Everything goes to review. The system’s route is recorded but not used. This builds the golden set and shows what the straight-through rate &lt;em&gt;would&lt;/em&gt; have been.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Then, turn on auto-approve for one vendor group&lt;/strong&gt; with clean history and low amounts. Watch the escaped error rate from the audit sample.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Widen only when the audit says so.&lt;/strong&gt; Each new vendor group needs its escaped error rate under the agreed limit for a full month.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A realistic early target is a straight-through rate between 40% and 60%, with most of the rest needing a fast review of one or two fields instead of full re-keying. The exact number depends on the vendor mix: clean, machine-generated PDFs from large suppliers will pass far more often than phone photos from small ones.&lt;/p&gt;

&lt;h3&gt;
  
  
  What we would do differently
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Start the corrections store on day one.&lt;/strong&gt; Shadow-mode data is the cheapest golden set you will ever get.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Agree the escaped error limit with finance before launch,&lt;/strong&gt; not after the first wrong payment. It is a business decision, not an engineering one.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Treat the vendor master as part of the system.&lt;/strong&gt; Half of the “extraction errors” in a project like this turn out to be stale vendor data. The checks are only as good as the data they check against.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Key takeaways
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Confidence measures reading, not correctness.&lt;/strong&gt; Route on deterministic checks against your own data.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Thresholds belong to fields,&lt;/strong&gt; and are set by the cost of being wrong.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Some cases never auto-approve:&lt;/strong&gt; duplicates, bank-detail changes and large amounts.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Show reviewers only what is flagged,&lt;/strong&gt; and make them touch each flagged field.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Audit a random sample of auto-approved invoices.&lt;/strong&gt; It is the only honest measure of risk.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Keep the extractor replaceable.&lt;/strong&gt; The routing and the review loop are the product.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;em&gt;Next in the series: Case 03, an LLM gateway that cut the bill (GCP). The design and decision records for this case, starting with “route on verified checks, not model confidence,” are in the&lt;/em&gt; &lt;a href="https://github.com/prasanth2006z/ai-architecture-casebook/tree/main/cases/02-invoice-extraction" rel="noopener noreferrer"&gt;&lt;strong&gt;&lt;em&gt;series repository&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt;&lt;em&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;AWS service details reflect public documentation as of October 2026. Check current AWS docs before you build.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>ai</category>
      <category>aws</category>
      <category>machinelearning</category>
      <category>architecture</category>
    </item>
    <item>
      <title>Designing a RAG System That Knows What You’re Allowed to See</title>
      <dc:creator>Prasanth Pattathil</dc:creator>
      <pubDate>Tue, 20 Oct 2026 12:31:01 +0000</pubDate>
      <link>https://dev.to/prasanth_pattathil/designing-a-rag-system-that-knows-what-youre-allowed-to-see-jf8</link>
      <guid>https://dev.to/prasanth_pattathil/designing-a-rag-system-that-knows-what-youre-allowed-to-see-jf8</guid>
      <description>&lt;p&gt;A design case study: choosing between fine-tuning, long context and RAG, and why access control belongs in retrieval, not in the prompt.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fu2qm81lymd2spjf3yia0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fu2qm81lymd2spjf3yia0.png" alt="Diagram: an identity filter for an India employee selects the India parental leave policy and blocks the near-identical UK policy." width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Part of the AI Architecture Casebook: real-world AI problems, redesigned one decision at a time.&lt;/em&gt; &lt;a href="https://medium.com/@prasanth_pattathil/51131e78a179" rel="noopener noreferrer"&gt;&lt;em&gt;Start here&lt;/em&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;An employee in Bengaluru asks the company HR assistant how much parental leave she gets. The answer comes back confident, well written and wrong: it is the UK policy.&lt;/p&gt;

&lt;p&gt;A week later, a junior engineer asks about relocation support and receives details of the executive relocation package, a document he was never meant to see.&lt;/p&gt;

&lt;p&gt;Neither failure came from a bad model. Both came from the architecture around it. This post walks through how I approached the redesign as a design exercise: the options, the decisions, the trade-offs, and one problem that better search alone cannot solve.&lt;/p&gt;

&lt;p&gt;I work on enterprise APIs, where one rule is non-negotiable: authorization is decided at the gateway, from a verified token, never by trusting what the caller says. When I started looking at how RAG systems handle access control, I kept seeing the opposite: permissions written into prompts and left to the model. This case study applies the gateway mindset to retrieval.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;The company, people and documents in this case study are fictional. The architecture patterns are real.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  The problem
&lt;/h3&gt;

&lt;p&gt;Globex is a fictional company with 20,000 employees in 12 countries and about 4,000 HR policy documents. Leave, travel, benefits and expenses all differ by country, and some policies are restricted to senior grades. HR updates around 40 documents every week.&lt;/p&gt;

&lt;p&gt;The first chatbot was built in two weeks. It found the documents that best matched a question, placed them in a long prompt, and asked the model to answer. Within a month, HR had logged three kinds of failure:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Wrong country.&lt;/strong&gt; The UK and India parental leave policies match “parental leave” almost equally well. The bot picked the wrong one.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Leaked policy.&lt;/strong&gt; A junior employee received text from a policy restricted to senior grades.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Stale and uncited answers.&lt;/strong&gt; Answers had no source, and some quoted a travel policy that had been replaced two weeks earlier.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The bot was switched off. HR still answers about 6,000 policy questions a month by hand, and most of the answers are already written down somewhere.&lt;/p&gt;

&lt;p&gt;The second failure is the serious one. A wrong answer is an inconvenience. A restricted document shown to the wrong person is a compliance incident.&lt;/p&gt;

&lt;h3&gt;
  
  
  What the fix had to satisfy
&lt;/h3&gt;

&lt;p&gt;Before choosing any technology, I wrote down what a good answer must look like. Most design mistakes come from skipping this step.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F4kmwydl6mcmnr7e9cme3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F4kmwydl6mcmnr7e9cme3.png" width="800" height="393"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The first line is the one that cannot bend. Every other decision is tuned around it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Four options, and why I rejected three
&lt;/h3&gt;

&lt;p&gt;All four options use a language model. The real question is where the model gets its policy knowledge: trained into the model, or looked up from the documents for each question.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F4l3vddcr0pbur9rigwk6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F4l3vddcr0pbur9rigwk6.png" width="800" height="257"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why not fine-tuning?&lt;/strong&gt; A model is billions of numbers, called weights. Fine-tuning blends the policies into those numbers: no document remains inside the model. Think of a student who memorized the whole handbook, versus one who looks up the right page each time. The memorizer can’t tell you which page a fact came from, so answers can’t be cited. Every one of the 40 weekly policy changes means retraining. And because one model answers everyone, the executive policy can’t be hidden from a junior engineer; it’s mixed into the same numbers. Fine-tuning is good for &lt;em&gt;how&lt;/em&gt; a model writes, not for &lt;em&gt;what&lt;/em&gt; it knows when facts change and have access rules.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why not long context?&lt;/strong&gt; Every model has a context window: how much text it can read for one question. Modern windows hold hundreds of pages, so it’s tempting to skip search and paste a large pile of policies into every prompt. If fine-tuning is the student who memorized the handbook, this is handing the student the &lt;em&gt;whole handbook for every single question&lt;/em&gt;. It fails three ways.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Cost: you pay for every word read, and 100,000 tokens per question costs far more than the 3,000 that matter.&lt;/li&gt;
&lt;li&gt;Accuracy: models miss facts buried in long prompts, and with two near-identical parental leave policies in the pile, they pick the wrong one.&lt;/li&gt;
&lt;li&gt;Security: if the executive policy is in the pile, the model has already read it. And once you choose which documents to send based on who is asking, you have built a retrieval system anyway.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Long context shines when the material is small, shared by everyone and rarely changes, like one contract or one product manual.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why not pure vector search?&lt;/strong&gt; This is the subtle one. Vector search finds text with similar &lt;em&gt;meaning&lt;/em&gt;, and the UK and India policies mean almost exactly the same thing. What separates them are exact words: country names, policy codes, grade labels. That is what keyword search is good at.&lt;/p&gt;

&lt;h3&gt;
  
  
  The architecture
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fbvapjez75f2alzj5eguk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fbvapjez75f2alzj5eguk.png" width="799" height="302"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The Answer API is the only component that sees both who is asking and what is being searched. It reads country and grade from the verified sign-in token, builds the filter itself, and sends only the five best allowed chunks to the model. On AWS this maps to API Gateway and Lambda, Bedrock Knowledge Bases over OpenSearch Serverless, and a Bedrock model with a grounding check.&lt;/p&gt;

&lt;h3&gt;
  
  
  Three decisions that carry the design
&lt;/h3&gt;

&lt;p&gt;I recorded each decision as an Architecture Decision Record (ADR): the context, what we chose, what we rejected, and the signal that would make us change our mind.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Hybrid search with a reranker
&lt;/h3&gt;

&lt;p&gt;Run keyword search (BM25) and vector search together, merge the results, rerank the top candidates and keep the best five. Keyword search catches “India” and “HR-PL-IN-001”; vector search catches “time off after having a baby”.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The AWS detail that almost undid this:&lt;/strong&gt; Bedrock Knowledge Bases support hybrid search on several vector stores, including OpenSearch (Serverless and managed clusters), Aurora PostgreSQL and MongoDB Atlas. On other stores, such as the low-cost S3 Vectors, a hybrid request quietly falls back to semantic-only search, with no error. Choosing the cheapest store would have silently brought back the original bug. That is why the store choice is part of the decision, not an implementation detail.&lt;/p&gt;

&lt;p&gt;Trade-off: OpenSearch Serverless has a minimum cost even when idle, and reranking adds latency. I accept both: a cheaper store would bring back the wrong-country bug, and the reranker still fits within the 3-second budget.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Access control in retrieval, not in the prompt
&lt;/h3&gt;

&lt;p&gt;Every document carries the countries and grades allowed to see it. The API reads the user’s country and grade from their verified sign-in token, builds a filter, and passes it into every search. The model only ever receives chunks the user is entitled to.&lt;/p&gt;

&lt;p&gt;The tempting alternative is to tell the model: “Do not reveal documents the user is not allowed to see.” That is a security control in name only. The model has already received the restricted text, and a cleverly worded question can talk it out of the instruction.&lt;/p&gt;

&lt;p&gt;Two other alternatives I rejected:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Filtering after retrieval, in code:&lt;/strong&gt; restricted chunks still take up slots in the results, pushing out good ones, and one bug leaks data.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A model-generated filter&lt;/strong&gt; (some platforms can build a filter from the question text): useful for relevance, but it is controlled by the user’s words, so it must never carry security.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;The rule:&lt;/strong&gt; the security filter is built from verified identity only. Never from user input, never from model output.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Chunk by policy section
&lt;/h3&gt;

&lt;p&gt;HR policies have numbered sections, and the rules often depend on each other. “Up to 26 weeks of leave” in section 3.1 means little without “after 12 months of service” in section 3.2. A fixed 500-token split can cut a rule away from its condition, so the model answers with half the policy.&lt;/p&gt;

&lt;p&gt;So I split on section headings, and long sections on sub-headings, keeping the parent heading as a prefix.&lt;/p&gt;

&lt;h3&gt;
  
  
  The part that surprised me: better search is not enough
&lt;/h3&gt;

&lt;p&gt;While building the test documents, I put the UK and India parental leave policies side by side. They are the same template with different numbers. Here is section 3.1 from each:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fkz1bk02aoy9ije7cpz25.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fkz1bk02aoy9ije7cpz25.png" width="800" height="197"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Notice what is missing: the country. Once a policy is split into sections, the chunk that holds the answer contains no country name and no policy code. Those live in the title and the first section. So neither vector search nor keyword search can tell these two chunks apart.&lt;/p&gt;

&lt;p&gt;That leads to two separate fixes for two separate cases:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The question names the country&lt;/strong&gt; (“How much parental leave do employees in India get?”). Hybrid search can win here, but only if every chunk is prefixed with its document title, country and policy code. Section chunking without that prefix throws away exactly the words keyword search needs.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The question doesn’t name the country&lt;/strong&gt; (“How much parental leave do I get?”). Here the text contains nothing to search on. No retrieval method can pick the right version from the words alone. Only the identity filter can, because it knows who is asking.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is the real argument for access control in retrieval. It is not only a security feature. For a large share of everyday questions, the user’s identity is the &lt;em&gt;only&lt;/em&gt; signal that separates the right answer from the wrong one.&lt;/p&gt;

&lt;h3&gt;
  
  
  What can still go wrong, and how I’d measure it
&lt;/h3&gt;

&lt;p&gt;No design removes every risk. These are the ones I would watch:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fjnsg8gzcejlzlp2qbwdj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fjnsg8gzcejlzlp2qbwdj.png" width="800" height="435"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Measure retrieval separately from answers.&lt;/strong&gt; Most RAG failures are retrieval failures: if the right section isn’t in the top five, no prompt will fix it. So the evaluation set has 60 questions with a known correct document and section: 30 ordinary questions, 15 where the answer differs by country, 10 with no matching policy, and 5 probes asking about something the user may not see.&lt;/p&gt;

&lt;p&gt;One rule for the leak count: if a result can’t be verified (say, it has no document id), count it as a leak. A broken system should make the security number worse, never better.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;At 10x scale&lt;/strong&gt; (200,000 employees), I would expect these to change first:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Country and grade lists become too coarse; a dedicated authorization service takes over.&lt;/li&gt;
&lt;li&gt;Repeated questions (open-enrollment week) need caching.&lt;/li&gt;
&lt;li&gt;Indexes split by region for data residency.&lt;/li&gt;
&lt;li&gt;Personal questions (“how many leave days do I have left?”) need access to the HR system, which turns this into an agent.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Takeaways
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Write the constraints before choosing the technology.&lt;/strong&gt; The one that cannot bend (access control here) decides most of the design.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Fine-tuning stores style, not changing facts.&lt;/strong&gt; For policies that change weekly and need citations, retrieval wins.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Security belongs in retrieval, not in the prompt.&lt;/strong&gt; If the model has seen the text, a prompt instruction will not reliably keep it secret.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Small platform details can undo big decisions.&lt;/strong&gt; A cheaper vector store would have silently turned hybrid search back into semantic search.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Identity is also a relevance signal.&lt;/strong&gt; When a question doesn’t name the country, only knowing who asks can pick the right answer.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This is the first case study in a series where I take a realistic AI problem, compare the options, and record the decisions as ADRs. The design doc and ADRs are on GitHub: &lt;a href="https://github.com/prasanth2006z/ai-architecture-casebook" rel="noopener noreferrer"&gt;ai-architecture-casebook&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;One simplification to note: real enterprises usually have far richer access rules, such as departments, projects and temporary access. Country and grade are enough to show the pattern. At larger scale, a dedicated authorization service makes the decision, but the principle stays the same: the filter comes from verified identity, never from the prompt.&lt;/p&gt;

&lt;p&gt;How would you have handled the wrong-country problem? I’d like to hear other approaches in the comments.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>rag</category>
      <category>aws</category>
      <category>architecture</category>
    </item>
    <item>
      <title>Designing AI Systems with an API Mindset</title>
      <dc:creator>Prasanth Pattathil</dc:creator>
      <pubDate>Tue, 20 Oct 2026 12:01:02 +0000</pubDate>
      <link>https://dev.to/prasanth_pattathil/designing-ai-systems-with-an-api-mindset-2gbd</link>
      <guid>https://dev.to/prasanth_pattathil/designing-ai-systems-with-an-api-mindset-2gbd</guid>
      <description>&lt;p&gt;Introducing the AI Architecture Casebook, a series on real-world AI design decisions&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fp9st6abprgxv0ii6b8y0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fp9st6abprgxv0ii6b8y0.png" width="800" height="450"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;The system around the model&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;For years, my work has been about how systems talk to each other: who is allowed to call what, where trust is decided, what happens when a dependency fails, and what every call costs. Those questions shaped how I think about any system before a single line of code is written.&lt;/p&gt;

&lt;p&gt;When I started working with AI, I noticed how often those questions go unasked. Permissions end up written into prompts. Costs grow with no budget. Answers ship with no way to tell whether they’re right. The model gets all the attention, while the system around it, where most failures actually happen, gets very little.&lt;/p&gt;

&lt;p&gt;I’m not the only one noticing. In everyday conversations with friends and colleagues from different companies, AI scenarios keep coming up that made the people who designed them stop and rethink. Nobody has all the answers yet. Everyone is learning, me included. And each time, I catch myself asking the same question: &lt;em&gt;what would I do in this situation?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This series is my answer to that question, one problem at a time. Each post takes a real-world AI scenario and works through it the way I’d approach any design: what must never go wrong, which options exist, what each one costs, and which trade-offs I’d accept. I record the decisions as architecture decision records, including the options I rejected and when I’d revisit them.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fv5qrudnpuzr182lc5xzp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fv5qrudnpuzr182lc5xzp.png" width="800" height="510"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The companies and people in these stories are made up. The problems are very real. To keep each case study readable, I simplify on purpose: one company, a few rules, a clear scope. Your real systems are probably messier, with more roles, more exceptions and more history. Where a simplification would change the design at a larger scale, I’ll say so. If you’ve faced something similar, or you’d design it differently, I’d like to hear it in the comments. Learning together is the point.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>architecture</category>
      <category>systemdesign</category>
      <category>api</category>
    </item>
  </channel>
</rss>
