<?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: Avneet bansal</title>
    <description>The latest articles on DEV Community by Avneet bansal (@avneet_bansal_a65b3f31fc4).</description>
    <link>https://dev.to/avneet_bansal_a65b3f31fc4</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%2F4116498%2F405a55bb-2d18-4b5d-b61c-7fc909692a12.jpg</url>
      <title>DEV Community: Avneet bansal</title>
      <link>https://dev.to/avneet_bansal_a65b3f31fc4</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/avneet_bansal_a65b3f31fc4"/>
    <language>en</language>
    <item>
      <title>Sonnet 5.5 Effort Levels: 720 Real Calls, and the One Case Where the Setting Changed the Answer</title>
      <dc:creator>Avneet bansal</dc:creator>
      <pubDate>Mon, 30 Nov 2026 15:21:46 +0000</pubDate>
      <link>https://dev.to/avneet_bansal_a65b3f31fc4/sonnet-55-effort-levels-720-real-calls-and-the-one-case-where-the-setting-changed-the-answer-5ag0</link>
      <guid>https://dev.to/avneet_bansal_a65b3f31fc4/sonnet-55-effort-levels-720-real-calls-and-the-one-case-where-the-setting-changed-the-answer-5ag0</guid>
      <description>&lt;p&gt;&lt;strong&gt;🎚️ Sonnet 5.5 Effort Levels: 720 Real Calls, and the One Case Where the Setting Changed the Answer&lt;/strong&gt;&lt;br&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%2Fs9zkiegych9au4pdjxma.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%2Fs9zkiegych9au4pdjxma.png" alt=" " width="800" height="336"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Sonnet 5.5 on Amazon Bedrock thinks by default (adaptive thinking, effort &lt;code&gt;high&lt;/code&gt;). I ran 8 settings over 4 test sets, 720 calls in total, for about USD 9 at list price.&lt;/li&gt;
&lt;li&gt;On extraction, bug fixing and ordinary capacity-planning questions, every setting scored the same (one ambiguous ticket aside). The default cost 1.5 to 2.8 times more and ran 1.5 to 2 times slower than turning up-front thinking off, and &lt;code&gt;max&lt;/code&gt; cost 4 to 9 times more.&lt;/li&gt;
&lt;li&gt;On an exact-search problem (fewest nodes that fit a set of pods), the low and no-thinking settings scored 75% to 83%. &lt;code&gt;high&lt;/code&gt; scored 92%. &lt;code&gt;xhigh&lt;/code&gt; and &lt;code&gt;max&lt;/code&gt; scored 100%. &lt;code&gt;xhigh&lt;/code&gt; cost half of &lt;code&gt;max&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Every wrong answer was the same one: the greedy first-fit-decreasing answer, one node too many.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;h2&gt;
  
  
  Why a sweep
&lt;/h2&gt;

&lt;p&gt;The Bedrock documentation for Sonnet 5.5 says the effort levels are calibrated differently from Sonnet 5 and recommends running a fresh effort sweep on your own evaluations. It gives generic starting points: &lt;code&gt;medium&lt;/code&gt; for well-specified agentic coding, &lt;code&gt;medium&lt;/code&gt; or &lt;code&gt;low&lt;/code&gt; for latency-sensitive work, &lt;code&gt;high&lt;/code&gt; otherwise. It does not say what each level costs you or buys you on a particular kind of task. This post measures that on four small test sets.&lt;/p&gt;

&lt;p&gt;Three facts from the documentation shape the test:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Adaptive thinking is on by default, and the default effort is &lt;code&gt;high&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;thinking: {"type": "disabled"}&lt;/code&gt; returns a 400 error on Sonnet 5.5. The lowest setting is &lt;code&gt;between_tools&lt;/code&gt;, which turns off up-front thinking and works only at &lt;code&gt;low&lt;/code&gt;, &lt;code&gt;medium&lt;/code&gt; and &lt;code&gt;high&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Thinking tokens are billed as output tokens and are reported in &lt;code&gt;usage.output_tokens_details.thinking_tokens&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Setup
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Model: &lt;code&gt;us.anthropic.claude-sonnet-5-5&lt;/code&gt; on Amazon Bedrock, &lt;code&gt;us-west-2&lt;/code&gt;, &lt;code&gt;max_tokens&lt;/code&gt; 32,000. No call hit the limit.&lt;/li&gt;
&lt;li&gt;8 settings: adaptive thinking at &lt;code&gt;low&lt;/code&gt;, &lt;code&gt;medium&lt;/code&gt;, &lt;code&gt;high&lt;/code&gt;, &lt;code&gt;xhigh&lt;/code&gt; and &lt;code&gt;max&lt;/code&gt;, and &lt;code&gt;between_tools&lt;/code&gt; at &lt;code&gt;low&lt;/code&gt;, &lt;code&gt;medium&lt;/code&gt; and &lt;code&gt;high&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;All data is synthetic, generated from a fixed seed, and every answer is scored by code. Nothing came from a customer.&lt;/li&gt;
&lt;li&gt;Cost uses the third-party list price of USD 2.20 per million input tokens and USD 11.00 per million output tokens. Check the Bedrock pricing page before you rely on the cost columns.&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Test set&lt;/th&gt;
&lt;th&gt;Tasks&lt;/th&gt;
&lt;th&gt;Repeats&lt;/th&gt;
&lt;th&gt;What it checks&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Extraction&lt;/td&gt;
&lt;td&gt;20 incident tickets&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;Four fields as JSON, with distractors such as an older severity and a downstream service&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Capacity planning&lt;/td&gt;
&lt;td&gt;15 problems of 6 to 15 pods&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;Minimum nodes, exact answer, 7 of the 15 built so that the shortcuts fail&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Bug fixing&lt;/td&gt;
&lt;td&gt;8 small Python functions&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;The fix must pass hidden unit tests&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Hard capacity planning&lt;/td&gt;
&lt;td&gt;12 problems of 16 to 20 pods&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;Same question, where first-fit-decreasing or the sum-based bound gives the wrong answer&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;
&lt;h2&gt;
  
  
  Result 1: on three test sets, the setting did not change accuracy
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Setting&lt;/th&gt;
&lt;th&gt;Extraction (20)&lt;/th&gt;
&lt;th&gt;Capacity planning (30)&lt;/th&gt;
&lt;th&gt;Bug fixing (16)&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;between_tools&lt;/code&gt;, low / medium / high&lt;/td&gt;
&lt;td&gt;20 / 20 / 20&lt;/td&gt;
&lt;td&gt;30 / 30 / 30&lt;/td&gt;
&lt;td&gt;16 / 16 / 16&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;adaptive, low&lt;/td&gt;
&lt;td&gt;19&lt;/td&gt;
&lt;td&gt;30&lt;/td&gt;
&lt;td&gt;16&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;adaptive, medium&lt;/td&gt;
&lt;td&gt;19&lt;/td&gt;
&lt;td&gt;30&lt;/td&gt;
&lt;td&gt;16&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;adaptive, high (the default)&lt;/td&gt;
&lt;td&gt;19&lt;/td&gt;
&lt;td&gt;29&lt;/td&gt;
&lt;td&gt;16&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;adaptive, xhigh&lt;/td&gt;
&lt;td&gt;20&lt;/td&gt;
&lt;td&gt;30&lt;/td&gt;
&lt;td&gt;16&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;adaptive, max&lt;/td&gt;
&lt;td&gt;20&lt;/td&gt;
&lt;td&gt;30&lt;/td&gt;
&lt;td&gt;16&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The three extraction misses come from two tickets with the same wording. Each states an error count and then says that a few dozen errors "belong to an unrelated maintenance window". Adaptive &lt;code&gt;low&lt;/code&gt; and &lt;code&gt;high&lt;/code&gt; returned 8505 instead of 8575 on one ticket, and adaptive &lt;code&gt;medium&lt;/code&gt; returned 63425 instead of 63497 on the other, because they subtracted the maintenance errors. That is ambiguity in my wording more than a reasoning failure, and the &lt;code&gt;between_tools&lt;/code&gt; and &lt;code&gt;max&lt;/code&gt; runs read it the way I intended.&lt;/p&gt;

&lt;p&gt;What changed was cost and time:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Setting&lt;/th&gt;
&lt;th&gt;Extraction cost per 100 calls&lt;/th&gt;
&lt;th&gt;p50&lt;/th&gt;
&lt;th&gt;Capacity cost per 100 calls&lt;/th&gt;
&lt;th&gt;p50&lt;/th&gt;
&lt;th&gt;Bug-fix cost per 100 calls&lt;/th&gt;
&lt;th&gt;p50&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;between_tools&lt;/code&gt; low&lt;/td&gt;
&lt;td&gt;USD 0.08&lt;/td&gt;
&lt;td&gt;0.7 s&lt;/td&gt;
&lt;td&gt;USD 0.65&lt;/td&gt;
&lt;td&gt;3.7 s&lt;/td&gt;
&lt;td&gt;USD 0.29&lt;/td&gt;
&lt;td&gt;1.5 s&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;adaptive high (default)&lt;/td&gt;
&lt;td&gt;USD 0.22&lt;/td&gt;
&lt;td&gt;1.4 s&lt;/td&gt;
&lt;td&gt;USD 1.10&lt;/td&gt;
&lt;td&gt;5.7 s&lt;/td&gt;
&lt;td&gt;USD 0.43&lt;/td&gt;
&lt;td&gt;2.2 s&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;adaptive max&lt;/td&gt;
&lt;td&gt;USD 0.66&lt;/td&gt;
&lt;td&gt;3.8 s&lt;/td&gt;
&lt;td&gt;USD 2.52&lt;/td&gt;
&lt;td&gt;10.2 s&lt;/td&gt;
&lt;td&gt;USD 2.66&lt;/td&gt;
&lt;td&gt;11.2 s&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Two details are worth knowing. First, adaptive &lt;code&gt;low&lt;/code&gt; and &lt;code&gt;medium&lt;/code&gt; did not think at all on any call in the sweep (0 thinking tokens on all 66 calls per setting in the three sets above). Second, the default &lt;code&gt;high&lt;/code&gt; thought on 13 of 20 extraction calls, 28 of 30 capacity calls and 6 of 16 bug fixes, so it decides per request, and it paid for thinking that did not change the answer.&lt;/p&gt;
&lt;h2&gt;
  
  
  Result 2: on exact search, the setting decided the answer
&lt;/h2&gt;

&lt;p&gt;The hard set asks for the minimum number of nodes of 10,000 millicores for 16 to 20 pods. An exact solver gives the true answer. Greedy first-fit-decreasing, the usual shortcut, gives a wrong answer on 9 of the 12 problems, and the sum-based lower bound is wrong on 3. I selected problems where the shortcuts fail.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Setting&lt;/th&gt;
&lt;th&gt;Correct (of 24)&lt;/th&gt;
&lt;th&gt;Thinking tokens, median&lt;/th&gt;
&lt;th&gt;p50&lt;/th&gt;
&lt;th&gt;Cost per 100 calls&lt;/th&gt;
&lt;th&gt;Cost per correct answer&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;between_tools&lt;/code&gt; low&lt;/td&gt;
&lt;td&gt;18 (75%)&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;9.8 s&lt;/td&gt;
&lt;td&gt;USD 1.20&lt;/td&gt;
&lt;td&gt;USD 0.016&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;between_tools&lt;/code&gt; medium&lt;/td&gt;
&lt;td&gt;20 (83%)&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;9.3 s&lt;/td&gt;
&lt;td&gt;USD 1.22&lt;/td&gt;
&lt;td&gt;USD 0.015&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;between_tools&lt;/code&gt; high&lt;/td&gt;
&lt;td&gt;18 (75%)&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;10.4 s&lt;/td&gt;
&lt;td&gt;USD 1.40&lt;/td&gt;
&lt;td&gt;USD 0.019&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;adaptive low&lt;/td&gt;
&lt;td&gt;18 (75%)&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;10.6 s&lt;/td&gt;
&lt;td&gt;USD 1.42&lt;/td&gt;
&lt;td&gt;USD 0.019&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;adaptive medium&lt;/td&gt;
&lt;td&gt;18 (75%)&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;12.2 s&lt;/td&gt;
&lt;td&gt;USD 1.51&lt;/td&gt;
&lt;td&gt;USD 0.020&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;adaptive high (default)&lt;/td&gt;
&lt;td&gt;22 (92%)&lt;/td&gt;
&lt;td&gt;1,585&lt;/td&gt;
&lt;td&gt;18.4 s&lt;/td&gt;
&lt;td&gt;USD 2.76&lt;/td&gt;
&lt;td&gt;USD 0.030&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;adaptive xhigh&lt;/td&gt;
&lt;td&gt;24 (100%)&lt;/td&gt;
&lt;td&gt;2,144&lt;/td&gt;
&lt;td&gt;17.9 s&lt;/td&gt;
&lt;td&gt;USD 3.75&lt;/td&gt;
&lt;td&gt;USD 0.038&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;adaptive max&lt;/td&gt;
&lt;td&gt;24 (100%)&lt;/td&gt;
&lt;td&gt;5,763&lt;/td&gt;
&lt;td&gt;41.2 s&lt;/td&gt;
&lt;td&gt;USD 7.76&lt;/td&gt;
&lt;td&gt;USD 0.078&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;All 30 wrong answers across the eight settings were one node above the true minimum, which is the first-fit-decreasing answer. None was below it. Without thinking, the model gives the greedy answer. With enough thinking, it checks whether a tighter packing exists and finds it.&lt;/p&gt;

&lt;p&gt;Setting &lt;code&gt;between_tools&lt;/code&gt; at &lt;code&gt;high&lt;/code&gt; did not help (75%), because effort only matters when thinking is on. Adaptive &lt;code&gt;low&lt;/code&gt; and &lt;code&gt;medium&lt;/code&gt; never thought on these problems either, and they scored the same as no thinking. Adaptive &lt;code&gt;high&lt;/code&gt; thought on all 24 calls and scored 22. &lt;code&gt;xhigh&lt;/code&gt; thought a little longer (median 2,144 tokens against 1,585) and scored 24.&lt;/p&gt;
&lt;h2&gt;
  
  
  Which setting for which case
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Your case&lt;/th&gt;
&lt;th&gt;Setting I would start with&lt;/th&gt;
&lt;th&gt;Why, from these runs&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Short extraction or classification, latency matters&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;between_tools&lt;/code&gt;, &lt;code&gt;low&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Same accuracy, about one third of the default's cost, half its latency&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Routine, well-specified code fixes&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;between_tools&lt;/code&gt;, &lt;code&gt;medium&lt;/code&gt; or adaptive &lt;code&gt;low&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;16 of 16 everywhere, cost 1.5 to 9 times lower than &lt;code&gt;high&lt;/code&gt; or &lt;code&gt;max&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Answer must be exact and a greedy shortcut is wrong&lt;/td&gt;
&lt;td&gt;adaptive &lt;code&gt;xhigh&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;100%, and about half the cost and latency of &lt;code&gt;max&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Anything you have not tested&lt;/td&gt;
&lt;td&gt;Run a small sweep first&lt;/td&gt;
&lt;td&gt;The default is not wrong, it just costs more than the easy cases need&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;I would not use &lt;code&gt;max&lt;/code&gt; without a measured gain over &lt;code&gt;xhigh&lt;/code&gt;. In this sweep it never gained anything and cost about twice as much with 2.3 times the median latency.&lt;/p&gt;
&lt;h2&gt;
  
  
  Limits of this sweep
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;One model, one region, one day. Results can shift when the model or its settings are updated.&lt;/li&gt;
&lt;li&gt;24 calls per setting on the hard set. The gap between 75% and 100% is large, but a bigger sample would give tighter numbers, and the difference between &lt;code&gt;high&lt;/code&gt; (22) and &lt;code&gt;xhigh&lt;/code&gt; (24) is within noise.&lt;/li&gt;
&lt;li&gt;The tasks are synthetic and have one correct answer. Open-ended writing, long tool-use loops and multi-turn work were not tested. I dropped the tool-use case to keep the sweep focused.&lt;/li&gt;
&lt;li&gt;Costs use a list price I did not verify on the AWS page.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Decide for your own workload in an afternoon
&lt;/h2&gt;

&lt;p&gt;The post cannot tell you which setting your work needs, but the method fits on one page. Take 30 to 50 real cases that have a checkable answer, run them through the same settings, and compare accuracy against cost.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. One request function.&lt;/strong&gt; Effort goes in &lt;code&gt;output_config&lt;/code&gt;, not in &lt;code&gt;thinking&lt;/code&gt;, or the call fails with a 400 error. The thinking spend comes back in &lt;code&gt;usage&lt;/code&gt;.&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;json&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;boto3&lt;/span&gt;
&lt;span class="n"&gt;brt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;boto3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;client&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"bedrock-runtime"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;region_name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"us-west-2"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="no"&gt;SETTINGS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="s2"&gt;"no_thinking_low"&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="s2"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"between_tools"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"low"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;"adaptive_high"&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="s2"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"adaptive"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"high"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;"adaptive_xhigh"&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="s2"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"adaptive"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"xhigh"&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;def&lt;/span&gt; &lt;span class="nf"&gt;ask&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;setting&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;thinking&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;effort&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;SETTINGS&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;setting&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;body&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"anthropic_version"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"bedrock-2023-05-31"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"max_tokens"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;32000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="s2"&gt;"thinking"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;thinking&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"output_config"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"effort"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;effort&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="s2"&gt;"messages"&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="s2"&gt;"role"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"user"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"content"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;prompt&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;t0&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;time&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;loads&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;brt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;invoke_model&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;modelId&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"us.anthropic.claude-sonnet-5-5"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                                    &lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;dumps&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;body&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="s2"&gt;"body"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;read&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b&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="s2"&gt;"text"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&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;b&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"content"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s2"&gt;"text"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;u&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"usage"&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;"text"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"seconds"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;time&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;t0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"stop"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"stop_reason"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="s2"&gt;"in"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;u&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"input_tokens"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"out"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;u&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"output_tokens"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="s2"&gt;"thinking"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;u&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="s2"&gt;"output_tokens_details"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"thinking_tokens"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. A scorer that does not trust the model.&lt;/strong&gt; For the capacity problem the answer is checked against an exact solver. For bug fixes the corrected function runs against hidden unit tests.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;min_nodes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cap&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;           &lt;span class="c1"&gt;# exact minimum number of bins&lt;/span&gt;
    &lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;best&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;sorted&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;reverse&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="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="p"&gt;[&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;items&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;go&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;bins&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="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bins&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;best&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;==&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;items&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;best&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="o"&gt;=&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;bins&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="n"&gt;seen&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;set&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;b&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&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;bins&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;if&lt;/span&gt; &lt;span class="n"&gt;bins&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;cap&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;bins&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;]&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;seen&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="n"&gt;seen&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;bins&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;b&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;bins&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nf"&gt;go&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;bins&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;bins&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="n"&gt;bins&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nf"&gt;go&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;bins&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;bins&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;go&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="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="n"&gt;best&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3. Count wrong answers, not only the average.&lt;/strong&gt; A setting that is wrong 25 times in 100 is not a rounding error. In my hard set the wrong answers were all the greedy one, which told me what the cheap settings skip.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Compare cost with what a wrong answer costs you.&lt;/strong&gt; On the hard set, moving from no thinking to &lt;code&gt;xhigh&lt;/code&gt; raised the cost by USD 2.55 per 100 calls and removed about 25 wrong answers per 100, which is about USD 0.10 per wrong answer avoided. If a wrong answer costs you more than ten cents (a bad capacity plan, a failed deployment, an engineer's review time), the extra thinking pays for itself. If the answer is a label that someone checks anyway, as in my extraction set, it does not.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. Keep the setting per task type.&lt;/strong&gt; You can choose the setting per request, so extraction can run with no thinking and the exact-answer step can run at &lt;code&gt;xhigh&lt;/code&gt;. Choose one thinking mode per conversation and keep it, because switching modes invalidates the prompt cache, according to the Bedrock documentation.&lt;/p&gt;

&lt;h2&gt;
  
  
  Should you change anything?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;If you migrated from an older Claude model without changing the request, thinking is now on and billed as output tokens. Check &lt;code&gt;usage.output_tokens_details.thinking_tokens&lt;/code&gt; on your current traffic before you do anything else.&lt;/li&gt;
&lt;li&gt;If most of your calls look like my extraction or bug-fix sets, &lt;code&gt;between_tools&lt;/code&gt; at &lt;code&gt;low&lt;/code&gt; or &lt;code&gt;medium&lt;/code&gt; gave the same accuracy for less.&lt;/li&gt;
&lt;li&gt;If any step in your pipeline needs an exact answer that a shortcut gets wrong, test &lt;code&gt;xhigh&lt;/code&gt; on that step.&lt;/li&gt;
&lt;li&gt;If you have no evaluation set yet, start with the default and measure, since the sweep above shows the default is safe but not always necessary.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>ai</category>
      <category>aws</category>
      <category>llm</category>
      <category>bedrock</category>
    </item>
    <item>
      <title>Lambda Durable Functions from First Deploy to Production: Setup, Real Cost, and the Replay Rule</title>
      <dc:creator>Avneet bansal</dc:creator>
      <pubDate>Fri, 20 Nov 2026 15:16:21 +0000</pubDate>
      <link>https://dev.to/avneet_bansal_a65b3f31fc4/lambda-durable-functions-from-first-deploy-to-production-setup-real-cost-and-the-replay-rule-2g9e</link>
      <guid>https://dev.to/avneet_bansal_a65b3f31fc4/lambda-durable-functions-from-first-deploy-to-production-setup-real-cost-and-the-replay-rule-2g9e</guid>
      <description>&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%2Fgvakgveufgz7q8oh9i2j.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%2Fgvakgveufgz7q8oh9i2j.png" alt=" " width="800" height="336"&gt;&lt;/a&gt;&lt;br&gt;
A normal Lambda function that has to wait pays for the wait. If your workflow extracts a document, then waits five minutes for a confirmation, the function sits there billing for all five minutes, and anything longer than the 15-minute limit does not fit at all. Lambda durable functions change that. The function checkpoints its progress, suspends while it waits, and resumes on a new invocation with its state intact.&lt;/p&gt;

&lt;p&gt;I wanted numbers rather than the announcement, so I ran the same workflow both ways in my test account and measured what Lambda billed. I also tested the parts that matter once real users depend on it: human approvals, retries, deploying new code while executions are waiting, and the replay rule that can make a durable function quietly do the wrong thing.&lt;/p&gt;

&lt;p&gt;Everything below comes from runs on 2026-11-19 in &lt;code&gt;us-west-2&lt;/code&gt;, Python 3.14, x86, 512 MB, with &lt;code&gt;aws-durable-execution-sdk-python&lt;/code&gt; 3.1.0. Costs are list prices without the free tier. The whole test cost under two cents.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Same workflow with a five-minute wait: the normal function was billed 303,118 ms, the durable function 3,405 ms across two invocations. Per run that is $0.00253 against $0.000077, about 33 times less.&lt;/li&gt;
&lt;li&gt;Durable functions add a charge per operation ($8 per million). For a workflow with almost no waiting, the durable version cost 2.2 times more than the normal one ($0.0000768 against $0.0000343).&lt;/li&gt;
&lt;li&gt;Setup is different in five places: durable mode is set only when you create the function, you must invoke a version or alias, the IAM managed policy is different, logs default to JSON, and you should bundle the SDK because the runtime ships an older major version (1.5.0 against 3.1.0).&lt;/li&gt;
&lt;li&gt;An execution started on a published version resumed on its original code after I deployed new code. An execution started on &lt;code&gt;$LATEST&lt;/code&gt; ran its first step on the old code and its last step on the new code.&lt;/li&gt;
&lt;li&gt;Code that is not wrapped in a step runs again on every resume. In five cloud runs with an unsafe approval gate, four executed a different action than the one shown to the approver.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;h2&gt;
  
  
  What changes compared to a normal Lambda
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;Normal Lambda&lt;/th&gt;
&lt;th&gt;Durable function&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Longest single run&lt;/td&gt;
&lt;td&gt;15 minutes (function timeout)&lt;/td&gt;
&lt;td&gt;Execution timeout up to about one year; each invocation still has the function timeout&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Waiting&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;time.sleep&lt;/code&gt;, billed for the whole wait&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;context.wait&lt;/code&gt; or a callback; no duration charge while suspended&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;How it resumes&lt;/td&gt;
&lt;td&gt;It does not&lt;/td&gt;
&lt;td&gt;A new invocation replays the handler from the top and skips completed steps&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Extra charges&lt;/td&gt;
&lt;td&gt;None&lt;/td&gt;
&lt;td&gt;$8 per million durable operations, $0.25 per GB written, $0.15 per GB-month retained&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Enabling it&lt;/td&gt;
&lt;td&gt;Not applicable&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;DurableConfig&lt;/code&gt; at creation; cannot be added to an existing function&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Invoking&lt;/td&gt;
&lt;td&gt;Function name is enough&lt;/td&gt;
&lt;td&gt;Must invoke a version, alias or &lt;code&gt;$LATEST&lt;/code&gt;; a bare name is rejected&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;IAM role&lt;/td&gt;
&lt;td&gt;&lt;code&gt;AWSLambdaBasicExecutionRole&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;AWSLambdaBasicDurableExecutionRolePolicy&lt;/code&gt; (logs plus checkpoint and state actions)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Default log format&lt;/td&gt;
&lt;td&gt;Text&lt;/td&gt;
&lt;td&gt;JSON&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;
&lt;h2&gt;
  
  
  The cost test: the same five-minute workflow, two ways
&lt;/h2&gt;

&lt;p&gt;The workflow is a small document pipeline: validate, extract (about 2 seconds of work), enrich (about 1 second), wait five minutes for a confirmation, then finalize. In the normal function the wait is a &lt;code&gt;time.sleep(300)&lt;/code&gt;. In the durable function it is &lt;code&gt;context.wait(Duration.from_seconds(300))&lt;/code&gt;. Both functions had 512 MB.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;Normal Lambda&lt;/th&gt;
&lt;th&gt;Durable function&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Invocations&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Billed duration&lt;/td&gt;
&lt;td&gt;303,118 ms&lt;/td&gt;
&lt;td&gt;3,259 ms + 146 ms = 3,405 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Compute&lt;/td&gt;
&lt;td&gt;$0.0025260&lt;/td&gt;
&lt;td&gt;$0.0000284&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Requests&lt;/td&gt;
&lt;td&gt;$0.0000002&lt;/td&gt;
&lt;td&gt;$0.0000004&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Durable operations (6)&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;$0.0000480&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Data written and retained (about 0.5 KB)&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;$0.0000001&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Total per run&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;$0.0025262&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;$0.0000769&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Per million runs (calculated)&lt;/td&gt;
&lt;td&gt;about $2,526&lt;/td&gt;
&lt;td&gt;about $77&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The six durable operations are the execution start, four steps and one wait. The data written is the step results and the input, about half a kilobyte here, so it barely registers.&lt;/p&gt;

&lt;p&gt;Lambda bills a durable function only while one of its invocations is running, so the question is how long the invocations ran. The execution history answers that, because it records when each invocation ended. The first invocation ran the three steps, started the wait and ended about 3.3 seconds after the start. For the next five minutes no invocation was running, so nothing was billed. Then a second invocation started, replayed the handler, ran &lt;code&gt;finalize&lt;/code&gt; and ended 174 ms later. Those two invocations are the 3,259 ms and 146 ms in the table:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ExecutionStarted                    01:18:05.833
StepSucceeded   validate            01:18:05.939
StepSucceeded   extract             01:18:07.973
StepSucceeded   enrich              01:18:09.012
WaitStarted     await_confirmation  01:18:09.047
InvocationCompleted                 01:18:09.163   first invocation ends
WaitSucceeded   await_confirmation  01:23:09.047
StepSucceeded   finalize            01:23:09.146
InvocationCompleted                 01:23:09.269   second invocation ends
ExecutionSucceeded                  01:23:09.269
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  When the durable version costs more
&lt;/h3&gt;

&lt;p&gt;The operation charge is fixed per execution, so it only pays off when there is real waiting. With a one-second wait, the normal function was billed 4,090 ms and cost $0.0000343. The durable function was billed 3,394 ms but cost $0.0000768, because its six operations alone cost $0.000048.&lt;/p&gt;

&lt;p&gt;That gives a simple rule of thumb. Six operations cost the same as about 5.8 seconds of compute at 512 MB (and about 2.9 seconds at 1 GB). If your workflow waits longer than that, durable is cheaper. If it mostly computes and never waits, a normal function or a single invocation is cheaper.&lt;/p&gt;

&lt;h3&gt;
  
  
  Past the 15-minute limit
&lt;/h3&gt;

&lt;p&gt;A normal function cannot wait 20 minutes, because its timeout stops at 900 seconds. The durable function with a 20-minute wait succeeded with two invocations billed 4,997 ms and 1,304 ms. The second one was slower because the function had been idle for 20 minutes and started cold. Total cost: $0.000101.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setup, step by step, and where it differs
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1. The role.&lt;/strong&gt; Attach &lt;code&gt;AWSLambdaBasicDurableExecutionRolePolicy&lt;/code&gt;. It covers CloudWatch Logs and adds &lt;code&gt;lambda:CheckpointDurableExecution&lt;/code&gt; and &lt;code&gt;lambda:GetDurableExecutionState&lt;/code&gt;, so you do not need the basic execution policy as well.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Bundle the SDK.&lt;/strong&gt; The Python 3.14 runtime includes the durable SDK, but the copy it loaded was version 1.5.0, while the current release is 3.1.0. AWS recommends bundling it for production, and with a major-version gap that matters. My deployment package with the SDK was 167 KB; the SDK needs only &lt;code&gt;boto3&lt;/code&gt;, which the runtime has.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Create the function with &lt;code&gt;DurableConfig&lt;/code&gt;.&lt;/strong&gt; This is the one setting you cannot fix later. I tried adding it to an existing normal function and got: "You cannot add a durable configuration to a function that was originally created with no durable configuration."&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;lam&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create_function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nc"&gt;FunctionName&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"dlab-durable"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nc"&gt;Runtime&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"python3.14"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nc"&gt;Role&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;role_arn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nc"&gt;Handler&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"fn_durable.handler"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nc"&gt;Code&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"ZipFile"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;zip_bytes&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nc"&gt;Timeout&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;          &lt;span class="c1"&gt;# per invocation, still capped at 900&lt;/span&gt;
    &lt;span class="nc"&gt;MemorySize&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;512&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nc"&gt;DurableConfig&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"ExecutionTimeout"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3600&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"RetentionPeriodInDays"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The two timeouts mean different things. &lt;code&gt;Timeout&lt;/code&gt; limits each invocation, so it only needs to cover the longest stretch of work between waits; 60 seconds was plenty here. &lt;code&gt;ExecutionTimeout&lt;/code&gt; limits the whole execution across all invocations and waits.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Publish a version and invoke it.&lt;/strong&gt; Invoking the bare function name failed with "You cannot invoke a durable function using an unqualified ARN." Use a version or an alias. Use asynchronous invocation for anything long. A synchronous call to my function was refused with "You cannot synchronously invoke a durable function with an executionTimeout greater than 30 minutes."&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;lam&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;invoke&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nc"&gt;FunctionName&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"dlab-durable"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nc"&gt;Qualifier&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"3"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;                       &lt;span class="c1"&gt;# a version or alias&lt;/span&gt;
    &lt;span class="nc"&gt;InvocationType&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"Event"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nc"&gt;DurableExecutionName&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"order-cd2e485d"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="c1"&gt;# optional, makes the start idempotent&lt;/span&gt;
    &lt;span class="nc"&gt;Payload&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;dumps&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;DurableExecutionName&lt;/code&gt; is worth setting. I started the same name twice, two seconds apart. Both calls returned the same execution ARN, and only one execution existed afterwards. That is the protection you want when a client retries after a timeout.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. Write the handler.&lt;/strong&gt; Wrap each unit of real work in a step, and use waits for waiting:&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;aws_durable_execution_sdk_python&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;DurableContext&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;StepContext&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;durable_execution&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;durable_step&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;aws_durable_execution_sdk_python&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;config&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;Duration&lt;/span&gt;

&lt;span class="nd"&gt;@durable_step&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;extract&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;step_ctx&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;StepContext&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;dict&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="c1"&gt;# real work, runs once, result checkpointed&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;"fields"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;fields&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nd"&gt;@durable_execution&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;handler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;DurableContext&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;doc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"doc_id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"doc_id"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"pages"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"pages"&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;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;step&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;validate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"validate"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;extracted&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;step&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;extract&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"extract"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;enriched&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;step&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;enrich&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;extracted&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"enrich"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;wait&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Duration&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;from_seconds&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;300&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"await_confirmation"&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;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;step&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;finalize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;enriched&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"finalize"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;6. Read the history.&lt;/strong&gt; &lt;code&gt;get_durable_execution&lt;/code&gt; gives the status and result, and &lt;code&gt;get_durable_execution_history&lt;/code&gt; gives every step, wait, retry and invocation with timestamps and request IDs. The request IDs match the &lt;code&gt;platform.report&lt;/code&gt; lines in CloudWatch Logs, which is how I got the billed duration of each invocation. The logs are JSON by default, so a filter for the text &lt;code&gt;REPORT RequestId&lt;/code&gt; that works on normal functions finds nothing; filter for &lt;code&gt;platform.report&lt;/code&gt; instead.&lt;/p&gt;

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

&lt;p&gt;When a durable function resumes, Lambda runs the handler again from the top. Steps that already finished are not executed again; their saved results are returned. Code outside a step simply runs again.&lt;/p&gt;

&lt;p&gt;That matters most in an approval gate. I ran one in the cloud two ways, with a stand-in agent that picks one of five actions at random, the way a model call can return a different answer each time. The unsafe version calls the agent outside a step:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;proposal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;agent_proposes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;                            &lt;span class="c1"&gt;# outside a step: runs again on resume&lt;/span&gt;
&lt;span class="n"&gt;shown&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;step&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;show_to_human&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;proposal&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"show_to_human"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;wait&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Duration&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;from_seconds&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"human_approval"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;executed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;step&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;execute_action&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;proposal&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"execute"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The safe version changes one line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;proposal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;step&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;agent&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;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"agent"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;         &lt;span class="c1"&gt;# runs once, result saved&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each run is one approval. The function asks the agent for an action, shows that action to the approver, waits, and then executes an action. The two columns that matter are what the approver saw and what the function actually executed. They should always be the same.&lt;/p&gt;

&lt;p&gt;I ran each version five times, as five separate executions, because the stand-in agent picks at random. With five possible actions, a single unsafe run can match by luck one time in five, so one run would prove nothing.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Run&lt;/th&gt;
&lt;th&gt;Version&lt;/th&gt;
&lt;th&gt;Approver saw&lt;/th&gt;
&lt;th&gt;Function executed&lt;/th&gt;
&lt;th&gt;Same?&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;Unsafe&lt;/td&gt;
&lt;td&gt;request_info&lt;/td&gt;
&lt;td&gt;request_info&lt;/td&gt;
&lt;td&gt;yes (by luck)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;Unsafe&lt;/td&gt;
&lt;td&gt;partial_refund&lt;/td&gt;
&lt;td&gt;refund&lt;/td&gt;
&lt;td&gt;no&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;Unsafe&lt;/td&gt;
&lt;td&gt;deny&lt;/td&gt;
&lt;td&gt;partial_refund&lt;/td&gt;
&lt;td&gt;no&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;Unsafe&lt;/td&gt;
&lt;td&gt;partial_refund&lt;/td&gt;
&lt;td&gt;escalate&lt;/td&gt;
&lt;td&gt;no&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;Unsafe&lt;/td&gt;
&lt;td&gt;escalate&lt;/td&gt;
&lt;td&gt;refund&lt;/td&gt;
&lt;td&gt;no&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;td&gt;Safe&lt;/td&gt;
&lt;td&gt;escalate&lt;/td&gt;
&lt;td&gt;escalate&lt;/td&gt;
&lt;td&gt;yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;td&gt;Safe&lt;/td&gt;
&lt;td&gt;request_info&lt;/td&gt;
&lt;td&gt;request_info&lt;/td&gt;
&lt;td&gt;yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;8&lt;/td&gt;
&lt;td&gt;Safe&lt;/td&gt;
&lt;td&gt;partial_refund&lt;/td&gt;
&lt;td&gt;partial_refund&lt;/td&gt;
&lt;td&gt;yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;9&lt;/td&gt;
&lt;td&gt;Safe&lt;/td&gt;
&lt;td&gt;escalate&lt;/td&gt;
&lt;td&gt;escalate&lt;/td&gt;
&lt;td&gt;yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;td&gt;Safe&lt;/td&gt;
&lt;td&gt;request_info&lt;/td&gt;
&lt;td&gt;request_info&lt;/td&gt;
&lt;td&gt;yes&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;In the unsafe runs, the agent ran a second time after the wait and picked a new action, and that new action is what got executed. In the safe runs, the agent ran once, its answer was saved, and the same answer was used after the wait.&lt;/p&gt;

&lt;p&gt;Every unsafe run finished with status &lt;code&gt;SUCCEEDED&lt;/code&gt;. Four of the five executed something the approver never saw, and nothing in the status shows it. The one match was luck. The rule: anything that can return a different value or has a side effect goes inside a step. That includes model calls, API calls, timestamps, random values and IDs.&lt;/p&gt;

&lt;h2&gt;
  
  
  Human approval with a callback
&lt;/h2&gt;

&lt;p&gt;For a real approval you do not want a timer, you want to wait until a person answers. A callback does that:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;proposal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;step&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;agent&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;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"agent"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;cb&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create_callback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"human_approval"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nc"&gt;CallbackConfig&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="nc"&gt;Duration&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;from_seconds&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;120&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;step&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;notify_approver&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cb&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;callback_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;proposal&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"notify_approver"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;raw&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cb&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;result&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;                  &lt;span class="c1"&gt;# suspends until someone answers or the timeout passes&lt;/span&gt;
&lt;span class="n"&gt;decision&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;loads&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;raw&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;isinstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;raw&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="n"&gt;raw&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The approver, or whatever system they use, answers with one API call:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;lam&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send_durable_execution_callback_success&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nc"&gt;CallbackId&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;callback_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nc"&gt;Result&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;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;"approved"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"approver"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"lab-approver"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;encode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="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;What I saw:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;I answered 20 seconds after the request. The execution resumed and executed the approved action, with two invocations billed 1,170 ms and 139 ms.&lt;/li&gt;
&lt;li&gt;The result came back to the handler as a JSON string, not a dictionary. My first version called &lt;code&gt;.get()&lt;/code&gt; on it and the execution failed with an &lt;code&gt;AttributeError&lt;/code&gt;, so parse it.&lt;/li&gt;
&lt;li&gt;A second answer to the same callback was rejected with &lt;code&gt;CallbackTimeoutException&lt;/code&gt;: "The callback is either timed out or already completed." A double click on "approve" cannot approve twice.&lt;/li&gt;
&lt;li&gt;With a 30-second timeout and no answer, the history recorded &lt;code&gt;CallbackTimedOut&lt;/code&gt; at exactly 30 seconds, and the execution ended &lt;code&gt;FAILED&lt;/code&gt; with &lt;code&gt;CallbackTimeoutError&lt;/code&gt;. A late answer after that was rejected the same way.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Leave the timeout at its default and the callback has no timeout, so a forgotten request can hold an execution until the execution timeout. Set one, and catch &lt;code&gt;CallbackTimeoutError&lt;/code&gt; so a missed approval escalates instead of failing silently.&lt;/p&gt;

&lt;h2&gt;
  
  
  Retries
&lt;/h2&gt;

&lt;p&gt;Steps retry with backoff when you give them a retry strategy:&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="no"&gt;RETRY&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;StepConfig&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;retry_strategy&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nf"&gt;create_retry_strategy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;RetryStrategyConfig&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;max_attempts&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;initial_delay&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nc"&gt;Duration&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;from_seconds&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;max_delay&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nc"&gt;Duration&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;from_seconds&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;backoff_rate&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mf"&gt;2.0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;retryable_error_types&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nc"&gt;ConnectionError&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;first&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;step&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;expensive&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;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"expensive"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;second&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;step&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;flaky&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;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"flaky"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="no"&gt;RETRY&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;flaky&lt;/code&gt; step failed on attempts 1 and 2 and succeeded on attempt 3, which the history records with &lt;code&gt;StepFailed&lt;/code&gt; and &lt;code&gt;RetryDetails&lt;/code&gt; for each attempt. Each retry ran in its own short invocation (130 ms and 149 ms billed), and the function was not running in between. The &lt;code&gt;expensive&lt;/code&gt; step before it ran once: its result, including a random token, was the same at the end as after attempt 1.&lt;/p&gt;

&lt;p&gt;Two things to know. Each attempt counts as a durable operation, so retries add a little cost. And the default step semantics are at least once per retry, so a step that charges a card or sends an email should be idempotent on its own, for example by passing an idempotency key to the downstream API.&lt;/p&gt;

&lt;h2&gt;
  
  
  Deploying while executions are waiting
&lt;/h2&gt;

&lt;p&gt;This is the production question few posts answer. I started two executions that each run a step, wait 90 seconds and run another step, and both steps report the code version. One started on published version 2, one on &lt;code&gt;$LATEST&lt;/code&gt;. While both were waiting, I deployed new code and published version 3.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Started on&lt;/th&gt;
&lt;th&gt;First step ran&lt;/th&gt;
&lt;th&gt;Step after the wait ran&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Version 2&lt;/td&gt;
&lt;td&gt;v1 code&lt;/td&gt;
&lt;td&gt;v1 code&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;$LATEST&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;v1 code&lt;/td&gt;
&lt;td&gt;v2 code&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The published version stayed on the code it started with. The &lt;code&gt;$LATEST&lt;/code&gt; execution switched code in the middle of one execution. Here that only changed a label, but if the new code adds, removes or renames a step, replay will not line up with the saved history. Invoke a published version or an alias in production, keep old versions until their executions finish, and treat &lt;code&gt;$LATEST&lt;/code&gt; as for testing only.&lt;/p&gt;

&lt;h2&gt;
  
  
  Production checklist
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Invoke a published version or an alias, never &lt;code&gt;$LATEST&lt;/code&gt;, and keep old versions until their executions drain.&lt;/li&gt;
&lt;li&gt;Bundle the SDK and pin its version.&lt;/li&gt;
&lt;li&gt;Put every model call, API call, timestamp, random value and side effect inside a step.&lt;/li&gt;
&lt;li&gt;Make side-effecting steps idempotent; retries are at least once by default.&lt;/li&gt;
&lt;li&gt;Set a callback timeout, and handle &lt;code&gt;CallbackTimeoutError&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Set &lt;code&gt;DurableExecutionName&lt;/code&gt; from a business key so client retries do not start duplicates.&lt;/li&gt;
&lt;li&gt;Size the function timeout to the longest stretch of work between waits, and the execution timeout to the whole process.&lt;/li&gt;
&lt;li&gt;Keep step results small; the limit is 1 MB per operation and every byte is written and retained.&lt;/li&gt;
&lt;li&gt;Set &lt;code&gt;RetentionPeriodInDays&lt;/code&gt; to what you need for audit (1 to 90 days, default 14), since retained data is billed.&lt;/li&gt;
&lt;li&gt;Watch the durable metrics (&lt;code&gt;DurableExecutionFailed&lt;/code&gt;, &lt;code&gt;DurableExecutionTimedOut&lt;/code&gt;, &lt;code&gt;DurableExecutionOperations&lt;/code&gt;) and alert on failures, because a wrong result can still show &lt;code&gt;SUCCEEDED&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Define the function, version and alias in infrastructure as code, since durable mode cannot be added later.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Durable functions, Managed Instances or Step Functions
&lt;/h2&gt;

&lt;p&gt;Durable functions fit a workflow that spends most of its time waiting: approvals, callbacks, scheduled delays, polling. They are the wrong tool for one continuous computation that is busy for a long time, because there is nothing to suspend. That case is Lambda Managed Instances, which I tested in &lt;a href="https://dev.to/avneet_bansal_a65b3f31fc4/i-ran-a-lambda-function-for-20-minutes-straight-inside-the-90-minute-managed-instances-timeout-12n5"&gt;I Ran a Lambda Function for 20 Minutes Straight&lt;/a&gt;. The short rule: if the clock is long because your code is waiting, use durable functions; if it is long because your code is busy, use Managed Instances.&lt;/p&gt;

&lt;p&gt;Step Functions still makes sense when the workflow coordinates many AWS services, needs a visual definition, or must never depend on replay discipline in your own code. Durable functions keep the logic in one handler you can read top to bottom, and in exchange you own the replay rule.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I did not test
&lt;/h2&gt;

&lt;p&gt;High concurrency, payloads near the 1 MB limit, parallel and map operations, encryption with a customer key, arm64, and anything longer than 20 minutes. Each run used one workflow shape on one day in one Region, so measure your own workload before you take the cost ratio as given.&lt;/p&gt;

</description>
      <category>aws</category>
      <category>serverless</category>
      <category>lambda</category>
      <category>python</category>
    </item>
    <item>
      <title>EKS Best Practices Across the SDLC: When to Apply Them and How to Catch Drift</title>
      <dc:creator>Avneet bansal</dc:creator>
      <pubDate>Thu, 19 Nov 2026 03:24:05 +0000</pubDate>
      <link>https://dev.to/avneet_bansal_a65b3f31fc4/eks-best-practices-across-the-sdlc-when-to-apply-them-and-how-to-catch-drift-4l4</link>
      <guid>https://dev.to/avneet_bansal_a65b3f31fc4/eks-best-practices-across-the-sdlc-when-to-apply-them-and-how-to-catch-drift-4l4</guid>
      <description>&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%2Fkguozbaxa45np5nyso9q.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%2Fkguozbaxa45np5nyso9q.png" alt=" " width="800" height="336"&gt;&lt;/a&gt;&lt;br&gt;
The AWS EKS Best Practices Guide is a strong reference, and most teams read it once, around launch. The harder part is knowing which practice to apply in which phase, and noticing when a cluster has quietly stopped following them. This post covers both, based on EKS platforms I have built and operated, including one migration where a GitOps-first approach saved about 50% of the planned time.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Decide the expensive-to-change practices (networking, identity, cluster layout) at design time. Enforce the rest in the pipeline. Verify all of them continuously.&lt;/li&gt;
&lt;li&gt;Security goes first on anything you cannot cheaply change later, such as identity and network boundaries. It does not outrank everything else forever.&lt;/li&gt;
&lt;li&gt;Practices change. The &lt;code&gt;aws-auth&lt;/code&gt; ConfigMap, IRSA versus Pod Identity, and add-on defaults have all moved. A cluster that was compliant a year ago may not be today.&lt;/li&gt;
&lt;li&gt;Drift is the default state. Treat every practice as something to check on a schedule, not something you did once.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Which phase, which practice
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;SDLC phase&lt;/th&gt;
&lt;th&gt;Practices to settle&lt;/th&gt;
&lt;th&gt;Why then&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Design&lt;/td&gt;
&lt;td&gt;VPC and subnet sizing, IPv4 versus IPv6, cluster-per-environment layout, identity model, multi-tenancy boundaries&lt;/td&gt;
&lt;td&gt;These are hard or impossible to change after workloads land. Pod IP exhaustion is a design-time failure that shows up in production.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Build&lt;/td&gt;
&lt;td&gt;Infrastructure as code, GitOps contract, node provisioning (Karpenter or Auto Mode), add-on versions&lt;/td&gt;
&lt;td&gt;Anything created by hand cannot be reviewed, repeated, or diffed later.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Test&lt;/td&gt;
&lt;td&gt;Upgrade rehearsal, PodDisruptionBudgets, probes, resource requests and limits, load and scale behavior&lt;/td&gt;
&lt;td&gt;Upgrades and scale events are where weak workload settings surface. Test them somewhere that is allowed to break.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Deploy&lt;/td&gt;
&lt;td&gt;Admission policy, image provenance, progressive delivery&lt;/td&gt;
&lt;td&gt;The pipeline is the cheapest enforcement point. A policy that blocks a bad manifest costs minutes. A finding in production costs days.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Operate&lt;/td&gt;
&lt;td&gt;Observability, cost visibility, upgrade cadence, access review, drift detection&lt;/td&gt;
&lt;td&gt;Operations is where practices decay, so this phase owns the checking.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  How I applied this on a migration
&lt;/h2&gt;

&lt;p&gt;The platform was an on-premises Docker estate moved to EKS, delivered across three environments (an internal lab, a sandbox, and production) with no direct access to the later ones. That constraint forced the practices into code early.&lt;/p&gt;

&lt;p&gt;Design decisions came first: cluster layout per environment, networking, and the identity model. Build followed with Terraform for the cluster and a GitOps Bridge for the handoff to ArgoCD, with Karpenter handling nodes. The bridge keeps infrastructure facts in one interface and application manifests free of hard-coded values. I covered that contract in &lt;a href="https://dev.to/avneet_bansal_a65b3f31fc4/the-gitops-bridge-keep-terraform-and-argocd-out-of-each-others-way-3721"&gt;The GitOps Bridge&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Because each environment came from the same code, validating in the lab meant the sandbox and production were the same shape. The saving of about 50% in delivery time came from removing per-environment hand-work, not from any one tool being fast.&lt;/p&gt;

&lt;p&gt;On an earlier platform with several clusters, the practice that mattered most was a fixed upgrade cadence. Quarterly, rehearsed in a lower environment first, with zero downtime as the target. A predictable rhythm keeps the version gap small, and a small gap makes each upgrade routine.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where to start, and how much it matters
&lt;/h2&gt;

&lt;p&gt;Start in design, not in operations. The practices with the highest cost of change are the ones to settle first:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;IP address planning and subnet sizing.&lt;/li&gt;
&lt;li&gt;Cluster and account boundaries.&lt;/li&gt;
&lt;li&gt;Identity for both humans and workloads.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Retrofitting these means rebuilding clusters. Practices like resource limits or probe tuning can be fixed per workload at any time, so they can wait for the Test phase.&lt;/p&gt;

&lt;p&gt;On importance, I rank by cost of being wrong. A missed probe setting causes a bad rollout. A wrong identity or network boundary causes an incident or a rebuild.&lt;/p&gt;

&lt;h2&gt;
  
  
  Should security come first
&lt;/h2&gt;

&lt;p&gt;For identity, network boundaries, and secrets, yes. These are the decisions that are expensive to reverse and that define blast radius. Settle them in design and enforce them in the pipeline.&lt;/p&gt;

&lt;p&gt;Security does not outrank everything indefinitely, though. A cluster that is perfectly locked down but cannot be upgraded, observed, or scaled becomes a security problem of its own, because stale versions accumulate known vulnerabilities. Reliability and operability practices protect the security posture over time.&lt;/p&gt;

&lt;p&gt;A working order that has held up for me:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Identity and network boundaries (design).&lt;/li&gt;
&lt;li&gt;Reproducibility through IaC and GitOps (build).&lt;/li&gt;
&lt;li&gt;Upgrade path and observability (test and operate).&lt;/li&gt;
&lt;li&gt;Cost controls (operate, but visible from day one).&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Practices change, so recheck them
&lt;/h2&gt;

&lt;p&gt;Several recommendations have moved, and an environment built a while ago may still follow the old ones.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Cluster access.&lt;/strong&gt; The &lt;code&gt;aws-auth&lt;/code&gt; ConfigMap is deprecated. The &lt;a href="https://docs.aws.amazon.com/eks/latest/best-practices/cluster-access-management.html" rel="noopener noreferrer"&gt;EKS best practices guide&lt;/a&gt; says to use the Cluster Access Management API (access entries) for new clusters and to migrate existing ones. Before removing ConfigMap entries that EKS created for managed node groups or Fargate profiles, confirm the matching access entries exist, as the &lt;a href="https://docs.aws.amazon.com/eks/latest/userguide/migrating-access-entries.html" rel="noopener noreferrer"&gt;migration guide&lt;/a&gt; notes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Workload identity.&lt;/strong&gt; EKS Pod Identity is now the recommended approach for new workloads, with IRSA still fully supported. Existing IRSA setups do not need an emergency rewrite, but new work should default to Pod Identity.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Node management.&lt;/strong&gt; EKS Auto Mode and Karpenter change who owns node lifecycle, upgrades, and add-ons. If your cluster predates them, the manual node-group practices in older runbooks may no longer be the right ones.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Component behavior.&lt;/strong&gt; Agents and add-ons have their own failure modes at scale. A public issue on the EKS Pod Identity agent (#171) showed credential renewals continuing for pods that no longer existed, and I opened a fix for it (pull request #192, under review at the time of writing). Keep add-on versions current and read release notes, because behavior at scale is not always in the defaults.&lt;/p&gt;

&lt;h2&gt;
  
  
  Catching drift
&lt;/h2&gt;

&lt;p&gt;Drift happens in two ways: the cluster moves away from your code, or your code moves away from current guidance. Both need a check.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Cluster versus code.&lt;/strong&gt; Run &lt;code&gt;terraform plan&lt;/code&gt; on a schedule and alert on a non-empty diff. Let ArgoCD report out-of-sync applications and treat that as a signal, not noise. Disable manual edits where you can.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cluster versus policy.&lt;/strong&gt; Admission policy (Kyverno or OPA Gatekeeper) catches new violations. Run the same policies in audit mode against existing resources to find old ones.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cluster versus guidance.&lt;/strong&gt; Put a recurring review on the calendar, quarterly alongside the upgrade, to compare your setup with the current best practices guide. Check access method, workload identity method, add-on versions, and deprecated API usage.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Deprecated APIs.&lt;/strong&gt; Scan for them before every upgrade, not after.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cost drift.&lt;/strong&gt; Review node utilization and Karpenter consolidation behavior monthly. Over-provisioning accumulates quietly.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A short checklist for the quarterly review:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Is the cluster version within the supported window?&lt;/li&gt;
&lt;li&gt;Is access managed through access entries, with no leftover ConfigMap entries?&lt;/li&gt;
&lt;li&gt;Are new workloads using Pod Identity?&lt;/li&gt;
&lt;li&gt;Do add-on versions match what the pipeline defines?&lt;/li&gt;
&lt;li&gt;Does the audit-mode policy report show zero new violations?&lt;/li&gt;
&lt;li&gt;Does the Terraform plan show no unexplained diff?&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Takeaway
&lt;/h2&gt;

&lt;p&gt;Treat best practices as a lifecycle, not a launch gate. Settle the expensive decisions in design, enforce the rest in the pipeline, and check all of them on a schedule. Security leads on identity and boundaries, and operability keeps that security from decaying.&lt;/p&gt;

&lt;p&gt;If your cluster is more than a year old, the access method is the first thing to look at.&lt;/p&gt;

</description>
      <category>aws</category>
      <category>kubernetes</category>
      <category>eks</category>
      <category>devops</category>
    </item>
    <item>
      <title>I Ran a Lambda Function for 20 Minutes Straight: Inside the 90-Minute Managed Instances Timeout</title>
      <dc:creator>Avneet bansal</dc:creator>
      <pubDate>Tue, 17 Nov 2026 03:49:08 +0000</pubDate>
      <link>https://dev.to/avneet_bansal_a65b3f31fc4/i-ran-a-lambda-function-for-20-minutes-straight-inside-the-90-minute-managed-instances-timeout-12n5</link>
      <guid>https://dev.to/avneet_bansal_a65b3f31fc4/i-ran-a-lambda-function-for-20-minutes-straight-inside-the-90-minute-managed-instances-timeout-12n5</guid>
      <description>&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%2Fx7y0aexul5p9l2vb3asu.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%2Fx7y0aexul5p9l2vb3asu.png" alt=" " width="800" height="336"&gt;&lt;/a&gt;&lt;br&gt;
For a decade, 900 seconds was the hard wall on a single Lambda invocation. If one continuous computation needed longer, Lambda was the wrong tool and you knew it. That wall just moved for one specific case: functions running on Lambda Managed Instances, invoked asynchronously or through an event source mapping, can now run for up to 90 minutes, six times the old limit. Synchronous request/response invocations are still capped at 15 minutes.&lt;/p&gt;

&lt;p&gt;I wanted the real thing rather than the announcement, so I built a Lambda Managed Instances (LMI) function and ran a single continuous invocation past the old 900-second ceiling to confirm it genuinely holds. It does. The setup is more involved than a normal Lambda, and a few steps are not obvious from the first read of the docs, so this is the practitioner's version with the parts that actually tripped me up.&lt;/p&gt;

&lt;p&gt;To be precise about scope up front: I ran one invocation for 20 minutes, not the full 90. Twenty minutes is enough to prove the behavior that matters, that a single invocation now runs well past the old limit, without holding a provisioned instance longer than necessary. The ceiling is 90 minutes; what I measured is a clean crossing of the old 15-minute wall and then some.&lt;/p&gt;

&lt;p&gt;All numbers below are from runs on Python 3.13 in &lt;code&gt;us-west-2&lt;/code&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  What Managed Instances actually is
&lt;/h2&gt;

&lt;p&gt;Classic Lambda runs your code in a Firecracker microVM that AWS spins up on demand. LMI runs your function on current-generation EC2 instances instead, while Lambda still handles instance lifecycle, patching, routing, and scaling. You get the EC2 pricing model and longer runtimes; you give up the pure per-millisecond, scale-to-zero behavior. The billing follows EC2 instance time, not the classic per-GB-second model, which is the single most important thing to understand before you turn it on.&lt;/p&gt;

&lt;p&gt;The function code itself is ordinary. My handler is a plain loop that does light CPU work and logs a heartbeat every minute with its elapsed time and remaining budget:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;handler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;run_seconds&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"run_seconds"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1200&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;start&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;time&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;minute&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;time&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;run_seconds&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;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;time&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;minute&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;dumps&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="s2"&gt;"heartbeat_minute"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;minute&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="s2"&gt;"elapsed_s"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;round&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;time&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="s2"&gt;"remaining_ms"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get_remaining_time_in_millis&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="s2"&gt;"past_15_min"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;time&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;900&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;minute&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
        &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="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;"status"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"DONE"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"elapsed_s"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;round&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;time&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Nothing about that code is special. The timeout, the capacity, and the invocation path are where LMI differs.&lt;/p&gt;

&lt;h2&gt;
  
  
  The proof, from the logs
&lt;/h2&gt;

&lt;p&gt;I set the function timeout to 1800 seconds and invoked it asynchronously with a 20-minute workload. The first heartbeat told me the longer timeout was really in effect:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;remaining_ms at start = 1,799,999
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A classic Lambda would report at most 900,000. Then the run crossed the line that used to be impossible:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nl"&gt;"heartbeat_minute"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"elapsed_s"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;900.5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"remaining_ms"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;899532&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"past_15_min"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nl"&gt;"heartbeat_minute"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"elapsed_s"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;960.5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"remaining_ms"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;839501&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"past_15_min"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nl"&gt;"heartbeat_minute"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;19&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"elapsed_s"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;1140.6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"remaining_ms"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;659406&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"past_15_min"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And the platform's own report gives the authoritative number. The &lt;code&gt;platform.report&lt;/code&gt; log record for the invocation shows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;durationMs = 1,200,625.546
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That is a single invocation billed for roughly 20 minutes of continuous execution. The classic hard cap is 900,000 ms. This one ran about a third past it, in one invocation, with no chaining, no state handoff, no Step Functions. That is the whole point of the feature in one number.&lt;/p&gt;

&lt;h2&gt;
  
  
  The setup gotchas
&lt;/h2&gt;

&lt;p&gt;This is the part worth the price of admission, because the happy-path docs do not dwell on these and each one cost me time.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A function is not enough; you need a capacity provider.&lt;/strong&gt; LMI functions run inside a capacity provider, which is the security and compute boundary. Creating one requires a VPC configuration (subnets and a security group) and an operator IAM role that lets Lambda manage EC2 on your behalf. The managed policy &lt;code&gt;AWSLambdaManagedEC2ResourceOperator&lt;/code&gt; grants exactly those permissions, with a trust policy for &lt;code&gt;lambda.amazonaws.com&lt;/code&gt;. The default VPC works fine for a test.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The function memory floor is 2048 MB.&lt;/strong&gt; My first &lt;code&gt;create_function&lt;/code&gt; call failed with a plain validation error: LMI functions must have memory greater than or equal to 2048. A 512 MB function that is perfectly normal on classic Lambda is rejected here. Set it to at least 2048 and move on.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The function is created but not invocable.&lt;/strong&gt; After creation, the function sits in state &lt;code&gt;ActiveNonInvocable&lt;/code&gt;. This surprised me, because classic Lambda is invocable the moment it is active. LMI does not cold-start on an incoming invocation. It scales asynchronously on resource signals, so until capacity exists, there is nothing to run your code. An invoke at this point goes nowhere.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;You make it invocable by provisioning capacity.&lt;/strong&gt; Publish a version, then set a minimum on it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;version&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;lam&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;publish_version&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;FunctionName&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="no"&gt;FN&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"Version"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;lam&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;put_function_scaling_config&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nc"&gt;FunctionName&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="no"&gt;FN&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Qualifier&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;version&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nc"&gt;FunctionScalingConfig&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"MinExecutionEnvironments"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"MaxExecutionEnvironments"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Setting &lt;code&gt;MinExecutionEnvironments&lt;/code&gt; to 1 forces Lambda to provision an execution environment, which brings the function to an invocable state. One caution: calling &lt;code&gt;put_function_scaling_config&lt;/code&gt; immediately after &lt;code&gt;publish_version&lt;/code&gt; can throw a transient &lt;code&gt;ResourceConflictException&lt;/code&gt;. A short retry with backoff clears it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The instances are hidden by default, not missing.&lt;/strong&gt; I looked for the EC2 instance with a plain &lt;code&gt;describe_instances&lt;/code&gt; and found nothing, in any state. It is easy to conclude that LMI instances never show up in your account, but that is not the case. Since EC2 Managed Resource Visibility launched in April 2026, instances that an AWS service launches on your behalf are hidden from the EC2 console and from &lt;code&gt;DescribeInstances&lt;/code&gt; unless you ask for them. One flag brings them back:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;aws ec2 describe-instances --include-managed-resources \
  --filters &lt;span class="nv"&gt;Name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;instance-state-name,Values=pending,running \
  --query &lt;span class="s1"&gt;'Reservations[].Instances[].[InstanceId,InstanceType,State.Name,LaunchTime]'&lt;/span&gt; \
  --output table
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each instance carries the tags &lt;code&gt;aws:ec2:managed-launch = lambda-managed-instances&lt;/code&gt; and &lt;code&gt;aws:lambda:capacity-provider = &amp;lt;capacity provider ARN&amp;gt;&lt;/code&gt;, which is the reliable way to tell which capacity provider owns it. If you leave instance types at the default, Lambda picks them for you (&lt;code&gt;m7a.xlarge&lt;/code&gt; is a typical choice), and a capacity provider can run more than one instance. Hidden instances still run and still bill, so this check is worth running after every teardown.&lt;/p&gt;

&lt;p&gt;You can also make managed instances visible in the EC2 console with the &lt;code&gt;ModifyManagedResourceVisibility&lt;/code&gt; setting, but that change is account-wide and also surfaces EKS, ECS, and other service-managed instances for everyone in the account. For a single check, the flag is the lighter option. For watching the run itself, CloudWatch logs and the &lt;code&gt;platform.report&lt;/code&gt; records remain the best view.&lt;/p&gt;

&lt;h2&gt;
  
  
  Changes from a normal Lambda, in one place
&lt;/h2&gt;

&lt;p&gt;If you already know how to deploy a classic Lambda, these are the only things that change. The snippets are the exact boto3 calls from my run.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Two IAM roles instead of one.&lt;/strong&gt; The execution role is the same as always (&lt;code&gt;AWSLambdaBasicExecutionRole&lt;/code&gt;). The new one is an operator role that Lambda assumes to manage EC2 for you, with the managed policy &lt;code&gt;AWSLambdaManagedEC2ResourceOperator&lt;/code&gt; and a trust policy for &lt;code&gt;lambda.amazonaws.com&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. A capacity provider, created before the function.&lt;/strong&gt; This is the piece with no classic Lambda equivalent. It holds the VPC, the operator role, and the instance requirements. Wait for it to reach &lt;code&gt;Active&lt;/code&gt; before creating the function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;lam&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create_capacity_provider&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nc"&gt;CapacityProviderName&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"lmi-lab-cp"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nc"&gt;VpcConfig&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"SubnetIds"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;subnets&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"SecurityGroupIds"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;sg&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="nc"&gt;PermissionsConfig&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"CapacityProviderOperatorRoleArn"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;operator_role_arn&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nc"&gt;InstanceRequirements&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"Architectures"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"x86_64"&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="nc"&gt;CapacityProviderScalingConfig&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"ScalingMode"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Auto"&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;strong&gt;3. Three settings on &lt;code&gt;create_function&lt;/code&gt;.&lt;/strong&gt; &lt;code&gt;CapacityProviderConfig&lt;/code&gt; is what makes it a Managed Instances function. &lt;code&gt;Timeout&lt;/code&gt; goes above 900 (I used 1800; the async ceiling is 5400). &lt;code&gt;MemorySize&lt;/code&gt; must be at least 2048.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;lam&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create_function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nc"&gt;FunctionName&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"lmi-lab-fn"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nc"&gt;Runtime&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"python3.13"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nc"&gt;Role&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;exec_role_arn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nc"&gt;Handler&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"lmi_handler.handler"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nc"&gt;Code&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"ZipFile"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;zip_bytes&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nc"&gt;Timeout&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1800&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;       &lt;span class="c1"&gt;# classic Lambda rejects anything above 900&lt;/span&gt;
    &lt;span class="nc"&gt;MemorySize&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;2048&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;    &lt;span class="c1"&gt;# LMI minimum&lt;/span&gt;
    &lt;span class="nc"&gt;CapacityProviderConfig&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="s2"&gt;"LambdaManagedInstancesCapacityProviderConfig"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"CapacityProviderArn"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;cp_arn&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;4. Publish a version and give it capacity.&lt;/strong&gt; A classic function is invocable once it is &lt;code&gt;Active&lt;/code&gt;. An LMI function stays &lt;code&gt;ActiveNonInvocable&lt;/code&gt; until you publish a version and set &lt;code&gt;MinExecutionEnvironments&lt;/code&gt; on it, as shown above. Poll &lt;code&gt;get_function&lt;/code&gt; on that version until it reports &lt;code&gt;Active&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. Invoke the version, asynchronously.&lt;/strong&gt; Invoke the qualified version (or an alias pointing to it), not &lt;code&gt;$LATEST&lt;/code&gt;, because the capacity is attached to the version. Use &lt;code&gt;InvocationType="Event"&lt;/code&gt; or an event source mapping. A synchronous &lt;code&gt;RequestResponse&lt;/code&gt; call is still capped at 15 minutes, whatever the function timeout says.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;lam&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;invoke&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nc"&gt;FunctionName&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"lmi-lab-fn:1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;   &lt;span class="c1"&gt;# qualified version, not $LATEST&lt;/span&gt;
    &lt;span class="nc"&gt;InvocationType&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"Event"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;        &lt;span class="c1"&gt;# async: up to 90 minutes&lt;/span&gt;
    &lt;span class="nc"&gt;Payload&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;dumps&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"run_seconds"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1200&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;encode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="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;strong&gt;6. Expect more than one invocation per environment.&lt;/strong&gt; Classic Lambda runs one invocation per execution environment at a time. LMI can run several at once in the same environment. For Python this uses separate processes, so global variables are not shared, but &lt;code&gt;/tmp&lt;/code&gt; is shared and memory use multiplies with concurrency. Use &lt;code&gt;context.aws_request_id&lt;/code&gt; in any temp file names. If you do not set them, Lambda applies these defaults: &lt;code&gt;PerExecutionEnvironmentMaxConcurrency&lt;/code&gt; of 16 and &lt;code&gt;ExecutionEnvironmentMemoryGiBPerVCpu&lt;/code&gt; of 2.0. Both live under &lt;code&gt;CapacityProviderConfig&lt;/code&gt; and can be lowered for CPU-heavy or memory-heavy work. My test ran a single invocation, so this did not come up, but it matters for real workloads.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;7. Tear down in order.&lt;/strong&gt; Delete the function first, which detaches its versions from the capacity provider. Then delete the capacity provider, retrying until the detach finishes, because it cannot be deleted while versions are still attached. Then delete the two roles and the log group. An LMI function left with capacity keeps an instance billing even when nothing invokes it.&lt;/p&gt;

&lt;p&gt;Do not try to terminate the instances yourself. Deleting the capacity provider is what removes them, and it is quick: the function is typically gone within a minute of &lt;code&gt;delete-function&lt;/code&gt;, and the instances move to &lt;code&gt;terminated&lt;/code&gt; shortly after the capacity provider delete is accepted. If you create the function in the console instead of code, there is one more thing to clean up: the console makes its own execution role (named like &lt;code&gt;&amp;lt;function&amp;gt;-role-xxxxxxxx&lt;/code&gt; under &lt;code&gt;service-role/&lt;/code&gt;) plus a customer-managed &lt;code&gt;AWSLambdaBasicExecutionRole-&amp;lt;id&amp;gt;&lt;/code&gt; policy. Detach and delete the policy after the role, or it stays behind in IAM. Finish with the &lt;code&gt;--include-managed-resources&lt;/code&gt; check above to confirm nothing is still running.&lt;/p&gt;

&lt;h2&gt;
  
  
  The same setup in the console
&lt;/h2&gt;

&lt;p&gt;If you prefer clicking to code, the Lambda console covers almost all of it. The order is the same: operator role, capacity provider, then the function.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Operator role (IAM console).&lt;/strong&gt; Go to IAM, then Roles, then Create role. Choose Lambda as the trusted service, attach &lt;code&gt;AWSLambdaManagedEC2ResourceOperator&lt;/code&gt;, and give it a name.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Capacity provider (Lambda console).&lt;/strong&gt; In the Lambda console, open Capacity providers from the left menu and choose Create capacity provider. Enter a name, then select the VPC, subnets, security group, and the operator role you just created. The default VPC is fine for a test. Under Advanced settings, choose the architecture (x86_64 or arm64) and set a maximum vCPU count. Keep that number small for a test so a mistake cannot scale up far. Leave instance types at the default unless you need a specific family. Create it and wait for the status to show Active.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Function.&lt;/strong&gt; Open Event Functions, choose Create function, then Author from scratch. Pick a runtime and the same architecture as the capacity provider. Under Additional configurations, set Compute type to Lambda Managed Instances, and the capacity provider you created should already be selected. Set memory to at least 2048 MB, then choose Create function. Creation takes several minutes, because the console publishes a version and provisions capacity on the capacity provider for you. This is the step you do by hand with &lt;code&gt;publish_version&lt;/code&gt; and &lt;code&gt;put_function_scaling_config&lt;/code&gt; in code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Timeout.&lt;/strong&gt; Under Configuration, then General configuration, set Timeout up to 5400 seconds. If the field will not accept a value above 900, set it from the CLI instead:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;aws lambda update-function-configuration --function-name &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;your-function&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; --timeout 5400
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Invoking it.&lt;/strong&gt; This is the one place the console cannot get you the long run. The Test button makes a synchronous call, and synchronous calls stop at 15 minutes whatever the timeout says. For a run past 15 minutes, invoke the published version asynchronously from the CLI, or attach an asynchronous trigger such as an S3 event or an EventBridge rule:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;aws lambda invoke --function-name &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;your-function&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;:1 --invocation-type Event \
  --cli-binary-format raw-in-base64-out --payload &lt;span class="s1"&gt;'{"run_seconds":1200}'&lt;/span&gt; out.json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Cleanup.&lt;/strong&gt; Delete the function, then the capacity provider, then the roles. Remember the extra execution role and policy the console creates, covered in step 7 above.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cost, stated plainly
&lt;/h2&gt;

&lt;p&gt;Because LMI bills on EC2 instance time, the cost model is "how long was an instance up," not "how many GB-seconds did I consume." For this experiment, one instance was provisioned for roughly the setup, the 20-minute run, and teardown, well under an hour. On a small general-purpose instance that is a spend of well under a dollar. The important operational habit is that an idle provisioned instance still bills, so I deleted the function and the capacity provider the moment the run finished. Leaving &lt;code&gt;MinExecutionEnvironments&lt;/code&gt; at 1 on a forgotten test function is how a few cents an hour quietly becomes a line item.&lt;/p&gt;

&lt;h2&gt;
  
  
  When to reach for this, and when not
&lt;/h2&gt;

&lt;p&gt;The 90-minute timeout is the right tool for one continuous computation that cannot be meaningfully checkpointed: a long model inference, a large media transcode, a batch job that holds state in memory and would be awkward to split. For that shape, LMI removes the wall that used to force you off Lambda entirely.&lt;/p&gt;

&lt;p&gt;It is the wrong tool for a workflow that spends most of its time waiting. If your long duration is mostly idle time between steps, a human approval, a callback, a scheduled delay, then Lambda durable functions are the better fit, because they suspend and release compute during the wait instead of holding a provisioned instance the whole time. I wrote up durable functions separately, including the two-invocation suspend-and-resume proof from their execution history.&lt;/p&gt;

&lt;p&gt;A simple way to decide: if the clock is long because your code is busy, Managed Instances. If the clock is long because your code is waiting, durable functions. The two features look similar from the headline ("Lambda can run longer now") and solve genuinely different problems underneath.&lt;/p&gt;

&lt;p&gt;For the waiting case, see the companion piece, "Lambda Durable Functions in Practice: What the Execution History Actually Shows," which walks through the two-invocation suspend-and-resume proof and the replay footgun that silently executes the wrong action if you miss it.&lt;/p&gt;

</description>
      <category>aws</category>
      <category>serverless</category>
      <category>lambda</category>
      <category>python</category>
    </item>
    <item>
      <title>One Line in a Pod Spec, a VM Underneath: A Practical Guide to AWS Nitro Sandboxes on EKS</title>
      <dc:creator>Avneet bansal</dc:creator>
      <pubDate>Fri, 13 Nov 2026 05:48:51 +0000</pubDate>
      <link>https://dev.to/avneet_bansal_a65b3f31fc4/one-line-in-a-pod-spec-a-vm-underneath-a-practical-guide-to-aws-nitro-sandboxes-on-eks-2e13</link>
      <guid>https://dev.to/avneet_bansal_a65b3f31fc4/one-line-in-a-pod-spec-a-vm-underneath-a-practical-guide-to-aws-nitro-sandboxes-on-eks-2e13</guid>
      <description>&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%2Focv2probnkzfvdrdpv7r.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%2Focv2probnkzfvdrdpv7r.png" alt=" " width="800" height="336"&gt;&lt;/a&gt;&lt;br&gt;
AWS launched Nitro Sandboxes on November 9, 2026. The short version: add &lt;code&gt;runtimeClassName: isolated-sandbox&lt;/code&gt; to a pod, and that pod runs in its own lightweight virtual machine, isolated by the same Nitro hypervisor that separates EC2 instances from each other.&lt;/p&gt;

&lt;p&gt;This post is what I would want to read as a platform engineer deciding whether to adopt it: the problem it addresses, how it works, how to roll it out, what changes for your workloads, and what it does not solve.&lt;/p&gt;

&lt;p&gt;Disclosure: I work at AWS. This post is my own and is based on the public &lt;a href="https://aws.amazon.com/blogs/containers/aws-nitro-sandboxes-hardware-enforced-workload-isolation-on-amazon-eks/" rel="noopener noreferrer"&gt;launch blog&lt;/a&gt;, the &lt;a href="https://docs.aws.amazon.com/eks/latest/userguide/sandboxing.html" rel="noopener noreferrer"&gt;Amazon EKS user guide&lt;/a&gt; and the public blueprints repository.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Pods on a node normally share one Linux kernel. A container escape or kernel bug in one pod can reach its neighbors. Nitro Sandboxes give each opted-in pod its own kernel, behind a hardware boundary.&lt;/li&gt;
&lt;li&gt;Adoption is one field: &lt;code&gt;runtimeClassName: isolated-sandbox&lt;/code&gt; (node trusted) or &lt;code&gt;confidential-sandbox&lt;/code&gt; (node untrusted, secrets gated by attestation). Images, Helm charts, &lt;code&gt;kubectl exec&lt;/code&gt; and &lt;code&gt;logs&lt;/code&gt; stay the same.&lt;/li&gt;
&lt;li&gt;Requirements: Kubernetes 1.36 or later, a supported Nitro instance type, and EKS Auto Mode (built in) or a custom AMI.&lt;/li&gt;
&lt;li&gt;Some pod features behave differently: no &lt;code&gt;hostNetwork&lt;/code&gt;, no user namespaces, no shared EBS volume, no in-place resize, no GPU passthrough at launch, and per-pod DNS and connection limits.&lt;/li&gt;
&lt;li&gt;Measured on a real cluster: a sandboxed pod was Ready in a median of 3.28 s against 1.38 s for an ordinary pod on the same node, and a warm-pool claim in 1.53 s. The sandboxed pod ran its own kernel (6.18.54) on a node running 6.18.56.&lt;/li&gt;
&lt;li&gt;It is an isolation boundary, not a policy engine. A default sandboxed pod still reached the internet in my test. You still need least-privilege identity, egress rules and, for agents, tool-level policy.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;h2&gt;
  
  
  The problem: one kernel, many tenants
&lt;/h2&gt;

&lt;p&gt;Containers are fast because they share the host kernel. That same property is the risk. Every syscall a container makes lands in the kernel that also serves every other pod on the node, so isolation depends on namespaces, cgroups, seccomp and LSM profiles being configured correctly and on the kernel and runtime having no exploitable bug.&lt;/p&gt;

&lt;p&gt;The record shows that assumption breaks regularly. runc had host-escape advisories in 2019 (CVE-2019-5736) and 2024 (CVE-2024-21626), and three more on a single day in November 2025. Kernel bugs such as Dirty Pipe (CVE-2022-0847) needed no special privileges at all. A &lt;a href="https://www.nofire.ai/blog/what-is-a-nitro-sandbox-really" rel="noopener noreferrer"&gt;recent analysis by NoFire&lt;/a&gt; collects these and makes the same point: where the untrusted code runs is the question that matters.&lt;/p&gt;

&lt;p&gt;Four trends make the shared kernel harder to accept, and the &lt;a href="https://aws.amazon.com/blogs/containers/aws-nitro-sandboxes-hardware-enforced-workload-isolation-on-amazon-eks/" rel="noopener noreferrer"&gt;launch post&lt;/a&gt; names each of them:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;AI agents run code nobody reviewed.&lt;/strong&gt; A coding agent writes code, runs it and talks to the internet. A single prompt injection can turn that into data exfiltration or lateral movement.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;SaaS platforms pack many customers onto shared clusters.&lt;/strong&gt; A dedicated EC2 instance per customer is the safe answer and the expensive one.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Regulated industries want proof.&lt;/strong&gt; Auditors increasingly ask for cryptographic evidence that a workload ran as expected, not a configuration checklist.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;CI/CD jobs run privileged.&lt;/strong&gt; &lt;code&gt;docker build&lt;/code&gt; and similar jobs need elevated rights, so teams isolate them on dedicated node groups.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The usual workaround for all four is the same: dedicate machines and pay for idle capacity. Nitro Sandboxes aim to remove that trade-off.&lt;/p&gt;
&lt;h2&gt;
  
  
  What a Nitro Sandbox is
&lt;/h2&gt;

&lt;p&gt;Each opted-in pod gets its own lightweight VM with its own Linux kernel. On the node, a shim (&lt;code&gt;containerd-shim-nitro-sandbox-v2&lt;/code&gt;) starts a Firecracker microVM for the pod. Inside the VM, an agent runs the containers with its own containerd. The Nitro hypervisor sits beneath both the node and the sandbox and enforces CPU and memory isolation between them. Per the &lt;a href="https://docs.aws.amazon.com/eks/latest/userguide/sandboxing.html" rel="noopener noreferrer"&gt;EKS user guide&lt;/a&gt;, each sandbox is a first-level guest with hardware-assisted virtualization, not nested virtualization or emulation.&lt;/p&gt;

&lt;p&gt;Every sandboxed pod also gets a Cedar policy that controls which images may load, which containers may run, and whether &lt;code&gt;exec&lt;/code&gt; and log streaming are allowed. The policy is enforced from inside the guest.&lt;/p&gt;
&lt;h3&gt;
  
  
  Two modes
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;isolated-sandbox&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;confidential-sandbox&lt;/code&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Who is trusted&lt;/td&gt;
&lt;td&gt;The node&lt;/td&gt;
&lt;td&gt;Only the guest&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Protects&lt;/td&gt;
&lt;td&gt;The node and other pods from a compromised workload&lt;/td&gt;
&lt;td&gt;The workload's data from the node operator or a compromised node&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;exec&lt;/code&gt;, logs, process listing&lt;/td&gt;
&lt;td&gt;Allowed&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;exec&lt;/code&gt;, stdin and process listing denied&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Policy&lt;/td&gt;
&lt;td&gt;Accepts any valid container spec&lt;/td&gt;
&lt;td&gt;Pins image digests, args, env, mounts and capabilities&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Secrets&lt;/td&gt;
&lt;td&gt;Normal Pod Identity&lt;/td&gt;
&lt;td&gt;AWS KMS releases keys only to a sandbox whose attestation matches&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;A third switch removes the network entirely. The annotation &lt;code&gt;sandbox.amazonaws.com/network-isolated: "true"&lt;/code&gt; boots the VM with no network device at all: no DNS, no IMDS, no Pod Identity. The user guide notes that the vsock channel still exists, so it calls this strong but not absolute isolation.&lt;/p&gt;
&lt;h2&gt;
  
  
  Where it fits among the options you already know
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Option&lt;/th&gt;
&lt;th&gt;Kernel the untrusted code runs on&lt;/th&gt;
&lt;th&gt;Typical cost of isolation&lt;/th&gt;
&lt;th&gt;Kubernetes change&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Standard container (runc) + seccomp/LSM&lt;/td&gt;
&lt;td&gt;Shared node kernel&lt;/td&gt;
&lt;td&gt;None&lt;/td&gt;
&lt;td&gt;None&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;User-space kernel sandbox (for example gVisor)&lt;/td&gt;
&lt;td&gt;Emulated kernel in user space&lt;/td&gt;
&lt;td&gt;Syscall overhead, compatibility gaps&lt;/td&gt;
&lt;td&gt;RuntimeClass&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;MicroVM on KVM (for example Kata Containers)&lt;/td&gt;
&lt;td&gt;Own guest kernel; KVM in the host kernel&lt;/td&gt;
&lt;td&gt;VM per pod, needs nested or bare-metal virtualization on cloud&lt;/td&gt;
&lt;td&gt;RuntimeClass&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Dedicated node per tenant&lt;/td&gt;
&lt;td&gt;Own node kernel&lt;/td&gt;
&lt;td&gt;Idle capacity per tenant&lt;/td&gt;
&lt;td&gt;Node groups, taints&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Nitro Sandbox&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;Own guest kernel; Nitro hypervisor beneath the node&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;VM per pod on standard Nitro instances&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;RuntimeClass&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;This table compares the isolation model, not performance. I have not benchmarked these options against each other, and you should measure on your own workload before deciding.&lt;/p&gt;
&lt;h2&gt;
  
  
  How to adopt it, step by step
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Step 0. Check prerequisites
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Kubernetes &lt;strong&gt;1.36 or later&lt;/strong&gt; (eksctl users: pass &lt;code&gt;--version&lt;/code&gt; explicitly).&lt;/li&gt;
&lt;li&gt;A &lt;a href="https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/amazon-ec2-nitro-sandboxes.html" rel="noopener noreferrer"&gt;supported Nitro instance type&lt;/a&gt;. Auto Mode skips FIPS AMIs, GPU and Neuron AMIs, and multi-NUMA types for sandbox nodes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;EKS Auto Mode&lt;/strong&gt; includes the runtime classes on every cluster. On managed or self-managed node groups, build a custom AMI with the sandbox components (&lt;a href="https://docs.aws.amazon.com/eks/latest/userguide/sandboxing-ami-create-update.html" rel="noopener noreferrer"&gt;guide&lt;/a&gt;).&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Step 1. Run one isolated pod
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;apiVersion&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;v1&lt;/span&gt;
&lt;span class="na"&gt;kind&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Pod&lt;/span&gt;
&lt;span class="na"&gt;metadata&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;hello-sandbox&lt;/span&gt;
&lt;span class="na"&gt;spec&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;runtimeClassName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;isolated-sandbox&lt;/span&gt;   &lt;span class="c1"&gt;# the only change
&lt;/span&gt;  &lt;span class="na"&gt;containers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;hello&lt;/span&gt;
      &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;public.ecr.aws/amazonlinux/amazonlinux:2023&lt;/span&gt;
      &lt;span class="na"&gt;command&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"sh"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"-c"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
      &lt;span class="na"&gt;args&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="na"&gt;echo "product&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;$(cat /sys/class/dmi/id/product_name)"&lt;/span&gt;
          &lt;span class="na"&gt;echo "disks&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;   &lt;span class="s"&gt;$(ls /sys/block | tr '\n' ' ')"&lt;/span&gt;
          &lt;span class="s"&gt;sleep infinity&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;kubectl apply -f hello-sandbox.yaml
kubectl &lt;span class="nb"&gt;wait&lt;/span&gt; pod/hello-sandbox --for=condition=Ready --timeout=300s
kubectl logs hello-sandbox
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;The logs are your proof. Inside a sandbox the product name ends in &lt;code&gt;.enclave&lt;/code&gt; and the disks are virtio devices (&lt;code&gt;vda&lt;/code&gt;, &lt;code&gt;vdb&lt;/code&gt;), where an ordinary pod on the same node sees the instance's own NVMe disks.&lt;/p&gt;
&lt;h3&gt;
  
  
  Step 2. Give it identity the sandbox can use
&lt;/h3&gt;

&lt;p&gt;Inside a sandbox, IMDS answers only a handful of placement paths and does not serve IAM credentials. Use &lt;strong&gt;EKS Pod Identity&lt;/strong&gt;, scoped to exactly what the workload needs. In isolated mode, the service account token is not copied into the sandbox, so if a workload needs to call the Kubernetes API, mount a token explicitly (see the hardening section below).&lt;/p&gt;
&lt;h3&gt;
  
  
  Step 3. Close the egress
&lt;/h3&gt;

&lt;p&gt;A VM boundary does not decide where traffic may go. Add egress rules. On EKS Auto Mode, an &lt;code&gt;ApplicationNetworkPolicy&lt;/code&gt; can allow egress by hostname, for example only to Amazon Bedrock and GitHub for a coding agent. The blueprints include a &lt;a href="https://github.com/aws-samples/sample-eks-nitro-sandbox" rel="noopener noreferrer"&gt;side-by-side network policy sample&lt;/a&gt; with an ordinary pod as the control.&lt;/p&gt;
&lt;h3&gt;
  
  
  Step 4. Tune the per-pod limits before load testing
&lt;/h3&gt;

&lt;p&gt;Two defaults surprise people under load:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;DNS:&lt;/strong&gt; 100 queries per second per pod (max 512). Raise it with &lt;code&gt;sandbox.amazonaws.com/dns-packets-per-second&lt;/code&gt;, or add a DNS cache sidecar.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Outbound connections:&lt;/strong&gt; 4,096 simultaneous per IP family. Adjust with &lt;code&gt;sandbox.amazonaws.com/conntrack-limit&lt;/code&gt; (64 to 65,536), or pool connections.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Step 5. Move secrets-heavy workloads to confidential mode
&lt;/h3&gt;

&lt;p&gt;Switch to &lt;code&gt;confidential-sandbox&lt;/code&gt;, generate the pod's Cedar policy with &lt;code&gt;eks-nitro-sandbox-genpolicy&lt;/code&gt;, and write KMS key policies with &lt;code&gt;kms:RecipientAttestation:PCR&amp;lt;N&amp;gt;&lt;/code&gt; conditions. The user guide is explicit that without those KMS conditions, the attestation is recorded but never checked, so confidential mode is incomplete.&lt;/p&gt;
&lt;h3&gt;
  
  
  Step 6. Use warm pools for interactive sessions
&lt;/h3&gt;

&lt;p&gt;Each sandbox boots a VM, so cold start is part of the design. For latency-sensitive agent sessions, the Kubernetes SIG project &lt;a href="https://github.com/kubernetes-sigs/agent-sandbox" rel="noopener noreferrer"&gt;agent-sandbox&lt;/a&gt; keeps pre-booted sandboxes ready to claim. On my test cluster, claims from a five-pod warm pool were ready in a median of 1.53 seconds against 3.28 seconds for a cold sandboxed pod (details in the measurements section below). The blueprints also include a KEDA sample that scales the pool with demand.&lt;/p&gt;
&lt;h2&gt;
  
  
  Migration checklist: what changes for your pods
&lt;/h2&gt;

&lt;p&gt;Run this against your manifests before you flip the runtime class. Source: the &lt;a href="https://docs.aws.amazon.com/eks/latest/userguide/sandboxing-limitations.html" rel="noopener noreferrer"&gt;EKS considerations page&lt;/a&gt;.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;If your pod uses&lt;/th&gt;
&lt;th&gt;In a sandbox&lt;/th&gt;
&lt;th&gt;Do this&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;hostNetwork: true&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Fails to start&lt;/td&gt;
&lt;td&gt;Remove it&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;hostUsers: false&lt;/code&gt; (user namespaces)&lt;/td&gt;
&lt;td&gt;Not started on &lt;code&gt;isolated-sandbox&lt;/code&gt; nodes&lt;/td&gt;
&lt;td&gt;Remove it&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;hostPID&lt;/code&gt; / &lt;code&gt;hostIPC&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Scoped to the sandbox only&lt;/td&gt;
&lt;td&gt;Do not rely on seeing node processes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;One EBS volume shared by several pods&lt;/td&gt;
&lt;td&gt;Not possible&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;ReadWriteOncePod&lt;/code&gt;, or EFS for shared data&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;In-place CPU or memory resize&lt;/td&gt;
&lt;td&gt;Not supported&lt;/td&gt;
&lt;td&gt;Recreate the pod&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;IMDS for credentials&lt;/td&gt;
&lt;td&gt;Returns 404&lt;/td&gt;
&lt;td&gt;EKS Pod Identity&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;GPUs or device passthrough&lt;/td&gt;
&lt;td&gt;Not supported at launch&lt;/td&gt;
&lt;td&gt;Keep those pods unsandboxed&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;kubectl port-forward&lt;/code&gt; to a &lt;code&gt;127.0.0.1&lt;/code&gt; server&lt;/td&gt;
&lt;td&gt;Connects but returns nothing&lt;/td&gt;
&lt;td&gt;Bind to &lt;code&gt;0.0.0.0&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;emptyDir&lt;/code&gt; usage metrics&lt;/td&gt;
&lt;td&gt;Reported as zero&lt;/td&gt;
&lt;td&gt;Set &lt;code&gt;ephemeral-storage&lt;/code&gt; limits&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;CPU Manager &lt;code&gt;static&lt;/code&gt; pinning&lt;/td&gt;
&lt;td&gt;No effect&lt;/td&gt;
&lt;td&gt;Rely on requests and limits&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Two sizing notes matter for cost:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Density:&lt;/strong&gt; total sandbox vCPUs on a node cannot exceed 5 times the node's vCPUs, and the sandbox count has the same cap (halved on CPU-overcommit types). Memory usually binds first.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;SMT instances:&lt;/strong&gt; on Intel and SMT-enabled AMD types, two different sandboxes never share sibling threads of one core, so usable compute is roughly the number of physical cores. Request CPU in multiples of two, or use Graviton, which has one thread per core.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Hardening tips for sandboxed workloads
&lt;/h2&gt;

&lt;p&gt;A VM boundary is the outer wall. These four habits keep the inside of the sandbox tight as well.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Mount only the token you need, with a short life.&lt;/strong&gt; If a workload must call the Kubernetes API, turn off the automatic service account mount and mount a projected token with an explicit one-hour expiry. The API server can extend the automatic token well past an hour, while a projected token with &lt;code&gt;expirationSeconds: 3600&lt;/code&gt; stays valid for one hour and the kubelet rotates it.&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;spec&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;automountServiceAccountToken&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
  &lt;span class="na"&gt;volumes&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;kube-api-token&lt;/span&gt;
      &lt;span class="na"&gt;projected&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;sources&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
          &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="na"&gt;serviceAccountToken&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
              &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;token&lt;/span&gt;
              &lt;span class="na"&gt;expirationSeconds&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3600&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. A privileged daemon can still have a read-only root.&lt;/strong&gt; Sandboxes make it reasonable to run privileged build daemons such as &lt;code&gt;dockerd&lt;/code&gt; or &lt;code&gt;buildkitd&lt;/code&gt; on shared nodes. Even then, set &lt;code&gt;readOnlyRootFilesystem: true&lt;/code&gt; and add one writable volume for each path the daemon actually needs. Add each volume only after you see the error without it, so the list stays minimal.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Prove a control works before you rely on it.&lt;/strong&gt; Scanners will flag a missing seccomp profile on privileged containers. Check what actually applies with &lt;code&gt;grep Seccomp /proc/1/status&lt;/code&gt; inside the container. On my test cluster, every privileged container showed &lt;code&gt;Seccomp: 0&lt;/code&gt; (no filter) even with &lt;code&gt;seccompProfile: RuntimeDefault&lt;/code&gt; set, on an ordinary pod and on a sandboxed pod alike, while unprivileged containers showed &lt;code&gt;Seccomp: 2&lt;/code&gt; (filter active). Privileged mode switches seccomp off in standard Kubernetes, so for privileged build daemons the VM boundary is the control that actually holds. Document the scanner finding with that evidence rather than silencing it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Use liveness probes that do not depend on what you are monitoring.&lt;/strong&gt; If a pod's health endpoint calls the Kubernetes API or another dependency, an outage there restarts the pod and loses its state. A TCP liveness probe on the serving port avoids that.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Nitro Sandboxes do not solve
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Tool-level policy.&lt;/strong&gt; A VM boundary cannot express "this agent may push to git only if tests pass". Marc Brooker, one of the Firecracker authors, makes the case for pairing a policy layer with a per-session microVM in &lt;a href="https://strandsagents.com/blog/strands-box-the-big-picture/" rel="noopener noreferrer"&gt;Strands Box: The Big Picture&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A trusted node in isolated mode.&lt;/strong&gt; In &lt;code&gt;isolated-sandbox&lt;/code&gt; the node is trusted. If the node itself is your threat, use &lt;code&gt;confidential-sandbox&lt;/code&gt; with attested KMS keys.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Over-broad credentials.&lt;/strong&gt; If the pod's IAM role can read every bucket, the sandbox does not change that. Scope identity per workload.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;GPU workloads&lt;/strong&gt;, at launch.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What I measured on a real cluster
&lt;/h2&gt;

&lt;p&gt;The docs describe the boundary. I wanted numbers, so I created a fresh EKS Auto Mode cluster (Kubernetes 1.36, &lt;code&gt;us-west-2&lt;/code&gt;, November 12, 2026), ran ordinary and sandboxed pods side by side, and deleted the cluster afterwards. Every figure below comes from that run. Timings are client-side, from &lt;code&gt;kubectl apply&lt;/code&gt; returning to &lt;code&gt;kubectl wait --for=condition=Ready&lt;/code&gt; returning, so they include a little kubectl overhead that is the same for both kinds of pod.&lt;/p&gt;

&lt;h3&gt;
  
  
  Startup time
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Scenario&lt;/th&gt;
&lt;th&gt;Ordinary pod&lt;/th&gt;
&lt;th&gt;Sandboxed pod&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;First pod on an empty cluster (node provisioning and image pull included)&lt;/td&gt;
&lt;td&gt;31.7 s (on a c6a.large)&lt;/td&gt;
&lt;td&gt;88.1 s (on a c8a.large)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Node already up, image cached, 20 runs each, same c8a.large node&lt;/td&gt;
&lt;td&gt;median &lt;strong&gt;1.38 s&lt;/strong&gt; (1.33 to 1.53)&lt;/td&gt;
&lt;td&gt;median &lt;strong&gt;3.28 s&lt;/strong&gt; (3.16 to 3.85)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Claim from a 5-pod agent-sandbox warm pool, 10 claims&lt;/td&gt;
&lt;td&gt;n/a&lt;/td&gt;
&lt;td&gt;median &lt;strong&gt;1.53 s&lt;/strong&gt; (1.35 to 2.20)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;So a sandbox adds about 1.9 seconds to a pod start once the node is warm, and a warm pool takes almost all of that back: a claim was ready in about the time an ordinary pod takes to start. All 10 claims adopted an already-booted sandbox (&lt;code&gt;launch-type: warm&lt;/code&gt;), and the pool was back to five ready a median of 1.4 seconds after each claim. One refill took 13.5 seconds.&lt;/p&gt;

&lt;p&gt;The first-pod row is a single run each and depends mostly on how fast Auto Mode provisions a node, so treat it as one data point, not a benchmark.&lt;/p&gt;

&lt;h3&gt;
  
  
  What the pod sees
&lt;/h3&gt;

&lt;p&gt;Both pods ran on the same c8a.large node with a 1 vCPU and 1,536 MiB memory request.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Inside the pod&lt;/th&gt;
&lt;th&gt;Ordinary pod&lt;/th&gt;
&lt;th&gt;Sandboxed pod&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;/sys/class/dmi/id/product_name&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;c8a.large&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;c8a.enclave&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Block devices&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;nvme0n1 nvme1n1 zram0&lt;/code&gt; (the node's disks)&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;vda vdb vdc&lt;/code&gt; (virtio)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Kernel (&lt;code&gt;uname -r&lt;/code&gt;)&lt;/td&gt;
&lt;td&gt;6.18.56, same as the node&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;6.18.54&lt;/strong&gt;, its own kernel&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;nproc&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;2, the whole node&lt;/td&gt;
&lt;td&gt;1, its request&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;MemTotal&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;3,804 MiB, the whole node&lt;/td&gt;
&lt;td&gt;1,651 MiB&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The kernel line is the whole story in one row: the sandboxed pod was running a different kernel from the node it was scheduled on.&lt;/p&gt;

&lt;h3&gt;
  
  
  Sizing facts from the node
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;isolated-sandbox&lt;/code&gt; RuntimeClass adds a fixed overhead of &lt;strong&gt;256 MiB&lt;/strong&gt; of memory per pod, on top of its requests.&lt;/li&gt;
&lt;li&gt;The 2-vCPU c8a.large node advertised &lt;strong&gt;10&lt;/strong&gt; &lt;code&gt;eks.amazonaws.com/nitro-sandbox&lt;/code&gt; slots, which matches the documented cap of 5 times the node's vCPUs.&lt;/li&gt;
&lt;li&gt;That 4 GiB node reported &lt;strong&gt;2,349 MiB&lt;/strong&gt; allocatable memory. A 2 GiB pod that would fit on many 4 GiB nodes did not fit next to one small pod here. Check allocatable on your sandbox nodes before sizing.&lt;/li&gt;
&lt;li&gt;For sandbox pods, Auto Mode chose c8a.large, which runs one thread per core, so the SMT note above did not apply on this run.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Egress
&lt;/h3&gt;

&lt;p&gt;Each pod ran &lt;code&gt;curl https://example.com&lt;/code&gt; with an 8-second timeout.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Pod&lt;/th&gt;
&lt;th&gt;Network interfaces&lt;/th&gt;
&lt;th&gt;Result&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Ordinary&lt;/td&gt;
&lt;td&gt;n/a&lt;/td&gt;
&lt;td&gt;HTTP 200&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Sandboxed, default&lt;/td&gt;
&lt;td&gt;&lt;code&gt;lo dummy0 eth0&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;HTTP 200&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Sandboxed, &lt;code&gt;sandbox.amazonaws.com/network-isolated: "true"&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;lo dummy0&lt;/code&gt; (no &lt;code&gt;eth0&lt;/code&gt;)&lt;/td&gt;
&lt;td&gt;Blocked: could not even resolve the host name&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The middle row is the point to remember: a sandbox is not a firewall. Without the annotation or an egress policy, a sandboxed pod reaches the internet like any other pod.&lt;/p&gt;

&lt;h3&gt;
  
  
  What I did not measure
&lt;/h3&gt;

&lt;p&gt;Throughput and latency of a real application inside the sandbox, the DNS and connection limits under load, confidential mode, and density at the cap. Those need a workload-specific test, and they are the next things I would run before production.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where to start
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Pick one workload that runs code you do not fully control: an agent, a CI build, or a tenant plugin.&lt;/li&gt;
&lt;li&gt;Run it in &lt;code&gt;isolated-sandbox&lt;/code&gt; with Pod Identity and an egress policy.&lt;/li&gt;
&lt;li&gt;Walk the migration checklist, tune DNS and connection limits, and load test.&lt;/li&gt;
&lt;li&gt;Move workloads that hold sensitive data to &lt;code&gt;confidential-sandbox&lt;/code&gt; with attested KMS keys.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The blueprints repository has runnable samples for coding agents, warm pools, CI runners, storage and confidential workloads: &lt;a href="https://github.com/aws-samples/sample-eks-nitro-sandbox" rel="noopener noreferrer"&gt;aws-samples/sample-eks-nitro-sandbox&lt;/a&gt;. If you try it on a workload not covered there, I would like to hear what changed and what broke.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Sources: &lt;a href="https://aws.amazon.com/blogs/containers/aws-nitro-sandboxes-hardware-enforced-workload-isolation-on-amazon-eks/" rel="noopener noreferrer"&gt;AWS Containers launch blog&lt;/a&gt;, &lt;a href="https://docs.aws.amazon.com/eks/latest/userguide/sandboxing.html" rel="noopener noreferrer"&gt;Amazon EKS user guide: sandboxing&lt;/a&gt; and &lt;a href="https://docs.aws.amazon.com/eks/latest/userguide/sandboxing-limitations.html" rel="noopener noreferrer"&gt;considerations&lt;/a&gt;, &lt;a href="https://www.nofire.ai/blog/what-is-a-nitro-sandbox-really" rel="noopener noreferrer"&gt;NoFire analysis&lt;/a&gt;. Content from these sources was paraphrased.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>aws</category>
      <category>kubernetes</category>
      <category>security</category>
      <category>ai</category>
    </item>
    <item>
      <title>Moving from Claude Sonnet 4.6 to 5.5 on Bedrock: What Broke, What Changed, and What It Cost</title>
      <dc:creator>Avneet bansal</dc:creator>
      <pubDate>Thu, 12 Nov 2026 15:22:24 +0000</pubDate>
      <link>https://dev.to/avneet_bansal_a65b3f31fc4/moving-from-claude-sonnet-46-to-55-on-bedrock-what-broke-what-changed-and-what-it-cost-3164</link>
      <guid>https://dev.to/avneet_bansal_a65b3f31fc4/moving-from-claude-sonnet-46-to-55-on-bedrock-what-broke-what-changed-and-what-it-cost-3164</guid>
      <description>&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%2Ff3on2ted0dxcs8cuebma.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%2Ff3on2ted0dxcs8cuebma.png" alt=" " width="800" height="336"&gt;&lt;/a&gt;&lt;br&gt;
I recently moved a structured-extraction pipeline from Claude Sonnet 4.6 to Sonnet 5.5 on Amazon Bedrock. On paper it is a model ID change: the same family, a lower price per token, better benchmark scores. In practice, some requests that worked on 4.6 were rejected, one kind of request came back empty with HTTP 200, and some answers changed in ways that schema validation does not catch.&lt;/p&gt;

&lt;p&gt;To share numbers rather than anecdotes, I rebuilt the test from scratch on fictional data. Twelve made-up team meeting notes, generated from a fixed seed, so every field has a known answer. Both models extracted the same fields from every note, three times each, through Bedrock in &lt;code&gt;us-west-2&lt;/code&gt;. I then ran smaller probes for the API changes, the usual fallback models, long documents and output limits. The whole second round cost about USD 2.50.&lt;/p&gt;

&lt;p&gt;The short version:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;Sonnet 4.6&lt;/th&gt;
&lt;th&gt;Sonnet 5.5&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Fields with one right answer, schema states formats&lt;/td&gt;
&lt;td&gt;100%&lt;/td&gt;
&lt;td&gt;100%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Same, schema with no descriptions&lt;/td&gt;
&lt;td&gt;81.0%&lt;/td&gt;
&lt;td&gt;93.7%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Same answer from both models (ignoring wording), explicit schema&lt;/td&gt;
&lt;td&gt;42% of notes&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Agreement after one added sentence&lt;/td&gt;
&lt;td&gt;100%&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Input tokens per call, short notes&lt;/td&gt;
&lt;td&gt;796&lt;/td&gt;
&lt;td&gt;979 (1.23x)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Input tokens per call, about 15,000 words&lt;/td&gt;
&lt;td&gt;18,502&lt;/td&gt;
&lt;td&gt;26,609 (1.44x)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Cost per call, short notes, list price&lt;/td&gt;
&lt;td&gt;USD 0.0056&lt;/td&gt;
&lt;td&gt;USD 0.0049 (13% lower)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Median latency, short notes&lt;/td&gt;
&lt;td&gt;1.79 s&lt;/td&gt;
&lt;td&gt;2.51 s&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The surprising row is the third one. Both models were right on every field that has one answer, and still produced the same complete answer for only 42% of notes. Here, "the same answer" means the same dates, attendees, number of decisions, and action items with the same owners and due dates; wording is ignored. The rest of this post is about where that gap comes from, and how to close it.&lt;/p&gt;

&lt;h2&gt;
  
  
  The test setup
&lt;/h2&gt;

&lt;p&gt;Each note has a title, a date, attendees, decisions and action items. The generator plants the cases that matter in any extraction job:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Dates in four styles (&lt;code&gt;March 4, 2025&lt;/code&gt;, &lt;code&gt;4 March 2025&lt;/code&gt;, &lt;code&gt;2025-03-04&lt;/code&gt;, &lt;code&gt;Tuesday, March 4th, 2025&lt;/code&gt;), and due dates written as "within 10 days of this meeting".&lt;/li&gt;
&lt;li&gt;An "update after the meeting" line at the end that moves one deadline (7 of 12 notes).&lt;/li&gt;
&lt;li&gt;A task with two owners, such as "Mei and Omar to update the FAQ page" (7 notes).&lt;/li&gt;
&lt;li&gt;A tentative task with no owner, such as "It would be good if someone could look into translating the guide" (7 notes).&lt;/li&gt;
&lt;li&gt;Notes with no decisions (3) and no next meeting date (5).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Both models received the same prompt and the same JSON schema through structured output (&lt;code&gt;output_config.format&lt;/code&gt; with &lt;code&gt;type: json_schema&lt;/code&gt;). The only request differences were the ones the API forces: &lt;code&gt;temperature: 0&lt;/code&gt; on 4.6, and no sampling parameters with thinking switched off on 5.5. I ran three schema variants:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;explicit&lt;/strong&gt;: descriptions state formats ("ISO 8601", "Null if none were made"), but say nothing about two-owner or no-owner tasks.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;loose&lt;/strong&gt;: the same schema with every description removed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;clarified&lt;/strong&gt;: explicit, plus one sentence on those two cases.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  1. Requests that fail loudly
&lt;/h2&gt;

&lt;p&gt;These are the easy ones, because Bedrock returns a &lt;code&gt;ValidationException&lt;/code&gt; with a clear message.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Request&lt;/th&gt;
&lt;th&gt;Sonnet 4.6&lt;/th&gt;
&lt;th&gt;Sonnet 5.5&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;temperature: 0&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;accepted&lt;/td&gt;
&lt;td&gt;rejected: "&lt;code&gt;temperature&lt;/code&gt; is deprecated for this model"&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;top_p&lt;/code&gt; or &lt;code&gt;top_k&lt;/code&gt; alone&lt;/td&gt;
&lt;td&gt;accepted&lt;/td&gt;
&lt;td&gt;rejected, same message&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;temperature&lt;/code&gt; and &lt;code&gt;top_p&lt;/code&gt; together&lt;/td&gt;
&lt;td&gt;rejected: "cannot both be specified"&lt;/td&gt;
&lt;td&gt;rejected&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;thinking: {type: disabled}&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;accepted&lt;/td&gt;
&lt;td&gt;rejected: send &lt;code&gt;{"type": "between_tools"}&lt;/code&gt; instead&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;thinking: {type: enabled, budget_tokens}&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;accepted&lt;/td&gt;
&lt;td&gt;rejected: use &lt;code&gt;adaptive&lt;/code&gt; and &lt;code&gt;output_config.effort&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;thinking: {type: between_tools}&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;rejected&lt;/td&gt;
&lt;td&gt;accepted&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Forced &lt;code&gt;tool_choice&lt;/code&gt; (&lt;code&gt;tool&lt;/code&gt; or &lt;code&gt;any&lt;/code&gt;)&lt;/td&gt;
&lt;td&gt;accepted&lt;/td&gt;
&lt;td&gt;rejected: not supported for this model&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Assistant prefill (last message is a partial assistant reply)&lt;/td&gt;
&lt;td&gt;rejected&lt;/td&gt;
&lt;td&gt;rejected&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Empty text block in an assistant turn&lt;/td&gt;
&lt;td&gt;rejected: "text content blocks must be non-empty"&lt;/td&gt;
&lt;td&gt;rejected&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;max_tokens&lt;/code&gt; above 128,000&lt;/td&gt;
&lt;td&gt;rejected&lt;/td&gt;
&lt;td&gt;rejected&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The thinking setting cannot be shared between the two models: the value that turns thinking off on 5.5 is rejected by 4.6. Thinking is adaptive by default on 5.5. In 24 extraction calls with no thinking field, 5.5 returned no thinking blocks and the same answers as with thinking off, but one short probe without a thinking field did return a thinking block before the text. Read content blocks by type instead of taking &lt;code&gt;content[0]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The forced &lt;code&gt;tool_choice&lt;/code&gt; change matters for anyone who gets structured output by defining one tool and forcing the model to call it. That request is rejected on 5.5. Structured output through &lt;code&gt;output_config.format&lt;/code&gt; works on both models, so that is the path I used for everything else.&lt;/p&gt;

&lt;p&gt;The practical fix is a small request profile per model ID (sampling, thinking, tool choice) in configuration, so the next migration is a config change and not a code change.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. The refusal that returns HTTP 200
&lt;/h2&gt;

&lt;p&gt;This one costs time, because nothing throws. Sonnet 5.5 returned &lt;code&gt;stop_reason: "refusal"&lt;/code&gt;, an empty content list and HTTP 200 for a schema with a reasoning field. A pipeline that only catches exceptions sees an empty answer, not an error.&lt;/p&gt;

&lt;p&gt;I expected the field name &lt;code&gt;chain_of_thought&lt;/code&gt; to be the trigger, so I separated the name from the description, five requests per case:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Reasoning field&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;th&gt;4.6 refusals&lt;/th&gt;
&lt;th&gt;5.5 refusals&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;none&lt;/td&gt;
&lt;td&gt;No reasoning field (control)&lt;/td&gt;
&lt;td&gt;0/5&lt;/td&gt;
&lt;td&gt;0/5&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;chain_of_thought&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;"Your reasoning before answering."&lt;/td&gt;
&lt;td&gt;0/5&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;5/5&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;rationale&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;"Your reasoning before answering."&lt;/td&gt;
&lt;td&gt;0/5&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;5/5&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;chain_of_thought&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;"One sentence quoting the line the date comes from."&lt;/td&gt;
&lt;td&gt;0/5&lt;/td&gt;
&lt;td&gt;0/5&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;rationale&lt;/code&gt;, &lt;code&gt;evidence&lt;/code&gt;, &lt;code&gt;thinking&lt;/code&gt;, &lt;code&gt;reasoning&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;same neutral description&lt;/td&gt;
&lt;td&gt;0/5&lt;/td&gt;
&lt;td&gt;0/5&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;In my tests the trigger was the description, not the name. A field that asks the model to write out its reasoning was refused every time. The same field asking for a quote from the source was answered every time, even when it was called &lt;code&gt;chain_of_thought&lt;/code&gt;. A quote is also more useful in an extraction result, because it can be checked against the document.&lt;/p&gt;

&lt;p&gt;The usual fallback models behave differently. With the reasoning description, Sonnet 5 and Haiku 5.5 returned 0 refusals in 5 requests each. So a fallback on refusal works, as long as your code treats &lt;code&gt;refusal&lt;/code&gt; as a failure. Check &lt;code&gt;stop_reason&lt;/code&gt; on every response, and when you retry, do not append the empty reply to the conversation.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Where the schema is silent, the models choose differently
&lt;/h2&gt;

&lt;p&gt;This is the part of the migration that tests and validation do not catch. Every answer below is valid JSON that matches the schema. The models simply made different choices, and each model made its choice consistently.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Inclusion: something that might or might not belong in a list.&lt;/strong&gt; The tentative task with no owner was included by 4.6 in 4 of 7 notes, the same 4 notes in every run. Sonnet 5.5 left it out in 21 of 21 answers. Because &lt;code&gt;owner&lt;/code&gt; was a required string, 4.6 had to put something there, and it wrote &lt;code&gt;""&lt;/code&gt;, &lt;code&gt;"Unassigned"&lt;/code&gt; or &lt;code&gt;"Unknown"&lt;/code&gt;. That placeholder even changed between runs at &lt;code&gt;temperature: 0&lt;/code&gt;, which is why 4.6 matched itself on only 89% of notes with the explicit schema. Neither choice is wrong. The schema never said.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Granularity: one entry, or one per part.&lt;/strong&gt; For "Mei and Omar to update the FAQ page", both models created one entry per owner when the owner field was described as "First name of the person responsible", and one entry with &lt;code&gt;"Mei and Omar"&lt;/code&gt; when there were no descriptions. The two models agreed with each other in both cases. The description, not the model, decided the shape.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Format: dates.&lt;/strong&gt; With "ISO 8601" in the descriptions, all 177 dates from each model were ISO. Without descriptions, 4.6 returned 51 of 156 dates in other formats and usually copied the style of the note (27 of 36 meeting dates). Sonnet 5.5 returned 16 of 156 in other formats, including once &lt;code&gt;"within 10 days of this meeting (11 June 2025)"&lt;/code&gt;. Every date was still the right day. On a different kind of document I tested earlier, the direction was reversed and 5.5 drifted more. Neither model has a format you can rely on unless you state it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Empty values: &lt;code&gt;null&lt;/code&gt; or &lt;code&gt;[]&lt;/code&gt;.&lt;/strong&gt; With "Null if none were made" in the description, both models returned &lt;code&gt;null&lt;/code&gt; for all notes without decisions. Without it, 4.6 returned &lt;code&gt;[]&lt;/code&gt; in 3 of 9 cases and 5.5 in 2 of 9. A &lt;code&gt;null&lt;/code&gt; that becomes &lt;code&gt;[]&lt;/code&gt; passes validation and breaks code that checks &lt;code&gt;is None&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Type: a score with no stated format.&lt;/strong&gt; I added a &lt;code&gt;meeting_date_confidence&lt;/code&gt; string field, described only as "How confident you are in the meeting date." Both models returned text for all 12 notes, such as &lt;code&gt;"High - the date is stated explicitly in the notes header"&lt;/code&gt;, not numbers. With "as a number from 0.0 to 1.0", both returned numbers between 0.95 and 1.0 every time. If code compares a score with a threshold, give the field a number type and a range.&lt;/p&gt;

&lt;p&gt;With one added sentence ("If a task has more than one owner, add one entry per owner. Include only tasks with a named owner."), both models scored 100% and produced identical answers for every note in every run.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Tokens, latency and cost
&lt;/h2&gt;

&lt;p&gt;Sonnet 5.5 uses a newer tokenizer. The &lt;a href="https://platform.claude.com/docs/en/about-claude/pricing" rel="noopener noreferrer"&gt;Claude pricing page&lt;/a&gt; says it produces about 30% more tokens for the same text, and that the exact increase depends on the content. That matched what I saw: the same short notes were 1.23x the input tokens, and the long version (the same notes padded to about 15,000 words of discussion) was 1.44x. Output was 1.38x larger on the short notes for nearly identical JSON.&lt;/p&gt;

&lt;p&gt;The price per token is lower: USD 2 and USD 10 per million input and output tokens for Sonnet 5.5, against USD 3 and USD 15 for Sonnet 4.6. These are Anthropic list prices, before the 10% premium for regional (&lt;code&gt;us.&lt;/code&gt;) inference profiles; check &lt;a href="https://aws.amazon.com/bedrock/pricing/" rel="noopener noreferrer"&gt;Amazon Bedrock pricing&lt;/a&gt; for your Region. At those prices, a short-note call cost 13% less on 5.5. A long-note call cost only 4% less, because the token increase was larger.&lt;/p&gt;

&lt;p&gt;Two things move with the tokenizer even when the bill goes down. Token budgets and context checks written for 4.6 trip earlier. And a cost model that counts tokens per document needs a new ratio, measured on your own text.&lt;/p&gt;

&lt;p&gt;Latency went the other way here: a median of 2.51 s against 1.79 s on short notes, and 3.04 s against 2.61 s on long ones. Both models applied the late deadline update at the end of every long note, 6 of 6 each. These are small documents in one Region on one day, so treat the latency numbers as something to measure on your own workload.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to make a schema migration-proof
&lt;/h2&gt;

&lt;p&gt;For every field, the schema description should answer five questions. Each one maps to a difference I measured:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Inclusion.&lt;/strong&gt; What belongs in this list, and what does not? ("Include only tasks with a named owner.")&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Granularity.&lt;/strong&gt; One entry per item, or one per part of an item? ("One entry per owner.")&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Format.&lt;/strong&gt; Exact format for dates, numbers and units. ("ISO 8601, YYYY-MM-DD.")&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Empty.&lt;/strong&gt; What to return when there is nothing. ("Null if none were made.")&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Type.&lt;/strong&gt; A real number type and range for anything compared with a threshold.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;And for the request itself:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Keep sampling, thinking and tool-choice settings in per-model configuration.&lt;/li&gt;
&lt;li&gt;Use structured output (&lt;code&gt;output_config.format&lt;/code&gt;) rather than a forced tool call.&lt;/li&gt;
&lt;li&gt;Check &lt;code&gt;stop_reason&lt;/code&gt; on every response, and treat &lt;code&gt;refusal&lt;/code&gt; and empty replies as failures.&lt;/li&gt;
&lt;li&gt;Ask for a quote from the source, not for the model's reasoning.&lt;/li&gt;
&lt;li&gt;Never send an empty assistant turn in a retry.&lt;/li&gt;
&lt;li&gt;Compare stored values and shapes between models, not only schema validity.&lt;/li&gt;
&lt;li&gt;Run each model more than once, so a model difference can be told apart from run-to-run variation.&lt;/li&gt;
&lt;li&gt;Recalculate token budgets and cost per document on your own text.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Limits of this test
&lt;/h2&gt;

&lt;p&gt;The notes are short, clean and fictional, so absolute accuracy is higher than real documents would give. The point is the difference between the models under identical conditions. I tested one Region, one day and one family of schemas, through &lt;code&gt;bedrock-runtime&lt;/code&gt; &lt;code&gt;InvokeModel&lt;/code&gt; only. The refusal finding comes from a small set of descriptions, and other wording may behave differently, which is the reason to check &lt;code&gt;stop_reason&lt;/code&gt; rather than rely on a list of safe phrases.&lt;/p&gt;

&lt;p&gt;Sonnet 5.5 came out of this test cheaper per call, fully consistent from run to run with an explicit schema, and as accurate as 4.6 on every field with one right answer. Most of the migration work was in the request settings and the schema text. Once those were explicit, the two models gave the same answers.&lt;/p&gt;

</description>
      <category>aws</category>
      <category>ai</category>
      <category>llm</category>
      <category>python</category>
    </item>
    <item>
      <title>Who Decides the Best AI, and Who Gets Left Behind?</title>
      <dc:creator>Avneet bansal</dc:creator>
      <pubDate>Thu, 12 Nov 2026 03:49:35 +0000</pubDate>
      <link>https://dev.to/avneet_bansal_a65b3f31fc4/who-decides-the-best-ai-and-who-gets-left-behind-ogl</link>
      <guid>https://dev.to/avneet_bansal_a65b3f31fc4/who-decides-the-best-ai-and-who-gets-left-behind-ogl</guid>
      <description>&lt;p&gt;&lt;em&gt;This is my perspective on a question I think deserves more attention. It is not a criticism of any company or country. The goal is to think through a risk early, while there is still time to shape the outcome.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Every few weeks a new AI model is announced as the best. Leaderboards shift, headlines follow, and the ranking changes again. Underneath that race sits a bigger question. If some people and countries get the best AI and others do not, AI could become a new version of the gap between rich and poor.&lt;/p&gt;

&lt;p&gt;I wanted to test one part of that question myself, so this post includes a small measurement: what the same text costs an AI model in twelve different languages. The result surprised me more than I expected.&lt;/p&gt;

&lt;h2&gt;
  
  
  How "best" is decided today
&lt;/h2&gt;

&lt;p&gt;Model superiority is mostly decided by benchmarks and leaderboards. Public arenas let people compare answers from two models side by side and vote for the better one, producing a rating similar to a chess score. Other tests measure coding, math, reasoning, and knowledge.&lt;/p&gt;

&lt;p&gt;The race at the top is very close. Stanford's 2026 AI Index reports that as of March 2026, the top closed model led the top open-weight model by 3.3% on the Arena leaderboard, up from 0.5% in August 2024, and six of the top ten models were closed. When the top is that tight, competition shifts away from raw capability toward cost, speed, and reliability.&lt;/p&gt;

&lt;p&gt;Benchmarks only tell part of the story. Many become saturated as models max them out, and some reward test-taking skill more than real-world usefulness. The real advantage comes from things a leaderboard does not show: the compute to train and serve models, the energy to power them (the subject of Part 3), large and diverse data, the talent to build with it, distribution to billions of existing users, and the trust of people and governments (Part 4). None of these are spread evenly around the world.&lt;/p&gt;

&lt;h2&gt;
  
  
  The gap is real and growing
&lt;/h2&gt;

&lt;p&gt;Microsoft's AI Diffusion reports track how many working-age people use AI tools. In the second quarter of 2026, usage reached 28.8% in the Global North and 16.2% in the Global South. The gap widened to 12.6 percentage points, from 12.1 the quarter before. At the top end, the UAE had 64.0% of its working-age population using AI by the end of 2025, with Singapore at 60.9%.&lt;/p&gt;

&lt;p&gt;Infrastructure tells the same story. A UN concept note states that Africa accounts for less than 1% of the world's data center capacity, and in October 2026 a UN official noted that Africa and Latin America together hold just 3.3% of global capacity. The UN Secretary-General has called for a global fund to build AI capacity in developing countries.&lt;/p&gt;

&lt;p&gt;These gaps appear between countries, between large and small companies, and between paid and free tiers. The one I find easiest to overlook, because it is built into the technology itself, is language.&lt;/p&gt;

&lt;h2&gt;
  
  
  A small test: what does the same text cost?
&lt;/h2&gt;

&lt;p&gt;AI models do not read words. They read tokens, small chunks of text produced by a tokenizer. Prices, rate limits, and context windows are counted in tokens. If one language needs more tokens to say the same thing, its speakers pay more, wait longer, and fit less into each request.&lt;/p&gt;

&lt;p&gt;To measure this, I took the Universal Declaration of Human Rights, which has official translations in hundreds of languages, and ran all 30 articles through two OpenAI tokenizers using the open-source &lt;code&gt;tiktoken&lt;/code&gt; library. &lt;code&gt;o200k_base&lt;/code&gt; is the current generation used by GPT-4o-era models, and &lt;code&gt;cl100k_base&lt;/code&gt; is the previous one used by GPT-4 and GPT-3.5. Each number is the token count compared with the English version of the same text.&lt;/p&gt;

&lt;p&gt;I started with Hindi and Punjabi because I speak both, and I have often noticed that AI tools feel slower and less fluent in them than in English. I wanted to see whether part of that came down to cost. I then added ten more languages to see whether the pattern held beyond the two I know.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Language&lt;/th&gt;
&lt;th&gt;Current tokenizer (o200k)&lt;/th&gt;
&lt;th&gt;Previous tokenizer (cl100k)&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;English&lt;/td&gt;
&lt;td&gt;1.00x&lt;/td&gt;
&lt;td&gt;1.00x&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Chinese (Simplified)&lt;/td&gt;
&lt;td&gt;1.12x&lt;/td&gt;
&lt;td&gt;1.69x&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Arabic&lt;/td&gt;
&lt;td&gt;1.18x&lt;/td&gt;
&lt;td&gt;2.66x&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Spanish&lt;/td&gt;
&lt;td&gt;1.23x&lt;/td&gt;
&lt;td&gt;1.46x&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;French&lt;/td&gt;
&lt;td&gt;1.30x&lt;/td&gt;
&lt;td&gt;1.54x&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Swahili&lt;/td&gt;
&lt;td&gt;1.47x&lt;/td&gt;
&lt;td&gt;2.02x&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Hindi&lt;/td&gt;
&lt;td&gt;1.57x&lt;/td&gt;
&lt;td&gt;5.41x&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Bengali&lt;/td&gt;
&lt;td&gt;1.67x&lt;/td&gt;
&lt;td&gt;5.94x&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Tamil&lt;/td&gt;
&lt;td&gt;2.27x&lt;/td&gt;
&lt;td&gt;9.36x&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Punjabi (Gurmukhi)&lt;/td&gt;
&lt;td&gt;2.71x&lt;/td&gt;
&lt;td&gt;8.51x&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Yoruba&lt;/td&gt;
&lt;td&gt;3.14x&lt;/td&gt;
&lt;td&gt;4.58x&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Amharic&lt;/td&gt;
&lt;td&gt;6.67x&lt;/td&gt;
&lt;td&gt;9.84x&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Two things stand out.&lt;/p&gt;

&lt;p&gt;The good news first. The newer tokenizer cut the gap dramatically for widely spoken languages. Hindi went from 5.41x to 1.57x, and Arabic from 2.66x to 1.18x. That is a design decision that made AI several times cheaper for hundreds of millions of people, and it shows this problem can be fixed.&lt;/p&gt;

&lt;p&gt;The improvement was much smaller for languages with fewer speakers online. Punjabi still costs 2.71x, almost twice as much as Hindi, even though both are major languages in India. Yoruba costs 3.14x and Amharic 6.67x. An Amharic speaker using the same model at the same per-token price pays roughly six and a half times more for the same content, and fits only about 15% as much text into the same context window. Where free tiers and rate limits are counted in tokens, those limits run out faster too.&lt;/p&gt;

&lt;p&gt;This finding is not new on its own. Petrov and colleagues showed in 2023 that tokenizers can produce large cost differences between languages. What this test adds is a check on the current generation: the gap has narrowed for large languages and remains wide for smaller ones. The languages that improved least are often spoken in the same regions that have the lowest AI usage and the least data center capacity. That is how a technology gap can quietly become an economic one.&lt;/p&gt;

&lt;p&gt;Token cost is also only one layer. The quality of answers in these languages is a separate question, and one that a tokenizer count cannot answer.&lt;/p&gt;

&lt;h2&gt;
  
  
  Forces that could close it
&lt;/h2&gt;

&lt;p&gt;The tokenizer result itself is a reason for hope. A single engineering choice moved Hindi from more than five times the English cost to about one and a half. Similar choices are being made across the stack.&lt;/p&gt;

&lt;p&gt;Open-weight models remain close to the frontier and can be downloaded, adapted, and run locally without paying per use, which lets a university, a startup, or a government build on capable AI without owning a frontier lab. The cost of running a given level of capability keeps falling, and the token discipline discussed in Part 1 stretches every budget further. Small models that run on a phone or laptop can reach people where cloud access is limited or expensive. Community efforts to build datasets and models for local languages go straight at the gap shown in the table above. National and regional compute programs, along with proposals like the UN global fund, can give researchers and public services access they could not afford alone.&lt;/p&gt;

&lt;h2&gt;
  
  
  What would help, and who can do it
&lt;/h2&gt;

&lt;p&gt;Benchmark and leaderboard maintainers could report cost per language alongside quality. A model that scores well in English but costs 6x more in Amharic should show both numbers. Adding a tokenizer ratio column, like the table above, takes very little effort.&lt;/p&gt;

&lt;p&gt;Model providers could publish the token ratio of their tokenizer for major languages, and treat a large gap as a defect to be fixed, the way the move from &lt;code&gt;cl100k&lt;/code&gt; to &lt;code&gt;o200k&lt;/code&gt; fixed much of it for Hindi and Arabic. Pricing free tiers by message rather than by token would also remove the penalty for users of costly languages.&lt;/p&gt;

&lt;p&gt;Developers building products for multilingual users can measure this before launch. The &lt;a href="https://gist.github.com/avneetbansal-aws/90fda99d5ceb5fdd80d34faa85c92bb3" rel="noopener noreferrer"&gt;script I used&lt;/a&gt; is about 60 lines and runs in seconds. If your users write in Tamil or Yoruba, your cost and latency estimates based on English test data will be wrong by a factor of two or more.&lt;/p&gt;

&lt;p&gt;Governments and funders can support open, local-language data and shared regional compute, and invest in the basics that every model depends on: electricity, connectivity, affordable devices, and skills. The ideas from Part 2 about sharing AI's value apply here as well. If AI creates wealth, some of it can fund access for those being left behind.&lt;/p&gt;

&lt;h2&gt;
  
  
  Closing
&lt;/h2&gt;

&lt;p&gt;The question of which AI is best will keep changing every few months. The more important question is who gets to use it, and at what price. Leaderboards measure capability. They do not measure fairness.&lt;/p&gt;

&lt;p&gt;The tokenizer result shows both sides of this. The same engineering effort that made AI several times cheaper for Hindi speakers has not yet reached Amharic or Yoruba speakers. The difference will not come from the models alone. It will come from the choices people make about access, cost, language, and infrastructure, and those choices are still open.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Next in this series: can AI feel, and should it predict us?&lt;/em&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  How the test was run
&lt;/h2&gt;

&lt;p&gt;The text is the 30 articles of the Universal Declaration of Human Rights from the Unicode UDHR project. Only articles present in all twelve translations were compared (all 30 were). Token counts come from &lt;code&gt;tiktoken&lt;/code&gt; 0.14.0 with the &lt;code&gt;o200k_base&lt;/code&gt; and &lt;code&gt;cl100k_base&lt;/code&gt; encodings. This measures token cost only, not answer quality, and covers OpenAI tokenizers only. Other providers use different tokenizers and will produce different numbers. Translations also differ slightly in length and style, so treat the ratios as approximate. The script is &lt;a href="https://gist.github.com/avneetbansal-aws/90fda99d5ceb5fdd80d34faa85c92bb3" rel="noopener noreferrer"&gt;available as a public gist&lt;/a&gt; and can be rerun with any language in the UDHR collection.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://hai.stanford.edu/ai-index/2026-ai-index-report/technical-performance" rel="noopener noreferrer"&gt;Stanford HAI: 2026 AI Index, technical performance&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://hai.stanford.edu/ai-index/2026-ai-index-report" rel="noopener noreferrer"&gt;Stanford HAI: 2026 AI Index report&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://blogs.microsoft.com/on-the-issues/2026/09/21/the-continued-state-of-global-ai-diffusion-in-2026/" rel="noopener noreferrer"&gt;Microsoft: The continued state of global AI diffusion in 2026 (Q2)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://blogs.microsoft.com/on-the-issues/2026/01/08/global-ai-adoption-in-2025/" rel="noopener noreferrer"&gt;Microsoft: Global AI adoption in 2025&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://sdgs.un.org/sites/default/files/2026-05/O2_Concept%20Note%20-%20Africa's%20AI%20Opportunity.pdf" rel="noopener noreferrer"&gt;UN concept note: Africa's AI opportunity&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.aljazeera.com/news/2026/10/25/ais-opportunity-divide-could-deepen-global-inequality-un-official-warns" rel="noopener noreferrer"&gt;Al Jazeera: AI's opportunity divide could deepen global inequality (October 2026)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.un.org/digital-emerging-technologies/content/secretary-general-calls-global-fund-address-ai-capacity-building-gaps-developing-countries" rel="noopener noreferrer"&gt;UN: Secretary-General calls for a global fund for AI capacity building&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://blogs.worldbank.org/en/developmenttalk/the-ai-wave-is-global--readiness-is-not--here-s-what-the-latest-" rel="noopener noreferrer"&gt;World Bank: The AI wave is global. Readiness is not.&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://arxiv.org/abs/2305.15425" rel="noopener noreferrer"&gt;Petrov et al. (2023): Language Model Tokenizers Introduce Unfairness Between Languages&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/eric-muller/udhr" rel="noopener noreferrer"&gt;Unicode UDHR project (translations used in the test)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/openai/tiktoken" rel="noopener noreferrer"&gt;OpenAI tiktoken&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>ai</category>
      <category>llm</category>
      <category>opensource</category>
      <category>discuss</category>
    </item>
    <item>
      <title>Textract vs Bedrock Data Automation vs Vision LLMs on Invoice Photos: 140 Images and a 6-Page Invoice, Real Calls</title>
      <dc:creator>Avneet bansal</dc:creator>
      <pubDate>Mon, 09 Nov 2026 15:04:19 +0000</pubDate>
      <link>https://dev.to/avneet_bansal_a65b3f31fc4/textract-vs-bedrock-data-automation-vs-vision-llms-on-invoice-photos-140-images-and-a-6-page-59n9</link>
      <guid>https://dev.to/avneet_bansal_a65b3f31fc4/textract-vs-bedrock-data-automation-vs-vision-llms-on-invoice-photos-140-images-and-a-6-page-59n9</guid>
      <description>&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%2F3gmj9cjsutx4e6p3uv8n.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%2F3gmj9cjsutx4e6p3uv8n.png" alt=" " width="800" height="336"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Invoices from the field rarely arrive as clean PDFs. They arrive as phone photos: the invoice lying on a stack of other papers, a coffee receipt on top of it, or a sheet held against a car dashboard. I wanted to know which extraction setup holds up in those conditions, what goes wrong, and what a team can do about it in a few lines of code.&lt;/p&gt;

&lt;p&gt;This post is a small comparison. Every number comes from real API calls, and every raw response was saved so the scoring could be re-run without calling AWS again. The code snippets below are the code I used, trimmed to the parts that matter. I ran each one against the saved responses to check that it reproduces the numbers in this post. The last section explains how the benchmark is built, so you can rebuild it for your own documents.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Out of the box, no tool got the invoice total right on more than 83% of the 40 photos. For Textract and BDA, the usual failure was not reading the text. It was picking the wrong amount: "Amount paid", "Amount due" or a previous balance.&lt;/li&gt;
&lt;li&gt;Small configuration changes moved totals as much as the choice of tool. A label filter plus a confidence-gated crop took Textract from 42.5% to 85% on totals. A custom BDA blueprint took BDA from 67.5% to 100%.&lt;/li&gt;
&lt;li&gt;A subtotal + tax = total check catches most OCR mistakes but few vision-model mistakes. Nova 2 Lite passed that check on 35 of 40 photos, and 20 of those had a wrong field, mostly the date or the invoice number.&lt;/li&gt;
&lt;li&gt;Using Textract for the values and Nova 2 Lite as a cross-check auto-approved 22 of 40 invoices with zero silent errors, for about $11 per 1,000.&lt;/li&gt;
&lt;li&gt;On photographed pages of a 6-page invoice, checking each page against its own carried-forward amount flagged every page with a real error and missed none.&lt;/li&gt;
&lt;li&gt;On 95 real receipts with a labelled total, Textract read it right 97.9% of the time. BDA and Nova often read "31.000" (Indonesian rupiah) as 31.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  What was tested
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Data.&lt;/strong&gt; Three sets.&lt;/p&gt;

&lt;p&gt;The first set is 5 synthetic invoices in 2 layouts, each photographed in 8 scenes: flat, angled, blur and glare, folded, low light, a stack of papers, a receipt lying on the invoice, and a dark dashboard-like background with a fuel receipt and a parking stub. That makes 40 images. The invoices are generated, so the answer key is exact, and every background paper has known values too. They contain traps that come up in accounts payable work: "Amount paid" and "Balance due" next to "Total", a "Previous balance" line, separate "Invoice to" and "Deliver to" blocks, and DD/MM/YYYY dates.&lt;br&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%2Fufbfolwnyn761jd987c0.jpg" 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%2Fufbfolwnyn761jd987c0.jpg" alt="he 40 synthetic photos: 5 invoices in 8 scenes" width="800" height="667"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The second set is the CORD v2 test split: 100 real phone photos of receipts from Indonesia (CC BY 4.0). 95 of them have a labelled total.&lt;/p&gt;

&lt;p&gt;The third set is one synthetic 6-page invoice with 149 line items. Each page ends with "Page subtotal (carried forward)" and the next page starts with "Brought forward". The header is only on page 1 and the totals are only on page 6. It was tested twice: as a clean PDF, and with each page photographed in a different scene and combined into one PDF, the way a phone scanner app would.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Tools.&lt;/strong&gt; All run in us-west-2.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Amazon Textract AnalyzeExpense (synchronous for single images, StartExpenseAnalysis for the 6-page PDF)&lt;/li&gt;
&lt;li&gt;Amazon Bedrock Data Automation (BDA) with the AWS public Invoice and Receipt blueprints, and with a custom blueprint&lt;/li&gt;
&lt;li&gt;Claude Sonnet 4.5 and Amazon Nova 2 Lite on Amazon Bedrock, through the &lt;code&gt;us.&lt;/code&gt; inference profiles, with one JSON prompt that asks for each value plus a 0 to 1000 bounding box (the prompt follows the pattern in &lt;a href="https://github.com/aws-samples/sample-document-information-localization" rel="noopener noreferrer"&gt;aws-samples/sample-document-information-localization&lt;/a&gt;), temperature 0&lt;/li&gt;
&lt;li&gt;Pre-processing before Textract: Apple Vision document detection with perspective correction, and PaddleOCR's DocPreprocessor (orientation plus UVDoc unwarping)&lt;/li&gt;
&lt;li&gt;A spatial check after Textract: &lt;a href="https://github.com/aws-samples/sample-textract-field-memory" rel="noopener noreferrer"&gt;textract-field-memory&lt;/a&gt;, an open-source library I wrote that learns where fields sit on a known layout (&lt;a href="https://dev.to/avneet_bansal_a65b3f31fc4/give-your-document-ocr-pipeline-a-memory-59l1"&gt;earlier post&lt;/a&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Two modes for Textract and BDA.&lt;/strong&gt; Out of the box means defaults and the highest-confidence answer. Configured means the change a team would make after reading the output once. The vision models ran with one prompt and no tuning. The configured changes are shown as code below, so you can judge whether they are fair.&lt;/p&gt;
&lt;h2&gt;
  
  
  Result 1: accuracy on 40 photos
&lt;/h2&gt;

&lt;p&gt;Brackets are 95% intervals. With 40 images they are wide, and that is the honest size of this study.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Setup&lt;/th&gt;
&lt;th&gt;Mode&lt;/th&gt;
&lt;th&gt;Invoice total right&lt;/th&gt;
&lt;th&gt;All 6 key fields right&lt;/th&gt;
&lt;th&gt;Median latency&lt;/th&gt;
&lt;th&gt;Cost / 1,000&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Textract AnalyzeExpense&lt;/td&gt;
&lt;td&gt;default&lt;/td&gt;
&lt;td&gt;42.5% (29 to 58)&lt;/td&gt;
&lt;td&gt;42.5%&lt;/td&gt;
&lt;td&gt;3.7 s&lt;/td&gt;
&lt;td&gt;$10.00&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;BDA, public Invoice blueprint&lt;/td&gt;
&lt;td&gt;default&lt;/td&gt;
&lt;td&gt;67.5% (52 to 80)&lt;/td&gt;
&lt;td&gt;65.0%&lt;/td&gt;
&lt;td&gt;25.9 s&lt;/td&gt;
&lt;td&gt;$40.00&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Claude Sonnet 4.5&lt;/td&gt;
&lt;td&gt;default&lt;/td&gt;
&lt;td&gt;70.0% (55 to 82)&lt;/td&gt;
&lt;td&gt;50.0%&lt;/td&gt;
&lt;td&gt;6.8 s&lt;/td&gt;
&lt;td&gt;$10.70&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Amazon Nova 2 Lite&lt;/td&gt;
&lt;td&gt;default&lt;/td&gt;
&lt;td&gt;82.5% (68 to 91)&lt;/td&gt;
&lt;td&gt;37.5%&lt;/td&gt;
&lt;td&gt;3.7 s&lt;/td&gt;
&lt;td&gt;$1.07&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Textract + label filter + confident crop&lt;/td&gt;
&lt;td&gt;configured&lt;/td&gt;
&lt;td&gt;85.0% (71 to 93)&lt;/td&gt;
&lt;td&gt;82.5%&lt;/td&gt;
&lt;td&gt;4.0 s&lt;/td&gt;
&lt;td&gt;$10.00&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;BDA, custom blueprint&lt;/td&gt;
&lt;td&gt;configured&lt;/td&gt;
&lt;td&gt;100.0% (91 to 100)&lt;/td&gt;
&lt;td&gt;67.5%&lt;/td&gt;
&lt;td&gt;25.2 s&lt;/td&gt;
&lt;td&gt;$40.00&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The six key fields are vendor, invoice number, invoice date, subtotal, tax and total. BDA latency includes the S3 upload and polling for the asynchronous job.&lt;/p&gt;
&lt;h3&gt;
  
  
  Textract picks the wrong amount
&lt;/h3&gt;

&lt;p&gt;Textract read the text well. The problem was which amount it labelled TOTAL. On Layout A it took the "Amount paid" line, which is printed as a negative number such as -$100.00, on 7 of 24 photos. On Layout B it was worse: 14 of 16 photos returned the amount due or the previous balance.&lt;br&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%2F60awqwq19nslywukaep1.jpg" 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%2F60awqwq19nslywukaep1.jpg" alt="Textract returns the Amount paid line as the total" width="800" height="700"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Every candidate in the AnalyzeExpense response carries the label printed next to it, so the fix is a few lines:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;re&lt;/span&gt;

&lt;span class="n"&gt;FIELDS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;VENDOR_NAME&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;vendor_name&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;INVOICE_RECEIPT_ID&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;invoice_number&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;INVOICE_RECEIPT_DATE&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;invoice_date&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;DUE_DATE&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;due_date&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;RECEIVER_NAME&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;customer_name&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;SUBTOTAL&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;subtotal&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;TAX&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;tax&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;TOTAL&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;total&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;NOT_THE_TOTAL&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;re&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;compile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;r&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;paid|balance|previous|due&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;re&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;I&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;SKIP_LABEL&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;TOTAL&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;NOT_THE_TOTAL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;SUBTOTAL&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;NOT_THE_TOTAL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;TAX&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;NOT_THE_TOTAL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
              &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;RECEIVER_NAME&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;re&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;compile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;r&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;deliver|ship&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;re&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;I&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;
&lt;span class="n"&gt;MONEY&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;SUBTOTAL&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;TAX&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;TOTAL&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;pick_fields&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;configured&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;Highest-confidence candidate per field, skipping amounts that are not the invoice total.&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
    &lt;span class="n"&gt;best&lt;/span&gt; &lt;span class="o"&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;doc&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;ExpenseDocuments&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;SummaryFields&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt;
            &lt;span class="n"&gt;kind&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Type&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Text&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
            &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;ValueDetection&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="p"&gt;{}).&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Text&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;""&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;label&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;LabelDetection&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="p"&gt;{}).&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Text&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;""&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;kind&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;FIELDS&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="k"&gt;continue&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;configured&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;kind&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;MONEY&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;strip&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;startswith&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;-&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
                &lt;span class="k"&gt;continue&lt;/span&gt;  &lt;span class="c1"&gt;# "Amount paid" is printed as a negative number
&lt;/span&gt;            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;configured&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;kind&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;SKIP_LABEL&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;SKIP_LABEL&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;kind&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;search&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;label&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
                &lt;span class="k"&gt;continue&lt;/span&gt;
            &lt;span class="n"&gt;rank&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Type&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Confidence&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;ValueDetection&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Confidence&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;kind&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;best&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="n"&gt;rank&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;best&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;kind&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt;
                &lt;span class="n"&gt;best&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;kind&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rank&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;FIELDS&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;best&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;items&lt;/span&gt;&lt;span class="p"&gt;()}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On real receipts the same filter cost 3.2 points (97.9% to 94.7%), so it is a per-document-type choice, not a free win.&lt;/p&gt;

&lt;h3&gt;
  
  
  BDA's public blueprint defines TOTAL as the amount due
&lt;/h3&gt;

&lt;p&gt;The public Invoice blueprint defines TOTAL as "total amount due including subtotal and tax". On the two invoices that print a previous balance, it returned the amount due on 13 of 16 photos.&lt;br&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%2Fow7jds9oeh62eiiw83b9.jpg" 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%2Fow7jds9oeh62eiiw83b9.jpg" alt="BDA public blueprint returns the amount due as TOTAL" width="800" height="700"&gt;&lt;/a&gt;&lt;br&gt;
A custom blueprint that asks for the two amounts separately got every total right. This is the full schema (9 fields):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;boto3&lt;/span&gt;

&lt;span class="n"&gt;S&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;lambda&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;type&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;inferenceType&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;explicit&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;instruction&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;SCHEMA&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;$schema&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;http://json-schema.org/draft-07/schema#&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;description&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;A single invoice photographed on a desk or dashboard. &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
                   &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Other papers may be visible; use only the invoice.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;class&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Invoice&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;type&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;object&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;properties&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;VENDOR_NAME&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;S&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;string&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Name of the company that issued the invoice, from the invoice header&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;INVOICE_NUMBER&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;S&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;string&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;The invoice number exactly as printed&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;INVOICE_DATE&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;S&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;string&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Invoice issue date as YYYY-MM-DD. If the invoice says dates &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
                                    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;are DD/MM/YYYY, convert accordingly&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;DUE_DATE&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;S&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;string&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Payment due date as YYYY-MM-DD, same date-format rule as INVOICE_DATE&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;CUSTOMER_NAME&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;S&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;string&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Name in the Bill to or Invoice to block. Not the Deliver to or &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
                                     &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Ship to block&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;SUBTOTAL&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;S&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;number&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Subtotal or net amount before tax&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;TAX&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;S&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;number&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Total tax or VAT amount on this invoice&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;INVOICE_TOTAL&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;S&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;number&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Total of this invoice: subtotal plus tax. Not the balance due, &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
                                     &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;amount due, amount paid or previous balance&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;AMOUNT_DUE&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;S&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;number&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Amount due or balance due, if printed separately from the &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
                                  &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;invoice total&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;bda&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;boto3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;client&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;bedrock-data-automation&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;region_name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;us-west-2&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;bda&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create_blueprint&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;blueprintName&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;invoice-benchmark-total-vs-due&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;DOCUMENT&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                     &lt;span class="n"&gt;blueprintStage&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;LIVE&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;schema&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;dumps&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SCHEMA&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One date problem remained. Both Layout B invoices state "Dates are shown as DD/MM/YYYY". The custom blueprint converted 07/10/2026 correctly on all 8 photos of one invoice, and read 11/08/2026 as November 8 on all 8 photos of the other. Instructions guide the model; they do not guarantee the result.&lt;/p&gt;

&lt;h3&gt;
  
  
  Cropping helps on clutter, fails on the dashboard
&lt;/h3&gt;

&lt;p&gt;Apple Vision's document detection helped on stacks of paper (Textract fields went from 60% to 87.5% right on that scene) and failed on the dashboard scene, where it detected the wrong region with a confidence of about 0.41.&lt;br&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%2Fgnb2uwuu8fvplf6ck5dl.jpg" 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%2Fgnb2uwuu8fvplf6ck5dl.jpg" alt="Apple Vision detects the wrong region on the dashboard scene" width="800" height="812"&gt;&lt;/a&gt;&lt;br&gt;
Using the crop only when the detector is confident kept the gain and removed the loss:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;CROP_MIN_CONFIDENCE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.8&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;image_to_send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;original&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cropped&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;crop_confidence&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;cropped&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;crop_confidence&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;CROP_MIN_CONFIDENCE&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="n"&gt;original&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this set every dashboard crop scored about 0.41 and every other crop scored 0.855 or more, so the threshold was not tested on borderline cases.&lt;/p&gt;

&lt;p&gt;PaddleOCR's unwarping helped Textract on the cluttered scenes (stack 60% to 70%, receipt on top 72.5% to 82.5%, dashboard 40% to 50% of fields) and made no difference on angled photos. Its orientation step turned 5 upright pages by 90 degrees, and totals stayed at 42.5%.&lt;/p&gt;

&lt;p&gt;textract-field-memory did not help on uncropped photos: no learned template matched any of them. My reading is that the page sits in a different spot in every photo, but I did not test that directly. After cropping, it flagged 46 of 63 wrong fields for review, and also flagged 43 of 257 correct ones. That makes it a review signal, not an extractor.&lt;/p&gt;

&lt;h2&gt;
  
  
  Result 2: what this means in production
&lt;/h2&gt;

&lt;p&gt;Accuracy does not tell you how much work is left. Two numbers do: how many invoices can be approved without a person, and how many errors slip through.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Rule&lt;/th&gt;
&lt;th&gt;Auto-approved&lt;/th&gt;
&lt;th&gt;Silent errors&lt;/th&gt;
&lt;th&gt;Cost / 1,000&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Nova 2 Lite, validation only&lt;/td&gt;
&lt;td&gt;35 of 40&lt;/td&gt;
&lt;td&gt;20&lt;/td&gt;
&lt;td&gt;$1.07&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Claude Sonnet 4.5, validation only&lt;/td&gt;
&lt;td&gt;34 of 40&lt;/td&gt;
&lt;td&gt;14&lt;/td&gt;
&lt;td&gt;$10.70&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;BDA custom blueprint, validation only&lt;/td&gt;
&lt;td&gt;35 of 40&lt;/td&gt;
&lt;td&gt;8&lt;/td&gt;
&lt;td&gt;$40.00&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Textract configured, validation only&lt;/td&gt;
&lt;td&gt;34 of 40&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;$10.00&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Textract configured values, Nova 2 Lite as cross-check&lt;/td&gt;
&lt;td&gt;22 of 40&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;$11.07&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Textract configured values, BDA custom as cross-check&lt;/td&gt;
&lt;td&gt;30 of 40&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;$50.00&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;A silent error is an approved invoice with any of the six key fields wrong. Validation and the cross-check look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;datetime&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;datetime&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;money&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="sh"&gt;"""'&lt;/span&gt;&lt;span class="s"&gt;$1,141.60&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt; -&amp;gt; 1141.6, &lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;-$100.00&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt; -&amp;gt; -100.0, 412.68 -&amp;gt; 412.68.&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nf"&gt;isinstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;float&lt;/span&gt;&lt;span class="p"&gt;)):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;round&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;float&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;re&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;search&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;r&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;(-?)[$£€]?(\d+(?:\.\d+)?)&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;,&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;""&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt; &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;""&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;round&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;float&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;group&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;group&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)),&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;validates&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;Key fields present and subtotal + tax = total, to the cent.&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
    &lt;span class="n"&gt;s&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;total&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;money&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;subtotal&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)),&lt;/span&gt; &lt;span class="nf"&gt;money&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;tax&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)),&lt;/span&gt; &lt;span class="nf"&gt;money&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;total&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&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;total&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;False&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nf"&gt;any&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="nf"&gt;str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&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;k&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="sh"&gt;""&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;strip&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;vendor_name&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;invoice_number&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;invoice_date&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;False&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;abs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;total&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mf"&gt;0.011&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;iso_date&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;Only unambiguous formats. 07/10/2026 could be July or October, so it returns None.&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;fmt&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;%Y-%m-%d&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;%b %d, %Y&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;datetime&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;strptime&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="sh"&gt;""&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;strip&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;date&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;isoformat&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nb"&gt;ValueError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;pass&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;same&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;field&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;field&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;invoice_date&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;da&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;iso_date&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nf"&gt;iso_date&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;da&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="n"&gt;da&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;field&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;subtotal&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;tax&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;total&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;money&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="nf"&gt;money&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nf"&gt;money&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;norm&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;lambda&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;re&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sub&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;r&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;[^a-z0-9]&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="sh"&gt;""&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;lower&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;bool&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;norm&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="nf"&gt;norm&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nf"&gt;norm&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b&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;auto_approve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;values&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cross_check&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;Approve with the first tool&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;s values only when both tools pass validation and agree.&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
    &lt;span class="nf"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;validates&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;values&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="nf"&gt;validates&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cross_check&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt;
            &lt;span class="nf"&gt;all&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;same&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;values&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;k&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;cross_check&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;k&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;k&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;k&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;invoice_number&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;invoice_date&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;subtotal&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;tax&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;total&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The arithmetic check works well for Textract: none of the 23 Textract photos with a wrong amount passed it. It works poorly for the vision models, for two reasons. Most of their silent errors are in fields the check never looks at: of Nova's 20, the date was wrong on 15 and the invoice number on 7. Some of their money errors also add up. Sonnet read 381.23 + 31.45 = 412.68 on a blurred photo as 391.23 + 31.40 = 422.63, and 6 of the 12 Sonnet photos with a wrong amount passed the check this way.&lt;br&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%2Fdefz6yb6dbl2t90tg90o.jpg" 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%2Fdefz6yb6dbl2t90tg90o.jpg" alt="Sonnet misreads digits and the wrong numbers still add up" width="800" height="728"&gt;&lt;/a&gt;&lt;br&gt;
Agreement routing works, and the order matters. With Textract supplying the values and Nova checking them, 22 invoices were approved and none were wrong. Swapping the roles (Nova values, Textract check) approved the same 22 with 9 silent errors, all of them dates. Textract returns the date as printed, so a DD/MM/YYYY date cannot be compared safely, and Nova's conversion of those dates was its weak spot.&lt;/p&gt;

&lt;p&gt;All 8 of BDA custom's silent errors are the 11/08/2026 date from Result 1.&lt;/p&gt;
&lt;h2&gt;
  
  
  Result 3: real receipts
&lt;/h2&gt;

&lt;p&gt;95 of the 100 CORD receipts have a labelled total.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Setup&lt;/th&gt;
&lt;th&gt;Total right&lt;/th&gt;
&lt;th&gt;Read 31.000 as 31&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Textract AnalyzeExpense&lt;/td&gt;
&lt;td&gt;97.9% (93 to 99)&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Claude Sonnet 4.5&lt;/td&gt;
&lt;td&gt;91.6% (84 to 96)&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Amazon Nova 2 Lite&lt;/td&gt;
&lt;td&gt;83.2% (74 to 89)&lt;/td&gt;
&lt;td&gt;12&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;BDA, public Receipt blueprint&lt;/td&gt;
&lt;td&gt;76.8% (67 to 84)&lt;/td&gt;
&lt;td&gt;20&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;If your documents use a dot as the thousands separator, test that case first. It accounts for 20 of BDA's 22 misses and 12 of Nova's 16.&lt;br&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%2Feitt06uq679os2wwbj5u.jpg" 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%2Feitt06uq679os2wwbj5u.jpg" alt="BDA reads 31.000 rupiah as 31" width="800" height="780"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Result 4: a 6-page invoice with 149 line items
&lt;/h2&gt;

&lt;p&gt;Long invoices add problems that single pages do not have: the header and totals sit on different pages, every page has a running subtotal that looks like a total, and a model has to return about 150 rows without dropping or repeating one.&lt;/p&gt;

&lt;p&gt;Textract and BDA read the whole PDF natively, so they need no chunking. For the vision models I tested two ways. The first sends all 6 pages in one call. The second is structure-aware chunking: page 1 is read first, then pages 2 to 6 in parallel, each told the vendor, invoice number and date that page 1 returned. Each page is then checked against its own printed running totals, and each page's brought-forward amount must equal the previous page's carried-forward amount:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;decimal&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Decimal&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;cents&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;Decimal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nf"&gt;quantize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Decimal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;0.01&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nb"&gt;Exception&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;pages_to_review&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pages&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;subtotal&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;pages: the model&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;s JSON for each page, in order, with brought_forward, carried_forward and
    line_items. subtotal: from the totals block on the last page. Returns the page numbers to review.&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
    &lt;span class="n"&gt;flagged&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;prev&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[],&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;enumerate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pages&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;flagged&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;prev&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;
            &lt;span class="k"&gt;continue&lt;/span&gt;
        &lt;span class="n"&gt;lines&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nf"&gt;cents&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;li&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;amount&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="nc"&gt;Decimal&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="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;li&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;line_items&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="p"&gt;[])&lt;/span&gt;
        &lt;span class="n"&gt;start&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;cents&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;brought_forward&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="nc"&gt;Decimal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;end&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;cents&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;carried_forward&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;end&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pages&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
            &lt;span class="n"&gt;end&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;subtotal&lt;/span&gt;
        &lt;span class="n"&gt;chained&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;prev&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;prev&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;end&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;chained&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="n"&gt;lines&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;flagged&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;prev&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;cents&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;carried_forward&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;flagged&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A flagged page is read once more by the other model at 1.5x size. Code, not the model, merges the pages and checks that all lines add up to the subtotal.&lt;/p&gt;

&lt;p&gt;For Textract, the fix is different. StartExpenseAnalysis returns results per page and labels each page's "Page subtotal (carried forward)" as TOTAL with a confidence above 99.9. Take totals from the last page only:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;last_page_only&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;page&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;max&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;PageNumber&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;SummaryFields&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]),&lt;/span&gt; &lt;span class="n"&gt;default&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;last&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;page&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;d&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;d&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;ExpenseDocuments&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;ExpenseDocuments&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;ExpenseDocuments&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nf"&gt;page&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;last&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;pick_fields_multipage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;fields&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;pick_fields&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;totals&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;pick_fields&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;last_page_only&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="n"&gt;fields&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;totals&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;k&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;k&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;subtotal&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;tax&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;total&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)})&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;fields&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Clean PDF:&lt;/strong&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Setup&lt;/th&gt;
&lt;th&gt;Total&lt;/th&gt;
&lt;th&gt;Line items right&lt;/th&gt;
&lt;th&gt;Latency&lt;/th&gt;
&lt;th&gt;Cost&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Textract async, default&lt;/td&gt;
&lt;td&gt;wrong (balance due)&lt;/td&gt;
&lt;td&gt;149 of 149, plus 5 running-total rows taken as items&lt;/td&gt;
&lt;td&gt;18 s&lt;/td&gt;
&lt;td&gt;$0.06&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Textract async, label filter only&lt;/td&gt;
&lt;td&gt;wrong (a page 3 carried-forward amount)&lt;/td&gt;
&lt;td&gt;149 of 149&lt;/td&gt;
&lt;td&gt;18 s&lt;/td&gt;
&lt;td&gt;$0.06&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Textract async, label filter + totals from last page&lt;/td&gt;
&lt;td&gt;right&lt;/td&gt;
&lt;td&gt;149 of 149&lt;/td&gt;
&lt;td&gt;18 s&lt;/td&gt;
&lt;td&gt;$0.06&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;BDA, public Invoice blueprint&lt;/td&gt;
&lt;td&gt;right&lt;/td&gt;
&lt;td&gt;107 of 149&lt;/td&gt;
&lt;td&gt;135 s&lt;/td&gt;
&lt;td&gt;$0.24&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;BDA, custom blueprint&lt;/td&gt;
&lt;td&gt;right&lt;/td&gt;
&lt;td&gt;not in schema&lt;/td&gt;
&lt;td&gt;37 s&lt;/td&gt;
&lt;td&gt;$0.24&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Sonnet 4.5, whole document&lt;/td&gt;
&lt;td&gt;right&lt;/td&gt;
&lt;td&gt;149 of 149&lt;/td&gt;
&lt;td&gt;87 s&lt;/td&gt;
&lt;td&gt;$0.16&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Sonnet 4.5, page chunks + checksum&lt;/td&gt;
&lt;td&gt;right&lt;/td&gt;
&lt;td&gt;149 of 149&lt;/td&gt;
&lt;td&gt;48 s&lt;/td&gt;
&lt;td&gt;$0.18&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Nova 2 Lite, whole document&lt;/td&gt;
&lt;td&gt;right&lt;/td&gt;
&lt;td&gt;149 of 149&lt;/td&gt;
&lt;td&gt;38 s&lt;/td&gt;
&lt;td&gt;$0.02&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Nova 2 Lite, page chunks + checksum&lt;/td&gt;
&lt;td&gt;right&lt;/td&gt;
&lt;td&gt;149 of 149&lt;/td&gt;
&lt;td&gt;21 s&lt;/td&gt;
&lt;td&gt;$0.03&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;BDA's public blueprint got every line amount right but returned the wrong quantity on 42 of them, mostly the quantity of a nearby row. Those amounts still add up to the subtotal, so a sum check cannot catch it. A quantity times unit price check per row would. On the clean PDF, chunking did not change accuracy, and it roughly halved latency because the pages run in parallel.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Photographed pages&lt;/strong&gt; (flat, angled, blur, folded, low light, and page 6 on a stack of other papers):&lt;br&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%2Fgqqckbgk7dqee0c1y28k.jpg" 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%2Fgqqckbgk7dqee0c1y28k.jpg" alt="he 6 photographed pages" width="800" height="178"&gt;&lt;/a&gt;&lt;br&gt;
| Setup | Total | Line items right | Lines add up to subtotal |&lt;br&gt;
|---|---|---|---|&lt;br&gt;
| Textract async, label filter + totals from last page | right | 144 of 149 | no |&lt;br&gt;
| BDA, public Invoice blueprint | right | 105 of 149 | yes |&lt;br&gt;
| Sonnet 4.5, whole document | wrong (tax misread) | 129 of 149 | no |&lt;br&gt;
| Sonnet 4.5, page chunks + checksum | right | 123 of 149 | no |&lt;br&gt;
| Nova 2 Lite, whole document | right | 127 of 149 | no |&lt;br&gt;
| Nova 2 Lite, page chunks + checksum | right | 136 of 149 | no |&lt;/p&gt;

&lt;p&gt;No setup got every line right on the photos. That is the real result, and it is why the checks matter. Textract and every vision-model setup failed the final "lines add up to subtotal" check, so none of them would be approved silently. BDA's public blueprint passed it while 44 rows did not match on amount and quantity.&lt;/p&gt;

&lt;p&gt;The page checksum is the part I would reuse. It flagged every page that had a wrong line, for both models, and missed none:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Model&lt;/th&gt;
&lt;th&gt;Pages with a real error&lt;/th&gt;
&lt;th&gt;Flagged&lt;/th&gt;
&lt;th&gt;Flagged but fine&lt;/th&gt;
&lt;th&gt;Missed&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Sonnet 4.5&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Nova 2 Lite&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The one false flag per model was page 4. Its lines were right, but the carried-forward amount on the blurred photo of page 3 was misread, so the chain check failed on the next page. The retry was less useful: reading a flagged page again with the other model at 1.5x size fixed 1 of the 7 pages with real errors. On photos, chunking tells you which pages a person should look at. It does not repair them. One more thing to watch: Sonnet's chunked run returned the customer as "Eastside Manufacturing Co.", a name that is not on the invoice ("Example Manufacturing Co." is). The whole-document call read it correctly.&lt;/p&gt;
&lt;h2&gt;
  
  
  Other things worth knowing
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Run-to-run variance.&lt;/strong&gt; At temperature 0, both Bedrock models gave the same answer in all 3 runs for about 94% of fields. Total accuracy moved by up to 2.5 points between runs.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Bounding boxes from vision models.&lt;/strong&gt; Nova 2 Lite's boxes overlapped the printed value 91.6% of the time. Sonnet 4.5's boxes overlapped 0% as returned. Its y values ran about 1.27 times too large, and dividing y by 4/3 brought 64.4% into overlap. If you need boxes for an audit trail, verify them.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Malformed output.&lt;/strong&gt; Sonnet returned JSON with an extra closing brace on 2 of 100 receipts and 1 of 120 invoice calls. Nova returned a malformed reply on 1 receipt and 2 invoice calls. Decode the first JSON object and log when you had to:
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;first_json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;JSONDecoder&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;raw_decode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;):])[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nb"&gt;ValueError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Line items on single pages.&lt;/strong&gt; The BDA public blueprint got every line item amount and quantity right on all 40 photos. Textract found 82% of them (88% with the confident crop).&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Which setup for which job
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;If you need&lt;/th&gt;
&lt;th&gt;Start with&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Lowest cost, with a person reviewing&lt;/td&gt;
&lt;td&gt;Nova 2 Lite, around $1 per 1,000. Do not auto-approve on an arithmetic check alone&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Auto-approval with few silent errors&lt;/td&gt;
&lt;td&gt;Textract with a label filter and confident crop, cross-checked by a cheap vision model&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Line items on single-page invoices&lt;/td&gt;
&lt;td&gt;BDA public Invoice blueprint. On long invoices, check quantity times unit price per row&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Line items on long or multi-page invoices&lt;/td&gt;
&lt;td&gt;Textract async with totals from the last page had the most lines right here. Add a page-level running-total checksum to whichever tool you use, so a person reviews only the flagged pages&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Bounding boxes you can show an auditor&lt;/td&gt;
&lt;td&gt;Textract and BDA return a box for every value. I did not measure the accuracy of either; verify any vision-model box&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Photos with lots of other papers&lt;/td&gt;
&lt;td&gt;A document crop gated on the detector's confidence&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;
&lt;h2&gt;
  
  
  Cost
&lt;/h2&gt;

&lt;p&gt;Prices are on-demand list prices in us-west-2, checked on the &lt;a href="https://aws.amazon.com/textract/pricing/" rel="noopener noreferrer"&gt;Textract&lt;/a&gt; and &lt;a href="https://aws.amazon.com/bedrock/pricing/" rel="noopener noreferrer"&gt;Bedrock&lt;/a&gt; pricing pages: Textract AnalyzeExpense $0.010 per page, BDA custom output $0.040 per page (the rate for blueprints with up to 30 fields; the public Invoice blueprint has 19, the Receipt blueprint 16 and my custom blueprint 9), Claude Sonnet 4.5 $3.30 and $16.50 per million input and output tokens, and Nova 2 Lite $0.33 and $2.75. The model prices are for the &lt;code&gt;us.&lt;/code&gt; profiles used here. The &lt;code&gt;global.&lt;/code&gt; profiles are about 10% cheaper ($3 and $15, $0.30 and $2.50).&lt;/p&gt;

&lt;p&gt;The whole study, including the 6-page runs and three runs I later replaced, cost about $14.18 at list price.&lt;/p&gt;
&lt;h2&gt;
  
  
  Limits
&lt;/h2&gt;

&lt;p&gt;This is a small study: 5 invoice designs in 2 layouts photographed 40 ways, 100 real receipts from one country, and one 6-page invoice run once per setup. The photo scenes are generated; the dashboard is a drawn background, not a car. ML Kit and VisionKit phone scanners were not tested. The vision models used one prompt with no tuning, while Textract and BDA got one round of configuration.&lt;/p&gt;
&lt;h2&gt;
  
  
  How the benchmark is built
&lt;/h2&gt;

&lt;p&gt;If you want to run the same comparison on your own documents, this is the structure. It has four stages, and each stage writes files the next one reads.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Stage&lt;/th&gt;
&lt;th&gt;What it does&lt;/th&gt;
&lt;th&gt;Output&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1. Generate&lt;/td&gt;
&lt;td&gt;Render each invoice from HTML with headless Chrome, and write the exact answer key from the same data&lt;/td&gt;
&lt;td&gt;Invoice PNG and PDF, &lt;code&gt;answer_key.json&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2. Photograph&lt;/td&gt;
&lt;td&gt;Warp each invoice onto a background with Pillow (perspective, blur, glare, folds, noise, other papers on top or behind) and record which background papers are in each shot&lt;/td&gt;
&lt;td&gt;40 JPEGs, a per-photo key with the background values&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3. Call&lt;/td&gt;
&lt;td&gt;Send each image to each tool once and save the full raw response with its measured latency&lt;/td&gt;
&lt;td&gt;One JSON file per image per tool&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4. Score&lt;/td&gt;
&lt;td&gt;Read the saved responses, apply the configured rules, compare with the answer key&lt;/td&gt;
&lt;td&gt;Tables, per-field outcomes, failure images&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;The answer key carries the traps.&lt;/strong&gt; Generating the invoices means the key is exact, and it can list every other amount printed on the page, so a wrong answer can be classified instead of just counted:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"inv-01"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"layout"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"A"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"vendor_name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Bluefin Office Supply Co."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"invoice_number"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"BOS-24817"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"invoice_date"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"2026-10-14"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"due_date"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"2026-11-13"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"customer_name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Example Corp"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"currency"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"USD"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"subtotal"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"381.23"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"tax"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"31.45"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"total"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"412.68"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"line_items"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[{&lt;/span&gt;&lt;span class="nl"&gt;"description"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Copy paper, A4, 80 gsm (box of 5 reams)"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
                    &lt;/span&gt;&lt;span class="nl"&gt;"quantity"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nl"&gt;"unit_price"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"38.50"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nl"&gt;"amount"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"154.00"&lt;/span&gt;&lt;span class="p"&gt;}],&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"traps"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nl"&gt;"amount_paid"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"100.00"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nl"&gt;"balance_due"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"312.68"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The background papers (another invoice, a bank statement, receipts, a parking stub) get their own key, and a check at generation time makes sure none of their amounts equals an amount on a main invoice. Otherwise a background value could not be told apart from a trap.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Every call is made once and saved.&lt;/strong&gt; Scoring then runs from disk, so you can change a rule and re-score in seconds without paying again:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;pathlib&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Path&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;cached_call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;folder&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;case_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;Run fn() once and save {&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;latency_s&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;, &lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;called_at&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;, &lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;response&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;}; later runs read the file.&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
    &lt;span class="n"&gt;path&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Path&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;results/raw&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;folder&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;case_id&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;/&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;__&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;.json&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mkdir&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parents&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;exist_ok&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;exists&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;loads&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;read_text&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
    &lt;span class="n"&gt;start&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;time&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;record&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;latency_s&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;round&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;time&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
              &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;called_at&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;strftime&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;%Y-%m-%dT%H:%M:%SZ&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;gmtime&lt;/span&gt;&lt;span class="p"&gt;()),&lt;/span&gt;
              &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;response&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;write_text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;dumps&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;record&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;indent&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;default&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nb"&gt;str&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;record&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Each money field gets one of five outcomes.&lt;/strong&gt; This is what turns "79% accurate" into "it picked the amount-paid line":&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;classify_money&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pred&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;truth&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;traps&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;background_values&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;correct, trap (another amount on the same invoice), background (an amount on another
    paper in the photo), wrong, or missing.&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
    &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;money&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pred&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;missing&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nf"&gt;money&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;truth&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;correct&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="c1"&gt;# "Amount paid" is printed as a negative number, so compare magnitudes for traps.
&lt;/span&gt;    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nf"&gt;abs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nf"&gt;abs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;money&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="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;traps&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;values&lt;/span&gt;&lt;span class="p"&gt;()}:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;trap&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;background_values&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;background&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;wrong&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Dates follow the tool's format. A tool that returns ISO dates must convert correctly, and swapping day and month counts as a trap. A tool that returns the printed text must match the printed text. Line items match only when amount and quantity are both right.&lt;/p&gt;

&lt;p&gt;The real receipts come from the CORD v2 test split on Hugging Face (&lt;code&gt;naver-clova-ix/cord-v2&lt;/code&gt;, CC BY 4.0). Parse the labels and the predictions with the same function, because the labels mix formats ("60.000", "63,000.00", "9.500,00").&lt;/p&gt;

&lt;p&gt;If you run invoices through any of these tools, I would like to hear which failure you hit most: the wrong amount, the wrong date, or the wrong row.&lt;/p&gt;

</description>
      <category>aws</category>
      <category>machinelearning</category>
      <category>python</category>
      <category>ocr</category>
    </item>
    <item>
      <title>Spatial Memory vs Correction Memory: I Benchmarked Both Against a Plain LLM</title>
      <dc:creator>Avneet bansal</dc:creator>
      <pubDate>Thu, 05 Nov 2026 04:16:24 +0000</pubDate>
      <link>https://dev.to/avneet_bansal_a65b3f31fc4/spatial-memory-vs-correction-memory-i-benchmarked-both-against-a-plain-llm-3c39</link>
      <guid>https://dev.to/avneet_bansal_a65b3f31fc4/spatial-memory-vs-correction-memory-i-benchmarked-both-against-a-plain-llm-3c39</guid>
      <description>&lt;p&gt;Earlier this year I wrote about two small open-source libraries for document extraction. The first, &lt;a href="https://dev.to/avneet_bansal_a65b3f31fc4/give-your-document-ocr-pipeline-a-memory-59l1"&gt;Give Your Document OCR Pipeline a Memory&lt;/a&gt;, introduced &lt;a href="https://github.com/aws-samples/sample-textract-field-memory" rel="noopener noreferrer"&gt;&lt;code&gt;textract-field-memory&lt;/code&gt;&lt;/a&gt;, which remembers where fields sit on a recurring layout. The second, &lt;a href="https://dev.to/avneet_bansal_a65b3f31fc4/your-llm-keeps-making-the-same-extraction-mistake-heres-how-to-make-it-learn-5bjb"&gt;Your LLM Keeps Making the Same Extraction Mistake&lt;/a&gt;, introduced &lt;a href="https://github.com/aws-samples/sample-prompt-correction-memory" rel="noopener noreferrer"&gt;&lt;code&gt;sample-prompt-correction-memory&lt;/code&gt;&lt;/a&gt;, which turns a human correction into something the pipeline keeps.&lt;/p&gt;

&lt;p&gt;Each post made its own case. Neither one answered the question readers kept asking me: if you can only add one, which one, and what does each actually buy you against a plain LLM?&lt;/p&gt;

&lt;p&gt;So I built a small benchmark that runs both mechanisms against the same document stream, next to a no-memory baseline, and measured them on the same five metrics. The short answer is that they are not competitors. One is a cost lever and the other is a quality lever.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR.&lt;/strong&gt; On 120 synthetic invoices and forms with Claude Haiku 4.5 on Amazon Bedrock: spatial memory cut LLM calls from &lt;strong&gt;600 to 30&lt;/strong&gt; (about 95%) and p50 latency from &lt;strong&gt;3.3 s to a memory read&lt;/strong&gt;. Correction memory left the call count alone but cut the repeated-error rate from &lt;strong&gt;0.185 to 0.012&lt;/strong&gt;. Both raised field accuracy from &lt;strong&gt;0.815 to about 0.99&lt;/strong&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  The setup
&lt;/h2&gt;

&lt;p&gt;Three arms process the identical seeded stream, in the same order.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Baseline.&lt;/strong&gt; Plain LLM extraction on every field of every document. No memory. This is the reference for cost and for how often a mistake comes back.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;+Spatial memory.&lt;/strong&gt; Models &lt;code&gt;textract-field-memory&lt;/code&gt;. For the first three documents of a type, it calls the model and records where each field sits. Once a field's position is stable within tolerance, it reads the value from the learned region and skips the model call.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;+Correction memory.&lt;/strong&gt; Models &lt;code&gt;sample-prompt-correction-memory&lt;/code&gt;. It always calls the model. The first time a field comes back wrong, a simulated reviewer corrects it, and the arm stores the correction as a generalizing transform (for example, "undo the day/month swap"), not as a frozen value. Later documents of that type get the transform applied.&lt;/p&gt;

&lt;p&gt;The dataset is 120 synthetic documents, 60 invoices and 60 forms, five fields each (invoice number, date, vendor, total, tax). Two fields per type carry a deterministic, systematic error, the kind a stateless pipeline repeats forever: a date with day and month swapped, and an amount with a stray leading digit (&lt;code&gt;12.34&lt;/code&gt; read as &lt;code&gt;112.34&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;The five metrics were written down in a test plan before any run: field accuracy, LLM calls, token cost, latency, and repeated-error rate. That last one is the share of later occurrences of a field that still show the same error after it was first corrected.&lt;/p&gt;

&lt;h2&gt;
  
  
  The results
&lt;/h2&gt;

&lt;p&gt;Real-model run: Amazon Bedrock, &lt;code&gt;us.anthropic.claude-haiku-4-5-20251001-v1:0&lt;/code&gt;, &lt;code&gt;us-west-2&lt;/code&gt;, seed 42.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Arm&lt;/th&gt;
&lt;th&gt;Field accuracy&lt;/th&gt;
&lt;th&gt;LLM calls&lt;/th&gt;
&lt;th&gt;Calls/doc&lt;/th&gt;
&lt;th&gt;Token cost (modeled)&lt;/th&gt;
&lt;th&gt;p50 latency&lt;/th&gt;
&lt;th&gt;p95 latency&lt;/th&gt;
&lt;th&gt;Repeated-error rate&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Baseline&lt;/td&gt;
&lt;td&gt;0.815&lt;/td&gt;
&lt;td&gt;600&lt;/td&gt;
&lt;td&gt;5.00&lt;/td&gt;
&lt;td&gt;$0.0556&lt;/td&gt;
&lt;td&gt;3,331 ms&lt;/td&gt;
&lt;td&gt;5,592 ms&lt;/td&gt;
&lt;td&gt;0.185&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;+Spatial memory&lt;/td&gt;
&lt;td&gt;0.990&lt;/td&gt;
&lt;td&gt;30&lt;/td&gt;
&lt;td&gt;0.25&lt;/td&gt;
&lt;td&gt;$0.0028&lt;/td&gt;
&lt;td&gt;~0 ms&lt;/td&gt;
&lt;td&gt;156 ms&lt;/td&gt;
&lt;td&gt;0.007&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;+Correction memory&lt;/td&gt;
&lt;td&gt;0.988&lt;/td&gt;
&lt;td&gt;600&lt;/td&gt;
&lt;td&gt;5.00&lt;/td&gt;
&lt;td&gt;$0.0556&lt;/td&gt;
&lt;td&gt;3,218 ms&lt;/td&gt;
&lt;td&gt;4,243 ms&lt;/td&gt;
&lt;td&gt;0.012&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;By document type, the baseline scored 0.80 on invoices and 0.83 on forms. Spatial memory reached 0.99 on both. Correction memory reached 1.00 on invoices and 0.98 on forms.&lt;/p&gt;

&lt;p&gt;The cost column is small because the run is small. The ratio is what matters: spatial memory ran at about 5% of the baseline's modeled token cost.&lt;/p&gt;

&lt;h2&gt;
  
  
  What each number is telling you
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Spatial memory is the cost story.&lt;/strong&gt; The 30 calls it made are exactly the warmup: 3 documents, times 5 fields, times 2 document types. After that, every field on every document was a lookup. That is why p50 latency collapses to a memory read and p95 drops to 156 ms (the tail is the warmup documents). On a stable, high-volume layout, the model is doing work the layout already answered.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Correction memory is the quality story.&lt;/strong&gt; It does not save a single call, because it still runs the model on every field. What it changes is what happens after the first mistake. In the baseline, the date swap and the stray digit come back on every later document, which is where the 0.185 repeated-error rate comes from. With correction memory, each systematic error is paid for once, by one review, and then it stops. The rate drops to 0.012.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;They fix different failure modes.&lt;/strong&gt; Spatial memory removes redundant calls on fields the layout pins down. Correction memory removes errors the model keeps making on calls that still happen. A pipeline that has both problems needs both layers, and a combined arm is the obvious next configuration to measure.&lt;/p&gt;

&lt;h2&gt;
  
  
  How this connects the two earlier posts
&lt;/h2&gt;

&lt;p&gt;The OCR memory post ended with a direct ask: the stress tests there were operational numbers, with no labeled benchmark and no comparison against a baseline. This benchmark is a first step on that. It has ground-truth values, a fixed baseline, and pre-registered metrics.&lt;/p&gt;

&lt;p&gt;The correction memory post described a three-tier design: graduated rules at zero cost, a confidence-gated LLM tier, and few-shot self-healing for low-confidence fields. This benchmark isolates one property of that design, that a correction applied once stops the same mistake from recurring, and measures it on a stream.&lt;/p&gt;

&lt;p&gt;Put together, the two posts describe two separate places a pipeline wastes effort. This post puts numbers on both, side by side.&lt;/p&gt;

&lt;h2&gt;
  
  
  What this benchmark does not show
&lt;/h2&gt;

&lt;p&gt;These caveats matter more than the headline, so read them before quoting a number.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The arms model the libraries, they do not import them.&lt;/strong&gt; Each arm is a self-contained reimplementation of the library's core mechanism, so the benchmark runs with no install and no network. The published libraries do more (IoU and center-distance scoring, template identification, drift detection, rule graduation, few-shot retry). Swapping the real libraries into the arms is the next step.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The spatial arm's accuracy is close to an upper bound.&lt;/strong&gt; When spatial confidence is high, the arm returns the value at the learned position, and on synthetic data that value is the ground truth. In a real pipeline, that read still depends on the OCR being right in that box. The defensible claim from this arm is the 95% call reduction. The accuracy gain says that on a stable layout, a positional read avoids the model's occasional misread, assuming the OCR is clean.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The correction arm knows two error patterns.&lt;/strong&gt; Its transform learner recognizes the day/month swap and the stray leading digit, because those are the errors the generator injects. A real correction log is messier, and the &lt;a href="https://dev.to/avneet_bansal_a65b3f31fc4/your-llm-keeps-making-the-same-extraction-mistake-heres-how-to-make-it-learn-5bjb"&gt;first post&lt;/a&gt; covers how the library handles that with few-shot retrieval instead of hand-written transforms.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The errors are synthetic.&lt;/strong&gt; In Bedrock mode, the model reads a pre-noised OCR span, so the baseline's errors come from the injected noise model, not from Haiku misreading a clean document. The accuracy and repeated-error deltas are properties of that noise model on this set. Treat them as directional.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Cost is modeled.&lt;/strong&gt; Measured input and output tokens from the Bedrock usage block, times the published Haiku 4.5 rate ($1.00 per 1M input tokens, $5.00 per 1M output tokens). It is not a bill.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;It is small.&lt;/strong&gt; 120 documents, two types, one seed for the real-model run.&lt;/p&gt;

&lt;h2&gt;
  
  
  The two mechanisms, in code
&lt;/h2&gt;

&lt;p&gt;The harness is small enough that the core of each arm fits on screen. This is the decision the spatial arm makes for every field:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;confident&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;memory&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;bbox&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;warmup&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;tol&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mf"&gt;0.02&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;True once a (doc_type, field) has a stable learned position.&lt;/span&gt;&lt;span class="sh"&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="ow"&gt;not&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;memory&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;False&lt;/span&gt;
    &lt;span class="n"&gt;learned_bbox&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;seen&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;memory&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;seen&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;warmup&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;False&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;all&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;abs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;tol&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;zip&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;learned_bbox&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;bbox&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

&lt;span class="c1"&gt;# per field:
&lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nf"&gt;confident&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;memory&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;doc_type&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;field_name&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;field_bbox&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;read_region&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;field_bbox&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;        &lt;span class="c1"&gt;# no LLM call
&lt;/span&gt;&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;call_llm&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;field&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;                &lt;span class="c1"&gt;# warmup or position moved
&lt;/span&gt;&lt;span class="nf"&gt;observe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;memory&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;doc_type&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;field_name&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;field_bbox&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And this is the correction arm. The important part is that the stored correction is a transform that inverts the systematic error, not the corrected value itself, so it applies to the next document regardless of its specific date or amount:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;learn_transform&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;raw&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;truth&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="c1"&gt;# Day/month swap: 2026-07-03 read as 2026-03-07
&lt;/span&gt;    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;raw&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;count&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;-&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;truth&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;count&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;-&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;ry&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rm&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rd&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;raw&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;-&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;ty&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;tm&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;td&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;truth&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;-&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;ry&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;ty&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;rm&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;td&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;rd&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;tm&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;lambda&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;-&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;-&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;-&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;-&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]])&lt;/span&gt;
    &lt;span class="c1"&gt;# Stray leading digit: 12.34 read as 112.34
&lt;/span&gt;    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;raw&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;raw&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;truth&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;lambda&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:]&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;lambda&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;   &lt;span class="c1"&gt;# not systematic, do not transform
&lt;/span&gt;
&lt;span class="c1"&gt;# per field:
&lt;/span&gt;&lt;span class="n"&gt;raw&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;call_llm&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;field&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="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;corrections&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;corrections&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;raw&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;          &lt;span class="c1"&gt;# apply what review taught us
&lt;/span&gt;&lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="n"&gt;raw&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;truth&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;reviewed&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;corrections&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="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;learn_transform&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;raw&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;truth&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;# one human review
&lt;/span&gt;    &lt;span class="n"&gt;reviewed&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="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;
    &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;truth&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;raw&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Bedrock call itself is a single-field Converse request at temperature 0 with &lt;code&gt;maxTokens&lt;/code&gt; 64, asking for the value only. Token counts come from the response &lt;code&gt;usage&lt;/code&gt; block, which is what drives the cost column.&lt;/p&gt;

&lt;h2&gt;
  
  
  Rebuilding it yourself
&lt;/h2&gt;

&lt;p&gt;The harness is not public, but nothing in it is hard to rebuild, and the method is the useful part. Generate a few hundred synthetic documents from two or three fixed templates, with ground-truth values and bounding boxes per field. Inject one or two systematic errors per type (a date swap and a stray digit are enough to start). Run every arm over the same seeded stream in the same order, log every field, and compute the metrics from the logs afterwards rather than inside the arms. That last step keeps the arms simple and makes each number auditable against the raw records.&lt;/p&gt;

&lt;p&gt;I also ran the same harness with a deterministic stub in place of the model, with no AWS account, to check the metric math. It showed the same structure as the Bedrock run: the baseline at 0.803 accuracy and a 0.197 repeated-error rate, spatial memory at 30 calls and 0.990 accuracy, and correction memory at 1.000 accuracy with no repeated errors. When the stub and the real model agree on the shape, the shape comes from the mechanism and not from one model's behavior.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where this goes next
&lt;/h2&gt;

&lt;p&gt;Three things are on the list. A combined arm, spatial memory first and correction memory on whatever still reaches the model. The real libraries swapped in for the reimplemented mechanisms. And document types where layouts drift, which is where spatial memory should hand back to the model and where &lt;code&gt;textract-field-memory&lt;/code&gt;'s drift detection earns its place.&lt;/p&gt;

&lt;p&gt;If you run document pipelines and have a layout-heavy, correction-heavy workload, I would like to hear which of the two problems costs you more. That answer decides which layer to add first.&lt;/p&gt;

&lt;p&gt;Libraries: &lt;a href="https://github.com/aws-samples/sample-textract-field-memory" rel="noopener noreferrer"&gt;&lt;code&gt;textract-field-memory&lt;/code&gt;&lt;/a&gt; and &lt;a href="https://github.com/aws-samples/sample-prompt-correction-memory" rel="noopener noreferrer"&gt;&lt;code&gt;sample-prompt-correction-memory&lt;/code&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>aws</category>
      <category>llm</category>
      <category>machinelearning</category>
      <category>benchmark</category>
    </item>
    <item>
      <title>From Writing the Spec to Approving It: What AI-DLC Looks Like on a Real Build</title>
      <dc:creator>Avneet bansal</dc:creator>
      <pubDate>Fri, 30 Oct 2026 10:19:29 +0000</pubDate>
      <link>https://dev.to/avneet_bansal_a65b3f31fc4/from-writing-the-spec-to-approving-it-what-ai-dlc-looks-like-on-a-real-build-58j3</link>
      <guid>https://dev.to/avneet_bansal_a65b3f31fc4/from-writing-the-spec-to-approving-it-what-ai-dlc-looks-like-on-a-real-build-58j3</guid>
      <description>&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%2Ffa0446039chfscftzjbf.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%2Ffa0446039chfscftzjbf.png" alt=" " width="800" height="336"&gt;&lt;/a&gt;&lt;br&gt;
Most AI-assisted development still follows the old split. A human writes the spec, a human writes the code, and the AI helps with autocomplete or generates tests at the end. AI-DLC (the AI-Driven Development Life Cycle) reverses that. The AI drafts the requirements, the design, and the code. The human answers short questions and approves each stage before the next one starts.&lt;/p&gt;

&lt;p&gt;To see what that looks like in practice, I built a small command-line tool with it and then read the record the workflow left behind. This post walks through that record, folder by folder.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;AI-DLC is public and open source: &lt;a href="https://github.com/awslabs/aidlc-workflows" rel="noopener noreferrer"&gt;awslabs/aidlc-workflows&lt;/a&gt;. The method comes from this &lt;a href="https://aws.amazon.com/blogs/devops/ai-driven-development-life-cycle/" rel="noopener noreferrer"&gt;AWS DevOps blog post&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;My only written input was one sentence. Everything else I did was pick options and approve gates.&lt;/li&gt;
&lt;li&gt;The run produced requirements, four architecture decisions, a unit plan, a traceability file, and 12 tests that passed when I ran them, in about 44 minutes from start to the code approval.&lt;/li&gt;
&lt;li&gt;The human role moves from author to decision-maker. The decisions are still yours, and the record shows where they changed the outcome.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;h2&gt;
  
  
  The app
&lt;/h2&gt;

&lt;p&gt;The tool is a Spooky Date Fact Generator. It picks one of 10 spooky dates (or takes &lt;code&gt;--date MM-DD&lt;/code&gt;) and prints a spooky event for that day from Wikipedia's "On this day" feed. It is small on purpose.&lt;/p&gt;

&lt;p&gt;The whole request was this sentence:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/aidlc build random date generator with fact around that number
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  The flip, in one table
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;Traditional with AI help&lt;/th&gt;
&lt;th&gt;AI-DLC&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Requirements&lt;/td&gt;
&lt;td&gt;Human writes them&lt;/td&gt;
&lt;td&gt;AI drafts them from multiple-choice answers&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Design&lt;/td&gt;
&lt;td&gt;Human sketches it&lt;/td&gt;
&lt;td&gt;AI proposes, human picks between options&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Code&lt;/td&gt;
&lt;td&gt;Human writes, AI assists&lt;/td&gt;
&lt;td&gt;AI writes against an approved plan&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Tests&lt;/td&gt;
&lt;td&gt;Human or AI at the end&lt;/td&gt;
&lt;td&gt;AI writes them to the stated requirements&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Human job&lt;/td&gt;
&lt;td&gt;Author&lt;/td&gt;
&lt;td&gt;Decide, validate, approve&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Evidence&lt;/td&gt;
&lt;td&gt;Pull request diff&lt;/td&gt;
&lt;td&gt;Audit log, decisions, traceability file&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Where everything was written
&lt;/h2&gt;

&lt;p&gt;Two locations matter. The generated app sits in its own folder, and the workflow record sits in an &lt;code&gt;aidlc/&lt;/code&gt; folder.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app/                                  &amp;lt;- the generated tool
  src/spooky-facts.ts      core: dates, fetch, keyword filter, nearest-date search
  src/cli.ts               command line: options, output, exit codes
  test/                    12 tests, offline, with a fake fetch
  package.json, biome.json, tsconfig.json, README.md

aidlc/
  codekb/                             &amp;lt;- 9 docs about the existing repo
  intents/&amp;lt;date&amp;gt;-date-fact-generator/ &amp;lt;- the record for this build
    aidlc-state.md                    stage checklist and settings
    audit/                            every event, decision, and approval
    inception/
      reverse-engineering/            scan of the existing repo
      requirements-analysis/          questions + requirements.md
      domain-design/                  questions + components.md + decisions.md
      units-generation/               unit-of-work files
    construction/u1-spooky-date-cli/code-generation/
      code-generation-plan.md, code-summary.md,
      unit-test-instructions.md, traceability.json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each stage writes a &lt;code&gt;-questions.md&lt;/code&gt; file, and your answers sit in the same file as the AI's questions. That makes the decision trail readable without any tooling.&lt;/p&gt;

&lt;h2&gt;
  
  
  Stage by stage: what it did and why it helps
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Initialization.&lt;/strong&gt; The workflow scaffolded the record folder, scanned the workspace, and classified it as brownfield TypeScript on Bun. This is automatic. It helps because every later stage starts from facts about the repo instead of guesses.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Reverse Engineering.&lt;/strong&gt; A developer agent scanned the existing repo and wrote nine knowledge-base documents (architecture, technology stack, code structure and others). This matters most in a brownfield repo. The later code followed the repo's Bun, TypeScript, and Biome conventions because the AI had read them first.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Requirements Analysis.&lt;/strong&gt; The AI asked six questions, such as where facts come from, what to do when a date has no spooky event, and whether this should be a web page or a command line. Each had lettered options. I answered, and one answer included a free-text note: generate dates that have spooky facts, from 10 such scenarios. The result is &lt;code&gt;requirements.md&lt;/code&gt; with 15 functional and 5 non-functional requirements, each with acceptance criteria and a tag pointing back to the answer that produced it, such as &lt;code&gt;[Q5]&lt;/code&gt;. This stage turns one vague sentence into testable statements before any code exists.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Domain Design.&lt;/strong&gt; Four more questions produced &lt;code&gt;components.md&lt;/code&gt; and four architecture decision records. The AI recommended splitting the tool into four parts. I chose two. The decision record states that the four-part split was rejected as more structure than needed. This is the clearest example of the validator role, because the human overrode the AI's recommendation and the reason is recorded.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Units Generation.&lt;/strong&gt; One question: build as one unit or two. I chose one, approved the plan, and got &lt;code&gt;unit-of-work.md&lt;/code&gt; with boundaries, responsibilities, and constraints. It fixes the blast radius. The unit is limited to &lt;code&gt;apps/date-fact-generator/&lt;/code&gt;, and nothing in the framework folders may change.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code Generation.&lt;/strong&gt; The AI wrote a plan, then the code, then the tests. It asked one thing here: Bun was not installed, so should it install Bun or switch to Node. I chose Bun. The &lt;code&gt;code-summary.md&lt;/code&gt; lists files created, decisions made, and deviations from the plan. One deviation worth noting: the AI added word forms such as "dies" and "sank" to the keyword list, because whole-word matching would otherwise miss "Houdini dies". The summary also states that some planned checks were grouped into fewer tests to match the 12-test count.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Build and Test.&lt;/strong&gt; In the record I read, this stage was still marked in progress, so its summary was not written yet. I ran the checks myself from the app folder: 12 tests passed with 93 assertions, and lint and typecheck were clean.&lt;/p&gt;

&lt;h2&gt;
  
  
  What the human actually did
&lt;/h2&gt;

&lt;p&gt;Reading the audit log, the human work was this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Typed one sentence to start.&lt;/li&gt;
&lt;li&gt;Approved a recomposed plan. The workflow's planner found the default express plan was missing inputs for later stages, and proposed adding Domain Design and Units Generation while dropping deployment and monitoring.&lt;/li&gt;
&lt;li&gt;Answered 11 multiple-choice questions across three stages, plus two more decisions: build inside the existing repo, and install Bun.&lt;/li&gt;
&lt;li&gt;Approved 5 stage gates: reverse engineering, requirements, domain design, units, and code generation.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That is the flip. Nothing in that list is writing a spec or writing code. All of it is deciding and checking.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why this holds up better than a prompt
&lt;/h2&gt;

&lt;p&gt;Two details make the validation real instead of a rubber stamp.&lt;/p&gt;

&lt;p&gt;First, traceability. &lt;code&gt;traceability.json&lt;/code&gt; maps every requirement ID (FR1.1 through NFR5, 20 in total) to the file that satisfies it. If a requirement has no target, it shows up.&lt;/p&gt;

&lt;p&gt;Second, the record is complete. The audit log holds the timestamp of each question, answer, and approval. A reviewer six months later can see why the design has two components and not four.&lt;/p&gt;

&lt;h2&gt;
  
  
  What this run does not show
&lt;/h2&gt;

&lt;p&gt;This was an express-scope build of a 658-line app, with plan approval, sensors, and learnings switched off by the scope. It shows the shape of the method, not how it behaves on a large system with many units and teams. The Build and Test stage had not completed in the record, and the tests are offline, so only one manual live smoke run, recorded in the code summary, exercised the real Wikipedia feed.&lt;/p&gt;

&lt;p&gt;The questions are only as good as the options offered. A weak option list can steer a hurried approver, so reading the alternatives still matters.&lt;/p&gt;

&lt;h2&gt;
  
  
  Takeaway
&lt;/h2&gt;

&lt;p&gt;The useful change is not that the AI writes more code. It is that the human's time moves to the points where judgment counts: what the tool should do, which design to accept, and whether the result meets the requirements. If you want to try it, start with a small brownfield repo and read the &lt;code&gt;-questions.md&lt;/code&gt; files first. They show the method faster than any description.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>aws</category>
      <category>softwaredevelopment</category>
      <category>devops</category>
    </item>
    <item>
      <title>Organization-Wide EC2 DNS Automation with EventBridge and AWS Config Aggregator: One Rule Per Account</title>
      <dc:creator>Avneet bansal</dc:creator>
      <pubDate>Mon, 26 Oct 2026 14:24:48 +0000</pubDate>
      <link>https://dev.to/avneet_bansal_a65b3f31fc4/organization-wide-ec2-dns-automation-with-eventbridge-and-aws-config-aggregator-one-rule-per-2p75</link>
      <guid>https://dev.to/avneet_bansal_a65b3f31fc4/organization-wide-ec2-dns-automation-with-eventbridge-and-aws-config-aggregator-one-rule-per-2p75</guid>
      <description>&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%2F50wivddg0wn8sy9s6wk5.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%2F50wivddg0wn8sy9s6wk5.png" alt=" " width="800" height="336"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Originally published on &lt;a href="https://medium.com/@avneetbansal9815/organization-wide-ec2-dns-automation-with-eventbridge-and-aws-config-aggregator-one-rule-per-ad334523f5d8" rel="noopener noreferrer"&gt;Medium&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;How to detect every EC2 launch and termination across an entire AWS Organization, and turn it into automated DNS registration, without deploying repeated infrastructure in every account.&lt;/p&gt;

&lt;p&gt;I spent longer than I would like to admit staring at this problem before the answer got simple. So if you are wiring up DNS across an AWS Organization right now, maybe this saves you a weekend.&lt;/p&gt;

&lt;p&gt;Here is the short version of what I landed on. I needed near real-time DNS registration for every EC2 instance across a multi-account, multi-region org, feeding an on-prem enterprise DDI system (think Infoblox). The two obvious ways to do it both annoyed me: one puts infrastructure in every account, the other leans on a fragile cross-account API-scraping job.&lt;/p&gt;

&lt;p&gt;What I used instead is a hub-and-spoke event setup:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Each spoke account gets exactly one EventBridge rule. That is the whole footprint.&lt;/li&gt;
&lt;li&gt;One hub account does everything else: a custom event bus, an SQS queue, one Lambda.&lt;/li&gt;
&lt;li&gt;The Lambda fills in the missing details by querying the AWS Config Aggregator, which, and this is the part I almost missed, already has every instance's IP, tags, and state for the entire org. No new data pipeline required.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Onboarding a new account came down to a one-line deploy. Below is the whole thing: the architecture, why I made the calls I made, and the code.&lt;/p&gt;

&lt;h2&gt;
  
  
  The problem (why the single-account version does not scale)
&lt;/h2&gt;

&lt;p&gt;In one account this is a five-minute job. EC2 fires a state-change event, you point an EventBridge rule at a Lambda, done. I have written that Lambda a dozen times.&lt;/p&gt;

&lt;p&gt;Across an organization it falls apart for a few reasons:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Instances come up in dozens of accounts and several regions.&lt;/li&gt;
&lt;li&gt;The people who run DNS are not the people who own the workload accounts, so "just add it to the account" is not really on the table.&lt;/li&gt;
&lt;li&gt;The EC2 state-change event does not include the IP address. You get an instance ID and a state. That is it. Useless for DNS on its own.&lt;/li&gt;
&lt;li&gt;Nobody wants to deploy, version, and babysit a Lambda plus queue plus IAM role in every account just to catch these events.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The usual reason you end up here is a move to an enterprise DDI platform. Every instance that boots needs a DNS record, every instance that dies needs its record cleaned up, org-wide, in near real-time, with as little junk in each account as you can manage.&lt;/p&gt;

&lt;p&gt;So two things really drove the design:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Adding an account should be almost free.&lt;/li&gt;
&lt;li&gt;The IP is not in the event, so I have to look it up, and that lookup cannot be flaky.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  The approaches I threw out
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Option A: put the whole stack in every account.&lt;/strong&gt; Rule, queue, Lambda, IAM role, shipped everywhere with StackSets. It works. But now you own N copies of the same code, and every bug fix or IAM tweak is an N-account rollout. I have maintained fleets like that. No thanks.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Option B: a central poller that assumes a role into the source account&lt;/strong&gt; and calls DescribeInstances for the IP. Logic is centralized, which is nice, but you need an assume-role trust in every account and you are paying a cross-account API hop on every single event. More IAM surface, more things to break.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Option C: hub-and-spoke, enriched from the Config Aggregator.&lt;/strong&gt; This is the one. All the logic sits in the hub. Spokes just forward. And instead of building anything to fetch the IP, the hub asks the Config Aggregator, which is already sitting there aggregating EC2 config (IP, tags, state) from every account and region.&lt;/p&gt;

&lt;p&gt;Option C won for a boring but decisive reason: the hard part, the enrichment data, already existed. I did not have to build it. I just had to notice it was there.&lt;/p&gt;

&lt;h2&gt;
  
  
  How it fits together
&lt;/h2&gt;

&lt;p&gt;Spokes forward EC2 state changes to the hub's custom bus; the hub enriches via the org Config Aggregator, then calls the DDI system. Spokes are dumb, the hub is smart, and the data already exists. That is the whole idea in one line.&lt;/p&gt;

&lt;p&gt;That second hub rule is easy to forget, by the way. If you only listen on the custom bus, the hub account's own instances never get DNS records, because they never leave home to reach the custom bus. Ask me how I know.&lt;/p&gt;

&lt;h2&gt;
  
  
  Filling in the blanks
&lt;/h2&gt;

&lt;p&gt;Most single-account tutorials skip this bit, which is exactly the bit that matters at scale. Here is what the event actually gives you:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"source"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"aws.ec2"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"detail-type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"EC2 Instance State-change Notification"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"detail"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"instance-id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"i-1234567890"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"state"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"running"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An ID and a state. No IP, no tags, no hostname. So the Lambda goes and gets the rest, from the aggregator, not from the source account.&lt;/p&gt;

&lt;p&gt;The nice thing: the aggregator is org-level, so one query resolves an instance no matter which account or region it lives in. No assume-role, no per-account permissions to manage.&lt;/p&gt;

&lt;h2&gt;
  
  
  The code
&lt;/h2&gt;

&lt;p&gt;Everything here is vendor-neutral and anonymized. Swap the Infoblox call for whatever you actually talk to: Route 53, a CMDB, an IPAM API.&lt;/p&gt;

&lt;h3&gt;
  
  
  The spoke's one and only rule (Terraform)
&lt;/h3&gt;

&lt;p&gt;This is the entire per-account footprint. Ship it with StackSets or a Terraform module.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight hcl"&gt;&lt;code&gt;&lt;span class="c1"&gt;# --- Deployed in EVERY spoke account/region ---&lt;/span&gt;

&lt;span class="nx"&gt;resource&lt;/span&gt; &lt;span class="s2"&gt;"aws_cloudwatch_event_rule"&lt;/span&gt; &lt;span class="s2"&gt;"forward_ec2_state"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;name&lt;/span&gt;           &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"forward-ec2-state-to-hub"&lt;/span&gt;
  &lt;span class="nx"&gt;description&lt;/span&gt;    &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Forward EC2 running/terminated events to the hub account"&lt;/span&gt;
  &lt;span class="nx"&gt;event_bus_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"default"&lt;/span&gt;

  &lt;span class="nx"&gt;event_pattern&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;jsonencode&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="nx"&gt;source&lt;/span&gt;        &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"aws.ec2"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="s2"&gt;"detail-type"&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"EC2 Instance State-change Notification"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="nx"&gt;detail&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"running"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"terminated"&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="nx"&gt;resource&lt;/span&gt; &lt;span class="s2"&gt;"aws_cloudwatch_event_target"&lt;/span&gt; &lt;span class="s2"&gt;"to_hub_bus"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;rule&lt;/span&gt;           &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;aws_cloudwatch_event_rule&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;forward_ec2_state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;
  &lt;span class="nx"&gt;event_bus_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"default"&lt;/span&gt;
  &lt;span class="nx"&gt;arn&lt;/span&gt;            &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"arn:aws:events:${var.region}:${var.hub_account_id}:event-bus/ec2-dns-hub-bus"&lt;/span&gt;
  &lt;span class="nx"&gt;role_arn&lt;/span&gt;       &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;aws_iam_role&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;eventbridge_cross_account&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;arn&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;# Minimal role that lets EventBridge PutEvents to the hub bus&lt;/span&gt;
&lt;span class="nx"&gt;resource&lt;/span&gt; &lt;span class="s2"&gt;"aws_iam_role"&lt;/span&gt; &lt;span class="s2"&gt;"eventbridge_cross_account"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"eventbridge-forward-to-hub"&lt;/span&gt;
  &lt;span class="nx"&gt;assume_role_policy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;jsonencode&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="nx"&gt;Version&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"2012-10-17"&lt;/span&gt;
    &lt;span class="nx"&gt;Statement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[{&lt;/span&gt;
      &lt;span class="nx"&gt;Effect&lt;/span&gt;    &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Allow"&lt;/span&gt;
      &lt;span class="nx"&gt;Principal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Service&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"events.amazonaws.com"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="nx"&gt;Action&lt;/span&gt;    &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"sts:AssumeRole"&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="nx"&gt;resource&lt;/span&gt; &lt;span class="s2"&gt;"aws_iam_role_policy"&lt;/span&gt; &lt;span class="s2"&gt;"put_events"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;role&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;aws_iam_role&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;eventbridge_cross_account&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;
  &lt;span class="nx"&gt;policy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;jsonencode&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="nx"&gt;Version&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"2012-10-17"&lt;/span&gt;
    &lt;span class="nx"&gt;Statement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[{&lt;/span&gt;
      &lt;span class="nx"&gt;Effect&lt;/span&gt;   &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Allow"&lt;/span&gt;
      &lt;span class="nx"&gt;Action&lt;/span&gt;   &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"events:PutEvents"&lt;/span&gt;
      &lt;span class="nx"&gt;Resource&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"arn:aws:events:${var.region}:${var.hub_account_id}:event-bus/ec2-dns-hub-bus"&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;h3&gt;
  
  
  The hub's custom bus, with an org-scoped resource policy
&lt;/h3&gt;

&lt;p&gt;This policy is the thing that makes "one rule per spoke" actually work: any account in the org can publish, and the hub needs zero per-account config.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight hcl"&gt;&lt;code&gt;&lt;span class="c1"&gt;# --- Deployed ONCE in the hub account ---&lt;/span&gt;

&lt;span class="nx"&gt;resource&lt;/span&gt; &lt;span class="s2"&gt;"aws_cloudwatch_event_bus"&lt;/span&gt; &lt;span class="s2"&gt;"hub"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"ec2-dns-hub-bus"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;resource&lt;/span&gt; &lt;span class="s2"&gt;"aws_cloudwatch_event_bus_policy"&lt;/span&gt; &lt;span class="s2"&gt;"allow_org"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;event_bus_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;aws_cloudwatch_event_bus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hub&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;
  &lt;span class="nx"&gt;policy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;jsonencode&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="nx"&gt;Version&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"2012-10-17"&lt;/span&gt;
    &lt;span class="nx"&gt;Statement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[{&lt;/span&gt;
      &lt;span class="nx"&gt;Sid&lt;/span&gt;       &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"AllowOrgAccountsToPutEvents"&lt;/span&gt;
      &lt;span class="nx"&gt;Effect&lt;/span&gt;    &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Allow"&lt;/span&gt;
      &lt;span class="nx"&gt;Principal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"*"&lt;/span&gt;
      &lt;span class="nx"&gt;Action&lt;/span&gt;    &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"events:PutEvents"&lt;/span&gt;
      &lt;span class="nx"&gt;Resource&lt;/span&gt;  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;aws_cloudwatch_event_bus&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hub&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;arn&lt;/span&gt;
      &lt;span class="nx"&gt;Condition&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;StringEquals&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="s2"&gt;"aws:PrincipalOrgID"&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;var&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;aws_org_id&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}]&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Do not skip the &lt;code&gt;aws:PrincipalOrgID&lt;/code&gt; condition. It scopes that &lt;code&gt;Principal = "*"&lt;/code&gt; down to your organization instead of, you know, the entire internet. It is the one line standing between "elegant pattern" and "open event bus."&lt;/p&gt;

&lt;h3&gt;
  
  
  Rules, queue, and Lambda wiring (hub)
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight hcl"&gt;&lt;code&gt;&lt;span class="nx"&gt;resource&lt;/span&gt; &lt;span class="s2"&gt;"aws_sqs_queue"&lt;/span&gt; &lt;span class="s2"&gt;"events"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;name&lt;/span&gt;                       &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"ec2-dns-events"&lt;/span&gt;
  &lt;span class="nx"&gt;visibility_timeout_seconds&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;120&lt;/span&gt;
  &lt;span class="nx"&gt;redrive_policy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;jsonencode&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="nx"&gt;deadLetterTargetArn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;aws_sqs_queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;dlq&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;arn&lt;/span&gt;
    &lt;span class="nx"&gt;maxReceiveCount&lt;/span&gt;     &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;resource&lt;/span&gt; &lt;span class="s2"&gt;"aws_sqs_queue"&lt;/span&gt; &lt;span class="s2"&gt;"dlq"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"ec2-dns-events-dlq"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;# Rule on the custom bus (events from spokes)&lt;/span&gt;
&lt;span class="nx"&gt;resource&lt;/span&gt; &lt;span class="s2"&gt;"aws_cloudwatch_event_rule"&lt;/span&gt; &lt;span class="s2"&gt;"hub_custom"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;name&lt;/span&gt;           &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"ec2-state-custom-bus"&lt;/span&gt;
  &lt;span class="nx"&gt;event_bus_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;aws_cloudwatch_event_bus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hub&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;
  &lt;span class="nx"&gt;event_pattern&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;jsonencode&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="nx"&gt;source&lt;/span&gt;        &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"aws.ec2"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="s2"&gt;"detail-type"&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"EC2 Instance State-change Notification"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="nx"&gt;detail&lt;/span&gt;        &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"running"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"terminated"&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="nx"&gt;resource&lt;/span&gt; &lt;span class="s2"&gt;"aws_cloudwatch_event_target"&lt;/span&gt; &lt;span class="s2"&gt;"custom_to_sqs"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;rule&lt;/span&gt;           &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;aws_cloudwatch_event_rule&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hub_custom&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;
  &lt;span class="nx"&gt;event_bus_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;aws_cloudwatch_event_bus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hub&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;
  &lt;span class="nx"&gt;arn&lt;/span&gt;            &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;aws_sqs_queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;events&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;arn&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;# Second rule on the default bus catches the hub account's OWN instances&lt;/span&gt;
&lt;span class="nx"&gt;resource&lt;/span&gt; &lt;span class="s2"&gt;"aws_cloudwatch_event_rule"&lt;/span&gt; &lt;span class="s2"&gt;"hub_default"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;name&lt;/span&gt;           &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"ec2-state-default-bus"&lt;/span&gt;
  &lt;span class="nx"&gt;event_bus_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"default"&lt;/span&gt;
  &lt;span class="nx"&gt;event_pattern&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;jsonencode&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="nx"&gt;source&lt;/span&gt;        &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"aws.ec2"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="s2"&gt;"detail-type"&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"EC2 Instance State-change Notification"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="nx"&gt;detail&lt;/span&gt;        &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"running"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"terminated"&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="nx"&gt;resource&lt;/span&gt; &lt;span class="s2"&gt;"aws_cloudwatch_event_target"&lt;/span&gt; &lt;span class="s2"&gt;"default_to_sqs"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;rule&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;aws_cloudwatch_event_rule&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hub_default&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;
  &lt;span class="nx"&gt;arn&lt;/span&gt;  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;aws_sqs_queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;events&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;arn&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;resource&lt;/span&gt; &lt;span class="s2"&gt;"aws_lambda_event_source_mapping"&lt;/span&gt; &lt;span class="s2"&gt;"sqs_to_lambda"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;event_source_arn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;aws_sqs_queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;events&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;arn&lt;/span&gt;
  &lt;span class="nx"&gt;function_name&lt;/span&gt;    &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;aws_lambda_function&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;dns_registrar&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;arn&lt;/span&gt;
  &lt;span class="nx"&gt;batch_size&lt;/span&gt;       &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  The enrichment Lambda (Python)
&lt;/h3&gt;

&lt;p&gt;This is the heart of it: resolve the instance against the Config Aggregator, then act on the state.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;boto3&lt;/span&gt;

&lt;span class="n"&gt;config&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;boto3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;client&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;config&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;AGGREGATOR_NAME&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;OrganizationConfigAggregator&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;get_instance_details&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;instance_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;dict&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;Resolve IP, tags, account, and region from the org Config Aggregator.&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
    &lt;span class="n"&gt;expression&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;SELECT resourceId, accountId, awsRegion, &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;configuration.privateIpAddress, &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;configuration.publicIpAddress, tags &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
        &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;WHERE resourceId = &lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;instance_id&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt; &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;AND resourceType = &lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;AWS::EC2::Instance&lt;/span&gt;&lt;span class="sh"&gt;'"&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;resp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;select_aggregate_resource_config&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;Expression&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;expression&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;ConfigurationAggregatorName&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;AGGREGATOR_NAME&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;Limit&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="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;results&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;resp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Results&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[])&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;loads&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;results&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="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;results&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;handler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_context&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;record&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Records&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt;
        &lt;span class="n"&gt;body&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;loads&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;record&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;body&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
        &lt;span class="n"&gt;detail&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;detail&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="n"&gt;instance_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;detail&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;instance-id&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="n"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;detail&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;state&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

        &lt;span class="n"&gt;details&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;get_instance_details&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;instance_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;details&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="c1"&gt;# Terminated instances may already be gone from the aggregator;
&lt;/span&gt;            &lt;span class="c1"&gt;# fall back to event data for deregistration.
&lt;/span&gt;            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;terminated&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="nf"&gt;deregister_dns&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;instance_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;continue&lt;/span&gt;

        &lt;span class="n"&gt;ip&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;details&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;configuration&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{}).&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;privateIpAddress&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="n"&gt;details&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;configuration&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{}).&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;publicIpAddress&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;running&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;ip&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="nf"&gt;register_dns&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;instance_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ip&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;details&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;tags&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[]))&lt;/span&gt;
        &lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;terminated&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="nf"&gt;deregister_dns&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;instance_id&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;register_dns&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;instance_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ip&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;tags&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="c1"&gt;# Replace with your Infoblox / Route 53 / IPAM call.
&lt;/span&gt;    &lt;span class="c1"&gt;# Infoblox example: POST to the WAPI record:a endpoint.
&lt;/span&gt;    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;REGISTER &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;instance_id&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; -&amp;gt; &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;ip&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;deregister_dns&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;instance_id&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;DEREGISTER &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;instance_id&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One thing worth calling out: that &lt;code&gt;SELECT&lt;/code&gt; against the aggregator resolves a resource across every account and region in a single call. That is the capability that collapses the whole cross-account enrichment headache into a one-liner. I keep coming back to it for other problems now.&lt;/p&gt;

&lt;p&gt;And notice the fallback in the handler. A terminated instance has often already dropped out of the aggregator by the time you look, so you cannot rely on the lookup for deregistration. You fall back to the instance ID from the event. Small thing, but it will bite you in testing if you miss it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why there is an SQS queue in the middle
&lt;/h2&gt;

&lt;p&gt;You can target the Lambda straight from the rule, and for a demo I would. In production I put SQS in between on purpose:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A bulk launch, a scale-out, a StackSet rollout, can throw hundreds of events at you at once. SQS soaks that up and lets the Lambda drain at its own pace instead of getting hammered.&lt;/li&gt;
&lt;li&gt;When Infoblox or the aggregator has a bad minute, messages retry and eventually land in a DLQ you can actually look at. Nothing quietly disappears.&lt;/li&gt;
&lt;li&gt;Batching. Ten events per invocation, fewer cold starts, lower cost.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Decoupling ingest from processing is most of what separates "worked in the demo" from "still working at 2am during a mass launch."&lt;/p&gt;

&lt;h2&gt;
  
  
  What actually came out of it
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Detection and DNS updates happened within seconds, across the whole org.&lt;/li&gt;
&lt;li&gt;Adding an account or region was a single forwarding rule. No Lambda, no queue, no Config setup per account.&lt;/li&gt;
&lt;li&gt;Registration and cleanup were fully automated across the instance lifecycle, nobody touching DNS by hand.&lt;/li&gt;
&lt;li&gt;Cost tracked instance activity, not the clock, because there is no idle compute.&lt;/li&gt;
&lt;li&gt;The shape is reusable. Same hub-spoke plus aggregator-enrichment works for CMDB sync, security tagging, IPAM updates, anything org-wide and event-driven.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The part worth stealing
&lt;/h2&gt;

&lt;p&gt;Take Infoblox out of the picture and you are left with a pattern I now reach for often:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Fan org-wide events into one hub, then enrich them from a data source the organization already has, instead of building a new one.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Two moves do the work:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A custom event bus with an &lt;code&gt;aws:PrincipalOrgID&lt;/code&gt; resource policy gives you cross-account fan-in and a one-rule spoke footprint.&lt;/li&gt;
&lt;li&gt;The Config Aggregator, used as an enrichment lookup, resolves any resource in any account or region without an assume-role fan-out.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So the next time you are about to copy the same Lambda into 40 accounts, stop and ask whether a fan-in plus an aggregator you already run could do it with one rule per account instead. Usually it can.&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrapping up
&lt;/h2&gt;

&lt;p&gt;Honestly, none of the individual pieces here are clever. Cross-account EventBridge is documented. Config aggregators are documented. The win was in composing them so the per-account cost dropped to near zero and the enrichment data was something we already had lying around. That is where most of the good architecture calls seem to live, not in the shiny new component, but in the one you talked yourself out of building.&lt;/p&gt;

&lt;p&gt;If you end up adapting the enrichment step to Route 53, a CMDB, or your own IPAM, I am happy to talk through it in the comments.&lt;/p&gt;

</description>
      <category>aws</category>
      <category>devops</category>
      <category>serverless</category>
      <category>automation</category>
    </item>
    <item>
      <title>Kubernetes Security Is Three Problems, Not One: Admission, Identity, Runtime</title>
      <dc:creator>Avneet bansal</dc:creator>
      <pubDate>Thu, 22 Oct 2026 03:11:34 +0000</pubDate>
      <link>https://dev.to/avneet_bansal_a65b3f31fc4/kubernetes-security-is-three-problems-not-one-admission-identity-runtime-22f7</link>
      <guid>https://dev.to/avneet_bansal_a65b3f31fc4/kubernetes-security-is-three-problems-not-one-admission-identity-runtime-22f7</guid>
      <description>&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%2Fwe13j7s8q81m2v4uqw4k.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%2Fwe13j7s8q81m2v4uqw4k.png" alt=" " width="800" height="336"&gt;&lt;/a&gt;&lt;br&gt;
Kubernetes security failures usually do not look like network intrusions. An attacker who gets code execution inside a container, through an application bug, a poisoned dependency, or a policy gap that let a privileged pod schedule, is already inside the perimeter the firewall was watching. From there the firewall verified nothing: which secrets the process can reach, which syscalls it can make, whether the running image is even the one you signed.&lt;/p&gt;

&lt;p&gt;That is the useful reframe. Kubernetes security is not one problem at the boundary. It is three problems inside it, and each one needs its own control that keeps working when the others fail:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Admission&lt;/strong&gt; decides what is allowed to deploy, and with what configuration.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Identity&lt;/strong&gt; decides what each running pod is allowed to reach.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Runtime&lt;/strong&gt; watches what a pod actually does, which is often not what its manifest claimed.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;None of the individual tools below are new, and none of this is a new technique. What is worth sharing is how the layers fit together, so a failure in any one of them does not open an exploitable gap. The model maps cleanly onto NIST SP 800-207 (zero trust) and NIST SP 800-190 (container security), and you can hold it to the CIS Kubernetes Benchmark as a measurable floor.&lt;/p&gt;

&lt;h2&gt;
  
  
  Layer 1: admission, decide what gets in
&lt;/h2&gt;

&lt;p&gt;Start with what Kubernetes gives you for free. Pod Security Admission (PSA) enforces security profiles per namespace with no external tooling. The Restricted profile blocks host namespace sharing, privilege escalation, root contexts, and unsafe seccomp settings. A practical split is Restricted for externally facing namespaces and Baseline for internal platform namespaces with documented exceptions.&lt;/p&gt;

&lt;p&gt;PSA is a floor, not a scalpel. It enforces at the namespace level, so it cannot say "this specific Deployment must run as UID 1000 with this seccomp profile." That is where &lt;strong&gt;Kyverno&lt;/strong&gt; comes in. Its policies are plain YAML, they live in the same GitOps repo as your manifests, and they go through the same pull request review as any other change. Four rule types cover most needs:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;validate&lt;/code&gt; rejects non-compliant resources (for example, require every image to come from an approved ECR registry).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;mutate&lt;/code&gt; corrects omissions automatically, which turns "you forgot a security context" from an admission failure into a silent fix.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;generate&lt;/code&gt; creates dependent resources on creation.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;verifyImages&lt;/code&gt; enforces signature checks at admission time (more on that below).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For constraint logic that depends on the state of &lt;em&gt;other&lt;/em&gt; cluster resources, or cross-namespace rules, &lt;strong&gt;OPA Gatekeeper&lt;/strong&gt; and Rego express things Kyverno's YAML cannot. Running both is a reasonable choice: Kyverno for the common case, Gatekeeper where you need Rego's relational power.&lt;/p&gt;

&lt;p&gt;One rollout lesson worth stating plainly: both tools have an audit mode, and you should use it first. Measure violations with enforcement off, remediate systematically, then switch to enforce. Flipping straight to enforcement is how a security rollout turns into a production incident.&lt;/p&gt;

&lt;h2&gt;
  
  
  Layer 2: identity, kill the static credential
&lt;/h2&gt;

&lt;p&gt;A static AWS key in a ConfigMap or an environment variable is a persistent risk with a wide blast radius. &lt;strong&gt;IRSA&lt;/strong&gt; (IAM Roles for Service Accounts) removes the category entirely. The AWS SDK in a pod annotated with an IRSA service account exchanges a short-lived, audience-scoped JWT for temporary IAM credentials through STS. Nothing static is created, stored, or rotated. The IAM trust policy validates the token's subject claim against a specific service account and namespace, so a compromised pod in one namespace cannot assume another namespace's role.&lt;/p&gt;

&lt;p&gt;The payoff is blast-radius reduction you can describe concretely. Without IRSA, a compromised pod inherits whatever the node's instance profile can reach. With IRSA, it is limited to that one pod's scoped role.&lt;/p&gt;

&lt;p&gt;Application secrets need a parallel mechanism, and that is the &lt;strong&gt;ExternalSecrets Operator&lt;/strong&gt;. It syncs values from AWS Secrets Manager or Parameter Store into Kubernetes Secrets and refreshes them on the source's rotation schedule. When Secrets Manager rotates a database password, the new value propagates without a deployment. IRSA plus ExternalSecrets together removes every static credential: cloud access through IRSA, application secrets through ESO, nothing hardcoded in a Helm values file.&lt;/p&gt;

&lt;p&gt;Scaling per-workload IAM roles across hundreds of workloads is the part teams tend to defer. Attribute-based access control with IAM permission boundaries makes it tractable: a role that expresses "this service account, in this namespace, for this environment" can be generated from workload metadata instead of hand-authored each time.&lt;/p&gt;

&lt;h2&gt;
  
  
  Layer 3: runtime, watch what actually happens
&lt;/h2&gt;

&lt;p&gt;Admission decides what can deploy. Identity decides what it can reach. Neither sees what a pod does after it is running. &lt;strong&gt;Falco&lt;/strong&gt; does, using eBPF probes at the kernel level to observe every syscall from every container on the node. A shell spawned in a container whose image has no shell in its approved process list raises an alert, regardless of how the compromise happened or whether the attacker edited the deployment manifest. Because eBPF sits below the container runtime, it keeps working even if the runtime is tampered with, and it needs no change to the image or the application.&lt;/p&gt;

&lt;p&gt;Two practical notes. First, uniform rules across every workload generate false positives that erode trust in the alerts. A data-processing pod that legitimately reads many files will trip rules tuned for an application server. Per-workload rule sets, stored in Git next to the manifests, let you tighten baselines for payment or secret-handling services and loosen them for CI runners. Second, route Falco alerts into a SIEM so individual detections become correlated events. A shell-spawn alert followed within seconds by an outbound connection to an unknown IP is a pattern a correlation engine can act on, including automated network-policy quarantine of the affected pod.&lt;/p&gt;

&lt;p&gt;Plan for a calibration period. Default Falco rules produce false-positive volume that takes iterative tuning, with both security and application knowledge, before the layer runs at production-grade sensitivity.&lt;/p&gt;

&lt;h2&gt;
  
  
  The layer most teams skip: supply chain
&lt;/h2&gt;

&lt;p&gt;An image that passes admission is trusted to be what its manifest says, but only if the reference is cryptographically verified. Tags are mutable. Someone can overwrite the content behind a trusted tag and the cluster will deploy it under the same name, which bypasses every admission policy that checks the image's &lt;em&gt;properties&lt;/em&gt; rather than its &lt;em&gt;identity&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Cosign&lt;/strong&gt; signs images at the registry, and Kyverno &lt;code&gt;verifyImages&lt;/code&gt; rules reject anything at admission that is not signed by an approved identity. That closes the tag-mutability gap. &lt;strong&gt;SLSA&lt;/strong&gt; provenance is the next step up: signing proves an image came from your build system, provenance proves what went into that build (source commit, build platform, resolved dependencies). A Software Bill of Materials attached as an attestation then lets you answer "which running images contain this newly disclosed CVE?" by querying the SBOM store instead of inspecting containers one by one.&lt;/p&gt;

&lt;h2&gt;
  
  
  Putting it together
&lt;/h2&gt;

&lt;p&gt;The point of three independent layers is that no single failure is fatal. A policy gap that lets a bad pod through is still caught by scoped identity and runtime monitoring. A runtime compromise still cannot reach credentials the pod's IAM role never granted. A swapped image is rejected before it schedules.&lt;/p&gt;

&lt;p&gt;If you are starting from scratch, a reasonable order is:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Turn on Pod Security Admission at Baseline or Restricted. Free, immediate.&lt;/li&gt;
&lt;li&gt;Add Kyverno in audit mode, learn your violations, then enforce.&lt;/li&gt;
&lt;li&gt;Move workloads to IRSA and ExternalSecrets to delete static credentials.&lt;/li&gt;
&lt;li&gt;Deploy Falco, then budget real time to tune per-workload rules.&lt;/li&gt;
&lt;li&gt;Add Cosign signing and verifyImages, then SLSA provenance and SBOMs.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The honest tradeoff is maintenance. Policy-as-code needs upkeep as Kubernetes APIs and workloads change, and Falco rule tuning is ongoing work. The reason to pay that cost is coverage no single control gives you: admission, identity, and runtime failures are different problems, and defending only one of them leaves the other two open.&lt;/p&gt;

&lt;h3&gt;
  
  
  Further reading
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;NIST SP 800-207, Zero Trust Architecture.&lt;/li&gt;
&lt;li&gt;NIST SP 800-190, Application Container Security Guide.&lt;/li&gt;
&lt;li&gt;CIS Kubernetes Benchmark.&lt;/li&gt;
&lt;li&gt;Kyverno, OPA Gatekeeper, Falco, and Sigstore/Cosign project docs.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;em&gt;If you run this in production, I would be interested in which layer gave you the most trouble to operationalize. For me it is runtime rule tuning. Drop a comment.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>kubernetes</category>
      <category>security</category>
      <category>aws</category>
      <category>devops</category>
    </item>
  </channel>
</rss>
