<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:googleplay="http://www.google.com/schemas/play-podcasts/1.0"><channel><title><![CDATA[The T-Shaped Dev]]></title><description><![CDATA[AI writes the code. You still make the architecture calls and own what ships. One issue every Tuesday for mid-to-senior React, Node.js and TypeScript engineers.]]></description><link>https://thetshaped.dev</link><image><url>https://substackcdn.com/image/fetch/$s_!9lD-!,w_256,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7f66bb7c-c96f-4c71-ba2d-d561152c83a2_600x600.png</url><title>The T-Shaped Dev</title><link>https://thetshaped.dev</link></image><generator>Substack</generator><lastBuildDate>Wed, 02 Dec 2026 17:09:42 GMT</lastBuildDate><atom:link href="https://thetshaped.dev/feed" rel="self" type="application/rss+xml"/><copyright><![CDATA[Petar Ivanov]]></copyright><language><![CDATA[en]]></language><webMaster><![CDATA[petarivanovv9@substack.com]]></webMaster><itunes:owner><itunes:email><![CDATA[petarivanovv9@substack.com]]></itunes:email><itunes:name><![CDATA[Petar Ivanov]]></itunes:name></itunes:owner><itunes:author><![CDATA[Petar Ivanov]]></itunes:author><googleplay:owner><![CDATA[petarivanovv9@substack.com]]></googleplay:owner><googleplay:email><![CDATA[petarivanovv9@substack.com]]></googleplay:email><googleplay:author><![CDATA[Petar Ivanov]]></googleplay:author><itunes:block><![CDATA[Yes]]></itunes:block><item><title><![CDATA[One update vanished, and nothing logged an error]]></title><description><![CDATA[The launch price ends Monday at 23:59 PT. First, one senior question.]]></description><link>https://thetshaped.dev/p/my-black-friday-price-is-the-launch</link><guid isPermaLink="false">https://thetshaped.dev/p/my-black-friday-price-is-the-launch</guid><dc:creator><![CDATA[Petar Ivanov]]></dc:creator><pubDate>Sun, 29 Nov 2026 12:02:41 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!9lD-!,w_256,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7f66bb7c-c96f-4c71-ba2d-d561152c83a2_600x600.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Hey there,</p><p>Two requests read the same row, change it and write it back. The last write wins, and one update is gone without an error. That's Question 2.22 in The Senior JavaScript Interview, tagged Senior.</p><p>Calling it a race condition is the easy part. The senior answer checks each layer where it can happen:</p><ul><li><p>In the UI, abort stale requests with AbortController, so only the newest result reaches the screen. That fixes only the screen. The server can still apply the old write.</p></li><li><p>In one Node process, a mutex per row around the read and the write makes them run one at a time. It doesn't stop a second instance. Across instances, lock the row with SELECT ... FOR UPDATE, with the read and the write in one short transaction on one connection.</p></li><li><p>In the database, skip the read-modify-write in app code. Use an atomic update like UPDATE ... SET n = n + 1. Or add a version column, bump it in the SET and check the value you read in the WHERE clause. If 0 rows change, re-read, recompute and retry.</p></li></ul><p>The trap is the mutex. It looks like a fix until the second instance starts.</p><p>I'm not running a separate Black Friday sale. The launch price is my Black Friday price, and it ends on Monday 30 November at 23:59 Pacific time, which is 09:59 on Tuesday in Sofia. Until then, Standard is $79 and Premium is $149, plus VAT where it applies. After that, they go to $119 and $229.</p><p>What you get:</p><ul><li><p>660+ questions and architecture scenarios on React, Node.js, TypeScript and system design, each tagged Junior, Mid or Senior.</p></li><li><p>The course on petarivanov.tech, in a reader that hides each answer until you've given your own and keeps count of what you've answered, plus an EPUB.</p></li><li><p>With Premium, Part 6 on the interview itself and the Complete Packages of The Conscious React and The Conscious Node.</p></li></ul><p><a href="https://petarivanov.tech/senior-javascript-interview/?utm_source=substack&amp;utm_medium=email&amp;utm_campaign=playbook-launch&amp;utm_content=sub-bf29">Get the course at the launch price</a></p><p>There's a 30-day money-back guarantee. Reply to your receipt for a full refund.</p><p>If you already bought the course, thank you, and you can skip this one.</p><p>Petar</p>]]></content:encoded></item><item><title><![CDATA[AI Agent Memory Breaks the Day a Fact Changes]]></title><description><![CDATA[The traveler switched to aisle seats in October. The agent still books a window, because both memories look current.]]></description><link>https://thetshaped.dev/p/7-decisions-that-make-ai-agent-memory</link><guid isPermaLink="false">https://thetshaped.dev/p/7-decisions-that-make-ai-agent-memory</guid><pubDate>Wed, 25 Nov 2026 14:04:37 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/cf2a04b2-3c25-4225-bad9-59e6ddbda9d8_2912x1632.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://thetshaped.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption"><span data-color="#9900ff" style="color: rgb(153, 0, 255);">Get my Full-Stack JavaScript Architect Bundle for FREE on newsletter signup:</span></p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div><hr></div><p>In March, a traveler tells a trip agent she wants a window seat on long flights. In October, she says she wants an aisle seat from now on. A week later, the agent books her a window seat.</p><p>Nothing crashed. Both memories match "seat preference", both look current, and nothing tells the agent which one wins.</p><p>Agent memory is data with a lifecycle, and backend engineers solved most of this years ago for ordinary data. Here are seven decisions that carry those habits over to agents.</p><div><hr></div><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://thetshaped.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Join <strong>30,000+ engineers</strong> getting one practical issue a week on production-grade React, Node, Architecture, and <strong>real AI systems</strong>, and leveling up from Mid &#8594; Senior &#8594; Architect.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div><hr></div><h2>1. Store facts, not transcripts</h2><p>A memory is a durable fact, such as "prefers aisle seats on long flights". The 40-message chat where she said it is a log.</p><p>Keep the two apart. Search transcripts at recall time and you get chatter, plus the same fact in five phrasings competing for one slot in the prompt.</p><p><strong>Go deeper:</strong> <a href="https://arxiv.org/abs/2304.03442">Generative Agents: Interactive Simulacra of Human Behavior</a> (UIST 2023), where a reflection step "synthesizes memories into higher-level inferences over time".</p><div><hr></div><h2>2. Scope every search before you rank</h2><p>Filter by tenant and user before similarity search runs, and by trip only for facts that belong to one trip. Ranking decides what's relevant, not what a caller may see, so it won't keep another traveler's passport details out of the prompt.</p><p>A search that names no user should fail with an error instead of searching everyone's memories. A job that really needs to search across users should say so in the code, where a reviewer sees it.</p><p>Check that your vector index applies the filter during the search. Many approximate indexes filter after they scan, so a per-user query can come back empty. An index on tenant and user, or a partition per tenant, fixes it.</p><p><strong>Go deeper:</strong> OWASP, <a href="https://genai.owasp.org/llmrisk/llm082025-vector-and-embedding-weaknesses/">LLM08:2025 Vector and Embedding Weaknesses</a>. Jeremy Daly, <a href="https://fandf.co/4laDTqJ">Hybrid Retrieval for Agent Memory</a> on the Oracle Developers blog.</p><div><hr></div><h2>3. Search two ways, then rerank</h2><p>Some questions are about meaning. "Which seat does she like?" has to find "would rather sit by the aisle", although the two share almost no words. Vector search is good at that.</p><p>Other questions are about exact strings, like the booking reference <code>K7Q2XM</code>. Keyword search is good at those.</p><p>Run both inside the scope from decision 2, then take two more steps.</p><ol><li><p><strong>Merge the two lists.</strong> Reciprocal rank fusion (RRF) is the usual method. It looks only at positions, so a memory near the top of both lists ends up near the top of the merged one.</p></li><li><p><strong>Rerank the top few results.</strong> A reranker is a slower second model that scores each candidate against the question.</p></li></ol><p>Measure each step on your own data. Merging alone can score lower than a single good search.</p><p><strong>Go deeper:</strong> Cormack, Clarke and B&#252;ttcher, <a href="https://doi.org/10.1145/1571941.1572114">Reciprocal Rank Fusion outperforms Condorcet and individual Rank Learning Methods</a> (SIGIR 2009). The <a href="https://fandf.co/4laDTqJ">Hybrid Retrieval for Agent Memory</a> post scores each step on a small example.</p><div><hr></div><h2>4. When a fact changes, link it instead of overwriting it</h2><p>This is the decision that fixes the window seat.</p><p>Overwrite the old fact, and the agent loses the history it needs to explain itself ("you switched to aisle in October"). Append the new fact, and both look current. That's how the window seat got booked.</p><p>Data warehouses settled this long ago. They don't overwrite a customer's address when history matters. They close the old row, open a new one, and flag the current version. Kimball calls it a type 2 slowly changing dimension.</p><p>Do the same with memory. Keep both facts, link the new one to the old one with a typed link such as "supersedes", and mark the old one as no longer current. A search for the seat preference then has one current answer, the October one, with the March one attached as history.</p><p>Do the swap in one transaction, and let the database enforce the rule. Give each fact a slot key, such as the user plus "seat preference", with a unique index on it for current rows only. Read the current fact and decide whether it changed before the transaction opens, so a slow model call doesn't keep the transaction open. Then, inside the transaction, mark that exact fact as no longer current first, and insert the new one with its link after it. If the old fact was no longer current, or the insert hits the unique index, another writer got there first. Roll back, read again and decide again.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!SYWm!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89728def-7bbf-47d6-b3f5-c2b386a1ce09_1920x1080.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!SYWm!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89728def-7bbf-47d6-b3f5-c2b386a1ce09_1920x1080.png 424w, https://substackcdn.com/image/fetch/$s_!SYWm!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89728def-7bbf-47d6-b3f5-c2b386a1ce09_1920x1080.png 848w, https://substackcdn.com/image/fetch/$s_!SYWm!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89728def-7bbf-47d6-b3f5-c2b386a1ce09_1920x1080.png 1272w, https://substackcdn.com/image/fetch/$s_!SYWm!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89728def-7bbf-47d6-b3f5-c2b386a1ce09_1920x1080.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!SYWm!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89728def-7bbf-47d6-b3f5-c2b386a1ce09_1920x1080.png" width="1920" height="1080" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/89728def-7bbf-47d6-b3f5-c2b386a1ce09_1920x1080.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1080,&quot;width&quot;:1920,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:244649,&quot;alt&quot;:&quot;Two ways to store a changed fact: append both, or link the new one to the old one with supersedes&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://thetshaped.dev/i/224789827?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89728def-7bbf-47d6-b3f5-c2b386a1ce09_1920x1080.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Two ways to store a changed fact: append both, or link the new one to the old one with supersedes" title="Two ways to store a changed fact: append both, or link the new one to the old one with supersedes" srcset="https://substackcdn.com/image/fetch/$s_!SYWm!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89728def-7bbf-47d6-b3f5-c2b386a1ce09_1920x1080.png 424w, https://substackcdn.com/image/fetch/$s_!SYWm!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89728def-7bbf-47d6-b3f5-c2b386a1ce09_1920x1080.png 848w, https://substackcdn.com/image/fetch/$s_!SYWm!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89728def-7bbf-47d6-b3f5-c2b386a1ce09_1920x1080.png 1272w, https://substackcdn.com/image/fetch/$s_!SYWm!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89728def-7bbf-47d6-b3f5-c2b386a1ce09_1920x1080.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button class="pencraft pc-reset pencraft icon-container restack-image buttonBase-tqxFUX" tabindex="0" type="button"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button class="pencraft pc-reset pencraft icon-container view-image buttonBase-tqxFUX" tabindex="0" type="button"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>The application makes the link when it knows the event, such as a rebooking. When the change comes out of conversation, like the seat preference, the step that extracts memories has to notice it. Store each fact with its message id and that message's place in the conversation. A fact from an earlier message can't replace the current one. When the slot already has a fact from this message, skip the write, so a job that runs twice changes nothing.</p><p><strong>Go deeper:</strong> Ralph Kimball, <a href="https://www.kimballgroup.com/2008/09/slowly-changing-dimensions-part-2/">Slowly Changing Dimensions, Part 2</a> (2008), and Martin Fowler, <a href="https://martinfowler.com/eaaDev/EventSourcing.html">Event Sourcing</a> (2005). For typed links between memory records, see Oracle's post <a href="https://fandf.co/4l8Nqi2">What's New in Oracle AI Agent Memory</a>.</p><div><hr></div><h2>5. Expand one hop, and set the defaults yourself</h2><p>Following links brings back the history behind a fact, and it can also flood the prompt. Start with one hop and a small cap on linked results. The trip agent then gets the October preference on top with the March one under it, so it books the aisle and can still say why.</p><p>Then check what your library returns for a superseded record when you set nothing. When old records still reach the prompt and the prompt builder ignores their status, the link from decision 4 fixes nothing. Write your defaults down where a reviewer can see them.</p><div><hr></div><h2>6. Budget the prompt, not the result count</h2><p>Ten results of 500 tokens make a different prompt from ten of 20 tokens.</p><p>A bigger context window doesn't fix this. <em>Lost in the Middle</em> found that models often do best when the relevant text sits at the start or end of the input, and that performance "significantly degrades" when it sits in the middle. More memories can push the one that matters into the middle.</p><p>So cap tokens instead of rows, and pick the kind of cap on purpose. A hard budget never goes over, even if that means returning nothing. A soft budget is a target. Use a hard cap when the prompt has a fixed slot, and a soft one when an empty answer is worse than a long one.</p><p><strong>Go deeper:</strong> <a href="https://arxiv.org/abs/2307.03172">Lost in the Middle: How Language Models Use Long Contexts</a> (TACL 2024). Casius Lee, <a href="https://fandf.co/3X2XQG5">What is context engineering?</a> on the Oracle Developers blog.</p><div><hr></div><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://thetshaped.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Join <strong>30,000+ engineers</strong> getting one practical issue a week on production-grade React, Node, Architecture, and <strong>real AI systems</strong>, and leveling up from Mid &#8594; Senior &#8594; Architect.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div><hr></div><h2>7. Test the update path, not just recall</h2><p>Most memory tests ask "does it remember?" The production bug is "does it notice the change?"</p><p>State a fact in session one and change it in session three. In session five, ask a question that only the new fact answers, and fail the test if the old one shows up as current. Run it whenever extraction, linking or retrieval settings change.</p><p>LongMemEval treats "knowledge updates" as one of five long-term memory abilities it tests. Across the whole benchmark, it reports a 30% accuracy drop for commercial chat assistants and long-context models over sustained interactions.</p><p><strong>Go deeper:</strong> <a href="https://arxiv.org/abs/2410.10813">LongMemEval: Benchmarking Chat Assistants on Long-Term Interactive Memory</a> (ICLR 2025).</p><div><hr></div><h2>Start with the link</h2><p>If you only change one thing this week, make it decision 4. Find the place where your agent writes a memory, and ask what happens when the same fact arrives with a new value. If the answer is "we append it", you have the window-seat bug.</p><p><em>This article is sponsored by Oracle Developers.</em></p><p>To go deeper on typed links and one-hop retrieval, Oracle's developer team has a walkthrough with a runnable Python notebook: <a href="https://fandf.co/4l8Nqi2">What's New in Oracle AI Agent Memory 26.8</a>.</p><div><hr></div><h2>&#128204; TL;DR</h2><ul><li><p><strong>Store facts, not transcripts.</strong> Keep the chat in a log.</p></li><li><p><strong>Scope before you rank.</strong> Filter by tenant and user first.</p></li><li><p><strong>Search two ways, then rerank.</strong> Measure each step on your data.</p></li><li><p><strong>Link changed facts instead of overwriting them.</strong> Swap them in one transaction, and let a unique index on current rows keep one fact current.</p></li><li><p><strong>Expand one hop, and set the defaults yourself.</strong> Check what comes back for superseded records.</p></li><li><p><strong>Budget tokens, not rows.</strong> Choose hard or soft caps on purpose.</p></li><li><p><strong>Test the update path.</strong> State a fact, change it, and check the agent uses the new one.</p></li></ul><div><hr></div><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://thetshaped.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Subscribe to get practical tips on AI Agents, Agentic Systems, and Software Architecture delivered straight to your inbox:</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div class="pullquote"><div class="captioned-image-container"><figure><div class="image-link image2" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!cmMn!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 424w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 848w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 1272w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!cmMn!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png" width="248" height="207.7" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:670,&quot;width&quot;:800,&quot;resizeWidth&quot;:248,&quot;bytes&quot;:257716,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://thetshaped.dev/i/188025481?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb8ebf3ac-ad2f-415d-9e32-111e959c8e4e_800x800.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!cmMn!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 424w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 848w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 1272w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div></div></div></div></figure></div><p><strong>Follow me on <a href="https://www.linkedin.com/in/petarivanovv9/">LinkedIn</a></strong> | <strong><a href="https://x.com/intent/follow?screen_name=petarivanovv9">Twitter(X)</a></strong> |<strong> <a href="https://www.threads.net/@petarivanovv9">Threads</a></strong></p></div><p>Thank you for supporting this newsletter.</p><p>Consider sharing this post with your friends and get rewards.</p><p>You are the best! &#128591;</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!EvJY!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!EvJY!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 424w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 848w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!EvJY!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg" width="800" height="500" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:500,&quot;width&quot;:800,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:83253,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://thetshaped.dev/i/188025481?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!EvJY!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 424w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 848w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button class="pencraft pc-reset pencraft icon-container restack-image buttonBase-tqxFUX" tabindex="0" type="button"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button class="pencraft pc-reset pencraft icon-container view-image buttonBase-tqxFUX" tabindex="0" type="button"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://thetshaped.dev/p/7-decisions-that-make-ai-agent-memory?utm_source=substack&utm_medium=email&utm_content=share&action=share&quot;,&quot;text&quot;:&quot;Share&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://thetshaped.dev/p/7-decisions-that-make-ai-agent-memory?utm_source=substack&utm_medium=email&utm_content=share&action=share"><span>Share</span></a></p><div class="pullquote"><div class="captioned-image-container"><figure><div class="image-link image2" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!UzWz!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 424w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 848w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 1272w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!UzWz!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif" width="48" height="57.6" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/39609b45-f68c-4557-8772-8e2a45182842_500x600.gif&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:600,&quot;width&quot;:500,&quot;resizeWidth&quot;:48,&quot;bytes&quot;:7028,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:&quot;&quot;,&quot;type&quot;:&quot;image/gif&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://thetshaped.dev/i/188785536?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!UzWz!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 424w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 848w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 1272w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 1456w" sizes="auto, 100vw" loading="lazy"></picture><div></div></div></div></figure></div><p style="text-align: center;"><em><strong>Press the Like button if you found this post helpful</strong></em></p></div>]]></content:encoded></item><item><title><![CDATA[The Senior JavaScript Interview Is Open Today]]></title><description><![CDATA[660+ questions and architecture scenarios, each tagged Junior, Mid or Senior. Launch prices until Monday 30 November.]]></description><link>https://thetshaped.dev/p/the-senior-javascript-interview-is</link><guid isPermaLink="false">https://thetshaped.dev/p/the-senior-javascript-interview-is</guid><dc:creator><![CDATA[Petar Ivanov]]></dc:creator><pubDate>Mon, 23 Nov 2026 08:00:37 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/e926c016-5105-4051-9ad0-5c2048d2b71f_2912x1632.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Earlier this month I wrote about the interview where I knew the terms and couldn't explain them. Today the course I built around that gap is open to everyone.</p><p>The Senior JavaScript Interview has 660+ questions and architecture scenarios on React, Node.js, TypeScript and system design. Each question is tagged Junior, Mid or Senior. You open the course on petarivanov.tech with your personal link, on any device. The reader hides each answer until you've given your own, so you practice the explaining, which is the part that breaks under pressure. You find the questions you can't answer yet before an interviewer does.</p><h2>What a Senior answer adds</h2><p>Here's one more question from the free chapters, tagged Senior. "Two requests both read-modify-write the same state and clobber each other. What's this called and how do you prevent it?"</p><p>Calling it a race condition is the easy part. The senior answer earns its tag by checking each layer where it can happen.</p><ul><li><p><strong>In the UI.</strong> Abort stale requests with <code>AbortController</code>, so only the newest result applies. That fixes only the screen, because the server can still apply the old write.</p></li><li><p><strong>In one process.</strong> A queue or a mutex per key around the read and the write makes them run one at a time. It doesn't stop a second instance. Across instances, lock the row with <code>SELECT ... FOR UPDATE</code>, with the read and the write in one transaction on one connection.</p></li><li><p><strong>In the database.</strong> Use an atomic update, or a version column checked in the <code>WHERE</code> clause. A stale write then changes 0 rows with no error, so check the row count, then re-read, recompute and retry.</p></li></ul><h2>What's inside</h2><ul><li><p>Questions on closures and the event loop, React rendering and Server Components, Node.js API design, databases, security and testing.</p></li><li><p>70 architecture scenarios with answers. Each one ends with what the interviewer is grading.</p></li><li><p>A chapter on AI-assisted engineering, including how to answer "How do you use AI?" in a senior interview.</p></li><li><p>A 40-question final exam to find the gaps that are left.</p></li></ul><p>Can't you ask ChatGPT for free? I tested that before launch. I gave 20 questions from the course to a free AI model and compared its answers with the course's. On concept questions it was often as good. The four answers where the course came out clearly ahead were all architecture scenarios. In those, the AI hedged on the calls an interviewer grades, like a payment call that times out and leaves you unsure whether the card was charged. That's why the course has 49 of them.</p><h2>Two tiers, at launch prices until Monday 30 November at 23:59 PT</h2><ul><li><p><strong>Standard, $79</strong> (then $119). Chapters 1 to 36 and the final exam, in the web reader, plus an EPUB.</p></li><li><p><strong>Premium, $149</strong> (then $229). Everything in Standard, plus Part 6 on the interview itself, and the Complete Packages of The Conscious React and The Conscious Node, $118 if bought separately.</p></li></ul><p>Part 6 covers behavioral questions, a framework for the system design round, and guides for LinkedIn, GitHub and side projects. It isn't for you if you've never written React or Node, or if you want LeetCode-style algorithm drills. It covers the other rounds.</p><blockquote><p>"What I like most about The Senior JavaScript Interview is the focus on explaining your reasoning. The answer-first format encourages you to pause and think before checking the explanation, and the architecture scenarios bring practical trade-offs into the conversation."</p><p>Venelin K., founder of Directify</p></blockquote><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://petarivanov.tech/senior-javascript-interview/?utm_source=substack&amp;utm_medium=email&amp;utm_campaign=playbook-launch&amp;utm_content=sub-l&quot;,&quot;text&quot;:&quot;See what's inside and pick a tier&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://petarivanov.tech/senior-javascript-interview/?utm_source=substack&amp;utm_medium=email&amp;utm_campaign=playbook-launch&amp;utm_content=sub-l"><span>See what's inside and pick a tier</span></a></p><p>If you'd like to check the format first, the 3 free chapters are on the same page.</p><p>There's a 30-day money-back guarantee. Reply to your receipt for a full refund. Updates are free for the life of the product.</p><p>Paying from a learning budget? Reply to your receipt and I'll send an invoice with your company's details. For a team purchase, write to me at info@petarivanov.tech.</p><p>If you already bought it during early access, thank you, and you can skip this one.</p><p>Thank you for supporting this newsletter.</p><p>Petar</p>]]></content:encoded></item><item><title><![CDATA[6 Things a Senior Engineer Covers When a Payment Is Retried]]></title><description><![CDATA[Where the key comes from, one payment per order, the decline, the crash, the receipt, and what the interviewer grades.]]></description><link>https://thetshaped.dev/p/how-to-design-a-payments-endpoint-idempotency</link><guid isPermaLink="false">https://thetshaped.dev/p/how-to-design-a-payments-endpoint-idempotency</guid><pubDate>Tue, 17 Nov 2026 11:03:34 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/974c15be-9b30-4db3-a86b-329d20897888_2912x1632.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://thetshaped.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption"><span data-color="#9900ff" style="color: rgb(153, 0, 255);">Get my Full-Stack JavaScript Architect Bundle for FREE on newsletter signup:</span></p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div><hr></div><p><strong>The Senior JavaScript Interview launches on Monday, 23 November.</strong> 660+ questions on React, Node.js, TypeScript and system design. <a href="https://petarivanov.tech/senior-javascript-interview/?utm_source=substack&amp;utm_medium=email&amp;utm_campaign=playbook-launch&amp;utm_content=t">Read 3 chapters free</a></p><div><hr></div><p>A checkout sends <code>POST /payments</code>. Your API charges the card, and the response never reaches the phone. The client retries, and the customer pays twice.</p><p>Almost every candidate answers "idempotency key" in the first ten seconds. It's the right start. Then the interviewer asks where the key comes from, and most answers stall.</p><p>Here's how I've built it, and how most payment teams do it. It comes down to three lessons:</p><ol><li><p><strong>A key minted per click isn't the purchase.</strong> Dedupe on an order the server created.</p></li><li><p><strong>The database decides who pays, not your code.</strong> One unique index stops the second tab.</p></li><li><p><strong>A retry has to be a replay.</strong> Same provider key, same stored amount and card, and the same guard on every email and event.</p></li></ol><p>The basics are in my June post, <a href="https://thetshaped.dev/p/how-to-stop-charging-customers-twice-practical-idempotency-guide-http-message-queues">How to Stop Charging Customers Twice</a>. This issue is the senior answer.</p><div><hr></div><h2><strong><a href="https://www.cloudways.com/en/velocity.php?id=948626&amp;data1=petar&amp;data2=sponsorship">Node.js hosting at one flat price (Partner)</a></strong></h2><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://www.cloudways.com/en/velocity.php?id=948626&amp;data1=petar&amp;data2=sponsorship" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!HT-D!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4eafbf7e-d5ed-4771-a904-9ab254ef81d3_1280x600.png 424w, https://substackcdn.com/image/fetch/$s_!HT-D!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4eafbf7e-d5ed-4771-a904-9ab254ef81d3_1280x600.png 848w, https://substackcdn.com/image/fetch/$s_!HT-D!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4eafbf7e-d5ed-4771-a904-9ab254ef81d3_1280x600.png 1272w, https://substackcdn.com/image/fetch/$s_!HT-D!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4eafbf7e-d5ed-4771-a904-9ab254ef81d3_1280x600.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!HT-D!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4eafbf7e-d5ed-4771-a904-9ab254ef81d3_1280x600.png" width="1280" height="600" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/4eafbf7e-d5ed-4771-a904-9ab254ef81d3_1280x600.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:600,&quot;width&quot;:1280,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:792055,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:&quot;https://www.cloudways.com/en/velocity.php?id=948626&amp;data1=petar&amp;data2=sponsorship&quot;,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://thetshaped.dev/i/224019833?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4eafbf7e-d5ed-4771-a904-9ab254ef81d3_1280x600.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!HT-D!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4eafbf7e-d5ed-4771-a904-9ab254ef81d3_1280x600.png 424w, https://substackcdn.com/image/fetch/$s_!HT-D!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4eafbf7e-d5ed-4771-a904-9ab254ef81d3_1280x600.png 848w, https://substackcdn.com/image/fetch/$s_!HT-D!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4eafbf7e-d5ed-4771-a904-9ab254ef81d3_1280x600.png 1272w, https://substackcdn.com/image/fetch/$s_!HT-D!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4eafbf7e-d5ed-4771-a904-9ab254ef81d3_1280x600.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button class="pencraft pc-reset pencraft icon-container restack-image buttonBase-tqxFUX" tabindex="0" type="button"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button class="pencraft pc-reset pencraft icon-container view-image buttonBase-tqxFUX" tabindex="0" type="button"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Want your Node.js app to skip cold starts? Cloudways Velocity runs it on a persistent, always-on server that starts at a flat $20 a month. Connect a GitHub, GitLab or Bitbucket repo, and your Next.js, Astro or Express app is live in minutes, with security patches, backups and Cloudflare Enterprise CDN included. This Black Friday, new accounts get 80% off Velocity for the first 2 months with code BFCM802, until December 4.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.cloudways.com/en/velocity.php?id=948626&amp;data1=petar&amp;data2=sponsorship&quot;,&quot;text&quot;:&quot;Get My 80% Off&quot;,&quot;action&quot;:null,&quot;class&quot;:&quot;button-wrapper&quot;}" data-component-name="ButtonCreateButton"><a class="button primary button-wrapper" href="https://www.cloudways.com/en/velocity.php?id=948626&amp;data1=petar&amp;data2=sponsorship"><span>Get My 80% Off</span></a></p><p>(Thanks to <a href="https://www.cloudways.com/en/velocity.php?id=948626&amp;data1=petar&amp;data2=sponsorship">Cloudways</a> for sponsoring this issue.)</p><div><hr></div><h2>1. Key the payment on the order, not on the click</h2><p>Stripe, Adyen, Square and the IETF draft all recommend a random key per request. That protects one request from network retries. It can't tell that two requests are the same purchase. A reload, a second tab or an app restart mints a new key, and the server sees two unrelated payments. That's how one purchase gets charged twice.</p><p>The order id is the one value that stays the same. When checkout starts, before the Pay button renders, the server gets or creates the one open order for this cart. A unique index on <code>cart_id</code> for open orders makes that a single insert, so a reload or a second tab gets the same order back. Every click calls <code>POST /orders/{id}/payments</code>. Once a payment on the order is live, the order is frozen, and a changed cart gets a new order. Stripe's own guidance points the same way. It pairs one PaymentIntent with one cart and bases the idempotency key on the cart's ID. The amount comes from the order too, so a retry can't change what gets charged. The provider key comes from the payment attempt on that order, so a new card after a decline gets its own key.</p><div><hr></div><h2>2. Let the database pick one payment per order</h2><p>Every payment attempt is a row, with the amount and currency copied from the order and the card from the request that claimed it. One partial unique index allows a single live attempt per order, and the claim is one insert against it:</p><pre><code>CREATE UNIQUE INDEX one_live_payment_per_order
  ON payments (order_id) WHERE status &lt;&gt; 'declined';

INSERT INTO payments (order_id, amount, currency, payment_method)
VALUES ($1, $2, $3, $4)
ON CONFLICT (order_id) WHERE status &lt;&gt; 'declined' DO NOTHING
RETURNING id, amount, currency, payment_method;</code></pre><p>The table defaults <code>status</code> to <code>'processing'</code> and <code>locked_until</code> to <code>now() + interval '30 seconds'</code>, so every claimed row starts with a 30-second lease.</p><p>One request gets a row back and charges. Every other request gets zero rows. When two inserts overlap, Postgres makes the second wait until the first one commits. The claim is its own statement, so that wait takes milliseconds. You write no lock, and you don't need Redis. A declined attempt drops out of the index, so the customer can try another card on the same order.</p><p>The tempting version reads the table first and inserts if it finds nothing. Two tabs both read "no payment", and both charge. With the index, 20 parallel clicks on one order still give one charge.</p><p>Don't wrap the claim, the charge and the save in one transaction. A slow provider would hold that row lock for seconds, and every other click on the order waits behind it. A crash would roll back the claim but not the charge, so the next click inserts a new row, sends a new key and charges the card again. Commit the <code>processing</code> row, call the provider, then save the result in a second, short statement.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!rVoe!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F04bd1b0f-821c-4e9e-ad38-d19fec29882d_960x867.gif" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!rVoe!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F04bd1b0f-821c-4e9e-ad38-d19fec29882d_960x867.gif 424w, https://substackcdn.com/image/fetch/$s_!rVoe!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F04bd1b0f-821c-4e9e-ad38-d19fec29882d_960x867.gif 848w, https://substackcdn.com/image/fetch/$s_!rVoe!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F04bd1b0f-821c-4e9e-ad38-d19fec29882d_960x867.gif 1272w, https://substackcdn.com/image/fetch/$s_!rVoe!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F04bd1b0f-821c-4e9e-ad38-d19fec29882d_960x867.gif 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!rVoe!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F04bd1b0f-821c-4e9e-ad38-d19fec29882d_960x867.gif" width="1456" height="1315" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/04bd1b0f-821c-4e9e-ad38-d19fec29882d_960x867.gif&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1315,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:231023,&quot;alt&quot;:&quot;Animated: Tab A and Tab B pay for the same order. Tab A's claim gets 1 row and charges the provider. Tab B's claim gets 0 rows, reads the live payment, and gets a 409 while it's processing; when Tab B retries later, it gets 201 with the same charge. A declined card leaves the index so the next card can pay&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/gif&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://thetshaped.dev/i/224019833?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F04bd1b0f-821c-4e9e-ad38-d19fec29882d_960x867.gif&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Animated: Tab A and Tab B pay for the same order. Tab A's claim gets 1 row and charges the provider. Tab B's claim gets 0 rows, reads the live payment, and gets a 409 while it's processing; when Tab B retries later, it gets 201 with the same charge. A declined card leaves the index so the next card can pay" title="Animated: Tab A and Tab B pay for the same order. Tab A's claim gets 1 row and charges the provider. Tab B's claim gets 0 rows, reads the live payment, and gets a 409 while it's processing; when Tab B retries later, it gets 201 with the same charge. A declined card leaves the index so the next card can pay" srcset="https://substackcdn.com/image/fetch/$s_!rVoe!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F04bd1b0f-821c-4e9e-ad38-d19fec29882d_960x867.gif 424w, https://substackcdn.com/image/fetch/$s_!rVoe!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F04bd1b0f-821c-4e9e-ad38-d19fec29882d_960x867.gif 848w, https://substackcdn.com/image/fetch/$s_!rVoe!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F04bd1b0f-821c-4e9e-ad38-d19fec29882d_960x867.gif 1272w, https://substackcdn.com/image/fetch/$s_!rVoe!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F04bd1b0f-821c-4e9e-ad38-d19fec29882d_960x867.gif 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button class="pencraft pc-reset pencraft icon-container restack-image buttonBase-tqxFUX" tabindex="0" type="button"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button class="pencraft pc-reset pencraft icon-container view-image buttonBase-tqxFUX" tabindex="0" type="button"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://thetshaped.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Join <strong>30,000+ engineers</strong> getting one practical issue a week on production-grade React, Node, Architecture, and <strong>real AI systems</strong>, and leveling up from Mid &#8594; Senior &#8594; Architect.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div><hr></div><h2>3. Make every retry a replay</h2><p>A request that loses the claim never charges. It replays a payment that succeeded, or it gets a 409 and tries again shortly. A decline is different. The declined row leaves the index, so the next request on the order is a new attempt with a new key, even when it's a retry of the request that got the 402. The request that wins sends the provider the amount and card stored on its row, under a key built from that row's id. Here <code>order</code> is the row the server created, already checked against the signed-in customer, and <code>CLAIM_SQL</code> is the insert above:</p><pre><code>export async function payOrder(
  db: Pool,
  paymentProvider: PaymentProvider,
  order: Order,
  paymentMethod: string,
): Promise&lt;Reply&gt; {
  let { rows: [attempt] } = await db.query(
    CLAIM_SQL, [order.id, order.amount, order.currency, paymentMethod],
  );
  if (!attempt) {
    const { rows: [live] } = await db.query(LIVE_SQL, [order.id]);
    if (live?.status === "succeeded") {
      return { status: 201, body: live.charge };
    }
    ({ rows: [attempt] } = await db.query(TAKEOVER_SQL, [order.id]));
    if (!attempt) {
      return { status: 409, body: { error: "IN_PROGRESS" } };
    }
  }

  try {
    const charge = await paymentProvider.charge({
      amount: attempt.amount,
      currency: attempt.currency,
      paymentMethod: attempt.payment_method,
      metadata: { paymentId: String(attempt.id) },
    }, { idempotencyKey: `payment/${attempt.id}` });
    await db.query(SUCCEED_SQL, [attempt.id, charge]);
    return { status: 201, body: charge };
  } catch (err) {
    if (err instanceof ProviderConflictError) {
      return { status: 409, body: { error: "IN_PROGRESS" } };
    }
    if (err instanceof CardDeclinedError) {
      await db.query(DECLINE_SQL, [attempt.id]);
      return { status: 402, body: { error: "CARD_DECLINED" } };
    }
    if (err instanceof ProviderRejectedError) {
      // any other 4xx: no money moved, so free the order
      await db.query(DECLINE_SQL, [attempt.id]);
      return { status: 422, body: { error: "PAYMENT_REJECTED" } };
    }
    if (err instanceof ProviderServerError) {
      // Stripe saved this 500 and replays it to every retry
      await db.query(RECONCILE_SQL, [attempt.id]);
    }
    // a timeout or a dropped connection: leave the row processing
    throw err;
  }
}</code></pre><p>The stored row is what makes a crash safe. Say the provider charges the card and the server dies before it saves the result. The row stays <code>processing</code>, and its <code>locked_until</code> lease runs out after 30 seconds. The next click takes the attempt over with <code>TAKEOVER_SQL</code>, an <code>UPDATE</code> that only matches a <code>processing</code> row whose lease expired, and calls the provider again. Same key, same amount, same card, so the provider returns the first charge instead of making a second one. Send a different card under that key and Stripe refuses the call. That's why the takeover re-sends the stored card, not the one in the new request.</p><p>The next click is only the fast path. A sweeper job runs every minute and takes over expired leases the same way, so a charged order doesn't wait on a customer who closed the tab.</p><p>Only a timeout or a dropped connection leaves the row at <code>processing</code>, because nobody knows yet whether the money moved. Treating it as a failure would free the order for a second charge. A decline or any other 4xx means no money moved, so the attempt leaves the index and the customer can pay again. A 500 is different. Stripe saved it under that key and replays it to every retry, so the row is marked <code>needs_reconciliation</code>, which the takeover and the sweeper skip.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!sziw!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F296ac017-01af-4f26-9237-c4ef00043e12_960x1002.gif" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!sziw!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F296ac017-01af-4f26-9237-c4ef00043e12_960x1002.gif 424w, https://substackcdn.com/image/fetch/$s_!sziw!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F296ac017-01af-4f26-9237-c4ef00043e12_960x1002.gif 848w, https://substackcdn.com/image/fetch/$s_!sziw!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F296ac017-01af-4f26-9237-c4ef00043e12_960x1002.gif 1272w, https://substackcdn.com/image/fetch/$s_!sziw!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F296ac017-01af-4f26-9237-c4ef00043e12_960x1002.gif 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!sziw!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F296ac017-01af-4f26-9237-c4ef00043e12_960x1002.gif" width="1456" height="1520" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/296ac017-01af-4f26-9237-c4ef00043e12_960x1002.gif&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1520,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:208087,&quot;alt&quot;:&quot;Animated: request 1 claims row 41 and the provider charges ch_1, then the process dies before saving the result. The next click asks whether the lease expired. At 5 seconds it hasn't, so the click gets a 409. At 40 seconds it has, so the click takes over row 41, sends the same key and stored card, the provider returns ch_1 with no new charge, and the payment is marked paid with 1 receipt. Taking over with a new key would charge the card twice&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/gif&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://thetshaped.dev/i/224019833?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F296ac017-01af-4f26-9237-c4ef00043e12_960x1002.gif&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Animated: request 1 claims row 41 and the provider charges ch_1, then the process dies before saving the result. The next click asks whether the lease expired. At 5 seconds it hasn't, so the click gets a 409. At 40 seconds it has, so the click takes over row 41, sends the same key and stored card, the provider returns ch_1 with no new charge, and the payment is marked paid with 1 receipt. Taking over with a new key would charge the card twice" title="Animated: request 1 claims row 41 and the provider charges ch_1, then the process dies before saving the result. The next click asks whether the lease expired. At 5 seconds it hasn't, so the click gets a 409. At 40 seconds it has, so the click takes over row 41, sends the same key and stored card, the provider returns ch_1 with no new charge, and the payment is marked paid with 1 receipt. Taking over with a new key would charge the card twice" srcset="https://substackcdn.com/image/fetch/$s_!sziw!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F296ac017-01af-4f26-9237-c4ef00043e12_960x1002.gif 424w, https://substackcdn.com/image/fetch/$s_!sziw!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F296ac017-01af-4f26-9237-c4ef00043e12_960x1002.gif 848w, https://substackcdn.com/image/fetch/$s_!sziw!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F296ac017-01af-4f26-9237-c4ef00043e12_960x1002.gif 1272w, https://substackcdn.com/image/fetch/$s_!sziw!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F296ac017-01af-4f26-9237-c4ef00043e12_960x1002.gif 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button class="pencraft pc-reset pencraft icon-container restack-image buttonBase-tqxFUX" tabindex="0" type="button"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button class="pencraft pc-reset pencraft icon-container view-image buttonBase-tqxFUX" tabindex="0" type="button"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><h2>Provider keys expire, so keep the guard in your database</h2><p>Stripe keeps idempotency keys for at least 24 hours, so a takeover only touches attempts younger than 23 hours. Older attempts go to a reconciliation job, and so do rows marked <code>needs_reconciliation</code>. The job looks the charge up by the <code>paymentId</code> in its metadata. A charge it can't find goes to a person, never back to the provider.</p><p>My own book store taught me the same thing about emails. Resend's send call returns an error instead of throwing, so my Stripe webhook answered 200 and a buyer could pay and get nothing. Returning 500 made Stripe retry, but a retry replays the whole event, every email included. One key per email, built from the checkout session id, means a retry sends only the missing ones. Resend keeps those keys for 24 hours too, and Stripe retries a webhook for up to three days, so after the first day the key alone no longer stops a resend.</p><p>So the guard that lasts is a row in your own database. <code>SUCCEED_SQL</code> marks the payment succeeded and queues the receipt and the <code>payment.succeeded</code> event in an outbox, in one statement. The outbox's primary key is <code>(order_id, kind)</code>. If a slow request and its takeover both finish, the second one queues nothing, so there's one receipt to send. The worker sends it with an email key like <code>receipt/&lt;order id&gt;</code>, because the outbox relay can deliver twice. I wrote up that pattern in <a href="https://thetshaped.dev/p/outbox-pattern-why-you-need-typescript-postgresql-nodejs">Saved the Order, Lost the Event</a>.</p><div><hr></div><h2>What the interviewer is listening for</h2><p>"Idempotency key" names the tool. The senior answer says what counts as a duplicate, which is a second payment on the same order. It says who picks the winner, which is a unique index. And it names the receipt and the event as the other things that have to take effect once. Say all of it before the interviewer has to ask.</p><div><hr></div><h2>&#128204; TL;DR</h2><ul><li><p>A random key per click stops network retries, not a reload or a second tab. Pay against the one open order the server keeps for each cart.</p></li><li><p>A partial unique index allows one live payment per order. Claim with <code>INSERT ... ON CONFLICT DO NOTHING</code>, never check-then-insert.</p></li><li><p>A retry of a live payment is a replay. It sends the same provider key with the stored amount and card. After a crash, the next click or a sweeper job takes over once the lease runs out.</p></li><li><p>Commit the claim before you call the provider. A rollback can't undo a charge.</p></li><li><p>Provider keys can expire after a day. Queue the receipt and the event in the statement that marks the payment paid.</p></li></ul><div><hr></div><p><strong>Whenever you&#8217;re ready, here&#8217;s how I can help you:</strong></p><p><strong>The Senior JavaScript Interview</strong> (opens 23 November, 3 chapters free now). Practice explaining closures, React rendering and system design trade-offs out loud, the way a senior interview asks. It has 660+ questions and 49 architecture scenarios, each tagged Junior, Mid or Senior. <a href="https://petarivanov.tech/senior-javascript-interview/?utm_source=substack&amp;utm_medium=email&amp;utm_campaign=playbook-launch&amp;utm_content=footer#get-sample">Read the 3 free chapters &#8594;</a></p><p><a href="https://petarivanov.tech/books/conscious-react/?utm_source=substack&amp;utm_medium=email&amp;utm_campaign=issue-footer&amp;utm_content=react">The Conscious React</a> and <a href="https://petarivanov.tech/books/conscious-node/?utm_source=substack&amp;utm_medium=email&amp;utm_campaign=issue-footer&amp;utm_content=node">The Conscious Node</a>. The Conscious React shows how to structure a React app as it grows. The Conscious Node has 150+ production patterns for Node.js backends and three companion projects. Both also come as one bundle. <a href="https://petarivanov.tech/bundles/?utm_source=substack&amp;utm_medium=email&amp;utm_campaign=issue-footer&amp;utm_content=books">See the books &#8594;</a></p><p>The <a href="https://petarivanov.tech/resources/?utm_source=substack&amp;utm_medium=email&amp;utm_campaign=issue-footer&amp;utm_content=playbooks">React and Node.js Architecture Playbooks</a> are free.</p><div><hr></div><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://thetshaped.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Subscribe to get practical tips on AI Agents, Agentic Systems, and Software Architecture delivered straight to your inbox:</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div class="pullquote"><div class="captioned-image-container"><figure><div class="image-link image2" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!cmMn!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 424w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 848w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 1272w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!cmMn!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png" width="248" height="207.7" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:670,&quot;width&quot;:800,&quot;resizeWidth&quot;:248,&quot;bytes&quot;:257716,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://thetshaped.dev/i/188025481?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb8ebf3ac-ad2f-415d-9e32-111e959c8e4e_800x800.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!cmMn!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 424w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 848w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 1272w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div></div></div></div></figure></div><p><strong>Follow me on <a href="https://www.linkedin.com/in/petarivanovv9/">LinkedIn</a></strong> | <strong><a href="https://x.com/intent/follow?screen_name=petarivanovv9">Twitter(X)</a></strong> |<strong> <a href="https://www.threads.net/@petarivanovv9">Threads</a></strong></p></div><p>Thank you for supporting this newsletter.</p><p>Consider sharing this post with your friends and get rewards.</p><p>You are the best! &#128591;</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!EvJY!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!EvJY!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 424w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 848w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!EvJY!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg" width="800" height="500" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:500,&quot;width&quot;:800,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:83253,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://thetshaped.dev/i/188025481?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!EvJY!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 424w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 848w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button class="pencraft pc-reset pencraft icon-container restack-image buttonBase-tqxFUX" tabindex="0" type="button"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button class="pencraft pc-reset pencraft icon-container view-image buttonBase-tqxFUX" tabindex="0" type="button"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://thetshaped.dev/p/how-to-design-a-payments-endpoint-idempotency?utm_source=substack&utm_medium=email&utm_content=share&action=share&quot;,&quot;text&quot;:&quot;Share&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://thetshaped.dev/p/how-to-design-a-payments-endpoint-idempotency?utm_source=substack&utm_medium=email&utm_content=share&action=share"><span>Share</span></a></p><div class="pullquote"><div class="captioned-image-container"><figure><div class="image-link image2" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!UzWz!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 424w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 848w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 1272w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!UzWz!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif" width="48" height="57.6" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/39609b45-f68c-4557-8772-8e2a45182842_500x600.gif&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:600,&quot;width&quot;:500,&quot;resizeWidth&quot;:48,&quot;bytes&quot;:7028,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:&quot;&quot;,&quot;type&quot;:&quot;image/gif&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://thetshaped.dev/i/188785536?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!UzWz!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 424w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 848w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 1272w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 1456w" sizes="auto, 100vw" loading="lazy"></picture><div></div></div></div></figure></div><p style="text-align: center;"><em><strong>Press the Like button if you found this post helpful</strong></em></p></div>]]></content:encoded></item><item><title><![CDATA[I Knew the Terms. I Couldn't Explain Them.]]></title><description><![CDATA[The interview I almost failed, the course I built around it, and 3 free chapters you can try today.]]></description><link>https://thetshaped.dev/p/i-knew-the-terms-i-couldnt-explain</link><guid isPermaLink="false">https://thetshaped.dev/p/i-knew-the-terms-i-couldnt-explain</guid><dc:creator><![CDATA[Petar Ivanov]]></dc:creator><pubDate>Thu, 12 Nov 2026 11:01:59 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!9lD-!,w_256,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7f66bb7c-c96f-4c71-ba2d-d561152c83a2_600x600.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>You know what hoisting and closures are. Explaining them out loud, while an interviewer waits, is a different skill.</p><p>Last year I wrote about an interview I almost failed because of that gap. Here's how I put it then:</p><blockquote><p><em>"The interviewer hit me with tough questions on Hoisting and Closures in JavaScript. I knew the terms, but couldn't explain them clearly."</em></p></blockquote><p>Most interview prep trains you to recognize answers. You read a question, read the answer, nod, and move on to the next one. Nodding along feels like knowing the answer.</p><p>For the past few months I've been building a course that trains you to explain them. It's called The Senior JavaScript Interview, and it opens on Monday 23 November. You answer each question out loud before you see the model answer, so you find the ones you can't explain yet while there's still time to fix them. By the interview, you've said the senior answers out loud at least once.</p><h2>Try one question first</h2><p>This one comes from the async chapter, and it's tagged Senior. Answer it out loud before you scroll.</p><p><strong>Why doesn't </strong><code>array.forEach(async (x) =&gt; await work(x))</code><strong> wait for the work to finish?</strong></p><p>Once you have your answer, here's the core of the model answer.</p><p><code>forEach</code> ignores what its callback returns. Each async callback returns a promise, <code>forEach</code> throws that promise away, and the loop finishes while the work is still pending. A rejection goes unhandled too.</p><pre><code>// waits for every save, and a rejection reaches the caller
await Promise.all(items.map((item) =&gt; save(item)));

// or one at a time, if order matters
for (const item of items) await save(item);</code></pre><p>Most answers stop there. The model answer keeps going, because <code>map</code>, <code>filter</code> and <code>reduce</code> don't await either. An async <code>filter</code> predicate is a silent bug. A promise is always truthy, so every item passes the filter.</p><p>That second half is what the Senior tag is about. The first half shows you know the bug, and the second half shows where else it hides in your codebase.</p><h2>How the course works</h2><p>The Senior JavaScript Interview has 660+ questions and architecture scenarios on React, Node.js, TypeScript and system design. Every question is tagged Junior, Mid or Senior, so you know which level it tests.</p><p>The web reader hides each model answer until you've given your own, the same way you just answered the question above. There are 70 architecture scenarios with answers, and every scenario ends with what the interviewer is grading.</p><h2>3 chapters are free today</h2><p>The free sample has 3 full chapters, exactly as they appear in the course: Async JavaScript, the React rendering model and Node.js API design. That's 76 questions and 3 architecture scenarios, with a five-question self-test at the end of each chapter. The question above is one of them.</p><p>Take the sample before Friday 20 November, and you get checkout that day, three days before everyone else, at the launch price.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://petarivanov.tech/senior-javascript-interview/?utm_source=substack&amp;utm_medium=email&amp;utm_campaign=playbook-launch&amp;utm_content=sub-s#get-sample&quot;,&quot;text&quot;:&quot;Get the 3 free chapters&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://petarivanov.tech/senior-javascript-interview/?utm_source=substack&amp;utm_medium=email&amp;utm_campaign=playbook-launch&amp;utm_content=sub-s#get-sample"><span>Get the 3 free chapters</span></a></p><p>You enter your email on my site, confirm it, and the chapters arrive right after, as a web reader and an EPUB. That also puts you on my email list at petarivanov.tech. One click unsubscribes you from that list, and you keep the chapters.</p><p>Launch prices run until Monday 30 November at 23:59 PT: $79 for Standard, which covers the technical rounds, and $149 for Premium, which adds the interview itself. After that, they're $119 and $229. Prices are plus VAT where it applies.</p><p>Thank you for supporting this newsletter.</p><p>Petar</p>]]></content:encoded></item><item><title><![CDATA[5 LLM Output Failures That Still Get Past Structured Outputs]]></title><description><![CDATA[Legal but wrong values, IDs that point at nothing, rules your SDK never sends, refusals and drift. Where each check lives now, in TypeScript.]]></description><link>https://thetshaped.dev/p/llm-structured-outputs-always-parse</link><guid isPermaLink="false">https://thetshaped.dev/p/llm-structured-outputs-always-parse</guid><pubDate>Tue, 10 Nov 2026 11:28:54 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/045163ab-946f-4fc8-ab7c-0962877decbb_2912x1632.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://thetshaped.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption"><span data-color="#9900ff" style="color: rgb(153, 0, 255);">Get my Full-Stack JavaScript Architect Bundle for FREE on newsletter signup:</span></p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div><hr></div><p>A year ago, the LLM-output bug was almost-JSON. The model wrapped its answer in markdown fences, <code>JSON.parse</code> threw, and you added a retry loop. Native structured outputs ended that.</p><p>The new failure is quieter. Teams read "guaranteed schema-compliant responses" in the docs and delete their checks along with the retry loop. The output always parses now. Nobody promised it was true. <strong>LLM output is still user input.</strong></p><div><hr></div><h2><strong><a href="https://fandf.co/4kHnE4g">Can I just give an agent access to my database? (Partner)</a></strong></h2><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://fandf.co/4kHnE4g" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!zXMJ!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd749c09c-0639-4381-8d34-4de969b01a89_1200x600.png 424w, https://substackcdn.com/image/fetch/$s_!zXMJ!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd749c09c-0639-4381-8d34-4de969b01a89_1200x600.png 848w, https://substackcdn.com/image/fetch/$s_!zXMJ!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd749c09c-0639-4381-8d34-4de969b01a89_1200x600.png 1272w, https://substackcdn.com/image/fetch/$s_!zXMJ!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd749c09c-0639-4381-8d34-4de969b01a89_1200x600.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!zXMJ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd749c09c-0639-4381-8d34-4de969b01a89_1200x600.png" width="1456" height="728" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/d749c09c-0639-4381-8d34-4de969b01a89_1200x600.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:728,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:117188,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:&quot;https://fandf.co/4kHnE4g&quot;,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://thetshaped.dev/i/223994935?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd749c09c-0639-4381-8d34-4de969b01a89_1200x600.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!zXMJ!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd749c09c-0639-4381-8d34-4de969b01a89_1200x600.png 424w, https://substackcdn.com/image/fetch/$s_!zXMJ!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd749c09c-0639-4381-8d34-4de969b01a89_1200x600.png 848w, https://substackcdn.com/image/fetch/$s_!zXMJ!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd749c09c-0639-4381-8d34-4de969b01a89_1200x600.png 1272w, https://substackcdn.com/image/fetch/$s_!zXMJ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd749c09c-0639-4381-8d34-4de969b01a89_1200x600.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button class="pencraft pc-reset pencraft icon-container restack-image buttonBase-tqxFUX" tabindex="0" type="button"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button class="pencraft pc-reset pencraft icon-container view-image buttonBase-tqxFUX" tabindex="0" type="button"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Every AI project runs into this question. Before you wire an agent to business data over MCP, decide what it can see, how much each result costs in context, and when to give it reviewed queries instead of letting it write its own.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://fandf.co/4kHnE4g&quot;,&quot;text&quot;:&quot;Read the breakdown and included repo&quot;,&quot;action&quot;:null,&quot;class&quot;:&quot;button-wrapper&quot;}" data-component-name="ButtonCreateButton"><a class="button primary button-wrapper" href="https://fandf.co/4kHnE4g"><span>Read the breakdown and included repo</span></a></p><p>(Thanks to <a href="https://fandf.co/4kHnE4g">Oracle</a> for sponsoring this issue.)</p><div><hr></div><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://thetshaped.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Join <strong>30,000+ engineers</strong> getting one practical issue a week on production-grade React, Node, Architecture, and <strong>real AI systems</strong>, and leveling up from Mid &#8594; Senior &#8594; Architect.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div><hr></div><h2>What structured outputs guarantee</h2><p>Pass a JSON schema in <code>output_config.format</code>, or set <code>strict: true</code> on a tool, and Anthropic's API constrains decoding to that schema. Missing fields, wrong types and fenced JSON are gone. That guarantee covers the schema the API receives. It says nothing about whether the values are true, and that schema may not even be the one you wrote.</p><h2>Five failures that still get through</h2><h3>1. Legal values that are wrong</h3><p>An <code>employeeId</code> that belongs to nobody. A refund three times the original charge. A <code>"travel"</code> category on a software invoice. Every check passes, because a schema describes shapes and says nothing about your data.</p><p>OpenAI's structured-outputs guide warns that the model <a href="https://developers.openai.com/api/docs/guides/structured-outputs">"will always try to adhere to the provided schema, which can result in hallucinations if the input is completely unrelated to the schema"</a>. Send a photo of a lunch menu to an expense schema and you still get a well-formed expense.</p><h3>2. IDs that point at nothing</h3><p>Models invent plausible identifiers. Spracklen and colleagues checked 576,000 code samples from 16 models. On average, at least 5.2% of the packages from commercial models and 21.7% from open-source models didn't exist (<a href="https://arxiv.org/abs/2406.10279">USENIX Security 2025</a>). A package name passes any schema, and so does your <code>employeeId</code>. Look up every ID before you act on it.</p><h3>3. Rules your SDK never sends</h3><p>Anthropic's schema subset has no numeric bounds or string lengths. The SDKs move those rules into the field's description and check the response after it lands. The TypeScript SDK moves more. In <code>@anthropic-ai/sdk</code> 1.6.0, the current release, <code>zodOutputFormat</code> keeps types, required fields, ten string formats and <code>minItems</code> of 0 or 1. Everything else becomes description text, even <code>z.enum()</code>:</p><pre><code>"amountCents": { "type": "integer", "description": "{exclusiveMinimum: 0, maximum: 9007199254740991}" },
"category": { "type": "string", "description": "{enum: [\"travel\",\"meals\",\"software\",\"other\"]}" }</code></pre><p>The decoder never enforces a description. Only your Zod parse catches <code>amountCents: 0</code> or a category of <code>"approved &#9989;"</code>. The docs list <code>enum</code> as supported, so a later release may fix this. Print your own wire schema before you trust a rule.</p><h3>4. Refusals and cut-offs</h3><p>A refusal comes back as <code>stop_reason: "refusal"</code>, with HTTP 200 and billed tokens. A response that hits your token cap comes back as <code>stop_reason: "max_tokens"</code>, with JSON that just stops. Neither has to match your schema.</p><p><code>messages.parse()</code> never reads <code>stop_reason</code>. A refusal with text, or a cut-off, throws the same <code>AnthropicError</code> as a bad value. Your retry logic can't tell "the model said no" from "the model got a field wrong".</p><h3>5. Drift inside the schema</h3><p>A prompt tweak or a model upgrade shifts the values without tripping a validator. <code>category: "other"</code> creeps from 5% of extractions to 30%, and no alert fires because every output is valid. Only a chart catches drift like this.</p><h2>Validate three things, in order</h2><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!D2v-!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9c2d18f1-7a8a-431c-a296-5edcc854c4fb_1830x880.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!D2v-!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9c2d18f1-7a8a-431c-a296-5edcc854c4fb_1830x880.png 424w, https://substackcdn.com/image/fetch/$s_!D2v-!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9c2d18f1-7a8a-431c-a296-5edcc854c4fb_1830x880.png 848w, https://substackcdn.com/image/fetch/$s_!D2v-!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9c2d18f1-7a8a-431c-a296-5edcc854c4fb_1830x880.png 1272w, https://substackcdn.com/image/fetch/$s_!D2v-!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9c2d18f1-7a8a-431c-a296-5edcc854c4fb_1830x880.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!D2v-!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9c2d18f1-7a8a-431c-a296-5edcc854c4fb_1830x880.png" width="1456" height="700" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9c2d18f1-7a8a-431c-a296-5edcc854c4fb_1830x880.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:700,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:264715,&quot;alt&quot;:&quot;The three checks in order, with a failure policy decided up front&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://thetshaped.dev/i/223994935?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9c2d18f1-7a8a-431c-a296-5edcc854c4fb_1830x880.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="The three checks in order, with a failure policy decided up front" title="The three checks in order, with a failure policy decided up front" srcset="https://substackcdn.com/image/fetch/$s_!D2v-!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9c2d18f1-7a8a-431c-a296-5edcc854c4fb_1830x880.png 424w, https://substackcdn.com/image/fetch/$s_!D2v-!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9c2d18f1-7a8a-431c-a296-5edcc854c4fb_1830x880.png 848w, https://substackcdn.com/image/fetch/$s_!D2v-!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9c2d18f1-7a8a-431c-a296-5edcc854c4fb_1830x880.png 1272w, https://substackcdn.com/image/fetch/$s_!D2v-!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9c2d18f1-7a8a-431c-a296-5edcc854c4fb_1830x880.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button class="pencraft pc-reset pencraft icon-container restack-image buttonBase-tqxFUX" tabindex="0" type="button"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button class="pencraft pc-reset pencraft icon-container view-image buttonBase-tqxFUX" tabindex="0" type="button"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Call <code>messages.create()</code> instead of <code>parse()</code>, so you can read <code>stop_reason</code> before you validate. The same Zod schema builds the wire schema and checks the rules it lost:</p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;typescript&quot;,&quot;nodeId&quot;:null}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-typescript">import Anthropic from "@anthropic-ai/sdk";
import { zodOutputFormat } from "@anthropic-ai/sdk/helpers/zod";
import { z } from "zod";

const Expense = z.object({
  employeeId: z.string(),
  amountCents: z.number().int().positive(), // not in the wire schema
  category: z.enum(["travel", "meals", "software", "other"]),
});
const expenseFormat = zodOutputFormat(Expense);
const client = new Anthropic();

export async function extractExpense(receiptText: string) {
  const message = await client.messages.create({
    model: "claude-opus-5-5",
    max_tokens: 1024,
    messages: [{ role: "user", content: receiptText }],
    output_config: { format: expenseFormat },
  });

  // 1. Did the model finish? A refusal or a cut-off isn't a bad value.
  if (message.stop_reason === "refusal" || message.stop_reason === "max_tokens") {
    return sendToReview(message);
  }
  // 2. Shape, plus the rule the decoder never saw: amountCents &gt; 0.
  const text = message.content.find((block) =&gt; block.type === "text")?.text ?? "";
  const expense = expenseFormat.parse(text);
  // 3. Claims only your own data can check.
  await assertEmployeeExists(expense.employeeId);
  return expense;
}</code></pre></div><p>Give each step its own policy. Send refusals and cut-offs to review, because a retry won't change the answer. Retry a bad value once with the error fed back. Reject an unknown ID loudly. Then chart the pass rate per prompt version and the spread of fields like <code>category</code>, so you see drift in the week it starts.</p><h2>Keep validating the claims</h2><p>Structured outputs solved the syntax half of validation. The other half depends on your data, like your employee table, your refund rules, and the fact that a menu isn't a receipt. No schema can check those, so keep the validation step and point it at the claims.</p><div><hr></div><h2>&#128204; TL;DR</h2><ul><li><p><strong>Structured outputs guarantee shape, not truth.</strong> Wrong but legal values and invented IDs pass every schema check.</p></li><li><p><code>zodOutputFormat</code><strong> sends less than you wrote.</strong> In <code>@anthropic-ai/sdk</code> 1.6.0, <code>.positive()</code> and <code>z.enum()</code> reach the model as description hints, and only your Zod parse enforces them.</p></li><li><p><code>messages.parse()</code><strong> ignores </strong><code>stop_reason</code><strong>.</strong> Call <code>create()</code>, route refusals and cut-offs to review, then parse, then check IDs against your data.</p></li></ul><p><em>This is the deep dive behind item 7 in <a href="https://thetshaped.dev/p/7-things-that-break-llm-apps-in-production-ai">7 Things That Break LLM Apps in Production</a>.</em></p><p>&#128075; P.S. If this issue was useful, Chapter 19 of my upcoming course, <a href="https://petarivanov.tech/senior-javascript-interview/?utm_source=substack&amp;utm_medium=email&amp;utm_campaign=playbook-launch&amp;utm_content=ps-1110#get-sample">The Senior JavaScript Interview</a>, covers Node.js API design. It&#8217;s one of <a href="https://petarivanov.tech/senior-javascript-interview/?utm_source=substack&amp;utm_medium=email&amp;utm_campaign=playbook-launch&amp;utm_content=ps-1110#get-sample">3 free chapters you can read now</a>, before the course opens on 23 November.</p><div><hr></div><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://thetshaped.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Subscribe to get practical tips on AI Agents, Agentic Systems, and Software Architecture delivered straight to your inbox:</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div class="pullquote"><div class="captioned-image-container"><figure><div class="image-link image2" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!cmMn!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 424w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 848w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 1272w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!cmMn!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png" width="248" height="207.7" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:670,&quot;width&quot;:800,&quot;resizeWidth&quot;:248,&quot;bytes&quot;:257716,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://thetshaped.dev/i/188025481?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb8ebf3ac-ad2f-415d-9e32-111e959c8e4e_800x800.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!cmMn!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 424w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 848w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 1272w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div></div></div></div></figure></div><p><strong>Follow me on <a href="https://www.linkedin.com/in/petarivanovv9/">LinkedIn</a></strong> | <strong><a href="https://x.com/intent/follow?screen_name=petarivanovv9">Twitter(X)</a></strong> |<strong> <a href="https://www.threads.net/@petarivanovv9">Threads</a></strong></p></div><p>Thank you for supporting this newsletter.</p><p>Consider sharing this post with your friends and get rewards.</p><p>You are the best! &#128591;</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!EvJY!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!EvJY!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 424w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 848w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!EvJY!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg" width="800" height="500" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:500,&quot;width&quot;:800,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:83253,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://thetshaped.dev/i/188025481?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!EvJY!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 424w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 848w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button class="pencraft pc-reset pencraft icon-container restack-image buttonBase-tqxFUX" tabindex="0" type="button"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button class="pencraft pc-reset pencraft icon-container view-image buttonBase-tqxFUX" tabindex="0" type="button"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://thetshaped.dev/p/llm-structured-outputs-always-parse?utm_source=substack&utm_medium=email&utm_content=share&action=share&quot;,&quot;text&quot;:&quot;Share&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://thetshaped.dev/p/llm-structured-outputs-always-parse?utm_source=substack&utm_medium=email&utm_content=share&action=share"><span>Share</span></a></p><div class="pullquote"><div class="captioned-image-container"><figure><div class="image-link image2" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!UzWz!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 424w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 848w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 1272w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!UzWz!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif" width="48" height="57.6" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/39609b45-f68c-4557-8772-8e2a45182842_500x600.gif&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:600,&quot;width&quot;:500,&quot;resizeWidth&quot;:48,&quot;bytes&quot;:7028,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:&quot;&quot;,&quot;type&quot;:&quot;image/gif&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://thetshaped.dev/i/188785536?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!UzWz!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 424w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 848w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 1272w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 1456w" sizes="auto, 100vw" loading="lazy"></picture><div></div></div></div></figure></div><p style="text-align: center;"><em><strong>Press the Like button if you found this post helpful</strong></em></p></div>]]></content:encoded></item><item><title><![CDATA[Find Your Stage on the Way to JavaScript Architect]]></title><description><![CDATA[A 20-box self-check across six stages, and a free 7-page roadmap with the plan for each one.]]></description><link>https://thetshaped.dev/p/find-your-stage-on-the-way-to-javascript</link><guid isPermaLink="false">https://thetshaped.dev/p/find-your-stage-on-the-way-to-javascript</guid><dc:creator><![CDATA[Petar Ivanov]]></dc:creator><pubDate>Sat, 07 Nov 2026 14:30:57 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!cmMn!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://thetshaped.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption"><span data-color="#9900ff" style="color: rgb(153, 0, 255);">Get my Full-Stack JavaScript Architect Bundle for FREE on newsletter signup:</span></p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div><hr></div><p>Most architect roadmaps are a chart with a hundred boxes and no order. Nothing on the chart tells you when a topic is done, so you skim all of them and practice none.</p><p>My own first version had the same problem. It was an 8-slide social carousel with six lists of five topics each, and nothing in it told you what to do with them. I rebuilt my JavaScript Architect Roadmap 2026 to fix that. It's now a 7-page PDF with six stages in the order they build on each other. Each stage has a check that tells you when to move on, and one project to do in the codebase you already have.</p><p>This issue gives you the part to start with, the self-check from page 2. It takes about five minutes and tells you which stage to work on next.</p><ul><li><p><em><a href="https://thetshaped.dev/p/postgres-schema-migrations-lock-queue-zero-downtime-alter-table?action=share">Share this post</a> &amp; I&#8217;ll send you some rewards for the referrals.</em></p></li></ul><div><hr></div><h2>The six stages</h2><ol><li><p><strong>Master the language.</strong> You can predict what any JavaScript does before you run it.</p></li><li><p><strong>Write code that lasts.</strong> A stranger can change your code safely.</p></li><li><p><strong>Design the application.</strong> A change to one feature touches one module, and the business rules don't know about Express, React or Postgres.</p></li><li><p><strong>Tame the database.</strong> You design schemas, read query plans and pick isolation levels on purpose.</p></li><li><p><strong>Survive distributed systems.</strong> Your design stays correct when a call times out or a message arrives twice.</p></li><li><p><strong>Think like an architect.</strong> You compare real options, write the decision down and get a team to build it the same way.</p></li></ol><p>Stages 1 and 2 are about writing code other people can trust. Stages 3 and 4 shape the application and the data under it. In stages 5 and 6 you make calls that span services and teams.</p><h2>The self-check</h2><p>Tick a box only if you could do it today without looking anything up, and explain it to a teammate. Your starting stage is the first one with an empty box.</p><p>Be strict with yourself. The gaps from a skipped stage tend to show up later as production incidents.</p><h3>1. Master the language</h3><ul><li><p>Predict the output order of code that mixes <code>setTimeout</code>, <code>await</code> and <code>Promise.then</code>.</p></li><li><p>Type a function whose return type depends on its argument, with no <code>any</code> and no <code>as</code>.</p></li><li><p>Find an async bug with a debugger or a failing test, without adding <code>console.log</code> lines.</p></li></ul><h3>2. Write code that lasts</h3><ul><li><p>Refactor a 200-line function in small, tested steps.</p></li><li><p>Say when your code throws and when it returns an error, and point to the rule.</p></li><li><p>Get a pull request approved by someone who has never seen the module, without a call.</p></li><li><p>Reject an AI-written pull request for a design reason, and name the rule it broke.</p></li></ul><h3>3. Design the application</h3><ul><li><p>Draw your app's modules and the direction of every dependency between them.</p></li><li><p>Test a feature's business rules without a database, a server or a browser.</p></li><li><p>Point to the one place where each kind of external input gets validated.</p></li></ul><h3>4. Tame the database</h3><ul><li><p>Read an <code>EXPLAIN ANALYZE</code> plan and say why Postgres picked it.</p></li><li><p>Name the isolation level your transactions run at, and one anomaly it allows.</p></li><li><p>Rename a column in production with zero downtime.</p></li></ul><h3>5. Survive distributed systems</h3><ul><li><p>Explain what happens when a payment call times out and the client retries.</p></li><li><p>Publish an event after a database write without writing to two systems.</p></li><li><p>Follow one request through logs and traces across every service it touches.</p></li></ul><h3>6. Think like an architect</h3><ul><li><p>Write a one-page decision record with two real options and what each costs.</p></li><li><p>Explain a technical trade-off to a product manager in terms of time, money and risk.</p></li><li><p>Name the two or three qualities your system optimizes for, and what it gives up.</p></li><li><p>List the trust boundaries in one feature and the attack you'd try at each.</p></li></ul><h2>What your result means</h2><p><strong>Empty boxes in stage 1 or 2?</strong> These stages move fastest, and everything later rests on them. Close those gaps first, even if your title already says senior.</p><p><strong>Stuck at stage 4?</strong> That's the most common place to stall. Application code can hide a weak data model for years, until traffic grows.</p><p><strong>Every box ticked?</strong> Go to page 6 of the roadmap. The habits there are what people notice when they decide who gets to make the big calls.</p><h2>Finish one stage before you open the next</h2><p>My estimate is two to four months per stage, with a full-time job and five focused hours a week. You'll move faster on the stages you already use at work.</p><p>The most common way to waste a year is to collect stages. Reading about sagas while your own service has no timeouts feels like progress, and it changes nothing in production. Pick your stage, do its practice project at work, and take the self-check again when you're done.</p><h2>What's in the full roadmap</h2><p>The self-check is page 2. The rest of the PDF is the plan for each stage:</p><ul><li><p><strong>Pages 3 to 5.</strong> Each stage gets a paragraph on why it matters, a diagram, six skills, a "move on when you can" checklist, a practice project for your own codebase, and two sources worth your time.</p></li><li><p><strong>Page 6.</strong> Four habits that apply at every stage, with a sample decision record, the shape I use to write a trade-off, how to read a large codebase fast, and how to explain a design to people who don't write code.</p></li><li><p><strong>Page 7.</strong> A 90-day plan for your current stage, and one book for each stage.</p></li></ul><p>Here's the practice project for stage 4, so you can see the format:</p><blockquote><p>Turn on <code>pg_stat_statements</code>, or open your provider's query insights. Take the three queries with the most total time and read their plans. Fix one with an index or a rewrite, and record the numbers before and after.</p></blockquote><p>The roadmap is free. Enter your email on my site and the PDF arrives in your inbox a minute later.</p><p><a href="https://petarivanov.tech/resources/javascript-architect-roadmap/?utm_source=thetshaped&amp;utm_medium=newsletter&amp;utm_campaign=architect-roadmap-2026">Get the free JavaScript Architect Roadmap</a></p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://petarivanov.tech/resources/javascript-architect-roadmap/?utm_source=thetshaped&amp;utm_medium=newsletter&amp;utm_campaign=architect-roadmap-2026&quot;,&quot;text&quot;:&quot;Free JavaScript Architect Roadmap&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://petarivanov.tech/resources/javascript-architect-roadmap/?utm_source=thetshaped&amp;utm_medium=newsletter&amp;utm_campaign=architect-roadmap-2026"><span>Free JavaScript Architect Roadmap</span></a></p><p>Signing up also adds you to my email list on petarivanov.tech. You'll get a welcome email from there, and one click unsubscribes you from that list while you keep the PDF.</p><h2>One favor</h2><p>If you know a developer who's working toward an architect role, forward them this email or send them the link.</p><div><hr></div><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://thetshaped.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Subscribe to get practical tips on AI Agents, Agentic Systems, and Software Architecture delivered straight to your inbox:</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div class="pullquote"><div class="captioned-image-container"><figure><div class="image-link image2" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!cmMn!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 424w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 848w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 1272w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!cmMn!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png" width="248" height="207.7" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:670,&quot;width&quot;:800,&quot;resizeWidth&quot;:248,&quot;bytes&quot;:257716,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://thetshaped.dev/i/188025481?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb8ebf3ac-ad2f-415d-9e32-111e959c8e4e_800x800.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!cmMn!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 424w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 848w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 1272w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div></div></div></div></figure></div><p><strong>Follow me on <a href="https://www.linkedin.com/in/petarivanovv9/">LinkedIn</a></strong> | <strong><a href="https://x.com/intent/follow?screen_name=petarivanovv9">Twitter(X)</a></strong> |<strong> <a href="https://www.threads.net/@petarivanovv9">Threads</a></strong></p></div><p>Thank you for supporting this newsletter.</p><p>Consider sharing this post with your friends and get rewards.</p><p>You are the best! &#128591;</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!EvJY!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!EvJY!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 424w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 848w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!EvJY!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg" width="800" height="500" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:500,&quot;width&quot;:800,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:83253,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://thetshaped.dev/i/188025481?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!EvJY!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 424w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 848w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button class="pencraft pc-reset pencraft icon-container restack-image buttonBase-tqxFUX" tabindex="0" type="button"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button class="pencraft pc-reset pencraft icon-container view-image buttonBase-tqxFUX" tabindex="0" type="button"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://thetshaped.dev/p/find-your-stage-on-the-way-to-javascript?utm_source=substack&utm_medium=email&utm_content=share&action=share&quot;,&quot;text&quot;:&quot;Share&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://thetshaped.dev/p/find-your-stage-on-the-way-to-javascript?utm_source=substack&utm_medium=email&utm_content=share&action=share"><span>Share</span></a></p><div class="pullquote"><div class="captioned-image-container"><figure><div class="image-link image2" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!UzWz!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 424w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 848w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 1272w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!UzWz!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif" width="48" height="57.6" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/39609b45-f68c-4557-8772-8e2a45182842_500x600.gif&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:600,&quot;width&quot;:500,&quot;resizeWidth&quot;:48,&quot;bytes&quot;:7028,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:&quot;&quot;,&quot;type&quot;:&quot;image/gif&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://thetshaped.dev/i/188785536?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!UzWz!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 424w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 848w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 1272w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 1456w" sizes="auto, 100vw" loading="lazy"></picture><div></div></div></div></figure></div><p style="text-align: center;"><em><strong>Press the Like button if you found this post helpful</strong></em></p></div>]]></content:encoded></item><item><title><![CDATA[5 Postgres Migrations That Work Fine in Dev and Lock Up Production]]></title><description><![CDATA[Each one looks harmless until the table is big. Why it locks, what your users see, and the safe version of each.]]></description><link>https://thetshaped.dev/p/postgres-schema-migrations-lock-queue-zero-downtime-alter-table</link><guid isPermaLink="false">https://thetshaped.dev/p/postgres-schema-migrations-lock-queue-zero-downtime-alter-table</guid><pubDate>Wed, 04 Nov 2026 11:42:44 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/2725e563-086b-4fc5-8e07-a03fb01e34df_2912x1632.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://thetshaped.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption"><span data-color="#9900ff" style="color: rgb(153, 0, 255);">Get my Full-Stack JavaScript Architect Bundle for FREE on newsletter signup:</span></p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div><hr></div><p>In a 2015 post on <a href="https://gocardless.com/blog/zero-downtime-postgres-migrations-the-hard-parts/">zero-downtime Postgres migrations</a>, Chris Sinjakli of GoCardless, a payments company, describes a migration that looked routine. It renamed a few new, still-empty tables, then dropped and re-created their foreign keys to fix the constraint names.</p><p>"Just after the schema migration started, we started getting alerts about API requests timing out," he writes. The timeouts lasted about 15 seconds, until the migration went through.</p><p>Nothing in the migration was slow, and the tables had no rows. A long-running read on the parent table happened to be open when it started, and that was enough.</p><p>So a migration's lock queue can stall any busy table, whatever its size. This post explains it, then covers five schema changes that turn dangerous once a table is big. The examples use Postgres and an <code>expenses</code> table from a payments app. MySQL has the same queue (its <code>lock_wait_timeout</code> defaults to a year), but the fixes differ.</p><ul><li><p><em><a href="https://thetshaped.dev/p/postgres-schema-migrations-lock-queue-zero-downtime-alter-table?action=share">Share this post</a> &amp; I&#8217;ll send you some rewards for the referrals.</em></p></li></ul><div><hr></div><h2><strong><a href="https://petarivanov.tech/?utm_source=substack&amp;utm_medium=newsletter&amp;utm_campaign=2026-11-03-issue-schema-migrations&amp;utm_content=house-ad">What I'm building next</a></strong></h2><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!_jPz!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18597639-71c7-4814-9d3d-5f4602c83e91_2912x1524.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!_jPz!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18597639-71c7-4814-9d3d-5f4602c83e91_2912x1524.png 424w, https://substackcdn.com/image/fetch/$s_!_jPz!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18597639-71c7-4814-9d3d-5f4602c83e91_2912x1524.png 848w, https://substackcdn.com/image/fetch/$s_!_jPz!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18597639-71c7-4814-9d3d-5f4602c83e91_2912x1524.png 1272w, https://substackcdn.com/image/fetch/$s_!_jPz!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18597639-71c7-4814-9d3d-5f4602c83e91_2912x1524.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!_jPz!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18597639-71c7-4814-9d3d-5f4602c83e91_2912x1524.png" width="1456" height="762" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/18597639-71c7-4814-9d3d-5f4602c83e91_2912x1524.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:762,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1128101,&quot;alt&quot;:&quot;The Senior JavaScript Interview, out 23 November, and Own What AI Writes, course waitlist&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://thetshaped.dev/i/223024609?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18597639-71c7-4814-9d3d-5f4602c83e91_2912x1524.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="The Senior JavaScript Interview, out 23 November, and Own What AI Writes, course waitlist" title="The Senior JavaScript Interview, out 23 November, and Own What AI Writes, course waitlist" srcset="https://substackcdn.com/image/fetch/$s_!_jPz!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18597639-71c7-4814-9d3d-5f4602c83e91_2912x1524.png 424w, https://substackcdn.com/image/fetch/$s_!_jPz!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18597639-71c7-4814-9d3d-5f4602c83e91_2912x1524.png 848w, https://substackcdn.com/image/fetch/$s_!_jPz!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18597639-71c7-4814-9d3d-5f4602c83e91_2912x1524.png 1272w, https://substackcdn.com/image/fetch/$s_!_jPz!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18597639-71c7-4814-9d3d-5f4602c83e91_2912x1524.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button class="pencraft pc-reset pencraft icon-container restack-image buttonBase-tqxFUX" tabindex="0" type="button"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button class="pencraft pc-reset pencraft icon-container view-image buttonBase-tqxFUX" tabindex="0" type="button"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p><strong><a href="https://petarivanov.tech/senior-javascript-interview/?utm_source=substack&amp;utm_medium=newsletter&amp;utm_campaign=2026-11-03-issue-schema-migrations&amp;utm_content=house-ad">The Senior JavaScript Interview</a></strong> launches on 23 November. It shows what a senior answer sounds like across React, Node.js, and system design, and the first 3 chapters are free.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://petarivanov.tech/senior-javascript-interview/?utm_source=substack&amp;utm_medium=newsletter&amp;utm_campaign=2026-11-03-issue-schema-migrations&amp;utm_content=house-ad&quot;,&quot;text&quot;:&quot;Read 3 chapters free&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://petarivanov.tech/senior-javascript-interview/?utm_source=substack&amp;utm_medium=newsletter&amp;utm_campaign=2026-11-03-issue-schema-migrations&amp;utm_content=house-ad"><span>Read 3 chapters free</span></a></p><p><strong><a href="https://petarivanov.tech/courses/own-what-ai-writes/?utm_source=substack&amp;utm_medium=newsletter&amp;utm_campaign=2026-11-03-issue-schema-migrations&amp;utm_content=house-ad">Own What AI Writes</a></strong> is my course for TypeScript engineers who review the pull requests AI agents write. Join the waitlist to get the Module 1 exercise now and hear first when the $199 presale opens in early 2027.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://petarivanov.tech/courses/own-what-ai-writes/?utm_source=substack&amp;utm_medium=newsletter&amp;utm_campaign=2026-11-03-issue-schema-migrations&amp;utm_content=house-ad&quot;,&quot;text&quot;:&quot;Join the course waitlist&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://petarivanov.tech/courses/own-what-ai-writes/?utm_source=substack&amp;utm_medium=newsletter&amp;utm_campaign=2026-11-03-issue-schema-migrations&amp;utm_content=house-ad"><span>Join the course waitlist</span></a></p><div><hr></div><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://thetshaped.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Join <strong>30,000+ engineers</strong> getting one practical issue a week on production-grade React, Node, Architecture, and <strong>real AI systems</strong>, and leveling up from Mid &#8594; Senior &#8594; Architect.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div><hr></div><h2>The lock is short. The queue behind it is the outage.</h2><p>Most of the risky <code>ALTER TABLE</code> statements take <code>ACCESS EXCLUSIVE</code>, the strongest lock Postgres has. It blocks everything, plain <code>SELECT</code>s included. Many ALTERs hold it for about a millisecond, so the lock alone isn't the problem.</p><p>The problem is the queue. If an open transaction has touched the table (a slow report, or a transaction someone forgot to close), your ALTER can't get its lock yet. It waits at the front of the line.</p><p>Any query that arrives after it, even a trivial <code>SELECT ... WHERE id = $1</code>, waits behind the ALTER, because it conflicts with the lock the ALTER is about to take.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Hh3X!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F11eaa9b9-c0bb-473e-ae55-5d80373de44c_3600x1972.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Hh3X!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F11eaa9b9-c0bb-473e-ae55-5d80373de44c_3600x1972.png 424w, https://substackcdn.com/image/fetch/$s_!Hh3X!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F11eaa9b9-c0bb-473e-ae55-5d80373de44c_3600x1972.png 848w, https://substackcdn.com/image/fetch/$s_!Hh3X!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F11eaa9b9-c0bb-473e-ae55-5d80373de44c_3600x1972.png 1272w, https://substackcdn.com/image/fetch/$s_!Hh3X!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F11eaa9b9-c0bb-473e-ae55-5d80373de44c_3600x1972.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!Hh3X!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F11eaa9b9-c0bb-473e-ae55-5d80373de44c_3600x1972.png" width="1456" height="798" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/11eaa9b9-c0bb-473e-ae55-5d80373de44c_3600x1972.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:798,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:401298,&quot;alt&quot;:&quot;A long transaction holds a lock, the ALTER waits for it, and every query behind the ALTER waits too&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://thetshaped.dev/i/223024609?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F11eaa9b9-c0bb-473e-ae55-5d80373de44c_3600x1972.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="A long transaction holds a lock, the ALTER waits for it, and every query behind the ALTER waits too" title="A long transaction holds a lock, the ALTER waits for it, and every query behind the ALTER waits too" srcset="https://substackcdn.com/image/fetch/$s_!Hh3X!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F11eaa9b9-c0bb-473e-ae55-5d80373de44c_3600x1972.png 424w, https://substackcdn.com/image/fetch/$s_!Hh3X!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F11eaa9b9-c0bb-473e-ae55-5d80373de44c_3600x1972.png 848w, https://substackcdn.com/image/fetch/$s_!Hh3X!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F11eaa9b9-c0bb-473e-ae55-5d80373de44c_3600x1972.png 1272w, https://substackcdn.com/image/fetch/$s_!Hh3X!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F11eaa9b9-c0bb-473e-ae55-5d80373de44c_3600x1972.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button class="pencraft pc-reset pencraft icon-container restack-image buttonBase-tqxFUX" tabindex="0" type="button"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button class="pencraft pc-reset pencraft icon-container view-image buttonBase-tqxFUX" tabindex="0" type="button"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>In a test on Postgres 17 and 18, an 8-second report held a read lock on <code>expenses</code>. An <code>ALTER TABLE expenses ADD COLUMN</code> arrived a second later and waited. A primary-key lookup that arrived after the ALTER waited 6.5 seconds for one row.</p><p>Two settings cap the damage:</p><pre><code>SET lock_timeout = '2s';        -- give up on the lock fast, retry later
SET statement_timeout = '30s';  -- stop a statement that runs longer than planned</code></pre><p>With <code>lock_timeout</code>, the ALTER failed after 2 seconds with <code>canceling statement due to lock timeout</code>, and the lookup went through after 1.5. The timeout doesn't remove the stall. It caps it, so pick a number your app can tolerate.</p><p>Then retry a few times with a short pause. Knex, node-pg-migrate, and Drizzle don't retry, so put the loop in your deploy script. Each attempt can still stall requests for up to 2 seconds.</p><p>GoCardless puts it plainly: "It's better to abort a deploy than take your application down."</p><p>The rest of this post assumes both are set, with one exception. <code>VALIDATE CONSTRAINT</code> and <code>CREATE INDEX CONCURRENTLY</code> run for minutes, and their lock lets reads and writes through. Set both timeouts to 0 for them, or a timeout cancels them halfway.</p><div><hr></div><h2>NOT NULL on a big table: add a check first, then the constraint</h2><p><code>ALTER TABLE expenses ALTER COLUMN merchant_id SET NOT NULL</code> has to prove that every existing row passes. That takes a full table scan under <code>ACCESS EXCLUSIVE</code>. With 300 million rows, that scan reads tens of gigabytes. Reading that from a cloud disk can take minutes, and nothing gets through until it ends.</p><p>The safe version takes three migrations:</p><pre><code>-- Migration 1: instant, checks new rows only
ALTER TABLE expenses ADD CONSTRAINT expenses_merchant_id_check
  CHECK (merchant_id IS NOT NULL) NOT VALID;

-- Migration 2: scans the table while reads and writes continue
ALTER TABLE expenses VALIDATE CONSTRAINT expenses_merchant_id_check;

-- Migration 3: instant on PG12+, because the CHECK proves it
ALTER TABLE expenses ALTER COLUMN merchant_id SET NOT NULL;
ALTER TABLE expenses DROP CONSTRAINT expenses_merchant_id_check;</code></pre><p><code>VALIDATE</code> still reads the whole table, but its lock (<code>SHARE UPDATE EXCLUSIVE</code>) lets reads and writes through. Ship the three steps in separate deploys. Knex's <code>migrate:latest</code> and Drizzle run all pending migrations in one transaction, and there the first step's <code>ACCESS EXCLUSIVE</code> lock stays held through the whole scan.</p><p>Postgres 18 can skip the CHECK, with <code>ADD CONSTRAINT expenses_merchant_id_not_null NOT NULL merchant_id NOT VALID</code> and a later <code>VALIDATE</code>.</p><p>Foreign keys get the same split, <code>NOT VALID</code> first and <code>VALIDATE</code> in a later deploy. That skips the scan, but a foreign key change still locks both tables. Adding one blocks writes on both. Dropping one takes <code>ACCESS EXCLUSIVE</code> on both, so one slow read on the parent stalls every query on either table. That's what happened when GoCardless's migration dropped its foreign keys.</p><div><hr></div><h2>An index build blocks writes unless you say CONCURRENTLY</h2><p><code>CREATE INDEX</code> takes a <code>SHARE</code> lock for the whole build. Reads keep working, but every <code>INSERT</code>, <code>UPDATE</code>, and <code>DELETE</code> on <code>expenses</code> waits until the index is done. A big table takes minutes to index, and every card payment in that window waits or times out.</p><p><code>CREATE INDEX CONCURRENTLY</code> builds the index without blocking writes. It has three catches:</p><ul><li><p><strong>It can't run inside a transaction.</strong> Most migration runners wrap migrations in one, so opt that migration out. In Knex, export <code>config = { transaction: false }</code> from the file. In node-pg-migrate, call <code>pgm.noTransaction()</code>. Drizzle has no opt-out (issue <a href="https://github.com/drizzle-team/drizzle-orm/issues/860">#860</a>, open since 2023), so run that statement outside its migrator.</p></li><li><p><strong>It's slower, and it waits.</strong> It scans the table twice, then waits until every older transaction in the database finishes, whatever table it touched. Under a 2-second <code>lock_timeout</code>, one slow report cancels it.</p></li><li><p><strong>A failed or canceled build can leave an </strong><code>INVALID</code><strong> index.</strong> No query uses it, and writes may still update it. Check <code>pg_index.indisvalid</code> after each concurrent build. If it's false, run <code>DROP INDEX CONCURRENTLY</code> and build it again, or use <code>REINDEX INDEX CONCURRENTLY</code>. A re-run with <code>IF NOT EXISTS</code> silently keeps the broken one.</p></li></ul><div><hr></div><h2>Renames and drops break the app version that's still running</h2><p>The migration itself is instant, so the outage comes from your app. During a rolling deploy, old and new instances run side by side for a few minutes.</p><p>Rename <code>expenses.memo</code> to <code>expenses.description</code>, and every old instance that still selects <code>memo</code> fails with <code>column "memo" does not exist</code>. A drop does the same, even in code that never names the column. postgres.js prepares statements by default, and a prepared <code>SELECT *</code> fails with <code>cached plan must not change result type</code>.</p><p>The safe version is expand, migrate, contract, across three deploys:</p><ol><li><p>Add <code>description</code>. Deploy code that writes both columns and still reads <code>memo</code>.</p></li><li><p>Backfill <code>description</code> from <code>memo</code>. Deploy code that reads and writes only <code>description</code>.</p></li><li><p>Drop <code>memo</code>.</p></li></ol><p>It's slow, and it's the only version that survives a rolling deploy.</p><div><hr></div><h2>A backfill belongs in a job, not in the migration</h2><p>The quick way is to run <code>UPDATE expenses SET description = memo</code> in the same migration as the <code>ADD COLUMN</code>. It runs as one long transaction over 80 million rows.</p><p>It locks every row it updates until it commits. The <code>ADD COLUMN</code> lock (<code>ACCESS EXCLUSIVE</code>) stays held until the commit too, so nobody can even read the table. In a test on 5 million rows, a primary-key lookup waited 9.7 seconds behind it.</p><p>The update writes a new version of every row, so the table more than doubles in size. If it fails at row 79 million, Postgres rolls back the whole thing and leaves 79 million dead rows for vacuum to clean up.</p><p>The safe version is a separate job. Update in batches by primary key range, commit each batch, and pause between batches. The <code>description IS NULL</code> check makes it safe to re-run:</p><pre><code>DO $$
DECLARE last_id bigint := 0;
BEGIN
  WHILE last_id &lt; (SELECT max(id) FROM expenses) LOOP
    UPDATE expenses SET description = memo
     WHERE id &gt; last_id AND id &lt;= last_id + 5000 AND description IS NULL;
    last_id := last_id + 5000;
    COMMIT;                 -- each batch releases its row locks
    PERFORM pg_sleep(0.1);  -- give replicas and vacuum room
  END LOOP;
END $$;</code></pre><p>Run it outside any transaction, from psql or a job runner. Against a million-row test table, concurrent lookups and inserts stayed under 20 ms the whole time.</p><div><hr></div><h2>A default that isn't a constant rewrites the table, or lies</h2><p>Since Postgres 11, <code>ADD COLUMN status text NOT NULL DEFAULT 'pending'</code> is instant. The default lives in the catalog and fills in when an old row is read.</p><p>Two cases still bite. A volatile default (<code>DEFAULT gen_random_uuid()</code>, <code>DEFAULT random()</code>) needs a different value for every row, so Postgres rewrites the whole table under <code>ACCESS EXCLUSIVE</code>.</p><p><code>DEFAULT now()</code> is fast, because <code>now()</code> isn't volatile. The ALTER evaluates it once, so every existing row gets the start time of the migration's transaction. If the column is <code>submitted_at</code>, your finance reports now say 80 million expenses were submitted at 2:04 PM on a Tuesday.</p><p>For anything that isn't a constant, add the column with no default. Set the default in a second statement, which is instant and applies only to new rows. Then backfill the old rows in batches, from real data such as an audit log.</p><div><hr></div><h2>Five questions catch these before review ends</h2><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!YXNp!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F158d3f37-ebac-4fdf-b876-1e2ed0d0975a_3640x2112.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!YXNp!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F158d3f37-ebac-4fdf-b876-1e2ed0d0975a_3640x2112.png 424w, https://substackcdn.com/image/fetch/$s_!YXNp!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F158d3f37-ebac-4fdf-b876-1e2ed0d0975a_3640x2112.png 848w, https://substackcdn.com/image/fetch/$s_!YXNp!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F158d3f37-ebac-4fdf-b876-1e2ed0d0975a_3640x2112.png 1272w, https://substackcdn.com/image/fetch/$s_!YXNp!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F158d3f37-ebac-4fdf-b876-1e2ed0d0975a_3640x2112.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!YXNp!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F158d3f37-ebac-4fdf-b876-1e2ed0d0975a_3640x2112.png" width="1456" height="845" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/158d3f37-ebac-4fdf-b876-1e2ed0d0975a_3640x2112.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:845,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:675782,&quot;alt&quot;:&quot;Five migrations that look harmless in dev, and the safe version of each at scale&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://thetshaped.dev/i/223024609?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F158d3f37-ebac-4fdf-b876-1e2ed0d0975a_3640x2112.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Five migrations that look harmless in dev, and the safe version of each at scale" title="Five migrations that look harmless in dev, and the safe version of each at scale" srcset="https://substackcdn.com/image/fetch/$s_!YXNp!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F158d3f37-ebac-4fdf-b876-1e2ed0d0975a_3640x2112.png 424w, https://substackcdn.com/image/fetch/$s_!YXNp!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F158d3f37-ebac-4fdf-b876-1e2ed0d0975a_3640x2112.png 848w, https://substackcdn.com/image/fetch/$s_!YXNp!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F158d3f37-ebac-4fdf-b876-1e2ed0d0975a_3640x2112.png 1272w, https://substackcdn.com/image/fetch/$s_!YXNp!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F158d3f37-ebac-4fdf-b876-1e2ed0d0975a_3640x2112.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button class="pencraft pc-reset pencraft icon-container restack-image buttonBase-tqxFUX" tabindex="0" type="button"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button class="pencraft pc-reset pencraft icon-container view-image buttonBase-tqxFUX" tabindex="0" type="button"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Before a migration touches a table you'd call big, ask:</p><ul><li><p>Is <code>lock_timeout</code> set, and does the deploy retry when it fires?</p></li><li><p>Does this statement scan or rewrite the table, as a type change from <code>int</code> to <code>bigint</code> does? If yes, is there a <code>NOT VALID</code> or <code>CONCURRENTLY</code> version?</p></li><li><p>Will the previous version of the app still work while the deploy rolls out?</p></li><li><p>Is every data change batched, re-runnable, and outside the schema migration?</p></li><li><p>Can I undo this without a second outage?</p></li></ul><p>For the last one, ship a tested rollback in the same PR (a <code>down</code> function in Knex and node-pg-migrate). It can't bring back a dropped column's data, so the drop always comes last.</p><p>For a tool that asks these for you, <a href="https://squawkhq.com/">squawk</a> lints raw SQL migrations for most of these patterns (not backfills), and <a href="https://github.com/xataio/pgroll">pgroll</a> runs expand and contract for you.</p><div><hr></div><h2>&#128204; TL;DR</h2><ul><li><p><strong>The queue is the outage.</strong> A waiting <code>ALTER</code> blocks every query behind it. GoCardless's API went down for about 15 seconds this way, from one long read and a migration on empty tables. Set <code>lock_timeout</code> so the ALTER gives up, and retry.</p></li><li><p><code>SET NOT NULL</code><strong> and new foreign keys scan under a strong lock.</strong> Add the constraint <code>NOT VALID</code>, then <code>VALIDATE</code> it in a separate deploy.</p></li><li><p><code>CREATE INDEX</code><strong> blocks writes.</strong> Use <code>CONCURRENTLY</code> outside a transaction, with both timeouts off, and check for <code>INVALID</code> leftovers.</p></li><li><p><strong>Renames and drops break the old app version</strong> during a rolling deploy. Expand, migrate, contract.</p></li><li><p><strong>Backfills are jobs, not migrations.</strong> Batch by primary key, commit often, and make the job re-runnable.</p></li><li><p><strong>Constant defaults are instant since Postgres 11.</strong> Volatile defaults rewrite the table, and <code>DEFAULT now()</code> stamps every old row with the deploy time.</p></li></ul><div><hr></div><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://thetshaped.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Subscribe to get practical tips on AI Agents, Agentic Systems, and Software Architecture delivered straight to your inbox:</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div class="pullquote"><div class="captioned-image-container"><figure><div class="image-link image2" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!cmMn!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 424w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 848w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 1272w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!cmMn!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png" width="248" height="207.7" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:670,&quot;width&quot;:800,&quot;resizeWidth&quot;:248,&quot;bytes&quot;:257716,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://thetshaped.dev/i/188025481?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb8ebf3ac-ad2f-415d-9e32-111e959c8e4e_800x800.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!cmMn!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 424w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 848w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 1272w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div></div></div></div></figure></div><p><strong>Follow me on <a href="https://www.linkedin.com/in/petarivanovv9/">LinkedIn</a></strong> | <strong><a href="https://x.com/intent/follow?screen_name=petarivanovv9">Twitter(X)</a></strong> |<strong> <a href="https://www.threads.net/@petarivanovv9">Threads</a></strong></p></div><p>Thank you for supporting this newsletter.</p><p>Consider sharing this post with your friends and get rewards.</p><p>You are the best! &#128591;</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!EvJY!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!EvJY!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 424w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 848w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!EvJY!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg" width="800" height="500" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:500,&quot;width&quot;:800,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:83253,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://thetshaped.dev/i/188025481?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!EvJY!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 424w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 848w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button class="pencraft pc-reset pencraft icon-container restack-image buttonBase-tqxFUX" tabindex="0" type="button"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button class="pencraft pc-reset pencraft icon-container view-image buttonBase-tqxFUX" tabindex="0" type="button"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://thetshaped.dev/p/postgres-schema-migrations-lock-queue-zero-downtime-alter-table?utm_source=substack&utm_medium=email&utm_content=share&action=share&quot;,&quot;text&quot;:&quot;Share&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://thetshaped.dev/p/postgres-schema-migrations-lock-queue-zero-downtime-alter-table?utm_source=substack&utm_medium=email&utm_content=share&action=share"><span>Share</span></a></p><div class="pullquote"><div class="captioned-image-container"><figure><div class="image-link image2" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!UzWz!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 424w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 848w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 1272w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!UzWz!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif" width="48" height="57.6" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/39609b45-f68c-4557-8772-8e2a45182842_500x600.gif&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:600,&quot;width&quot;:500,&quot;resizeWidth&quot;:48,&quot;bytes&quot;:7028,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:&quot;&quot;,&quot;type&quot;:&quot;image/gif&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://thetshaped.dev/i/188785536?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!UzWz!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 424w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 848w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 1272w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 1456w" sizes="auto, 100vw" loading="lazy"></picture><div></div></div></div></figure></div><p style="text-align: center;"><em><strong>Press the Like button if you found this post helpful</strong></em></p></div>]]></content:encoded></item><item><title><![CDATA[Domain Events Are Easy. Until One Fails Silently.]]></title><description><![CDATA[Every tutorial stops at the typed event bus. Here are the 3 production failures it skips, and the fix for each in Node.js.]]></description><link>https://thetshaped.dev/p/domain-events-production-failures-nodejs-typescript</link><guid isPermaLink="false">https://thetshaped.dev/p/domain-events-production-failures-nodejs-typescript</guid><pubDate>Thu, 22 Oct 2026 12:09:33 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/db7827ea-b499-48ae-89a4-1b1fbdae5493_2912x1632.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://thetshaped.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption"><span data-color="#9900ff" style="color: rgb(153, 0, 255);">Get my Full-Stack JavaScript Architect Bundle for FREE on newsletter signup:</span></p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><p>Every time a traveler confirms a booking, your code sends a confirmation email, creates an invoice in accounting, pings the trip organizer on Slack, and tracks an analytics event. Four side effects live in one function. </p><p>When product asks for a fifth, you open <code>confirmBooking()</code> and wedge it in between the email and the invoice.</p><p>Domain events are the standard fix. The core announces a business fact, <code>BookingConfirmed</code>, and separate handlers react to it. </p><p>Every tutorial shows that part with a typed event bus, then stops right before the three things that bite in production: handler errors that disappear, events that die with the process, and latency that never left your request.</p><ul><li><p><em><a href="https://thetshaped.dev/p/domain-events-production-failures-nodejs-typescript/?action=share">Share this post</a> &amp; I&#8217;ll send you some rewards for the referrals.</em></p></li></ul><div><hr></div><h2><a href="https://inngest.link/t-shaped-dev-nl">Build for the long run (Partner)</a></h2><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://inngest.link/t-shaped-dev-nl" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!FkkV!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff8034326-70e4-4589-a79a-ad15997e4a68_2190x1061.png 424w, https://substackcdn.com/image/fetch/$s_!FkkV!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff8034326-70e4-4589-a79a-ad15997e4a68_2190x1061.png 848w, https://substackcdn.com/image/fetch/$s_!FkkV!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff8034326-70e4-4589-a79a-ad15997e4a68_2190x1061.png 1272w, https://substackcdn.com/image/fetch/$s_!FkkV!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff8034326-70e4-4589-a79a-ad15997e4a68_2190x1061.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!FkkV!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff8034326-70e4-4589-a79a-ad15997e4a68_2190x1061.png" width="1456" height="705" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/f8034326-70e4-4589-a79a-ad15997e4a68_2190x1061.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:705,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:4101995,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:&quot;https://inngest.link/t-shaped-dev-nl&quot;,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://thetshaped.dev/i/213818275?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff8034326-70e4-4589-a79a-ad15997e4a68_2190x1061.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!FkkV!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff8034326-70e4-4589-a79a-ad15997e4a68_2190x1061.png 424w, https://substackcdn.com/image/fetch/$s_!FkkV!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff8034326-70e4-4589-a79a-ad15997e4a68_2190x1061.png 848w, https://substackcdn.com/image/fetch/$s_!FkkV!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff8034326-70e4-4589-a79a-ad15997e4a68_2190x1061.png 1272w, https://substackcdn.com/image/fetch/$s_!FkkV!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff8034326-70e4-4589-a79a-ad15997e4a68_2190x1061.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button class="pencraft pc-reset pencraft icon-container restack-image buttonBase-tqxFUX" tabindex="0" type="button"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button class="pencraft pc-reset pencraft icon-container view-image buttonBase-tqxFUX" tabindex="0" type="button"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Long-running agents need endurance.</p><p>They must endure waiting for users, and weather tool failures. They must take each new jump in model intelligence in stride.</p><p><a href="https://inngest.link/t-shaped-dev-nl">Inngest</a> makes long-running agents durable, observable and improvable over time.</p><p>Check out their generous free tier and a sleek local dev server for testing your workflows.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://inngest.link/t-shaped-dev-nl&quot;,&quot;text&quot;:&quot;Build for the long run with Inngest&quot;,&quot;action&quot;:null,&quot;class&quot;:&quot;button-wrapper&quot;}" data-component-name="ButtonCreateButton"><a class="button primary button-wrapper" href="https://inngest.link/t-shaped-dev-nl"><span>Build for the long run with Inngest</span></a></p><p>(Thanks to <a href="https://inngest.link/t-shaped-dev-nl">Inngest</a> for partnering on this post.)</p><div><hr></div><h2>Events turn four side effects into one call</h2><p>Here's the before, which is most Node.js codebases:</p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;typescript&quot;,&quot;nodeId&quot;:null}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-typescript">async function confirmBooking(bookingId: string): Promise&lt;Booking&gt; {
  const booking = await bookings.markConfirmed(bookingId);

  await email.sendConfirmation(booking);
  await accounting.createInvoice(booking);
  await slack.notifyOrganizer(booking);
  await analytics.track('booking_confirmed', { bookingId });

  return booking;
}</code></pre></div><p><code>confirmBooking</code> knows four services that have nothing to do with confirming a booking. Testing it means mocking all four. A Slack outage fails the confirmation, which is a silly reason to lose a booking.</p><p>A domain event is a record of something that already happened, named in the past tense: <code>BookingConfirmed</code>, <code>PaymentFailed</code>, <code>TripCancelled</code>. The core publishes it and moves on. Each side effect becomes a handler in its own file, wired once at startup:</p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;typescript&quot;,&quot;nodeId&quot;:null}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-typescript">async function confirmBooking(bookingId: string, bus: EventBus): Promise&lt;Booking&gt; {
  const booking = await bookings.markConfirmed(bookingId);

  await bus.emit({
    type: 'BookingConfirmed',
    eventId: randomUUID(),
    occurredAt: new Date(),
    payload: { bookingId: booking.id, travelerEmail: booking.travelerEmail },
  });

  return booking;
}

// composition-root.ts
bus.on('BookingConfirmed', sendConfirmationEmail(services.email));
bus.on('BookingConfirmed', createInvoice(services.accounting));
bus.on('BookingConfirmed', notifyOrganizer(services.slack));
bus.on('BookingConfirmed', trackConfirmation(services.analytics));</code></pre></div><p>The fifth side effect is now one new file and one new line, and testing <code>confirmBooking</code> needs zero service mocks. Two details in the event pay off later. The <code>eventId</code> lets a handler tell a retry from a new event. The payload carries IDs and plain facts instead of the ORM entity, so a handler can't mutate the booking.</p><p>Every tutorial covers this much. The first thing they skip hides inside the bus itself.</p><div><hr></div><h2>Skip #1: <code>Promise.allSettled</code> hides failed handlers</h2><p><strong>Node's built-in <code>EventEmitter</code> looks like enough, and since mid-2024 <code>@types/node</code> even types its event names and payloads.</strong> Its <code>emit()</code> is the problem. It's synchronous and never awaits an async handler. A rejected handler becomes an unhandled rejection, and since Node 15 that crashes the process by default. A Slack outage takes your whole API down with it.</p><p>You want a bus that awaits its handlers and decides what a failure means. It's about 25 lines:</p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;typescript&quot;,&quot;nodeId&quot;:null}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-typescript">type DomainEvent = BookingConfirmed | TripCancelled;
type EventOfType&lt;T extends DomainEvent['type']&gt; = Extract&lt;DomainEvent, { type: T }&gt;;
type Handler&lt;E&gt; = (event: E) =&gt; Promise&lt;void&gt;;

class EventBus {
  private handlers = new Map&lt;string, Handler&lt;any&gt;[]&gt;();

  on&lt;T extends DomainEvent['type']&gt;(type: T, handler: Handler&lt;EventOfType&lt;T&gt;&gt;): void {
    this.handlers.set(type, [...(this.handlers.get(type) ?? []), handler]);
  }

  async emit(event: DomainEvent): Promise&lt;void&gt; {
    const handlers = this.handlers.get(event.type) ?? [];
    const results = await Promise.allSettled(
      handlers.map(async (handle) =&gt; handle(event)), // a sync throw becomes a rejection too
    );

    results.forEach((result, i) =&gt; {
      if (result.status === 'rejected') {
        logger.error(
          { eventType: event.type, eventId: event.eventId, handler: handlers[i].name, err: result.reason },
          'event handler failed',
        );
      }
    });
  }
}</code></pre></div><p><strong><code>allSettled</code> is the right call, and it's also the trap.</strong> One failing handler shouldn't stop the others or fail the booking, so <code>Promise.all</code> is out. But <code>allSettled</code> never throws. A rejected handler vanishes unless you walk the results, with no log line, no alert, and no stack trace. Your invoice handler can fail for a week while <code>confirmBooking</code> returns 200, and finance finds out at month-end close.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!gh4D!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6f6747a9-c0d8-4f1e-86f7-044e919a311e_3760x1976.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!gh4D!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6f6747a9-c0d8-4f1e-86f7-044e919a311e_3760x1976.png 424w, https://substackcdn.com/image/fetch/$s_!gh4D!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6f6747a9-c0d8-4f1e-86f7-044e919a311e_3760x1976.png 848w, https://substackcdn.com/image/fetch/$s_!gh4D!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6f6747a9-c0d8-4f1e-86f7-044e919a311e_3760x1976.png 1272w, https://substackcdn.com/image/fetch/$s_!gh4D!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6f6747a9-c0d8-4f1e-86f7-044e919a311e_3760x1976.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!gh4D!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6f6747a9-c0d8-4f1e-86f7-044e919a311e_3760x1976.png" width="1456" height="765" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/6f6747a9-c0d8-4f1e-86f7-044e919a311e_3760x1976.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:765,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:336150,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://thetshaped.dev/i/213818291?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6f6747a9-c0d8-4f1e-86f7-044e919a311e_3760x1976.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!gh4D!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6f6747a9-c0d8-4f1e-86f7-044e919a311e_3760x1976.png 424w, https://substackcdn.com/image/fetch/$s_!gh4D!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6f6747a9-c0d8-4f1e-86f7-044e919a311e_3760x1976.png 848w, https://substackcdn.com/image/fetch/$s_!gh4D!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6f6747a9-c0d8-4f1e-86f7-044e919a311e_3760x1976.png 1272w, https://substackcdn.com/image/fetch/$s_!gh4D!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6f6747a9-c0d8-4f1e-86f7-044e919a311e_3760x1976.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button class="pencraft pc-reset pencraft icon-container restack-image buttonBase-tqxFUX" tabindex="0" type="button"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button class="pencraft pc-reset pencraft icon-container view-image buttonBase-tqxFUX" tabindex="0" type="button"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>The logging loop is the difference between degraded-and-visible and broken-and-silent, so put a metric and an alert on it too. Write handlers as named functions (<code>return async function createInvoice(event) {&#8230;}</code>) so the log says which one broke. The <code>async</code> wrapper inside <code>map</code> matters too. Without it, a plain function that throws before returning a promise blows up <code>map</code>, and <code>emit</code> itself rejects. Handlers also finish in any order, so two steps that must run in sequence belong in one handler.</p><div><hr></div><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://thetshaped.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Join <strong>30,000+ engineers</strong> getting one practical issue a week on production-grade React, Node, Architecture, and <strong>real AI systems</strong>, and leveling up from Mid &#8594; Senior &#8594; Architect.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div><hr></div><h2>Skip #2: the crash window between commit and handlers</h2><p>The booking is saved, then the event goes out. Two failures live in that gap.</p><p><strong>An event can fire before the data is real.</strong> Say confirming also reserves seats on the trip, inside one transaction. The event fires mid-transaction, the trip turns out to be full, and the whole transaction rolls back. Accounting now holds an invoice for a booking your database says was never confirmed.</p><p>The fix is to collect events during the transaction and emit them only after it commits:</p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;typescript&quot;,&quot;nodeId&quot;:null}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-typescript">const pending: DomainEvent[] = [];

await db.transaction(async (trx) =&gt; {
  const booking = await bookings.markConfirmed(bookingId, trx);
  await trips.reserveSeats(booking.tripId, booking.travelerCount, trx); // throws when full
  pending.push(bookingConfirmed(booking)); // builds the same event as before
});

// committed, so the facts are real now
for (const event of pending) await bus.emit(event);</code></pre></div><p>A rolled-back transaction never reaches the loop, so nothing gets announced. DDD folks know this shape as the aggregate recording events and the unit of work dispatching them after save.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!hwvT!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb69862ca-b52d-480e-9c4b-e592beb1e9ca_3600x2272.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!hwvT!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb69862ca-b52d-480e-9c4b-e592beb1e9ca_3600x2272.png 424w, https://substackcdn.com/image/fetch/$s_!hwvT!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb69862ca-b52d-480e-9c4b-e592beb1e9ca_3600x2272.png 848w, https://substackcdn.com/image/fetch/$s_!hwvT!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb69862ca-b52d-480e-9c4b-e592beb1e9ca_3600x2272.png 1272w, https://substackcdn.com/image/fetch/$s_!hwvT!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb69862ca-b52d-480e-9c4b-e592beb1e9ca_3600x2272.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!hwvT!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb69862ca-b52d-480e-9c4b-e592beb1e9ca_3600x2272.png" width="1456" height="919" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b69862ca-b52d-480e-9c4b-e592beb1e9ca_3600x2272.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:919,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:404980,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://thetshaped.dev/i/213818291?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb69862ca-b52d-480e-9c4b-e592beb1e9ca_3600x2272.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!hwvT!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb69862ca-b52d-480e-9c4b-e592beb1e9ca_3600x2272.png 424w, https://substackcdn.com/image/fetch/$s_!hwvT!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb69862ca-b52d-480e-9c4b-e592beb1e9ca_3600x2272.png 848w, https://substackcdn.com/image/fetch/$s_!hwvT!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb69862ca-b52d-480e-9c4b-e592beb1e9ca_3600x2272.png 1272w, https://substackcdn.com/image/fetch/$s_!hwvT!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb69862ca-b52d-480e-9c4b-e592beb1e9ca_3600x2272.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button class="pencraft pc-reset pencraft icon-container restack-image buttonBase-tqxFUX" tabindex="0" type="button"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button class="pencraft pc-reset pencraft icon-container view-image buttonBase-tqxFUX" tabindex="0" type="button"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p><strong>The process can die right after the commit.</strong> A deploy or an OOM kill is enough. The booking is confirmed, and the email, the invoice, and the Slack ping never happen. Nothing retries, because nothing knows. No in-process pattern fixes this, since the event lived only in the memory of the process that died. A graceful shutdown that drains in-flight handlers makes the window smaller, but it can't help with a <code>SIGKILL</code>.</p><p><strong>A side effect that must survive a crash has to be saved in the same transaction as the data.</strong> The transactional outbox does exactly that. You commit an outbox row together with the booking, and a separate process relays it out. <a href="https://thetshaped.dev/p/outbox-pattern-why-you-need-typescript-postgresql-nodejs">The outbox post</a> covers it end to end. A job queue that lives in Postgres gets you close too. graphile-worker's <code>add_job</code> is a SQL call, so a job queued inside the booking's transaction commits or rolls back with it.</p><p>Until then, treat in-process handlers as best-effort and keep each one safe to rerun, which is what the <code>eventId</code> is for (<a href="https://thetshaped.dev/p/how-to-stop-charging-customers-twice-practical-idempotency-guide-http-message-queues">the idempotency post</a> covers how).</p><div><hr></div><h2>Skip #3: events decouple your code, and your runtime didn't notice</h2><p><code>await bus.emit(...)</code> waits for every handler. A two-second Slack call makes confirming a booking take two seconds, and a hung one hangs the request, so every outbound call in a handler needs a timeout (<a href="https://thetshaped.dev/p/resilience-patterns-keep-backend-nodejs-service-up-production-timeout-retry-backoff-jitter-circuit-breaker">the resilience patterns post</a> covers those). It's the same process and the same event loop. What you decoupled is where the code lives, who owns it, and how you test it. That's worth a lot, and it's less than most people think they're buying.</p><p><code>void bus.emit(event)</code> stops awaiting and returns the response right away, and the bus's own logging still catches failures. The latency left the request, but the crash window from skip #2 got wider, because every deploy now kills whatever handlers were mid-flight. On serverless it's worse, since the platform can freeze your function once the response is sent and a fire-and-forget handler may never finish.</p><p><strong>In-process events give you decoupled code with a shared fate.</strong> Independent retries, delivery guarantees, and separate scaling show up only when the event leaves the process through an outbox, a broker, or a durable job queue. That's also the line between a domain event, which stays inside one service, and an integration event, which other services consume and whose shape becomes a contract you version. An in-process bus hasn't crossed that line for you.</p><div><hr></div><h2>Use events when one fact fans out</h2><p>Reach for domain events when one business fact triggers several independent side effects, the list keeps growing, and a side-effect failure must not fail the operation. Skip them when the side effect is the operation's actual job. Reserving seats is part of confirming a booking, so it belongs inside the transaction. Skip them too when the app is ten endpoints and the indirection costs more than it saves.</p><p>Domain events are still my favorite first move when a function starts collecting side effects. Go in knowing what the bus does for you, and exactly where it stops.</p><div><hr></div><h2>&#128204; TL;DR</h2><ul><li><p><strong>Domain events move side effects out of your core function.</strong> Each one becomes a handler of <code>BookingConfirmed</code>.</p></li><li><p><strong>Don't use a plain <code>EventEmitter</code> for async handlers.</strong> A rejected handler becomes an unhandled rejection, which crashes Node 15+ by default.</p></li><li><p><strong>Skip #1: log what <code>Promise.allSettled</code> returns.</strong> It never throws, so a failed handler disappears while the request keeps returning 200.</p></li><li><p><strong>Skip #2: emit after commit.</strong> That kills phantom events. Surviving a crash after the commit takes a transactional outbox or a job queued in the same transaction.</p></li><li><p><strong>Skip #3: events decouple your code, not your runtime.</strong> Awaited handlers add their latency. Fire-and-forget widens the crash window, and on serverless it may never run.</p></li><li><p><strong>Use events</strong> when one fact fans out into a growing list of side effects. Skip them when the "side effect" is the operation's real job.</p></li></ul><div><hr></div><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://thetshaped.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Subscribe to get practical tips on AI Agents, Agentic Systems, and Software Architecture delivered straight to your inbox:</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div class="pullquote"><div class="captioned-image-container"><figure><div class="image-link image2" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!cmMn!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 424w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 848w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 1272w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!cmMn!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png" width="248" height="207.7" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:670,&quot;width&quot;:800,&quot;resizeWidth&quot;:248,&quot;bytes&quot;:257716,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://thetshaped.dev/i/188025481?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb8ebf3ac-ad2f-415d-9e32-111e959c8e4e_800x800.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!cmMn!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 424w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 848w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 1272w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div></div></div></div></figure></div><p><strong>Follow me on <a href="https://www.linkedin.com/in/petarivanovv9/">LinkedIn</a></strong> | <strong><a href="https://x.com/intent/follow?screen_name=petarivanovv9">Twitter(X)</a></strong> |<strong> <a href="https://www.threads.net/@petarivanovv9">Threads</a></strong></p></div><p>Thank you for supporting this newsletter.</p><p>Consider sharing this post with your friends and get rewards.</p><p>You are the best! &#128591;</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!EvJY!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!EvJY!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 424w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 848w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!EvJY!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg" width="800" height="500" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:500,&quot;width&quot;:800,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:83253,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://thetshaped.dev/i/188025481?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!EvJY!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 424w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 848w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button class="pencraft pc-reset pencraft icon-container restack-image buttonBase-tqxFUX" tabindex="0" type="button"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button class="pencraft pc-reset pencraft icon-container view-image buttonBase-tqxFUX" tabindex="0" type="button"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://thetshaped.dev/p/domain-events-production-failures-nodejs-typescript?utm_source=substack&utm_medium=email&utm_content=share&action=share&quot;,&quot;text&quot;:&quot;Share&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://thetshaped.dev/p/domain-events-production-failures-nodejs-typescript?utm_source=substack&utm_medium=email&utm_content=share&action=share"><span>Share</span></a></p><div class="pullquote"><div class="captioned-image-container"><figure><div class="image-link image2" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!UzWz!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 424w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 848w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 1272w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!UzWz!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif" width="48" height="57.6" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/39609b45-f68c-4557-8772-8e2a45182842_500x600.gif&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:600,&quot;width&quot;:500,&quot;resizeWidth&quot;:48,&quot;bytes&quot;:7028,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:&quot;&quot;,&quot;type&quot;:&quot;image/gif&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://thetshaped.dev/i/188785536?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!UzWz!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 424w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 848w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 1272w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 1456w" sizes="auto, 100vw" loading="lazy"></picture><div></div></div></div></figure></div><p style="text-align: center;"><em><strong>Press the Like button if you found this post helpful</strong></em></p></div>]]></content:encoded></item><item><title><![CDATA[Your LLM Judge Is Untested Code]]></title><description><![CDATA[Your agent has tests. The thing grading them doesn't. Five rules for a judge you can trust.]]></description><link>https://thetshaped.dev/p/your-llm-judge-is-untested-code-five-rules-done-right</link><guid isPermaLink="false">https://thetshaped.dev/p/your-llm-judge-is-untested-code-five-rules-done-right</guid><pubDate>Tue, 20 Oct 2026 12:15:40 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/c27a6d5b-246f-409e-84c8-bac3a4604d61_2912x1632.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://thetshaped.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption"><span data-color="#9900ff" style="color: rgb(153, 0, 255);">Get my Full-Stack JavaScript Architect Bundle for FREE on newsletter signup:</span></p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><p>Every agent eval suite has one component nobody tests. You wrote careful scenarios, mocked the tools, and ran each one five times. Then you handed the final verdict to another LLM call and believed it.</p><p>That call is the judge. A wrong judge doesn&#8217;t fail your suite. It passes replies that should have failed, and nobody hunts for bugs in a green build.</p><p>Treat the judge like any other component. Give it a narrow contract and tests that pass before its grades count. Here are five rules, with examples in Python. The same shapes work in a TypeScript suite.</p><ul><li><p><em><a href="https://thetshaped.dev/p/your-llm-judge-is-untested-code-five-rules-done-right/?action=share">Share this post</a> &amp; I&#8217;ll send you some rewards for the referrals.</em></p></li></ul><div><hr></div><h2><strong><a href="https://www.alldayai.com/?utm_source=the_t_shaped_dev&amp;utm_medium=newsletter&amp;utm_campaign=all_day_ai_2026&amp;utm_content=petar_ivanov#register">Your AI team has enough frameworks. Here&#8217;s what ships. (Partner)</a></strong></h2><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://www.alldayai.com/?utm_source=the_t_shaped_dev&amp;utm_medium=newsletter&amp;utm_campaign=all_day_ai_2026&amp;utm_content=petar_ivanov#register" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!pDdn!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb3e219d2-32b7-412c-8cdc-238b891a1ce5_1734x907.png 424w, https://substackcdn.com/image/fetch/$s_!pDdn!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb3e219d2-32b7-412c-8cdc-238b891a1ce5_1734x907.png 848w, https://substackcdn.com/image/fetch/$s_!pDdn!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb3e219d2-32b7-412c-8cdc-238b891a1ce5_1734x907.png 1272w, https://substackcdn.com/image/fetch/$s_!pDdn!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb3e219d2-32b7-412c-8cdc-238b891a1ce5_1734x907.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!pDdn!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb3e219d2-32b7-412c-8cdc-238b891a1ce5_1734x907.png" width="1456" height="762" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b3e219d2-32b7-412c-8cdc-238b891a1ce5_1734x907.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:762,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:2795168,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:&quot;https://www.alldayai.com/?utm_source=the_t_shaped_dev&amp;utm_medium=newsletter&amp;utm_campaign=all_day_ai_2026&amp;utm_content=petar_ivanov#register&quot;,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://thetshaped.dev/i/221088659?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb3e219d2-32b7-412c-8cdc-238b891a1ce5_1734x907.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!pDdn!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb3e219d2-32b7-412c-8cdc-238b891a1ce5_1734x907.png 424w, https://substackcdn.com/image/fetch/$s_!pDdn!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb3e219d2-32b7-412c-8cdc-238b891a1ce5_1734x907.png 848w, https://substackcdn.com/image/fetch/$s_!pDdn!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb3e219d2-32b7-412c-8cdc-238b891a1ce5_1734x907.png 1272w, https://substackcdn.com/image/fetch/$s_!pDdn!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb3e219d2-32b7-412c-8cdc-238b891a1ce5_1734x907.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button class="pencraft pc-reset pencraft icon-container restack-image buttonBase-tqxFUX" tabindex="0" type="button"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button class="pencraft pc-reset pencraft icon-container view-image buttonBase-tqxFUX" tabindex="0" type="button"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Your team does not need another framework comparison. <a href="https://www.alldayai.com/?utm_source=the_t_shaped_dev&amp;utm_medium=newsletter&amp;utm_campaign=all_day_ai_2026&amp;utm_content=petar_ivanov#register">All Day AI</a> brings 122 builders and technical leaders together to explain what survived production and what failed.</p><p><strong>Agenda highlights:</strong></p><ul><li><p>See what broke when teams put agent swarms into production.</p></li><li><p>Learn why prompt injection is an architecture problem.</p></li><li><p>Follow how AI is changing software development from code to context.</p></li></ul><p>The BUILD, LEAD, SECURE, and SHIP tracks also cover evals, model routing, permissions, AI coding costs, and failure-safe design. Sessions run for 30 minutes, so you can focus on the problems your team has now.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.alldayai.com/?utm_source=the_t_shaped_dev&amp;utm_medium=newsletter&amp;utm_campaign=all_day_ai_2026&amp;utm_content=petar_ivanov#register&quot;,&quot;text&quot;:&quot;Register Free&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.alldayai.com/?utm_source=the_t_shaped_dev&amp;utm_medium=newsletter&amp;utm_campaign=all_day_ai_2026&amp;utm_content=petar_ivanov#register"><span>Register Free</span></a></p><p>(Thanks to <a href="https://www.alldayai.com/?utm_source=the_t_shaped_dev&amp;utm_medium=newsletter&amp;utm_campaign=all_day_ai_2026&amp;utm_content=petar_ivanov#register">All Day AI</a> for partnering on this post.)</p><div><hr></div><h2>1. Write criteria a human could check in five seconds</h2><p>"The response is helpful and professional" is a vibe, and a judge will wave almost anything through it. "The response says the card will be blocked" is a fact you can check in five seconds. If a criterion takes longer, split it.</p><p>Keep three or four criteria per step, and ask about one criterion per judge call. When one prompt holds four criteria, the verdict on one sways the verdict on the next (<a href="https://arxiv.org/abs/2405.01724">Stureborg et al. 2024</a> measured this anchoring effect).</p><p>Store criteria as named rubrics in YAML, so every scenario reuses the same "card-blocking reply". Load them through a Pydantic model with <code>extra="forbid"</code>. A typo in a field name then fails on load instead of quietly checking nothing.</p><div><hr></div><h2>2. Never ask the judge what code already knows</h2><p>A judge is the slowest, most expensive, and least reliable check you have. Plain code answers which tools ran, what arguments the API got, and whether a refund total adds up. It's exact and free, and your mocked tools already log their calls.</p><p>Save the judge for meaning. "Your card ending in 4821 is now blocked" and "Done, I've blocked it" are both correct, and no regex covers that space. A criterion the judge keeps getting wrong usually belongs in code.</p><div><hr></div><h2>3. Count the threshold, never ask for a score</h2><p>Sooner or later someone wants a number. Don't get it from the judge. In the same Stureborg study, models asked for a 1-100 rating put most replies between 70 and 100, bunched on round numbers, and shifted when the prompt was reworded.</p><p>Count binary verdicts instead:</p><ul><li><p><strong>A step:</strong> 3 of 4 criteria held &#8594; 75%.</p></li><li><p><strong>A scenario:</strong> 4 of 5 runs passed &#8594; 80%.</p></li><li><p><strong>The suite:</strong> 46 of 50 scenarios green &#8594; 92%, the number you gate a release on.</p></li></ul><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!TJMF!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff245f834-5883-450f-aed3-b51d2f0eae7b_3680x2320.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!TJMF!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff245f834-5883-450f-aed3-b51d2f0eae7b_3680x2320.png 424w, https://substackcdn.com/image/fetch/$s_!TJMF!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff245f834-5883-450f-aed3-b51d2f0eae7b_3680x2320.png 848w, https://substackcdn.com/image/fetch/$s_!TJMF!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff245f834-5883-450f-aed3-b51d2f0eae7b_3680x2320.png 1272w, https://substackcdn.com/image/fetch/$s_!TJMF!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff245f834-5883-450f-aed3-b51d2f0eae7b_3680x2320.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!TJMF!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff245f834-5883-450f-aed3-b51d2f0eae7b_3680x2320.png" width="1456" height="918" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/f245f834-5883-450f-aed3-b51d2f0eae7b_3680x2320.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:918,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:456537,&quot;alt&quot;:&quot;Count the percentage from binary verdicts instead of asking the judge for a 1-100 score&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Count the percentage from binary verdicts instead of asking the judge for a 1-100 score" title="Count the percentage from binary verdicts instead of asking the judge for a 1-100 score" srcset="https://substackcdn.com/image/fetch/$s_!TJMF!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff245f834-5883-450f-aed3-b51d2f0eae7b_3680x2320.png 424w, https://substackcdn.com/image/fetch/$s_!TJMF!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff245f834-5883-450f-aed3-b51d2f0eae7b_3680x2320.png 848w, https://substackcdn.com/image/fetch/$s_!TJMF!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff245f834-5883-450f-aed3-b51d2f0eae7b_3680x2320.png 1272w, https://substackcdn.com/image/fetch/$s_!TJMF!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff245f834-5883-450f-aed3-b51d2f0eae7b_3680x2320.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button class="pencraft pc-reset pencraft icon-container restack-image buttonBase-tqxFUX" tabindex="0" type="button"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button class="pencraft pc-reset pencraft icon-container view-image buttonBase-tqxFUX" tabindex="0" type="button"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>A drop in any of those traces back to the criterion that flipped.</p><p>Two caveats. Mark the must-hold criteria, because on a plain average, an agent that never blocks the card still scores 75%. One failed must-hold fails the step.</p><p>For something that isn't binary, like tone, use three labels (fails, partial, holds) rather than a hundred. A reviewer can tell "partial" from "holds". Nobody can defend 78 over 82.</p><div><hr></div><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://thetshaped.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Join <strong>30,000+ engineers</strong> getting one practical issue a week on production-grade React, Node, Architecture, and <strong>real AI systems</strong>, and leveling up from Mid &#8594; Senior &#8594; Architect.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div><hr></div><h2>4. Calibrate the judge, then gate on it</h2><p>This rule makes the rest trustworthy. A calibration set is a handful of hand-written replies, some clearly good and some clearly bad, each labeled with the verdict every criterion should produce. Run the judge over them with no agent and no mocks, because the judge is what's under test:</p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;python&quot;,&quot;nodeId&quot;:null}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-python"># evals/judge.py
from typing import Literal

from pydantic import BaseModel


class Verdict(BaseModel):
    reason: str                     # what you read when a check fails
    label: Literal["pass", "fail"]  # the only two answers the judge can give


def judge(reply: str, criterion: str) -&gt; Verdict:
    """One model call, one criterion, output parsed straight into a Verdict."""
    ...


# tests/evals/test_calibration.py
import pytest

from evals.calibration import load_calibration_cases
from evals.judge import judge


@pytest.mark.parametrize("case", load_calibration_cases(), ids=lambda c: c.name)
def test_judge_matches_hand_labels(case):
    for criterion, expected in case.expected_labels.items():
        verdict = judge(case.reply, criterion)
        assert verdict.label == expected, f"{criterion}: {verdict.reason}"</code></pre></div><p>With that <code>Literal</code> as the output schema, the judge can't answer "mostly passes" or "8/10". Keep <code>reason</code> to one sentence. The structured-outputs docs warn that a field asking for step-by-step reasoning can trigger a refusal on current Claude models.</p><p>This test catches <strong>calibration drift</strong>. Someone rewords a criterion to fix one case, and a known-bad reply starts passing. Without the test, nothing tells you.</p><p>Small changes move judges a lot. Swapping only the order of two answers made GPT-4 flip its verdict on 46.3% of Vicuna-13B versus ChatGPT pairs, two closely matched models (<a href="https://arxiv.org/abs/2305.17926">Wang et al. 2023</a>).</p><p>In a study of 13 judge models, most leaned toward "correct" when unsure (<a href="https://arxiv.org/abs/2406.12624">Judging the Judges</a>). Lenient is the direction that turns a suite green. Those were older models, and your calibration set tells you how yours behaves.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!TTsG!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77608637-db95-4448-aca8-3d0cfa3f5962_3680x1816.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!TTsG!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77608637-db95-4448-aca8-3d0cfa3f5962_3680x1816.png 424w, https://substackcdn.com/image/fetch/$s_!TTsG!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77608637-db95-4448-aca8-3d0cfa3f5962_3680x1816.png 848w, https://substackcdn.com/image/fetch/$s_!TTsG!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77608637-db95-4448-aca8-3d0cfa3f5962_3680x1816.png 1272w, https://substackcdn.com/image/fetch/$s_!TTsG!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77608637-db95-4448-aca8-3d0cfa3f5962_3680x1816.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!TTsG!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77608637-db95-4448-aca8-3d0cfa3f5962_3680x1816.png" width="1456" height="719" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/77608637-db95-4448-aca8-3d0cfa3f5962_3680x1816.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:719,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:394533,&quot;alt&quot;:&quot;Calibration runs as CI job 1; any mismatch with the hand labels skips the scenario job&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Calibration runs as CI job 1; any mismatch with the hand labels skips the scenario job" title="Calibration runs as CI job 1; any mismatch with the hand labels skips the scenario job" srcset="https://substackcdn.com/image/fetch/$s_!TTsG!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77608637-db95-4448-aca8-3d0cfa3f5962_3680x1816.png 424w, https://substackcdn.com/image/fetch/$s_!TTsG!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77608637-db95-4448-aca8-3d0cfa3f5962_3680x1816.png 848w, https://substackcdn.com/image/fetch/$s_!TTsG!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77608637-db95-4448-aca8-3d0cfa3f5962_3680x1816.png 1272w, https://substackcdn.com/image/fetch/$s_!TTsG!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77608637-db95-4448-aca8-3d0cfa3f5962_3680x1816.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button class="pencraft pc-reset pencraft icon-container restack-image buttonBase-tqxFUX" tabindex="0" type="button"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button class="pencraft pc-reset pencraft icon-container view-image buttonBase-tqxFUX" tabindex="0" type="button"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Run calibration as its own CI job, before the scenarios. If the judge fails, CI skips the scenario job, and you don't burn minutes of agent runs on a judge you know is broken.</p><div><hr></div><h2>5. Grade with a different model, and let calibration size it</h2><p>LLM judges <a href="https://arxiv.org/abs/2404.13076">recognize and favor their own writing</a>. <a href="https://arxiv.org/abs/2404.13076">Panickssery et al.</a> found GPT-4 and Llama 2 tell their own text from other models' text better than chance, and the bias grew when fine-tuning improved that recognition.</p><p>Let the agent's model grade its own replies, and that bias leans toward passing. Use a different model, ideally from another family, because judges also favor related models (<a href="https://arxiv.org/abs/2502.01534">Preference Leakage</a>).</p><p>A bigger judge isn't always a better one. A narrow yes/no check is easier than writing the reply, though in Judging the Judges only the largest judges came close to human agreement.</p><p>Judge calls also add up. Four criteria, five runs, and 50 scenarios make 1,000 calls for one graded step. Your calibration set settles the size question. If a cheaper judge passes it, it's good enough.</p><p>One more known bias. <a href="https://arxiv.org/abs/2306.05685">MT-Bench</a> found judges swayed by the order they read two replies in, and two of its three judges fell for padded answers. Grading one reply at a time removes the order problem, and binary criteria make length matter less.</p><div><hr></div><h2>A green suite should mean the agent passed</h2><p>The judge decides what "working" means. That makes it the last thing to trust on faith. Give it a narrow job, make it count instead of guess, and test it before it grades anything.</p><p>Do that, and a green suite means the agent passed. Skip it, and a green suite means the judge said so.</p><div><hr></div><h2>&#128204; TL;DR</h2><ul><li><p><strong>Criteria a human can check in five seconds.</strong> Three or four per step, one per judge call.</p></li><li><p><strong>Code checks what code knows.</strong> Tool calls, fields, and totals get exact checks. The judge grades meaning.</p></li><li><p><strong>Count verdicts instead of asking for a score.</strong> Tally pass/fail across criteria, runs, and scenarios, and mark the must-holds.</p></li><li><p><strong>Calibrate and gate.</strong> Hand-labeled replies run first in CI, and a failing judge skips the scenarios.</p></li><li><p><strong>Grade with a different model</strong>, ideally from another family, sized by calibration.</p></li></ul><div><hr></div><p><em>Split out of <a href="https://thetshaped.dev/p/the-3-layers-of-testing-ai-agents-unittests-mocking-alerts-judge-evals-production-agentic-systems">The 3 Layers of Testing AI Agents</a>, which covers the full three-layer setup these judges plug into.</em></p><div><hr></div><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://thetshaped.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Subscribe to get practical tips on AI Agents, Agentic Systems, and Software Architecture delivered straight to your inbox:</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div class="pullquote"><div class="captioned-image-container"><figure><div class="image-link image2" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!cmMn!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 424w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 848w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 1272w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!cmMn!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png" width="248" height="207.7" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:670,&quot;width&quot;:800,&quot;resizeWidth&quot;:248,&quot;bytes&quot;:257716,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://thetshaped.dev/i/188025481?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb8ebf3ac-ad2f-415d-9e32-111e959c8e4e_800x800.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!cmMn!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 424w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 848w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 1272w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div></div></div></div></figure></div><p><strong>Follow me on <a href="https://www.linkedin.com/in/petarivanovv9/">LinkedIn</a></strong> | <strong><a href="https://x.com/intent/follow?screen_name=petarivanovv9">Twitter(X)</a></strong> |<strong> <a href="https://www.threads.net/@petarivanovv9">Threads</a></strong></p></div><p>Thank you for supporting this newsletter.</p><p>Consider sharing this post with your friends and get rewards.</p><p>You are the best! &#128591;</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!EvJY!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!EvJY!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 424w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 848w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!EvJY!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg" width="800" height="500" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:500,&quot;width&quot;:800,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:83253,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://thetshaped.dev/i/188025481?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!EvJY!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 424w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 848w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button class="pencraft pc-reset pencraft icon-container restack-image buttonBase-tqxFUX" tabindex="0" type="button"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button class="pencraft pc-reset pencraft icon-container view-image buttonBase-tqxFUX" tabindex="0" type="button"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://thetshaped.dev/p/your-llm-judge-is-untested-code-five-rules-done-right?utm_source=substack&utm_medium=email&utm_content=share&action=share&quot;,&quot;text&quot;:&quot;Share&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://thetshaped.dev/p/your-llm-judge-is-untested-code-five-rules-done-right?utm_source=substack&utm_medium=email&utm_content=share&action=share"><span>Share</span></a></p><div class="pullquote"><div class="captioned-image-container"><figure><div class="image-link image2" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!UzWz!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 424w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 848w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 1272w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!UzWz!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif" width="48" height="57.6" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/39609b45-f68c-4557-8772-8e2a45182842_500x600.gif&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:600,&quot;width&quot;:500,&quot;resizeWidth&quot;:48,&quot;bytes&quot;:7028,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:&quot;&quot;,&quot;type&quot;:&quot;image/gif&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://thetshaped.dev/i/188785536?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!UzWz!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 424w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 848w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 1272w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 1456w" sizes="auto, 100vw" loading="lazy"></picture><div></div></div></div></figure></div><p style="text-align: center;"><em><strong>Press the Like button if you found this post helpful</strong></em></p></div>]]></content:encoded></item><item><title><![CDATA[I Wrote a Book on Node.js and Never Told You]]></title><description><![CDATA[The Conscious Node: 157 tips for Node.js backends that hold up in production. Not sure yet? The first three tips are free.]]></description><link>https://thetshaped.dev/p/i-wrote-a-book-on-nodejs-and-never</link><guid isPermaLink="false">https://thetshaped.dev/p/i-wrote-a-book-on-nodejs-and-never</guid><dc:creator><![CDATA[Petar Ivanov]]></dc:creator><pubDate>Tue, 13 Oct 2026 16:29:12 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!9lD-!,w_256,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7f66bb7c-c96f-4c71-ba2d-d561152c83a2_600x600.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>I released The Conscious Node, a book about building Node.js backends that hold up in production. It&#8217;s been on my website for a while, and I never announced it here. That&#8217;s an odd miss, because you&#8217;re the readers I wrote it for.</p><p>So this is the proper announcement.</p><h2>What&#8217;s inside</h2><p>157 tips across 10 chapters, about 250 pages. Every tip has the same four parts: the context, the code to avoid, the code to prefer, and a one-line takeaway.</p><ol><li><p>Application structure and architecture</p></li><li><p>Code design and patterns</p></li><li><p>Error handling and logging</p></li><li><p>Security</p></li><li><p>Testing</p></li><li><p>API design (REST and GraphQL)</p></li><li><p>Database and data</p></li><li><p>Performance and production</p></li><li><p>Tooling</p></li><li><p>The 10 Rules of Senior Node</p></li></ol><h2>One tip: the retry that charges your customer twice</h2><p>A client sends &#8220;create order&#8221;. The server creates the order, but the response gets lost on the way back. The client retries, and now the customer has two orders.</p><p>Retried GETs are harmless. Retried POSTs aren&#8217;t, unless the endpoint is idempotent. Tip 6.13 fixes it with an <code>Idempotency-Key</code> header: the client sends a unique key with the write, and the server uses that key to recognize a retry instead of creating a second order.</p><p>That&#8217;s one tip. The other 156 work the same way.</p><h2>Two ways to get it</h2><ul><li><p><strong>Just the Book, $29.</strong> PDF, MOBI and EPUB, with lifetime updates.</p></li><li><p><strong>The Complete Package, $59.</strong> The book plus three companion repos: book-examples runs every tip as an Avoid/Prefer demo, case-studies has 6 deep-dive services, and express-boilerplate is a production Express 5 starter. Each repo ships with CLAUDE.md, AGENTS.md and .cursorrules, so Claude Code, Cursor and Copilot write code that follows the book.</p></li></ul><p>Both come with a 30-day money-back guarantee.</p><p><a href="https://petarivanov.tech/books/conscious-node/?utm_source=thetshaped&amp;utm_medium=newsletter&amp;utm_campaign=conscious-node-announce">Check out The Conscious Node</a></p><h2>Not sure yet? Read the free sample first</h2><p>The first three tips of Chapter 1 are free, as a PDF and EPUB: structure the app in modules, start with a modular monolith, and split routes, services and repositories into layers. They look basic. Chapter 1 is where most Node services go wrong, before the first endpoint ships.</p><p>Read them. If the way the book thinks clicks for you, you&#8217;ll know in ten minutes.</p><p><a href="https://petarivanov.tech/books/conscious-node/?utm_source=thetshaped&amp;utm_medium=newsletter&amp;utm_campaign=conscious-node-announce#free-chapter">Get the 3 free tips</a></p><h2>One favor</h2><p>If you know a backend engineer who&#8217;d get something out of this, forward them this email or send them the link.</p><p>Thanks for reading,<br>Petar</p>]]></content:encoded></item><item><title><![CDATA[Your AI Agent Has No Stack Trace. Here's How to Debug It Anyway.]]></title><description><![CDATA[How to make LLM apps debuggable: traces, correlation IDs, and the five numbers on the dashboard.]]></description><link>https://thetshaped.dev/p/debugging-testing-ai-agents-llm-apps-4-decisions-observability-traces-correlation-ids-checklist</link><guid isPermaLink="false">https://thetshaped.dev/p/debugging-testing-ai-agents-llm-apps-4-decisions-observability-traces-correlation-ids-checklist</guid><pubDate>Tue, 06 Oct 2026 12:15:40 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/e2d34d42-4de8-48ea-960a-e7129dafe823_2912x1632.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://thetshaped.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption"><span data-color="#9900ff" style="color: rgb(153, 0, 255);">Get my Full-Stack JavaScript Architect Bundle for FREE on newsletter signup:</span></p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><p>A user files a ticket. <em>"The assistant said my expense was rejected because of the travel policy. That's not our policy."</em> No stack trace. You ask the same question and get the right answer. Behind that one wrong sentence sit several model calls and a couple of tool calls, and you kept none of them.</p><p>I work on AI agents in production, and this is the part that surprises people. A normal bug lives in your code, so you read the code and the logs and work backwards. With an LLM feature, the logic that failed lives in a prompt, a retrieved document, and the model's reply to them. That reply happened once, and if you didn't record it, there is nothing to debug.</p><p>So observability for an LLM feature is something you decide before launch. Four decisions, and here's what I'd pick for each.</p><ul><li><p><em><a href="https://thetshaped.dev/p/debugging-testing-ai-agents-llm-apps-4-decisions-observability-traces-correlation-ids-checklist/?action=share">Share this post</a> &amp; I&#8217;ll send you some rewards for the referrals.</em></p></li></ul><div><hr></div><h2><a href="https://sentry.io/welcome/?utm_source=tshapeddev&amp;utm_medium=paid-community&amp;utm_campaign=general-fy27q3-evergreen&amp;utm_content=partner-homepage-trysentry&amp;code=tshapeddev">Your agent can only fix what it can see (Partner)</a></h2><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://sentry.io/welcome/?utm_source=tshapeddev&amp;utm_medium=paid-community&amp;utm_campaign=general-fy27q3-evergreen&amp;utm_content=partner-homepage-trysentry&amp;code=tshapeddev" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!XlcB!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcab8a997-4ec8-4359-a734-d79b7fd59064_1816x900.jpeg 424w, https://substackcdn.com/image/fetch/$s_!XlcB!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcab8a997-4ec8-4359-a734-d79b7fd59064_1816x900.jpeg 848w, https://substackcdn.com/image/fetch/$s_!XlcB!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcab8a997-4ec8-4359-a734-d79b7fd59064_1816x900.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!XlcB!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcab8a997-4ec8-4359-a734-d79b7fd59064_1816x900.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!XlcB!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcab8a997-4ec8-4359-a734-d79b7fd59064_1816x900.jpeg" width="1456" height="722" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/cab8a997-4ec8-4359-a734-d79b7fd59064_1816x900.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:722,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1081723,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:&quot;https://sentry.io/welcome/?utm_source=tshapeddev&amp;utm_medium=paid-community&amp;utm_campaign=general-fy27q3-evergreen&amp;utm_content=partner-homepage-trysentry&amp;code=tshapeddev&quot;,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://thetshaped.dev/i/213818356?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcab8a997-4ec8-4359-a734-d79b7fd59064_1816x900.jpeg&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!XlcB!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcab8a997-4ec8-4359-a734-d79b7fd59064_1816x900.jpeg 424w, https://substackcdn.com/image/fetch/$s_!XlcB!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcab8a997-4ec8-4359-a734-d79b7fd59064_1816x900.jpeg 848w, https://substackcdn.com/image/fetch/$s_!XlcB!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcab8a997-4ec8-4359-a734-d79b7fd59064_1816x900.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!XlcB!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcab8a997-4ec8-4359-a734-d79b7fd59064_1816x900.jpeg 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button class="pencraft pc-reset pencraft icon-container restack-image buttonBase-tqxFUX" tabindex="0" type="button"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button class="pencraft pc-reset pencraft icon-container view-image buttonBase-tqxFUX" tabindex="0" type="button"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p><a href="https://sentry.io/welcome/?utm_source=tshapeddev&amp;utm_medium=paid-community&amp;utm_campaign=general-fy27q3-evergreen&amp;utm_content=partner-homepage-trysentry&amp;code=tshapeddev">Sentry</a> gives every coding agent something worth reading: traces, breadcrumbs, logs, and session replays. <a href="https://sentry.io/product/seer/?utm_source=tshapeddev&amp;utm_medium=paid-community&amp;utm_campaign=seer-fy27q3-evergreen&amp;utm_content=partner-product-trysentry&amp;code=tshapeddev">Seer</a> turns that context into a verified root cause, with the file and line, checked against your actual code.</p><p>From there, let Seer open the pull request, or hand the analysis to Cursor, Claude Code, or Copilot through the <a href="https://sentry.io/welcome/?utm_source=tshapeddev&amp;utm_medium=paid-community&amp;utm_campaign=general-fy27q3-evergreen&amp;utm_content=partner-homepage-trysentry&amp;code=tshapeddev">Sentry MCP</a> server. Your data is never used to train models.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://sentry.io/signup/?utm_source=tshapeddev&amp;utm_medium=paid-community&amp;utm_campaign=general-fy27q3-evergreen&amp;utm_content=partner-signup-trysentry&amp;code=tshapeddev&quot;,&quot;text&quot;:&quot;Try For Free&quot;,&quot;action&quot;:null,&quot;class&quot;:&quot;button-wrapper&quot;}" data-component-name="ButtonCreateButton"><a class="button primary button-wrapper" href="https://sentry.io/signup/?utm_source=tshapeddev&amp;utm_medium=paid-community&amp;utm_campaign=general-fy27q3-evergreen&amp;utm_content=partner-signup-trysentry&amp;code=tshapeddev"><span>Try For Free</span></a></p><p>(Thanks to <a href="https://sentry.io/welcome/?utm_source=tshapeddev&amp;utm_medium=paid-community&amp;utm_campaign=general-fy27q3-evergreen&amp;utm_content=partner-homepage-trysentry&amp;code=tshapeddev">Sentry</a> for partnering on this post.)</p><div><hr></div><h2>1. Trace every step of every request</h2><p>One user question is rarely one model call. An agent classifies the intent, routes to a sub-agent, retrieves the policy docs, calls a tool, and writes the answer. Record all of it as one tree per request, with the exact prompt and raw response of every model call and the arguments and result of every tool call.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!TCil!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F34ba9dbf-e624-4281-a7c2-5f0c07fd2ef0_1734x898.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!TCil!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F34ba9dbf-e624-4281-a7c2-5f0c07fd2ef0_1734x898.png 424w, https://substackcdn.com/image/fetch/$s_!TCil!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F34ba9dbf-e624-4281-a7c2-5f0c07fd2ef0_1734x898.png 848w, https://substackcdn.com/image/fetch/$s_!TCil!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F34ba9dbf-e624-4281-a7c2-5f0c07fd2ef0_1734x898.png 1272w, https://substackcdn.com/image/fetch/$s_!TCil!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F34ba9dbf-e624-4281-a7c2-5f0c07fd2ef0_1734x898.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!TCil!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F34ba9dbf-e624-4281-a7c2-5f0c07fd2ef0_1734x898.png" width="1456" height="754" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/34ba9dbf-e624-4281-a7c2-5f0c07fd2ef0_1734x898.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:754,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:181482,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://thetshaped.dev/i/213818356?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F34ba9dbf-e624-4281-a7c2-5f0c07fd2ef0_1734x898.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!TCil!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F34ba9dbf-e624-4281-a7c2-5f0c07fd2ef0_1734x898.png 424w, https://substackcdn.com/image/fetch/$s_!TCil!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F34ba9dbf-e624-4281-a7c2-5f0c07fd2ef0_1734x898.png 848w, https://substackcdn.com/image/fetch/$s_!TCil!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F34ba9dbf-e624-4281-a7c2-5f0c07fd2ef0_1734x898.png 1272w, https://substackcdn.com/image/fetch/$s_!TCil!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F34ba9dbf-e624-4281-a7c2-5f0c07fd2ef0_1734x898.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button class="pencraft pc-reset pencraft icon-container restack-image buttonBase-tqxFUX" tabindex="0" type="button"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button class="pencraft pc-reset pencraft icon-container view-image buttonBase-tqxFUX" tabindex="0" type="button"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p><strong>The tree finds the cause, not just the symptom.</strong> For the expense ticket, the wrong answer shows up at step 5, but the trace shows retrieval pulling an old travel policy at step 3. Without the tree you stare at step 5 for a day. With it you're done in ten minutes.</p><p><strong>Tag every trace with the model ID, the prompt version, and the correlation ID from decision 2.</strong> You'll need those tags the moment a number moves in decision 3.</p><p>Any LLM tracing tool records this tree, and so does OpenTelemetry. Decide three settings now, because they hurt to change later.</p><ul><li><p><strong>Content capture.</strong> OpenTelemetry's GenAI conventions (still in development) make prompt and response content opt-in. The default trace has token counts and timings but no conversation.</p></li><li><p><strong>Sampling.</strong> Trace 100% at launch. At high volume, keep every error and sample the successes. That takes tail sampling, which decides once the request is done. Head sampling decides at the start, before anything has failed.</p></li><li><p><strong>Privacy.</strong> Prompts carry user data. Decide redaction, access, and retention before compliance decides for you.</p></li></ul><div><hr></div><h2>2. Give every request one ID, and put the trace link in every log line</h2><p>When support says <em>"user X got a weird answer around 3 PM Tuesday,"</em> the correlation ID turns that into one search. It also lets you follow the request into systems the tracer can't see.</p><p><strong>Mint the ID at the edge and carry it everywhere.</strong> If the caller sends one, check its format before you trust it. Then put it in every log line, service call, queue message header, and trace. Python middleware can set a <code>contextvars.ContextVar</code> once, and async code reads it anywhere without passing it through forty functions. Watch out for <code>loop.run_in_executor</code>, which runs your function without that context, so the ID quietly disappears. <code>asyncio.to_thread</code> copies it. (Node's <code>AsyncLocalStorage</code> does the same job.)</p><p><strong>Put the trace link in the logger's context, too.</strong> On the agent system I've worked on, the logger middleware builds a link to the conversation's trace session from the thread ID in the request, before any model call runs. Every log line carries that link. On-call goes from any error to the full transcript in one click. If I could keep only one piece of this setup, I'd keep that click.</p><div><hr></div><h2>3. Watch five numbers, split by model and prompt version</h2><p>Traces tell you what happened in one request. Metrics tell you whether something changed. LLM features drift after a prompt tweak, a model upgrade, or a shift in what users ask, and none of that throws an exception. These five numbers catch it.</p><ol><li><p><strong>Cost per request, in money.</strong> Uncached input, cache writes, cache reads, and output each have their own price, so "total tokens &#215; one price" is wrong once caching is on. The newest models also think by default, billed as output even when hidden. Compute cost from the usage the API reports, summed over every call in the request.</p></li><li><p><strong>Latency, p95, per step.</strong> The end-to-end number tells you users are hurting. The per-step numbers tell you where. For streaming, track time to first token too, because that's the wait users actually feel.</p></li><li><p><strong>Error rate, quiet failures included.</strong> Count retries that ran out, tool failures the agent worked around, and fallbacks taken, on top of the 500s. Count stop reasons too, because a refusal and a reply cut off at <code>max_tokens</code> both come back as HTTP 200. If the fallback fires 30% of the time, your feature is 30% worse than you think.</p></li><li><p><strong>Schema-valid rate.</strong> The share of outputs that pass your validation on the first try, and the most sensitive alarm for prompt and model changes. With the provider's structured-output mode the JSON almost always parses, so count your business-rule checks instead.</p></li><li><p><strong>Cache-read rate.</strong> The share of input tokens served from the prompt cache. One edit to the cached prefix can multiply input cost overnight with no change in behavior, and nothing else here tells you why.</p></li></ol><p><strong>Check how your provider counts input before you compute the cache rate.</strong> Anthropic's <code>input_tokens</code> covers only the uncached part. OpenAI counts cached tokens inside its input total, and OpenTelemetry follows that convention. With Anthropic's fields:</p><pre><code>u = response.usage
total_input = u.input_tokens + u.cache_creation_input_tokens + u.cache_read_input_tokens
cache_read_rate = u.cache_read_input_tokens / total_input</code></pre><p>Divide by <code>input_tokens</code> alone and a well-cached agent reports rates above 100%.</p><p><strong>Baseline all five for a week, then alert on changes rather than fixed thresholds.</strong> Split every chart by the model and prompt-version tags from decision 1. That turns "validity dropped" into "validity dropped on prompt v14".</p><div><hr></div><h2>4. Log the system's verdicts, not "call completed"</h2><p>Traces hold the transcripts and metrics hold the trends. Logs hold your system's judgments about the model, and they're what you'll search during an incident.</p><p><strong>Log every decision your code makes about the model as a structured event.</strong> Validation failed, retry fired, fallback taken, guardrail triggered, sent to human review, each with the specifics as fields. A user's thumbs-down is a verdict too. Every event carries the correlation ID and the trace link:</p><pre><code>{"event": "llm.validation_failed", "severity": "warning",
 "correlation_id": "7f3a", "prompt_version": "expense-policy-v14",
 "field": "policy_status", "rule": "enum", "got": "partially_compliant",
 "trace_url": "https://traces.example.com/t/7f3a"}</code></pre><p>Skip <code>"LLM call completed successfully"</code> at info level, ten thousand times an hour. It carries no decision and buries the one line that mattered. That detail belongs in the trace.</p><p>Put the four together and debugging gets boring. The schema-valid alert fires on prompt v14. You filter logs to <code>llm.validation_failed</code>, see the same new enum value in every failure, and click three trace links. Tuesday's prompt change added "partially compliant" as an option. No guessing.</p><div><hr></div><h2>You're debugging conversations now</h2><p>The logic that fails in an LLM feature is a prompt and a reply that existed once. Either you kept the transcript or you'll never know what happened. The setup takes a day or two before launch, or you build it anyway, during the incident.</p><div><hr></div><h2>&#128204; TL;DR</h2><ul><li><p><strong>LLM bugs have no stack trace.</strong> The failing logic is a prompt and a reply that happened once, so record them from day one.</p></li><li><p><strong>Trace every step</strong> as one tree per request, with exact prompts and responses, tagged with model and prompt version. OpenTelemetry makes content capture opt-in, and "keep every error" needs tail sampling.</p></li><li><p><strong>One ID end to end</strong>, through logs, services, queues, and traces, plus the trace link in every log line.</p></li><li><p><strong>Five numbers:</strong> cost per request, p95 latency per step, error rate with refusals and fallbacks, schema-valid rate, and cache-read rate. Alert on changes, split by model and prompt version.</p></li><li><p><strong>Check the token math:</strong> price each token type separately, and know that Anthropic's <code>input_tokens</code> excludes cached tokens while OpenAI and OpenTelemetry include them.</p></li><li><p><strong>Log verdicts, not "call completed":</strong> validation failures, retries, fallbacks, guardrails, and thumbs-downs, each with the ID and trace link.</p></li></ul><p><em>Related: <a href="https://thetshaped.dev/p/7-things-that-break-llm-apps-in-production-ai">7 Things That Break LLM Apps in Production</a> covers the failures these numbers catch, and <a href="https://thetshaped.dev/p/the-3-layers-of-testing-ai-agents-unittests-mocking-alerts-judge-evals-production-agentic-systems">The 3 Layers of Testing AI Agents</a> shows how this dashboard becomes your production test report.</em></p><div><hr></div><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://thetshaped.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Subscribe to get practical tips on AI Agents, Agentic Systems, and Software Architecture delivered straight to your inbox:</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div class="pullquote"><div class="captioned-image-container"><figure><div class="image-link image2" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!cmMn!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 424w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 848w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 1272w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!cmMn!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png" width="248" height="207.7" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:670,&quot;width&quot;:800,&quot;resizeWidth&quot;:248,&quot;bytes&quot;:257716,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://thetshaped.dev/i/188025481?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb8ebf3ac-ad2f-415d-9e32-111e959c8e4e_800x800.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!cmMn!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 424w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 848w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 1272w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div></div></div></div></figure></div><p><strong>Follow me on <a href="https://www.linkedin.com/in/petarivanovv9/">LinkedIn</a></strong> | <strong><a href="https://x.com/intent/follow?screen_name=petarivanovv9">Twitter(X)</a></strong> |<strong> <a href="https://www.threads.net/@petarivanovv9">Threads</a></strong></p></div><p>Thank you for supporting this newsletter.</p><p>Consider sharing this post with your friends and get rewards.</p><p>You are the best! &#128591;</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!EvJY!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!EvJY!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 424w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 848w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!EvJY!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg" width="800" height="500" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:500,&quot;width&quot;:800,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:83253,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://thetshaped.dev/i/188025481?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!EvJY!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 424w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 848w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button class="pencraft pc-reset pencraft icon-container restack-image buttonBase-tqxFUX" tabindex="0" type="button"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button class="pencraft pc-reset pencraft icon-container view-image buttonBase-tqxFUX" tabindex="0" type="button"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://thetshaped.dev/p/debugging-testing-ai-agents-llm-apps-4-decisions-observability-traces-correlation-ids-checklist?utm_source=substack&utm_medium=email&utm_content=share&action=share&quot;,&quot;text&quot;:&quot;Share&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://thetshaped.dev/p/debugging-testing-ai-agents-llm-apps-4-decisions-observability-traces-correlation-ids-checklist?utm_source=substack&utm_medium=email&utm_content=share&action=share"><span>Share</span></a></p><div class="pullquote"><div class="captioned-image-container"><figure><div class="image-link image2" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!UzWz!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 424w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 848w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 1272w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!UzWz!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif" width="48" height="57.6" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/39609b45-f68c-4557-8772-8e2a45182842_500x600.gif&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:600,&quot;width&quot;:500,&quot;resizeWidth&quot;:48,&quot;bytes&quot;:7028,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:&quot;&quot;,&quot;type&quot;:&quot;image/gif&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://thetshaped.dev/i/188785536?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!UzWz!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 424w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 848w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 1272w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 1456w" sizes="auto, 100vw" loading="lazy"></picture><div></div></div></div></figure></div><p style="text-align: center;"><em><strong>Press the Like button if you found this post helpful</strong></em></p></div>]]></content:encoded></item><item><title><![CDATA[Saved the Order, Lost the Event: Why You Need the Outbox Pattern]]></title><description><![CDATA[Save-then-publish drops messages without a single error in your logs. Here's the outbox + inbox pair that stops it, in plain Postgres and Node.js.]]></description><link>https://thetshaped.dev/p/outbox-pattern-why-you-need-typescript-postgresql-nodejs</link><guid isPermaLink="false">https://thetshaped.dev/p/outbox-pattern-why-you-need-typescript-postgresql-nodejs</guid><pubDate>Sat, 03 Oct 2026 12:21:10 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/7c481057-a7e4-4afe-af6f-e56e56e8f03a_2912x1632.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://thetshaped.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption"><span data-color="#9900ff" style="color: rgb(153, 0, 255);">Get my Full-Stack JavaScript Architect Bundle for FREE on newsletter signup:</span></p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><p>A customer places an order. You save it to Postgres, then publish an <code>OrderPlaced</code> event so the warehouse, the email service, and billing can do their part.</p><p>The save works. Then the process crashes a millisecond later, before the publish.</p><p>Now the order exists and nobody downstream knows about it. No shipment, no receipt, no invoice. There's no error in your logs either, because your service didn't fail at anything. It saved a row and never got to tell anyone.</p><p>This is the <strong>dual-write problem</strong>, the quiet bug under most "we lost a message" incidents. The fix is two small tables, an outbox on the way out and an inbox on the way in. You need both.</p><ul><li><p><em><a href="https://thetshaped.dev/p/outbox-pattern-why-you-need-typescript-postgresql-nodejs/?action=share">Share this post </a>&amp; I&#8217;ll send you some rewards for the referrals.</em></p></li></ul><div><hr></div><h2><a href="https://inngest.link/t-shaped-dev-nl">Build for the long run (Partner)</a></h2><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://inngest.link/t-shaped-dev-nl" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!FkkV!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff8034326-70e4-4589-a79a-ad15997e4a68_2190x1061.png 424w, https://substackcdn.com/image/fetch/$s_!FkkV!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff8034326-70e4-4589-a79a-ad15997e4a68_2190x1061.png 848w, https://substackcdn.com/image/fetch/$s_!FkkV!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff8034326-70e4-4589-a79a-ad15997e4a68_2190x1061.png 1272w, https://substackcdn.com/image/fetch/$s_!FkkV!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff8034326-70e4-4589-a79a-ad15997e4a68_2190x1061.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!FkkV!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff8034326-70e4-4589-a79a-ad15997e4a68_2190x1061.png" width="1456" height="705" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/f8034326-70e4-4589-a79a-ad15997e4a68_2190x1061.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:705,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:4101995,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:&quot;https://inngest.link/t-shaped-dev-nl&quot;,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://thetshaped.dev/i/213818275?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff8034326-70e4-4589-a79a-ad15997e4a68_2190x1061.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!FkkV!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff8034326-70e4-4589-a79a-ad15997e4a68_2190x1061.png 424w, https://substackcdn.com/image/fetch/$s_!FkkV!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff8034326-70e4-4589-a79a-ad15997e4a68_2190x1061.png 848w, https://substackcdn.com/image/fetch/$s_!FkkV!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff8034326-70e4-4589-a79a-ad15997e4a68_2190x1061.png 1272w, https://substackcdn.com/image/fetch/$s_!FkkV!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff8034326-70e4-4589-a79a-ad15997e4a68_2190x1061.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button class="pencraft pc-reset pencraft icon-container restack-image buttonBase-tqxFUX" tabindex="0" type="button"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button class="pencraft pc-reset pencraft icon-container view-image buttonBase-tqxFUX" tabindex="0" type="button"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Long-running agents need endurance.</p><p>They must endure waiting for users, and weather tool failures. They must take each new jump in model intelligence in stride.</p><p><a href="https://inngest.link/t-shaped-dev-nl">Inngest</a> makes long-running agents durable, observable and improvable over time.</p><p>Check out their generous free tier and a sleek local dev server for testing your workflows.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://inngest.link/t-shaped-dev-nl&quot;,&quot;text&quot;:&quot;Build for the long run with Inngest&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://inngest.link/t-shaped-dev-nl"><span>Build for the long run with Inngest</span></a></p><p>(Thanks to <a href="https://inngest.link/t-shaped-dev-nl">Inngest</a> for partnering on this post.)</p><div><hr></div><h2>Two writes can't be made atomic, in any order</h2><p>Here's the code almost everyone writes first:</p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;typescript&quot;,&quot;nodeId&quot;:null}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-typescript">await db.orders.insert(order);   // system 1: your database
await broker.publish(event);     // system 2: the message broker</code></pre></div><p>The two systems don't share a transaction, so you can be wrong in two ways:</p><ul><li><p><strong>Lost message.</strong> The insert commits, then the publish fails or the process dies. The order exists and the event is gone.</p></li><li><p><strong>Phantom message.</strong> The publish goes out, then the database write fails. You get this when you publish inside a transaction that later rolls back, or when you flip the two lines. Consumers now act on an order that doesn't exist.</p></li></ul><p>Flipping the lines only swaps which failure you get. A retry around the publish doesn't help either, because a crashed process retries nothing.</p><p>The textbook fix is two-phase commit across the database and the broker. Skip it. It holds locks across network round trips, and Kafka, RabbitMQ, and SQS don't support it anyway.</p><div><hr></div><h2>The outbox turns two writes into one</h2><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!t1OW!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd3f2886a-5d98-4794-9d72-474cfeaf2f0a_1716x792.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!t1OW!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd3f2886a-5d98-4794-9d72-474cfeaf2f0a_1716x792.png 424w, https://substackcdn.com/image/fetch/$s_!t1OW!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd3f2886a-5d98-4794-9d72-474cfeaf2f0a_1716x792.png 848w, https://substackcdn.com/image/fetch/$s_!t1OW!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd3f2886a-5d98-4794-9d72-474cfeaf2f0a_1716x792.png 1272w, https://substackcdn.com/image/fetch/$s_!t1OW!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd3f2886a-5d98-4794-9d72-474cfeaf2f0a_1716x792.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!t1OW!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd3f2886a-5d98-4794-9d72-474cfeaf2f0a_1716x792.png" width="1456" height="672" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/d3f2886a-5d98-4794-9d72-474cfeaf2f0a_1716x792.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:672,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:151351,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://thetshaped.dev/i/213818275?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd3f2886a-5d98-4794-9d72-474cfeaf2f0a_1716x792.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!t1OW!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd3f2886a-5d98-4794-9d72-474cfeaf2f0a_1716x792.png 424w, https://substackcdn.com/image/fetch/$s_!t1OW!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd3f2886a-5d98-4794-9d72-474cfeaf2f0a_1716x792.png 848w, https://substackcdn.com/image/fetch/$s_!t1OW!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd3f2886a-5d98-4794-9d72-474cfeaf2f0a_1716x792.png 1272w, https://substackcdn.com/image/fetch/$s_!t1OW!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd3f2886a-5d98-4794-9d72-474cfeaf2f0a_1716x792.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button class="pencraft pc-reset pencraft icon-container restack-image buttonBase-tqxFUX" tabindex="0" type="button"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button class="pencraft pc-reset pencraft icon-container view-image buttonBase-tqxFUX" tabindex="0" type="button"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Stop publishing from the request. Write the event into an <code>outbox</code> table in your own database instead, <strong>in the same transaction as the order.</strong></p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;typescript&quot;,&quot;nodeId&quot;:null}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-typescript">await db.tx(async (t) =&gt; {
  const order = await t.orders.insert({ id, userId, total });
  await t.outbox.insert({
    eventId: crypto.randomUUID(),  // a stable id that travels with the message
    aggregateId: order.id,         // becomes the message key
    type: 'OrderPlaced',
    payload: { orderId: order.id, userId, total },
  });
}); // both rows commit together, or neither does</code></pre></div><p>There's no moment where the order exists without its event anymore, because there's only one write.</p><p>The outbox is a queue you keep in Postgres:</p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;sql&quot;,&quot;nodeId&quot;:null}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-sql">CREATE TABLE outbox (
  id           BIGSERIAL PRIMARY KEY,  -- the relay reads in this order
  event_id     UUID NOT NULL UNIQUE,   -- consumers dedup on this
  aggregate_id TEXT NOT NULL,          -- e.g. the order id
  type         TEXT NOT NULL,
  payload      JSONB NOT NULL,
  created_at   TIMESTAMPTZ NOT NULL DEFAULT now(),
  published_at TIMESTAMPTZ             -- NULL until the relay ships it
);
CREATE INDEX outbox_unpublished ON outbox (id) WHERE published_at IS NULL;</code></pre></div><p>Don't skip the partial index. The relay only ever asks for unpublished rows, and without it every poll scans a table that grows with every order.</p><div><hr></div><h2>The relay is one boring loop, and it's easy to get wrong</h2><p>The <strong>relay</strong> is a background worker that moves those rows to the broker:</p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;typescript&quot;,&quot;nodeId&quot;:null}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-typescript">await db.tx(async (t) =&gt; {
  const batch = await t.query(`
    SELECT * FROM outbox WHERE published_at IS NULL
    ORDER BY id LIMIT 100 FOR UPDATE SKIP LOCKED`);
  for (const row of batch) {
    // must resolve only after the broker confirms it has the message
    await broker.publish(row.type, row.aggregate_id, { ...row.payload, eventId: row.event_id });
    await t.query(`UPDATE outbox SET published_at = now() WHERE id = $1`, [row.id]);
  }
}); // select, publish, mark, then commit. One transaction.</code></pre></div><p>Two mistakes quietly break it.</p><p><strong>Splitting the select from the mark.</strong> <code>FOR UPDATE SKIP LOCKED</code> lets several relays run side by side, each on a different batch. The locks only live while the transaction is open, though. Run the SELECT on its own, and they vanish when it returns, so two relays publish the same rows.</p><p><strong>Not waiting for the broker.</strong> A publish call that returns once the bytes leave your process proves nothing. Turn on publisher confirms in RabbitMQ, keep <code>acks=all</code> in Kafka (the default since Kafka 3.0), and mark a row only after the broker says it has the message. Otherwise, the relay marks rows as published that the broker never got, and the dual-write bug is back inside the fix.</p><p>Holding row locks during network calls is the price. Keep batches small and publish timeouts short. If that's still too much, switch to a lease (claim rows with a short <code>UPDATE &#8230; SET claimed_at = now() &#8230; RETURNING *</code>, publish with no locks held, then mark them).</p><p>Either way, the relay is <strong>at-least-once</strong>. Crash after a publish but before the commit, and it sends that event again on restart. The inbox exists for exactly this.</p><div><hr></div><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://thetshaped.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Join <strong>30,000+ engineers</strong> getting one practical issue a week on production-grade React, Node, Architecture, and <strong>real AI systems</strong>, and leveling up from Mid &#8594; Senior &#8594; Architect.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div><hr></div><h2>The inbox makes the second delivery a no-op</h2><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!P5vZ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3647a54c-c46e-434d-a62b-53d5696d737d_1828x1374.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!P5vZ!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3647a54c-c46e-434d-a62b-53d5696d737d_1828x1374.png 424w, https://substackcdn.com/image/fetch/$s_!P5vZ!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3647a54c-c46e-434d-a62b-53d5696d737d_1828x1374.png 848w, https://substackcdn.com/image/fetch/$s_!P5vZ!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3647a54c-c46e-434d-a62b-53d5696d737d_1828x1374.png 1272w, https://substackcdn.com/image/fetch/$s_!P5vZ!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3647a54c-c46e-434d-a62b-53d5696d737d_1828x1374.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!P5vZ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3647a54c-c46e-434d-a62b-53d5696d737d_1828x1374.png" width="1456" height="1094" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/3647a54c-c46e-434d-a62b-53d5696d737d_1828x1374.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1094,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:231887,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://thetshaped.dev/i/213818275?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3647a54c-c46e-434d-a62b-53d5696d737d_1828x1374.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!P5vZ!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3647a54c-c46e-434d-a62b-53d5696d737d_1828x1374.png 424w, https://substackcdn.com/image/fetch/$s_!P5vZ!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3647a54c-c46e-434d-a62b-53d5696d737d_1828x1374.png 848w, https://substackcdn.com/image/fetch/$s_!P5vZ!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3647a54c-c46e-434d-a62b-53d5696d737d_1828x1374.png 1272w, https://substackcdn.com/image/fetch/$s_!P5vZ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3647a54c-c46e-434d-a62b-53d5696d737d_1828x1374.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button class="pencraft pc-reset pencraft icon-container restack-image buttonBase-tqxFUX" tabindex="0" type="button"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button class="pencraft pc-reset pencraft icon-container view-image buttonBase-tqxFUX" tabindex="0" type="button"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Brokers add duplicates of their own. If the network drops a consumer's ack, the broker can't tell "processed" from "lost", so it redelivers. Kafka's transactions give you exactly-once between Kafka topics and stop at your Postgres. SQS FIFO drops re-sends with the same deduplication ID for only five minutes.</p><p>Exactly-once delivery <em>into your database</em> doesn't exist. What you can build is <strong>effectively-once</strong>, meaning at-least-once delivery plus processing that's safe to repeat. The inbox is a table of event IDs you've already handled:</p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;sql&quot;,&quot;nodeId&quot;:null}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-sql">CREATE TABLE inbox (
  event_id     UUID PRIMARY KEY,   -- the id the outbox generated
  processed_at TIMESTAMPTZ NOT NULL DEFAULT now()
);</code></pre></div><p>Claim the event and do the work <strong>in one transaction:</strong></p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;typescript&quot;,&quot;nodeId&quot;:null}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-typescript">await db.tx(async (t) =&gt; {
  const claimed = await t.query(`
    INSERT INTO inbox (event_id) VALUES ($1)
    ON CONFLICT DO NOTHING RETURNING event_id`, [eventId]);
  if (claimed.length === 0) return; // seen it: skip, then ack
  await applyOrder(t, payload);     // the side effect, same transaction
});
// ack the broker only AFTER this commits</code></pre></div><p>Here's how it handles each failure:</p><ul><li><p><strong>Duplicate event.</strong> The insert hits the primary key, <code>RETURNING</code> comes back empty, and you skip.</p></li><li><p><strong>Two copies at once</strong> (say a visibility timeout expired mid-processing). The second insert waits on the first transaction's row. It skips if the first commits and does the work if the first rolls back.</p></li><li><p><strong>Crash mid-processing.</strong> The rollback takes the inbox row with it. No ack went out, so the broker redelivers and the retry runs clean.</p></li><li><p><strong>Crash after commit, before ack.</strong> The redelivery hits the inbox, skips, and acks.</p></li></ul><p><strong>Dedup on your event ID, never the broker's.</strong> RabbitMQ's delivery tag and SQS's receipt handle change on every delivery. Even the broker's message ID fails, because a relay republish after a crash is a brand-new message to the broker. Only the <code>event_id</code> from the outbox survives both kinds of duplicate.</p><p><strong>Ack only after the commit.</strong> Ack first, crash before committing, and you've told the broker "done" for work that rolled back. That message is gone for good.</p><p>You get a true exactly-once <em>effect</em> only for writes in the same database, since only they share the transaction. A card charge or an email can't roll back, so each external call needs its own idempotency key. I covered that side in <a href="https://thetshaped.dev/p/how-to-stop-charging-customers-twice-practical-idempotency-guide-http-message-queues">the idempotency guide</a>. The same goes for an inbox kept in Redis with <code>SET NX</code>, because Redis can evict a key.</p><div><hr></div><h2>The edges that bite in production</h2><ul><li><p><strong>Dedup isn't ordering.</strong> If <code>OrderCancelled</code> can land before <code>OrderPlaced</code>, the inbox won't save you. Sorting by <code>id</code> isn't enough either, since Postgres assigns ids at insert and a slow transaction can commit id 41 after id 45 shipped. Publish with <code>aggregate_id</code> as the message key (the Kafka partition key or the SQS FIFO message group) so one order's events go through one consumer, in sequence.</p></li><li><p><strong>Poison messages need an exit.</strong> A message that always throws redelivers forever and blocks its partition. Move it to a dead-letter queue after N attempts, and alert on DLQ depth.</p></li><li><p><strong>Both tables grow.</strong> Delete published outbox rows after a few days. Keep inbox rows longer than any redelivery can arrive, including a replay from the DLQ a week later.</p></li><li><p><strong>Alert on the oldest unpublished row.</strong> <code>now() - min(created_at)</code> over unpublished rows is your relay's heartbeat. When it climbs, events are piling up and nobody downstream knows yet.</p></li></ul><div><hr></div><h2>&#128204; TL;DR</h2><ul><li><p><strong>Save-then-publish isn't atomic.</strong> A crash in between loses the event, or sends one for an order that doesn't exist. No ordering of the two writes fixes it.</p></li><li><p><strong>Outbox:</strong> write the event to an <code>outbox</code> table in the <em>same transaction</em> as the business change. A relay ships it to the broker.</p></li><li><p><strong>Relay:</strong> select, publish, and mark in one transaction with <code>FOR UPDATE SKIP LOCKED</code>, and wait for the broker's confirm before marking.</p></li><li><p><strong>Inbox:</strong> exactly-once delivery into your database doesn't exist, so claim the event ID and do the work in one transaction. Duplicates skip. Ack only after the commit.</p></li><li><p><strong>Dedup on the <code>event_id</code> you generated,</strong> never a broker delivery tag or message ID.</p></li><li><p><strong>In production:</strong> key messages by entity for ordering, add a DLQ, clean up both tables, and alert on the oldest unpublished row.</p></li></ul><div><hr></div><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://thetshaped.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Subscribe to get practical tips on AI Agents, Agentic Systems, and Software Architecture delivered straight to your inbox:</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div class="pullquote"><div class="captioned-image-container"><figure><div class="image-link image2" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!cmMn!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 424w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 848w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 1272w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!cmMn!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png" width="248" height="207.7" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:670,&quot;width&quot;:800,&quot;resizeWidth&quot;:248,&quot;bytes&quot;:257716,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://thetshaped.dev/i/188025481?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb8ebf3ac-ad2f-415d-9e32-111e959c8e4e_800x800.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!cmMn!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 424w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 848w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 1272w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div></div></div></div></figure></div><p><strong>Follow me on <a href="https://www.linkedin.com/in/petarivanovv9/">LinkedIn</a></strong> | <strong><a href="https://x.com/intent/follow?screen_name=petarivanovv9">Twitter(X)</a></strong> |<strong> <a href="https://www.threads.net/@petarivanovv9">Threads</a></strong></p></div><p>Thank you for supporting this newsletter.</p><p>Consider sharing this post with your friends and get rewards.</p><p>You are the best! &#128591;</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!EvJY!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!EvJY!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 424w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 848w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!EvJY!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg" width="800" height="500" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:500,&quot;width&quot;:800,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:83253,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://thetshaped.dev/i/188025481?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!EvJY!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 424w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 848w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button class="pencraft pc-reset pencraft icon-container restack-image buttonBase-tqxFUX" tabindex="0" type="button"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button class="pencraft pc-reset pencraft icon-container view-image buttonBase-tqxFUX" tabindex="0" type="button"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://thetshaped.dev/p/outbox-pattern-why-you-need-typescript-postgresql-nodejs?utm_source=substack&utm_medium=email&utm_content=share&action=share&quot;,&quot;text&quot;:&quot;Share&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://thetshaped.dev/p/outbox-pattern-why-you-need-typescript-postgresql-nodejs?utm_source=substack&utm_medium=email&utm_content=share&action=share"><span>Share</span></a></p><div class="pullquote"><div class="captioned-image-container"><figure><div class="image-link image2" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!UzWz!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 424w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 848w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 1272w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!UzWz!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif" width="48" height="57.6" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/39609b45-f68c-4557-8772-8e2a45182842_500x600.gif&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:600,&quot;width&quot;:500,&quot;resizeWidth&quot;:48,&quot;bytes&quot;:7028,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:&quot;&quot;,&quot;type&quot;:&quot;image/gif&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://thetshaped.dev/i/188785536?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!UzWz!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 424w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 848w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 1272w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 1456w" sizes="auto, 100vw" loading="lazy"></picture><div></div></div></div></figure></div><p style="text-align: center;"><em><strong>Press the Like button if you found this post helpful</strong></em></p></div>]]></content:encoded></item><item><title><![CDATA[6 MCP Servers That Make My Coding Agent Actually Useful in 2026]]></title><description><![CDATA[The MCP servers I actually reach for when building features and shipping production apps.]]></description><link>https://thetshaped.dev/p/6-mcp-servers-that-make-my-coding-agent-2026</link><guid isPermaLink="false">https://thetshaped.dev/p/6-mcp-servers-that-make-my-coding-agent-2026</guid><pubDate>Tue, 29 Sep 2026 13:57:20 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/b5ea615a-f3c3-49b8-ba50-16f742cc90c3_2912x1632.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://thetshaped.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption"><span data-color="#9900ff" style="color: rgb(153, 0, 255);">Get my Full-Stack JavaScript Architect Bundle for FREE on newsletter signup:</span></p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><p>There are thousands of MCP servers now. </p><p>Most of the ones I tried added noise instead of signal. </p><p>These six earned a permanent spot in my setup, and each one closes a specific gap between my agent and reality.</p><ul><li><p><em><a href="https://thetshaped.dev/p/6-mcp-servers-that-make-my-coding-agent-2026/?action=share">Share this post</a> &amp; I&#8217;ll send you some rewards for the referrals.</em></p></li></ul><div><hr></div><h2>1. Context7: docs that match your installed versions</h2><p>Every model ships with a knowledge cutoff. Ask for a Next.js or TanStack Query pattern and there's a decent chance you get the API from two major versions ago, delivered with complete confidence. </p><p>I've reviewed agent code that called methods which no longer exist.</p><p>Context7 fixes exactly this. It pulls current, version-specific documentation and working code examples straight into the agent's context.</p><p>The prompt:</p><blockquote><p>Add optimistic updates to this mutation. use context7</p></blockquote><p>Add it to your agent rules and it triggers automatically for any library question. You stop thinking about it, and the deprecated-API class of bugs mostly disappears.</p><div><hr></div><h2>2. GitHub MCP: the memory around the code</h2><p>The code tells the agent what exists. The pull requests and issues tell it why. </p><p>Review threads, linked issues, CI runs. That history answers questions the code can't.</p><p>With the official GitHub MCP server, my agent reads the review comments on my open PR and applies them. It checks why CI is red and fixes the failing test. It finds the PR that last touched a file and reads the discussion before changing that file again.</p><p>The prompt:</p><blockquote><p>Read the review comments on my open PR, apply the requested changes, and summarize what you changed.</p></blockquote><p>To be honest, a terminal agent with the <code>gh</code> CLI already covers some of this. The MCP server matters for agents without a shell, and you can scope it to exactly the repos and permissions you want the agent to have.</p><div><hr></div><h2>3. Postgres MCP: the schema is the truth</h2><p>Agents guess your schema from ORM models and migration files. Those drift from reality more often than anyone admits, and every guess becomes a subtle bug in a query.</p><p>A database MCP server removes the guessing. Mine can inspect the live schema, run read-only queries, and check query plans against a staging database.</p><p>The prompt:</p><blockquote><p>This endpoint takes 900 ms. Find the query it runs, EXPLAIN ANALYZE it on staging, and propose the right index.</p></blockquote><p>One rule I never break:</p><blockquote><p><strong>The agent gets read-only credentials and staging or local data, nothing more.</strong> </p></blockquote><p>An agent with write access to production is an incident report waiting for a timestamp.</p><div><hr></div><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://thetshaped.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Join <strong>30,000+ engineers</strong> getting one practical issue a week on production-grade React, Node, Architecture, and <strong>real AI systems</strong>, and leveling up from Mid &#8594; Senior &#8594; Architect.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div><hr></div><h2>4. Playwright MCP: eyes on the running app</h2><p>Without a browser, I am the agent's QA department.</p><p>It edits a component, I click around, I paste the console error back, we repeat. That loop is slow, and I'm the bottleneck in it.</p><p>Playwright MCP hands the agent the browser. It opens the app, clicks through the flow, reads the console and the network tab, and takes screenshots along the way.</p><p>The prompt:</p><blockquote><p>Open localhost:3000/checkout, apply coupon SAVE10, and reproduce the bug where the total doesn't update. Check the console, fix the cause, then verify in the browser.</p></blockquote><p>The part that matters is "then verify". The agent checks its own fix before I ever look, and most of the back-and-forth disappears.</p><div><hr></div><h2>5. Atlassian MCP: the ticket is the spec</h2><p>Requirements don't live in the repo. They live in Jira, in the acceptance criteria, and in the comment thread where scope got cut and edge cases got decided. Pasting the description by hand loses exactly that thread.</p><p>With the Atlassian MCP server, the agent reads the whole ticket. Description, criteria, comments, the linked Confluence page.</p><p>The prompt:</p><blockquote><p>Read WTRAV-841 including all comments, implement it, and comment on the ticket with what shipped and what was left out of scope.</p></blockquote><p>The comments are the valuable part. Scope changes and edge-case decisions land there, and they rarely make it back into the description. An agent that reads only the description builds last week's version of the feature.</p><div><hr></div><h2>6. Sentry MCP + Seer: production bugs, fixed from the editor</h2><p><em><a href="https://sentry.io/welcome/?utm_source=tshapeddev&amp;utm_medium=paid-community&amp;utm_campaign=general-fy27q3-evergreen&amp;utm_content=partner-homepage-trysentry&amp;code=tshapeddev">Sentry</a> sponsors this post. This section describes the workflow I actually run, and it would be on this list either way.</em></p><p>Everything above helps the agent while I'm building. This one takes over after the code ships, which is where the real pain lives.</p><p>My old flow for a production bug went like this. An alert fires. I open the dashboard, stare at a stack trace, try to reproduce locally, fail, add a log line, deploy, and wait for the error to fire again.</p><p>The new flow is one prompt in my editor:</p><blockquote><p>Find the top unresolved issue in production, work out the root cause, and fix it.</p></blockquote><p>Here's what happens, end to end:</p><ol><li><p><strong>Find.</strong> Through the <a href="https://sentry.io/welcome/?utm_source=tshapeddev&amp;utm_medium=paid-community&amp;utm_campaign=general-fy27q3-evergreen&amp;utm_content=partner-homepage-trysentry&amp;code=tshapeddev">Sentry MCP server</a>, the agent lists unresolved issues and picks the top one by users affected.</p></li><li><p><strong>Diagnose.</strong> It kicks off <a href="https://sentry.io/product/seer/?utm_source=tshapeddev&amp;utm_medium=paid-community&amp;utm_campaign=seer-fy27q3-evergreen&amp;utm_content=partner-product-trysentry&amp;code=tshapeddev">Seer</a>, <a href="https://sentry.io/welcome/?utm_source=tshapeddev&amp;utm_medium=paid-community&amp;utm_campaign=general-fy27q3-evergreen&amp;utm_content=partner-homepage-trysentry&amp;code=tshapeddev">Sentry</a>'s root cause analysis, straight from an MCP tool call. This is the step I care about most. Seer doesn't guess from the stack trace alone. It starts from everything Sentry already captured for that issue (the stack trace, the breadcrumbs, the distributed trace, even the session replay of what the user clicked) and checks its hypothesis against the actual code before committing to an answer. Back comes the root cause: what broke, where, why, plus a proposed fix. Every tab I used to open by hand, read for me.</p></li><li><p><strong>Fix.</strong> My agent takes Seer's analysis and writes the fix in my editor. My branch, my code style, my review before anything merges.</p></li></ol><p>I never opened the dashboard.</p><p>Seer is the debugger, because it holds the production context and verifies its analysis against the code. My agent is the implementer, because it knows my codebase and my conventions. Each side does the half it's genuinely good at.</p><p>You can turn the automation up or down from there. Seer can open the pull request itself if you want the loop fully hands-off, it works from Slack alerts, and the same analysis reaches Claude or Cursor through Sentry's first-party integrations. I keep my own agent in the loop because I want the fix to land like my code and get reviewed like my code.</p><p>Try it yourself: <a href="https://sentry.io/product/seer/?utm_source=tshapeddev&amp;utm_medium=paid-community&amp;utm_campaign=seer-fy27q3-evergreen&amp;utm_content=partner-product-trysentry&amp;code=tshapeddev">Seer in Sentry</a>.</p><div><hr></div><h2>&#128204; TL;DR</h2><ul><li><p><strong>Context7</strong> feeds it docs that match your installed versions. No more deprecated APIs suggested with full confidence.</p></li><li><p><strong>GitHub MCP</strong> gives it the repo's memory: issues, PR reviews, CI runs.</p></li><li><p><strong>Postgres MCP</strong> shows it the real schema and real query plans.</p></li><li><p><strong>Playwright MCP</strong> gives it a browser, so it can reproduce bugs and verify its own fixes.</p></li><li><p><strong>Atlassian MCP</strong> hands it the ticket, the acceptance criteria, and the comments where the decisions actually happened.</p></li><li><p><strong>Sentry MCP + Seer</strong> closes the loop with production: one prompt goes from "top unresolved issue" to a fix in my editor. Full walkthrough in section 6.</p></li></ul><div><hr></div><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://thetshaped.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Subscribe to get practical tips on AI Agents, Agentic Systems, and Software Architecture delivered straight to your inbox:</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div class="pullquote"><div class="captioned-image-container"><figure><div class="image-link image2" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!cmMn!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 424w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 848w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 1272w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!cmMn!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png" width="248" height="207.7" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:670,&quot;width&quot;:800,&quot;resizeWidth&quot;:248,&quot;bytes&quot;:257716,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://thetshaped.dev/i/188025481?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb8ebf3ac-ad2f-415d-9e32-111e959c8e4e_800x800.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!cmMn!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 424w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 848w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 1272w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div></div></div></div></figure></div><p><strong>Follow me on <a href="https://www.linkedin.com/in/petarivanovv9/">LinkedIn</a></strong> | <strong><a href="https://x.com/intent/follow?screen_name=petarivanovv9">Twitter(X)</a></strong> |<strong> <a href="https://www.threads.net/@petarivanovv9">Threads</a></strong></p></div><p>Thank you for supporting this newsletter.</p><p>Consider sharing this post with your friends and get rewards.</p><p>You are the best! &#128591;</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!EvJY!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!EvJY!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 424w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 848w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!EvJY!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg" width="800" height="500" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:500,&quot;width&quot;:800,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:83253,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://thetshaped.dev/i/188025481?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!EvJY!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 424w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 848w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button class="pencraft pc-reset pencraft icon-container restack-image buttonBase-tqxFUX" tabindex="0" type="button"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button class="pencraft pc-reset pencraft icon-container view-image buttonBase-tqxFUX" tabindex="0" type="button"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://thetshaped.dev/p/6-mcp-servers-that-make-my-coding-agent-2026?utm_source=substack&utm_medium=email&utm_content=share&action=share&quot;,&quot;text&quot;:&quot;Share&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://thetshaped.dev/p/6-mcp-servers-that-make-my-coding-agent-2026?utm_source=substack&utm_medium=email&utm_content=share&action=share"><span>Share</span></a></p><div class="pullquote"><div class="captioned-image-container"><figure><div class="image-link image2" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!UzWz!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 424w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 848w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 1272w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!UzWz!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif" width="48" height="57.6" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/39609b45-f68c-4557-8772-8e2a45182842_500x600.gif&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:600,&quot;width&quot;:500,&quot;resizeWidth&quot;:48,&quot;bytes&quot;:7028,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:&quot;&quot;,&quot;type&quot;:&quot;image/gif&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://thetshaped.dev/i/188785536?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!UzWz!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 424w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 848w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 1272w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 1456w" sizes="auto, 100vw" loading="lazy"></picture><div></div></div></div></figure></div><p style="text-align: center;"><em><strong>Press the Like button if you found this post helpful</strong></em></p></div>]]></content:encoded></item><item><title><![CDATA[5 Resilience Patterns for AI Agents]]></title><description><![CDATA[Checkpoints, scoped retries, idempotent tools, and run budgets: how production agents survive the failure math of 20-step loops.]]></description><link>https://thetshaped.dev/p/5-resilience-patterns-for-ai-agents-retries-checkpoints-idempotency</link><guid isPermaLink="false">https://thetshaped.dev/p/5-resilience-patterns-for-ai-agents-retries-checkpoints-idempotency</guid><pubDate>Thu, 24 Sep 2026 10:19:30 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/621a9117-16b7-490a-9b68-3737836e768d_2912x1632.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://thetshaped.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption"><span data-color="#9900ff" style="color: rgb(153, 0, 255);">Get my Full-Stack JavaScript Architect Bundle for FREE on newsletter signup:</span></p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><p>Picture a support agent handling a trip cancellation, 14 steps into a run. At step 3 it emailed the traveler. Then a deploy restarts the pod, the job queue retries the job, and the agent starts over from step 1. The traveler gets the same email twice.</p><p>The retry did its job. Re-running from the top is harmless for a request and a problem for an agent that already acted.</p><p>It also happens a lot. Say each model or tool call succeeds 98% of the time. Over 20 calls, only &#8776; 67% of runs get through clean, so 1 run in 3 hits a failure. Even at 99%, it&#8217;s 1 in 5.</p><p>Timeout, retry, and circuit breaker from <a href="https://thetshaped.dev/p/resilience-patterns-keep-backend-nodejs-service-up-production-timeout-retry-backoff-jitter-circuit-breaker">my earlier post</a> all still apply. They just protect one call. This post is about protecting the whole run.</p><ul><li><p><em><a href="https://thetshaped.dev/p/5-resilience-patterns-for-ai-agents-retries-checkpoints-idempotency/?action=share">Share this post</a> &amp; I&#8217;ll send you some rewards for the referrals.</em></p></li></ul><div><hr></div><h2>An agent isn't a request</h2><p>Three things break the classic playbook:</p><ul><li><p><strong>It&#8217;s long.</strong> Minutes, not milliseconds. Plenty of time for a deploy to land mid-run.</p></li><li><p><strong>It&#8217;s stateful.</strong> Step 14 builds on steps 1 to 13. Lose the process and you lose the run.</p></li><li><p><strong>It acts.</strong> It sends emails and moves money, so replaying a step can do real damage.</p></li></ul><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!v-Vx!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F07194eb8-9c32-4c45-b1c7-e2c029775ea3_1788x1182.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!v-Vx!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F07194eb8-9c32-4c45-b1c7-e2c029775ea3_1788x1182.png 424w, https://substackcdn.com/image/fetch/$s_!v-Vx!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F07194eb8-9c32-4c45-b1c7-e2c029775ea3_1788x1182.png 848w, https://substackcdn.com/image/fetch/$s_!v-Vx!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F07194eb8-9c32-4c45-b1c7-e2c029775ea3_1788x1182.png 1272w, https://substackcdn.com/image/fetch/$s_!v-Vx!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F07194eb8-9c32-4c45-b1c7-e2c029775ea3_1788x1182.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!v-Vx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F07194eb8-9c32-4c45-b1c7-e2c029775ea3_1788x1182.png" width="1456" height="963" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/07194eb8-9c32-4c45-b1c7-e2c029775ea3_1788x1182.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:963,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:171127,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://thetshaped.dev/i/213818254?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F07194eb8-9c32-4c45-b1c7-e2c029775ea3_1788x1182.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!v-Vx!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F07194eb8-9c32-4c45-b1c7-e2c029775ea3_1788x1182.png 424w, https://substackcdn.com/image/fetch/$s_!v-Vx!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F07194eb8-9c32-4c45-b1c7-e2c029775ea3_1788x1182.png 848w, https://substackcdn.com/image/fetch/$s_!v-Vx!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F07194eb8-9c32-4c45-b1c7-e2c029775ea3_1788x1182.png 1272w, https://substackcdn.com/image/fetch/$s_!v-Vx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F07194eb8-9c32-4c45-b1c7-e2c029775ea3_1788x1182.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button class="pencraft pc-reset pencraft icon-container restack-image buttonBase-tqxFUX" tabindex="0" type="button"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button class="pencraft pc-reset pencraft icon-container view-image buttonBase-tqxFUX" tabindex="0" type="button"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Each classic pattern breaks on one of these properties. Here are the five adjustments that fix them.</p><div><hr></div><h2>1. Retry the step, not the run</h2><p>You can retry the call, the step, or the whole run. Retry the step, because retrying the run pays again for the 13 steps that worked and replays their side effects.</p><p>The call level is already covered. The OpenAI and Anthropic SDKs retry connection errors, 429s, and 5xx twice by default, with backoff. What&#8217;s left for you is sorting step failures into three piles.</p><ul><li><p><strong>Time fixes it.</strong> A 429, a timeout, a 500, Anthropic&#8217;s 529 &#8220;overloaded&#8221;. Retry the step later.</p></li><li><p><strong>The model fixes it.</strong> A booking ID that doesn&#8217;t exist. Send the error back as a tool result with <code>is_error: true</code>, and the model usually corrects its call.</p></li><li><p><strong>Nothing fixes it.</strong> A revoked API key. Fail the step now.</p></li></ul><p>Retries also multiply across layers. Three step attempts on top of the SDK&#8217;s own retries can mean nine model calls. Let one layer own retries. If it&#8217;s the step, set <code>max_retries=0</code> and handle the <code>Retry-After</code> header yourself.</p><p>If you don&#8217;t want to handle this by yourself, <a href="https://inngest.link/t-shaped-dev-nl">Inngest helps with durable agentic workflows</a>, so you don&#8217;t need to think about that. Each <code>step.run</code> gets its own retry counter, and the three piles become three <code>except</code> branches:</p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;python&quot;,&quot;nodeId&quot;:null}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-python">async def refund_booking(args, key):
    try:
        return await payments.refund(**args, idempotency_key=key)
    except BookingNotFound as e:      # the model fixes it
        return {"error": str(e)}
    except CredentialsRevoked as e:   # nothing fixes it
        raise inngest.NonRetriableError(str(e))
    except RateLimited as e:          # time fixes it
        raise inngest.RetryAfterError("payments rate limit", e.retry_after_ms)</code></pre></div><p><span>Run it with </span><code>ctx.step.run("refund-booking", refund_booking, args, key)</code><span>. Any other error gets Inngest's default retries. The </span><code>key</code><span> is what pattern 2 is about.</span></p><div><hr></div><h2>2. Make every tool safe to call twice</h2><p>A retry is only safe when the thing being retried is idempotent. </p><p>Model calls are safe. Tools that write are the problem. <code>create_ticket</code> called twice creates two tickets.</p><p>The fix is the one that already protects payment APIs: an idempotency key. Stripe popularized this pattern and keeps keys for 24 hours; I've written a <a href="https://thetshaped.dev/p/how-to-stop-charging-customers-twice-practical-idempotency-guide-http-message-queues">full guide to idempotency</a> if you want the deep dive.</p><div><hr></div><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://thetshaped.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Join <strong>30,000+ engineers</strong> getting one practical issue a week on production-grade React, Node, Architecture, and <strong>real AI systems</strong>, and leveling up from Mid &#8594; Senior &#8594; Architect.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div><hr></div><h2>3. Checkpoint, so a crash means resume</h2><p>Here's the pattern that single requests never needed. </p><p>A request that dies costs nothing. The client retries it from zero, and nothing is lost.</p><p>When a 20-minute run dies at step 14, "from zero" throws away 13 steps of work and replays every side effect along the way.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!abxl!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff9a5d1b8-2a46-4b9a-a0ab-f7ff394fc3b4_1778x1190.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!abxl!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff9a5d1b8-2a46-4b9a-a0ab-f7ff394fc3b4_1778x1190.png 424w, https://substackcdn.com/image/fetch/$s_!abxl!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff9a5d1b8-2a46-4b9a-a0ab-f7ff394fc3b4_1778x1190.png 848w, https://substackcdn.com/image/fetch/$s_!abxl!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff9a5d1b8-2a46-4b9a-a0ab-f7ff394fc3b4_1778x1190.png 1272w, https://substackcdn.com/image/fetch/$s_!abxl!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff9a5d1b8-2a46-4b9a-a0ab-f7ff394fc3b4_1778x1190.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!abxl!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff9a5d1b8-2a46-4b9a-a0ab-f7ff394fc3b4_1778x1190.png" width="1456" height="974" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/f9a5d1b8-2a46-4b9a-a0ab-f7ff394fc3b4_1778x1190.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:974,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:159275,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://thetshaped.dev/i/213818254?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff9a5d1b8-2a46-4b9a-a0ab-f7ff394fc3b4_1778x1190.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!abxl!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff9a5d1b8-2a46-4b9a-a0ab-f7ff394fc3b4_1778x1190.png 424w, https://substackcdn.com/image/fetch/$s_!abxl!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff9a5d1b8-2a46-4b9a-a0ab-f7ff394fc3b4_1778x1190.png 848w, https://substackcdn.com/image/fetch/$s_!abxl!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff9a5d1b8-2a46-4b9a-a0ab-f7ff394fc3b4_1778x1190.png 1272w, https://substackcdn.com/image/fetch/$s_!abxl!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff9a5d1b8-2a46-4b9a-a0ab-f7ff394fc3b4_1778x1190.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button class="pencraft pc-reset pencraft icon-container restack-image buttonBase-tqxFUX" tabindex="0" type="button"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button class="pencraft pc-reset pencraft icon-container view-image buttonBase-tqxFUX" tabindex="0" type="button"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Save the state after each step instead. It's small, just the message history, the tool results, and a step counter.</p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;python&quot;,&quot;nodeId&quot;:null}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-python">async def run(run_id, task):
    state = await load_state(run_id) or AgentState.new(task)
    while not state.done:
        state = await run_step(state)
        await save_state(run_id, state)  # after every step
    return state</code></pre></div><p>Now a crash costs one step, and pattern 2&#8217;s key makes re-running that step safe.</p><p>A checkpoint saves data, not execution, so something still has to notice a dead run and re-enter the loop, like a job queue or a durable-execution runtime. Runs also outlive deploys, so version your saved state like an API.</p><p><a href="https://inngest.link/t-shaped-dev-nl">Inngest is that kind of runtime</a>. It records each finished step, and after a crash it re-runs the function and skips the steps it already recorded. Here's the agent loop with one step per model call and one per tool call:</p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;python&quot;,&quot;nodeId&quot;:null}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-python">@inngest_client.create_function(
    fn_id="agent-run",
    trigger=inngest.TriggerEvent(event="agent/run.requested"),
)
async def agent_run(ctx: inngest.Context):
    messages = [ctx.event.data["request"]]
    for turn in range(20):
        reply = await ctx.step.run(f"model-{turn}", call_model, messages)
        if reply["done"]:
            return reply
        result = await ctx.step.run(f"tool-{turn}", call_tool, reply["tool"])
        messages += [reply, result]
    raise inngest.NonRetriableError("20 turns without an answer, needs a human")</code></pre></div><p>Say <code>tool-3</code> issued the refund and the run dies in <code>model-4</code>. The retry reads everything up to <code>tool-3</code> from Inngest&#8217;s record instead of calling it again, so the traveler isn&#8217;t refunded twice. If it dies inside <code>tool-3</code>, that step does run again, so <code>call_tool</code> passes the model&#8217;s tool-call ID as the idempotency key. The <code>range(20)</code> is the step cap from pattern 5, and hitting it fails the run instead of returning nothing.</p><p>One rule comes with this. Every model call, tool call, and database read goes inside a step, because code outside steps runs again each time the function resumes.</p><div><hr></div><h2>4. Fall back across failure domains, not just models</h2><p>In a real provider outage, retries only make it worse. That&#8217;s where a circuit breaker and a fallback come in, with two agent rules.</p><p><strong>Leave the failure domain.</strong> A sibling model at the same provider usually shares the outage. The cleanest fallback is often the same model on another cloud (Claude on Bedrock or Vertex AI, OpenAI models on Azure), so your prompts and evals still hold, as long as the features you use exist there. A gateway like LiteLLM or OpenRouter can do the routing.</p><p><strong>Only switch to a model your evals passed.</strong> Models differ in how reliably they call tools, and 20 steps amplify small differences. Switch at a step boundary, which your checkpoint makes clean.</p><div><hr></div><h2>5. Bound the loop, then hand it to a human</h2><p>The most expensive agent failure throws no error. The model loops on a tool that half-works, every call returns 200, and the run never ends.</p><p>So cap every run on steps, tokens, and wall-clock time, and stop it when a cap trips. Set a per-call timeout too. The OpenAI and Anthropic SDKs default to 10 minutes and retry a timeout twice, so one hung call can stall a run for half an hour.</p><p>A stopped run isn&#8217;t lost. Thanks to the checkpoint, it&#8217;s saved state plus a reason, which is exactly what a human needs to unstick it and resume. Runs that exhaust their retries belong there too.</p><p><a href="https://inngest.link/t-shaped-dev-nl">Inngest</a> can park the run while it waits for that human. Call this before any refund over your limit, and refund only if it comes back approved:</p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;python&quot;,&quot;nodeId&quot;:null}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-python">async def wait_for_approval(ctx: inngest.Context):
    request_id = ctx.event.data["request_id"]
    await ctx.step.run("ask-for-approval", request_approval, request_id)
    for i in range(288):  # check every 5 minutes, for 24 hours
        decision = await ctx.step.run(f"check-{i}", read_decision, request_id)
        if decision["state"] != "pending":
            return decision  # approved or rejected
        await ctx.step.wait_for_event(  # the event only wakes us early
            f"wait-{i}",
            event="refunds/decision-recorded",
            if_exp="async.data.request_id == event.data.request_id",
            timeout=datetime.timedelta(minutes=5),
        )
    return await ctx.step.run("expire", expire_if_pending, request_id)</code></pre></div><p>The saved decision is the source of truth, and the event only wakes the run early. <code>wait_for_event</code> only hears events sent after it starts listening, so a single 24-hour wait can miss a fast approver and expire an approved refund. Here a missed event costs five minutes. <code>expire_if_pending</code> only flips a record that&#8217;s still pending, so an approval and an expiry can&#8217;t both win.</p><p><span>The database does the rest. </span><code>request_approval</code><span> saves the request with its deadline under </span><code>request_id</code><span>, so a retry neither asks twice nor restarts the clock. The approval endpoint checks who's answering, accepts a decision only while the request is pending, saves it, then sends the event. </span><code>expire_if_pending</code><span> only flips a record that's still pending, so an approval and an expiry can't both win.</span></p><div><hr></div><h2>&#128204; TL;DR</h2><ul><li><p>At 98% per call, 1 in 3 runs of 20 calls hits a failure. Failure is the main path.</p></li><li><p>Retry the step, not the run. Retry what time fixes, send back what the model can fix, and fail fast on the rest. Let one layer own retries.</p></li><li><p>Give every tool that writes an idempotency key built from stable IDs, never one generated inside the step.</p></li><li><p>Checkpoint after every step, so a crash or a deploy means resume instead of restart.</p></li><li><p>Fall back to the same model on another cloud first, and only switch to models your evals passed.</p></li><li><p>Cap steps, tokens, and time; set per-call timeouts. The saved decision is the truth, and the approval event is only a wake-up.</p></li><li><p>Consider using <a href="https://inngest.link/t-shaped-dev-nl">Inngest</a> for durable execution for workflows and AI</p></li></ul><div><hr></div><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://thetshaped.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Subscribe to get practical tips on AI Agents, Agentic Systems, and Software Architecture delivered straight to your inbox:</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div class="pullquote"><div class="captioned-image-container"><figure><div class="image-link image2" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!cmMn!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 424w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 848w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 1272w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!cmMn!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png" width="248" height="207.7" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:670,&quot;width&quot;:800,&quot;resizeWidth&quot;:248,&quot;bytes&quot;:257716,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://thetshaped.dev/i/188025481?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb8ebf3ac-ad2f-415d-9e32-111e959c8e4e_800x800.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!cmMn!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 424w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 848w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 1272w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div></div></div></div></figure></div><p><strong>Follow me on <a href="https://www.linkedin.com/in/petarivanovv9/">LinkedIn</a></strong> | <strong><a href="https://x.com/intent/follow?screen_name=petarivanovv9">Twitter(X)</a></strong> |<strong> <a href="https://www.threads.net/@petarivanovv9">Threads</a></strong></p></div><p>Thank you for supporting this newsletter.</p><p>Consider sharing this post with your friends and get rewards.</p><p>You are the best! &#128591;</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!EvJY!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!EvJY!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 424w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 848w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!EvJY!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg" width="800" height="500" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:500,&quot;width&quot;:800,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:83253,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://thetshaped.dev/i/188025481?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!EvJY!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 424w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 848w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button class="pencraft pc-reset pencraft icon-container restack-image buttonBase-tqxFUX" tabindex="0" type="button"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button class="pencraft pc-reset pencraft icon-container view-image buttonBase-tqxFUX" tabindex="0" type="button"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://thetshaped.dev/p/5-resilience-patterns-for-ai-agents-retries-checkpoints-idempotency?utm_source=substack&utm_medium=email&utm_content=share&action=share&quot;,&quot;text&quot;:&quot;Share&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://thetshaped.dev/p/5-resilience-patterns-for-ai-agents-retries-checkpoints-idempotency?utm_source=substack&utm_medium=email&utm_content=share&action=share"><span>Share</span></a></p><div class="pullquote"><div class="captioned-image-container"><figure><div class="image-link image2" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!UzWz!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 424w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 848w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 1272w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!UzWz!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif" width="48" height="57.6" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/39609b45-f68c-4557-8772-8e2a45182842_500x600.gif&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:600,&quot;width&quot;:500,&quot;resizeWidth&quot;:48,&quot;bytes&quot;:7028,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:&quot;&quot;,&quot;type&quot;:&quot;image/gif&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://thetshaped.dev/i/188785536?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!UzWz!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 424w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 848w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 1272w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 1456w" sizes="auto, 100vw" loading="lazy"></picture><div></div></div></div></figure></div><p style="text-align: center;"><em><strong>Press the Like button if you found this post helpful</strong></em></p></div>]]></content:encoded></item><item><title><![CDATA[7 Identity Patterns for AI Agents in Production]]></title><description><![CDATA[Your Agent Isn't a User. Stop Authenticating It Like One.]]></description><link>https://thetshaped.dev/p/7-identity-patterns-for-ai-agents-production-systems</link><guid isPermaLink="false">https://thetshaped.dev/p/7-identity-patterns-for-ai-agents-production-systems</guid><pubDate>Fri, 18 Sep 2026 13:57:47 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/e7effdd3-0c08-4f7b-9cfd-13dcf6b10419_2912x1632.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://thetshaped.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption"><span data-color="#9900ff" style="color: rgb(153, 0, 255);">Get my Full-Stack JavaScript Architect Bundle for FREE on newsletter signup:</span></p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><p>In August 2025, attackers exported Salesforce data from <a href="https://thehackernews.com/2025/09/salesloft-takes-drift-offline-after.html">more than 700 companies</a>. Cloudflare, Zscaler, Palo Alto Networks. </p><p>Security vendors were breached. Nobody hacked Salesforce itself. </p><p>The attackers <a href="https://cloud.google.com/blog/topics/threat-intelligence/data-theft-salesforce-instances-via-salesloft-drift">stole OAuth tokens from Drift</a>, an AI chatbot bolted onto those Salesforce instances, and the tokens quietly kept working for ten days.</p><p>That incident is the cleanest preview I've seen of the next few years. </p><p>An AI agent gets broad access because it's useful, ends up holding more permissions than any single employee, and its credentials outlive everybody's attention.</p><p>Production teams are converging on seven patterns, the same way backend teams converged on <a href="https://thetshaped.dev/p/resilience-patterns-keep-backend-nodejs-service-up-production-timeout-retry-backoff-jitter-circuit-breaker">resilience patterns</a> once enough things had caught fire.</p><ul><li><p><em><a href="https://thetshaped.dev/p/7-identity-patterns-for-ai-agents-production-systems/?action=share">Share this post</a> &amp; I&#8217;ll send you some rewards for the referrals.</em></p></li></ul><div><hr></div><h2>1. Give every agent its own identity</h2><p>Most agents in production borrow someone else's identity. </p><p>Either they run as the user (your session, your OAuth grant) or as <code>svc-automation</code>, the shared service account that fourteen other things also use. Both setups fail the same test. Something went wrong at 3 AM, so who did it?</p><p>The pattern is to make the agent a first-class principal. Its own ID, its own credentials, its own permissions, and a named human owner. Microsoft went as far as adding a <a href="https://learn.microsoft.com/en-us/entra/agent-id/what-is-microsoft-entra-agent-id">new identity type to Entra ID</a>, next to users and service principals; every agent identity records a "sponsor", the person accountable for it. Google's version gives each agent a <a href="https://docs.cloud.google.com/iam/docs/agent-identity-overview">SPIFFE ID</a> with certificates that rotate every 24 hours and no way to mint a long-lived key at all.</p><p>You don't need either product to apply the idea. </p><blockquote><p><strong>One identity per agent, never shared. An owner you can name. Credentials that belong to the agent itself.</strong></p></blockquote><div><hr></div><h2>2. Delegate, don't impersonate</h2><p>Here's the trap almost everyone falls into first. The agent needs to act for the user, so you hand it the user's token. It works. And your audit log now says <em>you</em> deleted those records, approved that PR, sent that email. Your name on actions you never saw.</p><p>The standards answer is delegation via <a href="https://datatracker.ietf.org/doc/html/rfc8693">OAuth token exchange (RFC 8693)</a>. The agent trades the user's token for a new one that keeps the user as the subject and adds itself as the actor:</p><pre><code>{
  "sub": "petar@example.com",
  "act": { "sub": "agent:content-creator" },
  "scope": "crm:read calendar:write",
  "exp": 1756723200
}</code></pre><p>One token, both facts. Authority flows from the user. The acting party is the agent.</p><p>The log can finally tell the difference between "Petar did this" and "Petar's agent did this for Petar".</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Jmc2!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5bfa43dc-02f3-46fa-8eee-c7f2c7cb82a6_1822x1080.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Jmc2!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5bfa43dc-02f3-46fa-8eee-c7f2c7cb82a6_1822x1080.png 424w, https://substackcdn.com/image/fetch/$s_!Jmc2!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5bfa43dc-02f3-46fa-8eee-c7f2c7cb82a6_1822x1080.png 848w, https://substackcdn.com/image/fetch/$s_!Jmc2!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5bfa43dc-02f3-46fa-8eee-c7f2c7cb82a6_1822x1080.png 1272w, https://substackcdn.com/image/fetch/$s_!Jmc2!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5bfa43dc-02f3-46fa-8eee-c7f2c7cb82a6_1822x1080.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!Jmc2!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5bfa43dc-02f3-46fa-8eee-c7f2c7cb82a6_1822x1080.png" width="1456" height="863" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/5bfa43dc-02f3-46fa-8eee-c7f2c7cb82a6_1822x1080.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:863,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:192359,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://thetshaped.dev/i/213818179?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5bfa43dc-02f3-46fa-8eee-c7f2c7cb82a6_1822x1080.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Jmc2!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5bfa43dc-02f3-46fa-8eee-c7f2c7cb82a6_1822x1080.png 424w, https://substackcdn.com/image/fetch/$s_!Jmc2!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5bfa43dc-02f3-46fa-8eee-c7f2c7cb82a6_1822x1080.png 848w, https://substackcdn.com/image/fetch/$s_!Jmc2!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5bfa43dc-02f3-46fa-8eee-c7f2c7cb82a6_1822x1080.png 1272w, https://substackcdn.com/image/fetch/$s_!Jmc2!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5bfa43dc-02f3-46fa-8eee-c7f2c7cb82a6_1822x1080.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button class="pencraft pc-reset pencraft icon-container restack-image buttonBase-tqxFUX" tabindex="0" type="button"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button class="pencraft pc-reset pencraft icon-container view-image buttonBase-tqxFUX" tabindex="0" type="button"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p><span>You don't have to build the exchange yourself. </span></p><p><a href="https://www.descope.com/use-cases/ai?utm_source=tshapeddev-2026&amp;utm_medium=email&amp;utm_campaign=tshapeddev-2026"><span>Descope's Agentic Identity Hub</span></a><span> issues delegated agent tokens as a standard </span><a href="https://www.descope.com/learn/post/oauth-token-exchange?utm_source=tshapeddev-2026&amp;utm_medium=email&amp;utm_campaign=tshapeddev-2026">OAuth token exchange</a><span>, and the exchanged token keeps the delegation chain.</span></p><p><span>This way, audit logs can trace an action from the user through every agent in between. That's the property this pattern is after.</span></p><div><hr></div><h2>3. Permissions are an intersection, not a union</h2><p>An agent that serves many users accumulates access. If the agent's own access is the ceiling, you've built a classic confused deputy. I ask the agent for a document I'm not allowed to see, and the agent, which can see everything, cheerfully fetches it. That's a data leak with extra steps.</p><p>The fix is what identity teams call the intersection rule. A delegated call may do only what the agent is provisioned for AND what the requesting user is currently allowed to do. It's evaluated on every call, because the user's permissions can change mid-task.</p><p>The same rule applies inside RAG. If your retrieval layer doesn't check document-level permissions for the asking user, the model becomes the leak. That's the problem fine-grained authorization for RAG exists to solve. It filters what the agent retrieves down to what this user may read, before the model sees a word of it.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!8Jjw!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd34809dd-c420-4d78-9a5a-f05ec05560bd_1508x1026.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!8Jjw!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd34809dd-c420-4d78-9a5a-f05ec05560bd_1508x1026.png 424w, https://substackcdn.com/image/fetch/$s_!8Jjw!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd34809dd-c420-4d78-9a5a-f05ec05560bd_1508x1026.png 848w, https://substackcdn.com/image/fetch/$s_!8Jjw!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd34809dd-c420-4d78-9a5a-f05ec05560bd_1508x1026.png 1272w, https://substackcdn.com/image/fetch/$s_!8Jjw!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd34809dd-c420-4d78-9a5a-f05ec05560bd_1508x1026.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!8Jjw!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd34809dd-c420-4d78-9a5a-f05ec05560bd_1508x1026.png" width="1456" height="991" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/d34809dd-c420-4d78-9a5a-f05ec05560bd_1508x1026.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:991,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:135410,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://thetshaped.dev/i/213818179?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd34809dd-c420-4d78-9a5a-f05ec05560bd_1508x1026.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!8Jjw!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd34809dd-c420-4d78-9a5a-f05ec05560bd_1508x1026.png 424w, https://substackcdn.com/image/fetch/$s_!8Jjw!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd34809dd-c420-4d78-9a5a-f05ec05560bd_1508x1026.png 848w, https://substackcdn.com/image/fetch/$s_!8Jjw!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd34809dd-c420-4d78-9a5a-f05ec05560bd_1508x1026.png 1272w, https://substackcdn.com/image/fetch/$s_!8Jjw!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd34809dd-c420-4d78-9a5a-f05ec05560bd_1508x1026.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button class="pencraft pc-reset pencraft icon-container restack-image buttonBase-tqxFUX" tabindex="0" type="button"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button class="pencraft pc-reset pencraft icon-container view-image buttonBase-tqxFUX" tabindex="0" type="button"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://thetshaped.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Join <strong>30,000+ engineers</strong> getting one practical issue a week on production-grade React, Node, Architecture, and <strong>real AI systems</strong>, and leveling up from Mid &#8594; Senior &#8594; Architect.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div><hr></div><h2>4. Kill the static API key</h2><p>Two numbers to sit with. GitGuardian counted <a href="https://blog.gitguardian.com/the-state-of-secrets-sprawl-2025/">23.77 million new hardcoded secrets</a> pushed to public GitHub in 2024. And 70% of the secrets leaked in 2022 were still valid years later. Nobody rotates what nobody remembers.</p><p>The <a href="https://www.wiz.io/blog/38-terabytes-of-private-data-accidentally-exposed-by-microsoft-ai-researchers">38TB Microsoft leak</a> is the same story at a bigger scale. One Azure storage token, scoped to the entire account with full-control rights, valid since 2020. Three years live, nobody looking.</p><p>The production pattern is a token vault. AWS built one into <a href="https://docs.aws.amazon.com/bedrock-agentcore/latest/devguide/identity.html">AgentCore Identity</a>, and HashiCorp does it with <a href="https://developer.hashicorp.com/validated-patterns/vault/ai-agent-identity-with-hashicorp-vault">dynamic just-in-time credentials</a>. The agent starts a task holding nothing. It asks the vault for the specific credential the task needs and gets a short-lived one that expires on its own.</p><pre><code># Before: the key lives forever in the environment
sf = Salesforce(token=os.environ["SALESFORCE_TOKEN"])

# After: the agent asks the vault per task, scoped and short-lived
token = vault.get_token(
    agent_id=AGENT_ID,
    resource="salesforce",
    scopes=["crm:read"],  # just this task's needs
)
sf = Salesforce(token=token)  # expires in minutes</code></pre><p>Nothing to grep out of <code>printenv</code>. Nothing to find in a leaked config, nothing still valid three years later.</p><p><span>In Descope, the vault is called </span><a href="https://docs.descope.com/agentic-identity-hub/core-components/connections?utm_source=tshapeddev-2026&amp;utm_medium=email&amp;utm_campaign=tshapeddev-2026">Connections</a><span>. It stores OAuth tokens and API keys per user or per tenant and handles refresh, and their docs add a rule worth stealing whatever you use. </span><strong><span>Don't cache the token; fetch it whenever a tool runs.</span></strong><span> </span></p><p><a href="https://docs.descope.com/agentic-identity-hub/auth-patterns?utm_source=tshapeddev-2026&amp;utm_medium=email&amp;utm_campaign=tshapeddev-2026"><span>Their recommended setup</span></a><span> also puts your MCP server between the agent and the vault, so the server pulls the third-party token at call time and the long-lived secret never reaches the agent at all.</span></p><div><hr></div><h2>5. One token, one audience</h2><p>The Drift breach hurt because the stolen tokens kept working: from anywhere, against a high-value target, for days. A token that works everywhere is a skeleton key, and the blast radius of the theft is the whole building.</p><p>The countermeasure is audience binding. When a client requests a token, it names the exact resource the token is for, and every other resource rejects it. It got codified in the <a href="https://www.descope.com/blog/post/mcp-auth-spec?utm_source=tshapeddev-2026&amp;utm_medium=email&amp;utm_campaign=tshapeddev-2026">MCP authorization spec</a>, which is where many of us will meet it first. Since mid-2025, an MCP server is formally an OAuth 2.1 resource server. Clients must name the target server when requesting tokens (RFC 8707), servers must validate the audience, and forwarding a token upstream ("token passthrough") is explicitly forbidden.</p><p>Steal an audience-bound token and you've stolen a key to one door. Which, thanks to pattern 4, expires in minutes anyway.</p><p>This is the part of the spec nobody wants to hand-roll. <a href="https://www.descope.com/mcp-auth?utm_source=tshapeddev-2026&amp;utm_medium=email&amp;utm_campaign=tshapeddev-2026">Descope's MCP auth</a> puts OAuth 2.1 on your server, with per-tool scopes and client registration through DCR or client ID metadata documents, and it works without replacing the login system you already have. On FastMCP, it's a provider you hand to the server:</p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;python&quot;,&quot;nodeId&quot;:null}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-python">import os

from fastmcp import FastMCP
from fastmcp.server.auth.providers.descope import DescopeProvider

auth_provider = DescopeProvider(
    config_url=os.environ["DESCOPE_CONFIG_URL"],
    base_url=os.environ.get("BASE_URL", "http://localhost:8000"),
)
mcp = FastMCP(name="crm-tools", auth=auth_provider)</code></pre></div><div><hr></div><h2>6. Async approval for the actions that scare you</h2><p>A human can't approve every agent call; that deletes the point of having agents. Letting the agent wire money unsupervised is the other bad answer.</p><p>The middle path is asynchronous authorization, built on a flow called <a href="https://openid.net/specs/openid-client-initiated-backchannel-authentication-core-1_0.html">CIBA</a> (client-initiated backchannel authentication). When the agent hits a sensitive action, it fires an approval request out of band. Your phone shows exactly what it wants to do, in structured form (<a href="https://datatracker.ietf.org/doc/rfc9396/">RFC 9396</a>, "rich authorization requests"):</p><pre><code>{
  "type": "payment_initiation",
  "instructedAmount": { "currency": "EUR", "amount": "500.00" },
  "creditorName": "AWS EMEA"
}</code></pre><p>Approve it and the agent continues. It kept working on everything else while it waited. Notice the shape of the approval, too. It's tied to a single action with real detail, never a blanket "your agent wants access, OK?". Identity vendors ship this as a product feature today.</p><div><hr></div><h2>7. Plan the retirement at the birth</h2><p>OWASP keeps a <a href="https://owasp.org/www-project-non-human-identities-top-10/">Top 10 just for non-human identities</a>, and the #1 risk isn't a clever attack. It's improper offboarding. Identities that outlive their purpose because deleting them is nobody's job. Agents multiply the problem, since teams now spin up agents the way they spin up feature branches.</p><p>Every agent has a named owner. Every action is traceable through the full chain (user &#8594; agent &#8594; tool &#8594; resource). Revocation is one move, not a scavenger hunt across env files and config repos.</p><div><hr></div><h2>Where to start</h2><p>Nobody rolls out seven patterns in one sprint. The order I'd actually do them in:</p><ol><li><p><strong>Pattern 4 first.</strong> Killing static keys is the biggest risk cut for the least effort, and secrets managers are mature tools.</p></li><li><p><strong>Pattern 1 next.</strong> Separate identities per agent, so your logs start meaning something.</p></li><li><p><strong>Patterns 2 and 3</strong> the moment an agent acts for other people, not just for you.</p></li><li><p><strong>Pattern 6</strong> the moment an agent touches money, prod data, or anything irreversible.</p></li></ol><p><span>You'll mostly assemble rather than build. This space moved fast in 18 months. The big cloud platforms now ship agent identity for their own ecosystems, identity platforms package the protocol-heavy patterns for any stack, and the MCP spec bakes the token rules into a protocol you're probably already using. </span></p><p><span>Patterns 2, 4, 5, and 6 are the ones </span><a href="https://www.descope.com/use-cases/ai?utm_source=tshapeddev-2026&amp;utm_medium=email&amp;utm_campaign=tshapeddev-2026">Descope's Agentic Identity Hub</a><span> packages, for when you'd rather not assemble them yourself.</span></p><div><hr></div><h2>&#128204; TL;DR</h2><ul><li><p><strong>Give every agent its own identity.</strong> Never a shared service account, never your session.</p></li><li><p><strong>Delegate, don't impersonate.</strong> The token names who's acting (the agent) and who granted it (the user).</p></li><li><p><strong>Intersect permissions.</strong> A delegated call may do only what both the agent and the user are allowed to do.</p></li><li><p><strong>No static keys.</strong> The agent fetches short-lived credentials from a vault at task time; nothing sits in an env var waiting to leak.</p></li><li><p><strong>One token, one audience.</strong> A stolen token should open exactly one door.</p></li><li><p><strong>Async human approval.</strong> Sensitive actions ping your phone with structured detail; the agent keeps working until you answer.</p></li><li><p><strong>Lifecycle from day one.</strong> Named owner, full-chain audit trail, and a kill switch you've actually tested.</p></li></ul><div><hr></div><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://thetshaped.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Subscribe to get practical tips on AI Agents, Agentic Systems, and Software Architecture delivered straight to your inbox:</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div class="pullquote"><div class="captioned-image-container"><figure><div class="image-link image2" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!cmMn!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 424w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 848w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 1272w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!cmMn!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png" width="248" height="207.7" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:670,&quot;width&quot;:800,&quot;resizeWidth&quot;:248,&quot;bytes&quot;:257716,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://thetshaped.dev/i/188025481?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb8ebf3ac-ad2f-415d-9e32-111e959c8e4e_800x800.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!cmMn!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 424w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 848w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 1272w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div></div></div></div></figure></div><p><strong>Follow me on <a href="https://www.linkedin.com/in/petarivanovv9/">LinkedIn</a></strong> | <strong><a href="https://x.com/intent/follow?screen_name=petarivanovv9">Twitter(X)</a></strong> |<strong> <a href="https://www.threads.net/@petarivanovv9">Threads</a></strong></p></div><p>Thank you for supporting this newsletter.</p><p>Consider sharing this post with your friends and get rewards.</p><p>You are the best! &#128591;</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!EvJY!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!EvJY!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 424w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 848w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!EvJY!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg" width="800" height="500" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:500,&quot;width&quot;:800,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:83253,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://thetshaped.dev/i/188025481?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!EvJY!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 424w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 848w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button class="pencraft pc-reset pencraft icon-container restack-image buttonBase-tqxFUX" tabindex="0" type="button"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button class="pencraft pc-reset pencraft icon-container view-image buttonBase-tqxFUX" tabindex="0" type="button"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://thetshaped.dev/p/7-identity-patterns-for-ai-agents-production-systems?utm_source=substack&utm_medium=email&utm_content=share&action=share&quot;,&quot;text&quot;:&quot;Share&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://thetshaped.dev/p/7-identity-patterns-for-ai-agents-production-systems?utm_source=substack&utm_medium=email&utm_content=share&action=share"><span>Share</span></a></p><div class="pullquote"><div class="captioned-image-container"><figure><div class="image-link image2" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!UzWz!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 424w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 848w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 1272w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!UzWz!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif" width="48" height="57.6" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/39609b45-f68c-4557-8772-8e2a45182842_500x600.gif&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:600,&quot;width&quot;:500,&quot;resizeWidth&quot;:48,&quot;bytes&quot;:7028,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:&quot;&quot;,&quot;type&quot;:&quot;image/gif&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://thetshaped.dev/i/188785536?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!UzWz!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 424w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 848w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 1272w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 1456w" sizes="auto, 100vw" loading="lazy"></picture><div></div></div></div></figure></div><p style="text-align: center;"><em><strong>Press the Like button if you found this post helpful</strong></em></p></div>]]></content:encoded></item><item><title><![CDATA[The 3 Layers of Testing AI Agents]]></title><description><![CDATA[Draw the line between the model and the machine, and 80% of agent testing is testing you already know how to do. (7 min)]]></description><link>https://thetshaped.dev/p/the-3-layers-of-testing-ai-agents-unittests-mocking-alerts-judge-evals-production-agentic-systems</link><guid isPermaLink="false">https://thetshaped.dev/p/the-3-layers-of-testing-ai-agents-unittests-mocking-alerts-judge-evals-production-agentic-systems</guid><pubDate>Tue, 15 Sep 2026 20:59:04 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/691189a1-784a-45ba-a20d-6a6b6b9beb48_2912x1632.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://thetshaped.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption"><span data-color="#9900ff" style="color: rgb(153, 0, 255);">Get my Full-Stack JavaScript Architect Bundle for FREE on newsletter signup:</span></p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><p>The first time someone on the team asks: "How do we test the agent?", the room splits into two wrong answers.</p><p>One camp says you can't, it's non-deterministic, we'll watch it carefully in production.</p><p>The other camp mocks the LLM call and asserts on the mock, which produces a green test suite that verifies precisely nothing.</p><p>We ship agents at work, and we test them. Not the way you test a REST endpoint, since <code>assertEquals</code> dies the moment the same input can produce five valid answers.</p><p>But the discipline underneath testing survives contact with AI just fine. It ends up as a pyramid again, with different layers. Deterministic tests for everything around the model, LLM judges for the conversations, and evals in production that never stop running.</p><ul><li><p><em><a href="https://thetshaped.dev/p/the-3-layers-of-testing-ai-agents-unittests-mocking-alerts-judge-evals-production-agentic-systems/?action=share">Share this post</a> &amp; I&#8217;ll send you some rewards for the referrals.</em></p></li></ul><div><hr></div><h2><a href="https://www.descope.com/blog/post/descope-mcp-server-claude-chatgpt?utm_source=tshapeddev-2026&amp;utm_medium=email&amp;utm_campaign=tshapeddev-2026">Run Your Auth With AI Agents (Partner)</a></h2><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://www.descope.com/blog/post/descope-mcp-server-claude-chatgpt?utm_source=tshapeddev-2026&amp;utm_medium=email&amp;utm_campaign=tshapeddev-2026" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!OQUP!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2800c196-e51e-451e-8663-0b7792d412f2_1200x620.png 424w, https://substackcdn.com/image/fetch/$s_!OQUP!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2800c196-e51e-451e-8663-0b7792d412f2_1200x620.png 848w, https://substackcdn.com/image/fetch/$s_!OQUP!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2800c196-e51e-451e-8663-0b7792d412f2_1200x620.png 1272w, https://substackcdn.com/image/fetch/$s_!OQUP!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2800c196-e51e-451e-8663-0b7792d412f2_1200x620.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!OQUP!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2800c196-e51e-451e-8663-0b7792d412f2_1200x620.png" width="1200" height="620" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/2800c196-e51e-451e-8663-0b7792d412f2_1200x620.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:620,&quot;width&quot;:1200,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:958745,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:&quot;&quot;,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:&quot;https://www.descope.com/blog/post/descope-mcp-server-claude-chatgpt?utm_source=tshapeddev-2026&amp;utm_medium=email&amp;utm_campaign=tshapeddev-2026&quot;,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://thetshaped.dev/i/213818037?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2800c196-e51e-451e-8663-0b7792d412f2_1200x620.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!OQUP!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2800c196-e51e-451e-8663-0b7792d412f2_1200x620.png 424w, https://substackcdn.com/image/fetch/$s_!OQUP!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2800c196-e51e-451e-8663-0b7792d412f2_1200x620.png 848w, https://substackcdn.com/image/fetch/$s_!OQUP!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2800c196-e51e-451e-8663-0b7792d412f2_1200x620.png 1272w, https://substackcdn.com/image/fetch/$s_!OQUP!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2800c196-e51e-451e-8663-0b7792d412f2_1200x620.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button class="pencraft pc-reset pencraft icon-container restack-image buttonBase-tqxFUX" tabindex="0" type="button"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button class="pencraft pc-reset pencraft icon-container view-image buttonBase-tqxFUX" tabindex="0" type="button"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Coding agents are here to stay, but vibe-coding auth is dangerous business. Connect your AI agents to the <a href="https://www.descope.com/blog/post/descope-mcp-server-claude-chatgpt?utm_source=tshapeddev-2026&amp;utm_medium=email&amp;utm_campaign=tshapeddev-2026">Descope</a> MCP server instead!</p><p>This remote MCP server gives agents the ability to read documentation, manage users and tenants, configure auth flows, generate FGA schemas, review audit logs, and more&#8230;all through natural language.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.descope.com/blog/post/descope-mcp-server-claude-chatgpt?utm_source=tshapeddev-2026&amp;utm_medium=email&amp;utm_campaign=tshapeddev-2026&quot;,&quot;text&quot;:&quot;Get started on Claude or ChatGPT&quot;,&quot;action&quot;:null,&quot;class&quot;:&quot;button-wrapper&quot;}" data-component-name="ButtonCreateButton"><a class="button primary button-wrapper" href="https://www.descope.com/blog/post/descope-mcp-server-claude-chatgpt?utm_source=tshapeddev-2026&amp;utm_medium=email&amp;utm_campaign=tshapeddev-2026"><span>Get started on Claude or ChatGPT</span></a></p><p>(Thanks to <a href="https://www.descope.com/blog/post/descope-mcp-server-claude-chatgpt?utm_source=tshapeddev-2026&amp;utm_medium=email&amp;utm_campaign=tshapeddev-2026">Descope</a> for partnering on this post.)</p><div><hr></div><h2>First, separate the model from the machine</h2><p>The realization that unlocks agent testing: <strong>an agent is mostly not a model.</strong> It&#8217;s routing logic, tool implementations, schema validation, retry policies, state management, formatting. All deterministic, ordinary code that happens to sit around a probabilistic core.</p><p>Draw the boundary explicitly, then test each side on its own terms. </p><blockquote><p>The machine (everything around the model) gets <strong>normal tests</strong>. <br>The model&#8217;s behavior (what it says and which tools it picks) gets <strong>judged scenarios</strong>. <br>The whole thing in the wild gets <strong>continuous evals</strong>.</p></blockquote><p>Skip the separation and you end up writing slow, expensive judge tests for things a unit test covers in a millisecond, or skipping tests entirely because &#8220;it&#8217;s all non-deterministic anyway&#8221;. It isn&#8217;t. Most of it is boring, and boring is testable.</p><div><hr></div><h2>Layer 1: Deterministic tests for the machine</h2><ul><li><p><strong>Tools</strong> are functions. <code>fetch_expense(id)</code> returns the expense or a typed error. Unit test it against a real test database, and fake any external HTTP API at the transport level with <code>respx</code> or <code>pytest-httpx</code>. No LLM anywhere.</p></li><li><p><strong>Schema validation</strong> is deterministic: given this malformed output, the boundary rejects it with this error.</p></li><li><p><strong>Routing and state logic.</strong> If the graph should move from <code>awaiting_confirmation</code> to <code>executing</code> on approval, that's a reducer test.</p></li><li><p><strong>Failure policies.</strong> Retry budgets, fallbacks, timeouts: fake the model's <em>failure</em>, not its intelligence. A stub that throws a timeout is a legitimate mock; a stub that fakes a smart answer is not.</p></li></ul><p>This layer should be the bulk of your suite, and it runs in seconds. </p><div><hr></div><h2>Layer 2: Scenario tests with an LLM judge</h2><p>Now the genuinely new part. For the model&#8217;s actual behavior, we write scenario tests.</p><p>Each one is a multi-step conversation, and every step is a small piece of data.</p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;python&quot;,&quot;nodeId&quot;:null}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-python">step = {
    "user": "I lost my card somewhere in the airport.",

    # graded by a judge, because meaning has no regex
    "criteria": [
        "acknowledges the lost card",
        "says the card will be blocked",
        "asks at most one clarifying question",
    ],

    # asserted against the mock's call log, exact match
    "expected_calls": [],  # empty on purpose: blocking a card before asking which one is a fail
    "mocks": {"cards_api": "ok", "accounts_api": "ok"},
}</code></pre></div><p>Four fields, two kinds of assertion. </p><p>The agent runs its full loop against faked external APIs, real model, real prompts, real tool selection. Then the test compares the mock&#8217;s call log against <code>expected_calls</code>, a hard deterministic check with one right answer, and sends only the free-text reply to a <em>second</em> LLM call, the judge, which grades it against <code>criteria</code> and returns pass or fail with a reason.</p><p>Why a judge instead of string matching? Because &#8220;your card ending in 4821 has been blocked&#8221; and &#8220;I&#8217;ve blocked the card for you&#8221; are both correct, and no regex captures that space. Meaning is the one assertion a string comparison can&#8217;t make.</p><p>This suite is pytest as well, and it runs the real agent in-process. The agent picks its own tool calls; only the tool <em>responses</em> are faked, so there&#8217;s no queue, database, worker, or mock server to keep alive. </p><p>In our repo, each step lives in a YAML file that a Pydantic model validates on load, so a typo like <code>expected_call</code> fails at collection time instead of quietly checking nothing.</p><p>Evals are tests, and tests belong in the runner your team already uses.</p><h3>Writing judges that don't lie to you</h3><p>The judge is an LLM too, so it can be wrong, and a wrong judge doesn&#8217;t turn your suite red. It turns it green on bad replies. The short version of keeping it honest:</p><ul><li><p><strong>Binary criteria</strong>, three or four per step, each one checkable by a human in five seconds.</p></li><li><p><strong>Never ask the judge what code already knows.</strong> Which tools ran is an exact answer, and <code>expected_calls</code> covers it.</p></li><li><p><strong>Count thresholds; never ask for a score.</strong> Tally pass/fail across criteria, runs, and scenarios.</p></li><li><p><strong>Calibrate the judge and gate on it.</strong> Known-good and known-bad replies run first, and a drifting judge skips the expensive suite.</p></li><li><p><strong>Use a different model than the agent</strong>, and let calibration decide how big it needs to be.</p></li></ul><p><em>Note: every one of those hides a trap, so the judge gets its own deep dive post.</em></p><div><hr></div><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://thetshaped.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption"><span>Join </span><strong>30,000+ engineers</strong><span> getting one practical issue a week on production-grade React, Node, Architecture, and </span><strong>real AI systems</strong><span>, and leveling up from Mid &#8594; Senior &#8594; Architect.</span></p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div><hr></div><h2>Layer 3: Production is the eval that never ends</h2><p>The uncomfortable truth about layers 1 and 2 is that they ran before the deploy. </p><p>The model&#8217;s behavior can still drift after it. </p><p>A provider-side model update, a data shift, users phrasing things nobody predicted.</p><p>So the <strong>last layer runs on live traffic</strong>, continuously, and it&#8217;s wired to alerts rather than a weekly report.</p><p>Split it by what each check costs.</p><ul><li><p><strong>Cheap checks on every trace.</strong> Schema-valid rate, tool-call errors, steps per run against the loop budget, latency, tokens per conversation. They&#8217;re free to compute at boundaries you already have, so run them on all of your traffic and page someone when one moves.</p></li><li><p><strong>Refusals, on every trace too.</strong> A safety refusal comes back as a <em>successful</em> response carrying a refusal stop reason, so it never shows up in your error rate. Count it, or your first hint is a support ticket.</p></li><li><p><strong>Judges on a sample, alerting on the rate.</strong> The same evaluators from layer 2 score a slice of real (redacted) conversations as they land. Judging everything means another model call per criterion on every conversation, and you don&#8217;t need all of it to see a trend. Say you handle 10,000 conversations a day: a 5% sample is 500 judged replies, plenty to catch a pass rate sliding from 95% to 85% the same day. Alert when a criterion&#8217;s rolling pass rate drops, never on one failed verdict. A single verdict is noise; a rate is signal.</p></li><li><p><strong>The regression gate.</strong> Before any prompt tweak or model swap ships, the full scenario suite reruns against the change. Prompt changes are deploys. They get deploy discipline.</p></li></ul><p>There&#8217;s one place where judging every conversation makes sense. It&#8217;s a flow where a single bad reply is worth acting on, like anything that moves money, and there the verdict routes that conversation to a human. At that point it&#8217;s a guardrail more than an eval.</p><div><hr></div><h2>&#128204; TL;DR</h2><ul><li><p><strong>Most of your agent is normal code</strong>: tools, routing, parsing, validation. Normal unit tests, no model call, milliseconds each.</p></li><li><p><strong>Layer 2 is judged scenarios:</strong> real model, mocked APIs, tool calls checked against the mock&#8217;s call log, and a judge that grades only the free-text reply.</p></li><li><p><strong>Judge criteria are binary facts.</strong> &#8220;States the card will be blocked&#8221; is a criterion; &#8220;is helpful&#8221; is a vibe.</p></li><li><p><strong>Thresholds are counted, not judged.</strong> Tally the binary checks (step &#8594; scenario &#8594; suite). A judge&#8217;s 0-100 rating drifts and never says what broke.</p></li><li><p><strong>Calibrate the judge, and gate on it.</strong> Known-good and known-bad replies with expected labels, run before the scenarios, so a drifting judge skips the expensive suite.</p></li><li><p><strong>Don&#8217;t let the agent&#8217;s model grade itself.</strong> Use a different judge model; calibration, not model size, tells you if it&#8217;s good enough.</p></li><li><p><strong>Gate on a pass rate</strong>, not one green run. Four of five.</p></li><li><p><strong>Layer 3 is production:</strong> schema-valid rate, refusal count, judged samples, and a regression run before any prompt or model change ships.</p></li><li><p>The mock-the-model anti-test verifies your mocks, not your agent. If a test can&#8217;t fail when the agent gets worse, it isn&#8217;t a test.</p></li></ul><div><hr></div><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://thetshaped.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Subscribe to get practical tips on AI Agents, Agentic Systems, and Software Architecture delivered straight to your inbox:</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div class="pullquote"><div class="captioned-image-container"><figure><div class="image-link image2" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!cmMn!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 424w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 848w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 1272w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!cmMn!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png" width="248" height="207.7" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:670,&quot;width&quot;:800,&quot;resizeWidth&quot;:248,&quot;bytes&quot;:257716,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://thetshaped.dev/i/188025481?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb8ebf3ac-ad2f-415d-9e32-111e959c8e4e_800x800.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!cmMn!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 424w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 848w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 1272w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div></div></div></div></figure></div><p><strong>Follow me on <a href="https://www.linkedin.com/in/petarivanovv9/">LinkedIn</a></strong> | <strong><a href="https://x.com/intent/follow?screen_name=petarivanovv9">Twitter(X)</a></strong> |<strong> <a href="https://www.threads.net/@petarivanovv9">Threads</a></strong></p></div><p>Thank you for supporting this newsletter.</p><p>Consider sharing this post with your friends and get rewards.</p><p>You are the best! &#128591;</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!EvJY!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!EvJY!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 424w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 848w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!EvJY!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg" width="800" height="500" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:500,&quot;width&quot;:800,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:83253,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://thetshaped.dev/i/188025481?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!EvJY!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 424w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 848w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button class="pencraft pc-reset pencraft icon-container restack-image buttonBase-tqxFUX" tabindex="0" type="button"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button class="pencraft pc-reset pencraft icon-container view-image buttonBase-tqxFUX" tabindex="0" type="button"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://thetshaped.dev/p/the-3-layers-of-testing-ai-agents-unittests-mocking-alerts-judge-evals-production-agentic-systems?utm_source=substack&utm_medium=email&utm_content=share&action=share&quot;,&quot;text&quot;:&quot;Share&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://thetshaped.dev/p/the-3-layers-of-testing-ai-agents-unittests-mocking-alerts-judge-evals-production-agentic-systems?utm_source=substack&utm_medium=email&utm_content=share&action=share"><span>Share</span></a></p><div class="pullquote"><div class="captioned-image-container"><figure><div class="image-link image2" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!UzWz!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 424w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 848w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 1272w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!UzWz!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif" width="48" height="57.6" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/39609b45-f68c-4557-8772-8e2a45182842_500x600.gif&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:600,&quot;width&quot;:500,&quot;resizeWidth&quot;:48,&quot;bytes&quot;:7028,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:&quot;&quot;,&quot;type&quot;:&quot;image/gif&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://thetshaped.dev/i/188785536?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!UzWz!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 424w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 848w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 1272w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 1456w" sizes="auto, 100vw" loading="lazy"></picture><div></div></div></div></figure></div><p style="text-align: center;"><em><strong>Press the Like button if you found this post helpful</strong></em></p></div>]]></content:encoded></item><item><title><![CDATA[Don't Start With Multi-Agent AI Systems]]></title><description><![CDATA[When one agent should become many, when it shouldn't, and the supervisor pattern that makes the split work. (8 min)]]></description><link>https://thetshaped.dev/p/multi-agent-is-the-new-microservices-one-agent-vs-many-agents-supervisor-pattern</link><guid isPermaLink="false">https://thetshaped.dev/p/multi-agent-is-the-new-microservices-one-agent-vs-many-agents-supervisor-pattern</guid><pubDate>Sat, 12 Sep 2026 05:19:14 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/97a7a93c-b6e0-40e7-b0cd-42f3236a8345_2912x1632.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://thetshaped.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption"><span data-color="#9900ff" style="color: rgb(153, 0, 255);">Get my Full-Stack JavaScript Architect Bundle for FREE on newsletter signup:</span></p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><p>We've run this movie before. </p><p>A monolith gets uncomfortable, a conference talk says the future is lots of small services, everyone splits everything, and two years later half the industry is writing "we went back to the monolith" retrospectives. </p><p>That was microservices. </p><p>Multi-agent AI systems are speed-running the same arc right now. Same hype curve, same whiteboard-driven splits, same costs discovered in production.</p><p>I work on a system that runs <strong>15+ specialized agents behind a supervisor</strong>, and I want to make two arguments that sound contradictory but aren't. </p><p>First, the split is genuinely worth it; our system couldn't work as one agent. Second, you almost certainly shouldn't start there, and the right moment to split is measurable rather than aesthetic.</p><ul><li><p><em><a href="https://thetshaped.dev/p/multi-agent-is-the-new-microservices-one-agent-vs-many-agents-supervisor-pattern/?action=share">Share this post</a> &amp; I&#8217;ll send you some rewards for the referrals.</em></p></li></ul><div><hr></div><h2><a href="https://www.descope.com/blog/post/descope-mcp-server-claude-chatgpt?utm_source=tshapeddev-2026&amp;utm_medium=email&amp;utm_campaign=tshapeddev-2026">Run Your Auth With AI Agents (Partner)</a></h2><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://www.descope.com/blog/post/descope-mcp-server-claude-chatgpt?utm_source=tshapeddev-2026&amp;utm_medium=email&amp;utm_campaign=tshapeddev-2026" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!OQUP!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2800c196-e51e-451e-8663-0b7792d412f2_1200x620.png 424w, https://substackcdn.com/image/fetch/$s_!OQUP!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2800c196-e51e-451e-8663-0b7792d412f2_1200x620.png 848w, https://substackcdn.com/image/fetch/$s_!OQUP!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2800c196-e51e-451e-8663-0b7792d412f2_1200x620.png 1272w, https://substackcdn.com/image/fetch/$s_!OQUP!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2800c196-e51e-451e-8663-0b7792d412f2_1200x620.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!OQUP!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2800c196-e51e-451e-8663-0b7792d412f2_1200x620.png" width="1200" height="620" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/2800c196-e51e-451e-8663-0b7792d412f2_1200x620.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:620,&quot;width&quot;:1200,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:958745,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:&quot;https://www.descope.com/blog/post/descope-mcp-server-claude-chatgpt?utm_source=tshapeddev-2026&amp;utm_medium=email&amp;utm_campaign=tshapeddev-2026&quot;,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://thetshaped.dev/i/213818037?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2800c196-e51e-451e-8663-0b7792d412f2_1200x620.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!OQUP!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2800c196-e51e-451e-8663-0b7792d412f2_1200x620.png 424w, https://substackcdn.com/image/fetch/$s_!OQUP!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2800c196-e51e-451e-8663-0b7792d412f2_1200x620.png 848w, https://substackcdn.com/image/fetch/$s_!OQUP!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2800c196-e51e-451e-8663-0b7792d412f2_1200x620.png 1272w, https://substackcdn.com/image/fetch/$s_!OQUP!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2800c196-e51e-451e-8663-0b7792d412f2_1200x620.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button class="pencraft pc-reset pencraft icon-container restack-image buttonBase-tqxFUX" tabindex="0" type="button"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button class="pencraft pc-reset pencraft icon-container view-image buttonBase-tqxFUX" tabindex="0" type="button"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Coding agents are here to stay, but vibe-coding auth is dangerous business. Connect your AI agents to the <a href="https://www.descope.com/blog/post/descope-mcp-server-claude-chatgpt?utm_source=tshapeddev-2026&amp;utm_medium=email&amp;utm_campaign=tshapeddev-2026">Descope</a> MCP server instead!</p><p>This remote MCP server gives agents the ability to read documentation, manage users and tenants, configure auth flows, generate FGA schemas, review audit logs, and more&#8230;all through natural language.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.descope.com/blog/post/descope-mcp-server-claude-chatgpt?utm_source=tshapeddev-2026&amp;utm_medium=email&amp;utm_campaign=tshapeddev-2026&quot;,&quot;text&quot;:&quot;Get started on Claude or ChatGPT&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.descope.com/blog/post/descope-mcp-server-claude-chatgpt?utm_source=tshapeddev-2026&amp;utm_medium=email&amp;utm_campaign=tshapeddev-2026"><span>Get started on Claude or ChatGPT</span></a></p><p>(Thanks to <a href="https://www.descope.com/blog/post/descope-mcp-server-claude-chatgpt?utm_source=tshapeddev-2026&amp;utm_medium=email&amp;utm_campaign=tshapeddev-2026">Descope</a> for partnering on this post.)</p><div><hr></div><h2>Why one agent degrades</h2><p>A single agent scales beautifully until it doesn't, and the failure is gradual enough that teams miss it. </p><p>Three forces compound.</p><h3><strong>1. Tool choice gets worse as the tool list grows.</strong> </h3><p>With 5 tools, the model picks correctly nearly every time. With 25, the descriptions blur (<code>update_expense</code> vs <code>modify_expense_details</code> vs <code>edit_submitted_expense</code>) and it starts grabbing the almost-right one.</p><p>Which means the cheap fix comes before the split. Mark the long tail <code>defer_loading: true</code>, register a tool-search tool, and let the model pull in what this turn needs. </p><p>If the <em>active</em> list is still too wide after that, the domain is telling you something.</p><h3><strong>2. The system prompt becomes a legal document.</strong> </h3><p>Instructions for expense approvals, travel booking, and knowledge lookup all live in one prompt, and they start conflicting. "Always confirm before any write" (payments needs this) fights "resolve the question in as few turns as possible" (support wants that). </p><p>The model doesn't crash on contradictions. It obeys them <em>inconsistently</em>, which is worse.</p><h3><strong>3. The blast radius becomes total.</strong> </h3><p>Tune one sentence to fix the refund flow, and you've changed behavior <em>everywhere</em>.</p><p>Nobody can say what a prompt edit affects, so nobody wants to touch it. Same freeze that hits a scary legacy module, except the module is English prose.</p><p>None of this hurting yet? </p><p>Then you don't need multi-agent yet. One agent with eight tools and a coherent prompt outperforms a fleet of specialists connected by hope.</p><div><hr></div><h2>The supervisor pattern</h2><p>Once the pressure is real, the split that works in production looks like this.</p><p><strong>A supervisor</strong> sits at the front. Its only job is classification and routing. Read the intent, pick the specialist, hand off. It has no domain tools and does no domain work.</p><p>The moment your supervisor starts <em>answering</em> questions, you've rebuilt the mega-agent with extra steps.</p><p><strong>Specialists</strong> own one domain each (payments, approvals, travel, procurement, knowledge lookup). Each gets three narrow things:</p><ul><li><p><strong>Its own prompt.</strong> A page of instructions about one job, with no contradictions, because there's nothing to contradict.</p></li><li><p><strong>Its own toolset.</strong> Five to ten tools, all obviously distinct. Tool-choice accuracy comes back for free.</p></li><li><p><strong>Its own state.</strong> The conversation slice and working memory for its task, checkpointed so a handoff or a crash doesn't lose the thread.</p></li></ul><p>The counterintuitive part is that <strong>specialists get </strong><em><strong>better by knowing less</strong></em>. </p><p>A payments agent that has never heard of travel booking can't be confused by it. The narrowing is the feature, the same reason a focused module beats a God class, transplanted into prompt space.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!a2aP!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F85d0c28f-daa4-42a2-afc0-ffd5bf21dd9a_2318x1398.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!a2aP!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F85d0c28f-daa4-42a2-afc0-ffd5bf21dd9a_2318x1398.png 424w, https://substackcdn.com/image/fetch/$s_!a2aP!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F85d0c28f-daa4-42a2-afc0-ffd5bf21dd9a_2318x1398.png 848w, https://substackcdn.com/image/fetch/$s_!a2aP!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F85d0c28f-daa4-42a2-afc0-ffd5bf21dd9a_2318x1398.png 1272w, https://substackcdn.com/image/fetch/$s_!a2aP!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F85d0c28f-daa4-42a2-afc0-ffd5bf21dd9a_2318x1398.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!a2aP!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F85d0c28f-daa4-42a2-afc0-ffd5bf21dd9a_2318x1398.png" width="1456" height="878" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/85d0c28f-daa4-42a2-afc0-ffd5bf21dd9a_2318x1398.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:878,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:347496,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://thetshaped.dev/i/213818064?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F85d0c28f-daa4-42a2-afc0-ffd5bf21dd9a_2318x1398.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!a2aP!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F85d0c28f-daa4-42a2-afc0-ffd5bf21dd9a_2318x1398.png 424w, https://substackcdn.com/image/fetch/$s_!a2aP!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F85d0c28f-daa4-42a2-afc0-ffd5bf21dd9a_2318x1398.png 848w, https://substackcdn.com/image/fetch/$s_!a2aP!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F85d0c28f-daa4-42a2-afc0-ffd5bf21dd9a_2318x1398.png 1272w, https://substackcdn.com/image/fetch/$s_!a2aP!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F85d0c28f-daa4-42a2-afc0-ffd5bf21dd9a_2318x1398.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button class="pencraft pc-reset pencraft icon-container restack-image buttonBase-tqxFUX" tabindex="0" type="button"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button class="pencraft pc-reset pencraft icon-container view-image buttonBase-tqxFUX" tabindex="0" type="button"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><h2>The costs nobody puts on the slide</h2><p>Every one of these showed up for us. Budget for them before you split.</p><h3><strong>1. Routing becomes your biggest failure mode.</strong> </h3><p>Every misroute is a guaranteed bad experience, because the travel agent cannot answer a payments question no matter how good it is. </p><p>You need routing accuracy in your evals, ambiguous-intent test cases, and a fallback ("ask a clarifying question") for genuinely unclear requests.</p><h3><strong>2. Every hop is latency, tokens, and a colder cache.</strong> </h3><p>Supervisor classification is a model call before any real work starts. Handoffs are more. A three-hop flow can double your latency and your bill against a single-agent baseline. And prompt caching works on an exact prefix, so each agent now has its own cached prefix to warm instead of one shared one. Measure it, because users feel it.</p><h3><strong>3. Handoffs need a contract.</strong> </h3><p>Say the approvals agent needs a payment executed mid-flow. What crosses the boundary? Pass raw conversation history, and specialists drown in irrelevant context. Pass too little and the user repeats themselves. </p><p>What works for us is a typed handoff, a structured summary of task, entities, and decisions so far, as a Pydantic model because our agents are Python:</p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;python&quot;,&quot;nodeId&quot;:null}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-python">from pydantic import BaseModel


class Handoff(BaseModel):
    task: str                        # "execute the approved refund"
    entities: dict[str, str]         # {"expense_id": "exp_9x2", "user_id": "u_17"}
    decisions: list[str]             # ["manager approved 2026-07-01", "amount 4200 cents"]
    open_questions: list[str] = []   # what the next agent still has to resolve
    source_agent: str                # "approvals_agent", for the trace</code></pre></div><p>Boundaries between agents are boundaries like any other.</p><h3><strong>3. Debugging spans agents now.</strong> </h3><p>"The answer was wrong" might live in the supervisor's routing, the specialist's reasoning, or a tool result two handoffs back. </p><p>Without one trace tree per request across all hops, you're doing archaeology.</p><h3><strong>4. Each new agent carries infrastructure, unless you template it.</strong></h3><p>A specialist needs its graph compiled, its state checkpointed, its invocations logged, its errors handled, and none of that is domain work. </p><p>In the system I work on, every specialist extends one shared <code>BaseAgent</code> that owns all four, so a new agent declares a name, a state shape, and its nodes. The supervisor, state graph, and checkpointer stay shared infrastructure too.</p><div><hr></div><h2>The payoff that justifies all of it</h2><p>Here's the operational win that made me a believer. </p><p>When each agent is a self-contained unit (prompt, tools, state), you can run <strong>two versions of one specialist side by side</strong>. </p><p>We literally keep <code>supplier_management_agent</code> and <code>supplier_management_agent_v2</code> as siblings. The router sends a slice of traffic to v2, evals compare the two on live work, and rollback is a config flip.</p><p>Try that with a mega-agent. Rewriting one section of a 2,000-line prompt changes behavior globally, instantly, for everyone. There is no gradual rollout of a paragraph.</p><p>The same boundary buys you a second thing: <strong>model choice per specialist</strong>. </p><p>Swapping models mid-conversation throws away the cached prefix, so a single agent is stuck on one model for the whole run. A separate specialist can sit on a cheaper, faster model without touching the main loop's cache. </p><p><strong>Agent boundaries are deployment boundaries, and deployment boundaries are what let you ship changes without holding your breath.</strong></p><div><hr></div><h2>Conclusion</h2><p>The microservices lesson transfers whole: </p><blockquote><p><strong>Split along domains, never along workflow steps.</strong> </p></blockquote><p>A payments agent and a travel agent are real domains, with different tools, rules, and failure modes. "Agent 1 gathers info, agent 2 makes a plan, agent 3 executes" is not a domain split. It's one workflow sliced into three prompts that must all change together, a distributed monolith with extra model calls. </p><p><strong>Two agents that can never be deployed or evaluated independently are one agent.</strong> </p><p>And if the supervisor keeps bouncing users between two specialists mid-conversation, those two are telling you they're one domain.</p><p>Multi-agent isn't hype. It's what a serious agent system grows into, the way serious codebases grow modules. Growth is the operative word. The mega-agent's pain arrives on a schedule (tool sprawl, prompt contradictions, frozen iteration), and the right response is the one that survived the microservices decade. </p><p>Start with one, try the cheap fixes, watch for the measurable signals, split along domains, and pay the routing-and-tracing tax with your eyes open.</p><p>One good agent beats five mediocre ones. Five focused agents beat one confused one. The skill is knowing which side of that line you're on, and that's a measurement rather than a vibe.</p><div><hr></div><h2>&#128204; TL;DR</h2><ul><li><p><strong>Start with one agent.</strong> Split under measured pressure, not because the architecture diagram looks smarter with more boxes.</p></li><li><p><strong>The splitting signals are concrete:</strong> a tool list you can't shrink even after deferring the long tail behind tool search, a system prompt that contradicts itself, and teams who can't change one behavior without breaking another.</p></li><li><p><strong>The supervisor pattern:</strong> a router classifies intent and hands off to a specialist, each with its own narrow prompt, own small toolset, own state. Specialists get <em>better</em> by knowing less.</p></li><li><p><strong>The costs are real:</strong> an extra model call before any work starts, misroutes (your new top failure mode), handoff contracts, traces that span agents, and a prompt cache per agent instead of one.</p></li><li><p>A shared <code>BaseAgent</code><strong> keeps the per-agent cost flat.</strong> Graph compilation, checkpointing, logging, and error handling are inherited; a new specialist is a name, a state shape, and nodes.</p></li><li><p><strong>The killer operational win is versioned coexistence.</strong> Run <code>agent_v2</code> next to <code>v1</code>, route a slice of traffic, roll back with a config flip. You can't A/B one paragraph of a 2,000-line mega-prompt.</p></li><li><p>Split along <strong>domains</strong> (payments, travel, knowledge), never along "steps" of a workflow that always run together. A distributed monolith is still a monolith, even with agents.</p></li></ul><div><hr></div><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://thetshaped.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Subscribe to get practical tips on React.js, Node.js, AI Agents, Agentic Systems, and Software Architecture delivered straight to your inbox:</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div class="pullquote"><div class="captioned-image-container"><figure><div class="image-link image2" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!cmMn!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 424w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 848w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 1272w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!cmMn!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png" width="248" height="207.7" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:670,&quot;width&quot;:800,&quot;resizeWidth&quot;:248,&quot;bytes&quot;:257716,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://thetshaped.dev/i/188025481?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb8ebf3ac-ad2f-415d-9e32-111e959c8e4e_800x800.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!cmMn!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 424w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 848w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 1272w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div></div></div></div></figure></div><p><strong>Follow me on <a href="https://www.linkedin.com/in/petarivanovv9/">LinkedIn</a></strong> | <strong><a href="https://x.com/intent/follow?screen_name=petarivanovv9">Twitter(X)</a></strong> |<strong> <a href="https://www.threads.net/@petarivanovv9">Threads</a></strong></p></div><p>Thank you for supporting this newsletter.</p><p>Consider sharing this post with your friends and get rewards.</p><p>You are the best! &#128591;</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!EvJY!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!EvJY!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 424w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 848w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!EvJY!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg" width="800" height="500" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:500,&quot;width&quot;:800,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:83253,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://thetshaped.dev/i/188025481?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!EvJY!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 424w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 848w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button class="pencraft pc-reset pencraft icon-container restack-image buttonBase-tqxFUX" tabindex="0" type="button"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button class="pencraft pc-reset pencraft icon-container view-image buttonBase-tqxFUX" tabindex="0" type="button"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://thetshaped.dev/p/multi-agent-is-the-new-microservices-one-agent-vs-many-agents-supervisor-pattern?utm_source=substack&utm_medium=email&utm_content=share&action=share&quot;,&quot;text&quot;:&quot;Share&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://thetshaped.dev/p/multi-agent-is-the-new-microservices-one-agent-vs-many-agents-supervisor-pattern?utm_source=substack&utm_medium=email&utm_content=share&action=share"><span>Share</span></a></p><div class="pullquote"><div class="captioned-image-container"><figure><div class="image-link image2" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!UzWz!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 424w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 848w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 1272w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!UzWz!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif" width="48" height="57.6" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/39609b45-f68c-4557-8772-8e2a45182842_500x600.gif&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:600,&quot;width&quot;:500,&quot;resizeWidth&quot;:48,&quot;bytes&quot;:7028,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:&quot;&quot;,&quot;type&quot;:&quot;image/gif&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://thetshaped.dev/i/188785536?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!UzWz!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 424w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 848w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 1272w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 1456w" sizes="auto, 100vw" loading="lazy"></picture><div></div></div></div></figure></div><p style="text-align: center;"><em><strong>Press the Like button if you found this post helpful</strong></em></p></div>]]></content:encoded></item><item><title><![CDATA[Your Agent Isn't Dumb. Your Tools Are.]]></title><description><![CDATA[7 rules for tool definitions a model can't misread, starting with the sentence that fixes the most bugs. (8 min)]]></description><link>https://thetshaped.dev/p/7-rules-tool-definitions-ai-agents-tool-design-software-design-architecture</link><guid isPermaLink="false">https://thetshaped.dev/p/7-rules-tool-definitions-ai-agents-tool-design-software-design-architecture</guid><pubDate>Tue, 08 Sep 2026 12:19:46 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/b2e87dc2-810c-4ca8-af69-cd8309549e28_2912x1632.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://thetshaped.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption"><span data-color="#9900ff" style="color: rgb(153, 0, 255);">Get my Full-Stack JavaScript Architect Bundle for FREE on newsletter signup:</span></p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><p>When an agent misbehaves, everyone stares at the prompt. </p><p>Add another "IMPORTANT:", beg the model in capital letters, ship it, and hope.</p><p>Most of those bugs aren't prompt bugs. They're tool bugs. A fuzzy description, two tools that overlap, an error message the model can't act on.</p><p>Here's the mental model that changed how I build these: </p><blockquote><p><strong>A tool definition is an API you publish for a client that guesses.</strong> </p></blockquote><p>A human developer reads your docs and checks the types. The model reads the name, the description, and the schema, then infers the rest. It never sees your source. So the contract does all the work, and every ambiguity in it becomes behavior.</p><p>The model decides. Your code executes. </p><p>Don't ask it to <em>perform</em> a change; ask for a small structured decision and let deterministic code apply it.</p><ul><li><p><em><a href="https://thetshaped.dev/p/7-rules-tool-definitions-ai-agents-tool-design-software-design-architecture/?action=share">Share this post</a> &amp; I&#8217;ll send you some rewards for the referrals.</em></p></li></ul><div><hr></div><h2><a href="https://www.descope.com/blog/post/descope-mcp-server-claude-chatgpt?utm_source=tshapeddev-2026&amp;utm_medium=email&amp;utm_campaign=tshapeddev-2026">Run Your Auth With AI Agents (Partner)</a></h2><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://www.descope.com/blog/post/descope-mcp-server-claude-chatgpt?utm_source=tshapeddev-2026&amp;utm_medium=email&amp;utm_campaign=tshapeddev-2026" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!OQUP!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2800c196-e51e-451e-8663-0b7792d412f2_1200x620.png 424w, https://substackcdn.com/image/fetch/$s_!OQUP!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2800c196-e51e-451e-8663-0b7792d412f2_1200x620.png 848w, https://substackcdn.com/image/fetch/$s_!OQUP!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2800c196-e51e-451e-8663-0b7792d412f2_1200x620.png 1272w, https://substackcdn.com/image/fetch/$s_!OQUP!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2800c196-e51e-451e-8663-0b7792d412f2_1200x620.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!OQUP!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2800c196-e51e-451e-8663-0b7792d412f2_1200x620.png" width="1200" height="620" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/2800c196-e51e-451e-8663-0b7792d412f2_1200x620.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:620,&quot;width&quot;:1200,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:958745,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:&quot;https://www.descope.com/blog/post/descope-mcp-server-claude-chatgpt?utm_source=tshapeddev-2026&amp;utm_medium=email&amp;utm_campaign=tshapeddev-2026&quot;,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://thetshaped.dev/i/213818037?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2800c196-e51e-451e-8663-0b7792d412f2_1200x620.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!OQUP!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2800c196-e51e-451e-8663-0b7792d412f2_1200x620.png 424w, https://substackcdn.com/image/fetch/$s_!OQUP!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2800c196-e51e-451e-8663-0b7792d412f2_1200x620.png 848w, https://substackcdn.com/image/fetch/$s_!OQUP!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2800c196-e51e-451e-8663-0b7792d412f2_1200x620.png 1272w, https://substackcdn.com/image/fetch/$s_!OQUP!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2800c196-e51e-451e-8663-0b7792d412f2_1200x620.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button class="pencraft pc-reset pencraft icon-container restack-image buttonBase-tqxFUX" tabindex="0" type="button"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button class="pencraft pc-reset pencraft icon-container view-image buttonBase-tqxFUX" tabindex="0" type="button"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Coding agents are here to stay, but vibe-coding auth is dangerous business. Connect your AI agents to the <a href="https://www.descope.com/blog/post/descope-mcp-server-claude-chatgpt?utm_source=tshapeddev-2026&amp;utm_medium=email&amp;utm_campaign=tshapeddev-2026">Descope</a> MCP server instead!</p><p>This remote MCP server gives agents the ability to read documentation, manage users and tenants, configure auth flows, generate FGA schemas, review audit logs, and more&#8230;all through natural language.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.descope.com/blog/post/descope-mcp-server-claude-chatgpt?utm_source=tshapeddev-2026&amp;utm_medium=email&amp;utm_campaign=tshapeddev-2026&quot;,&quot;text&quot;:&quot;Get started on Claude or ChatGPT&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.descope.com/blog/post/descope-mcp-server-claude-chatgpt?utm_source=tshapeddev-2026&amp;utm_medium=email&amp;utm_campaign=tshapeddev-2026"><span>Get started on Claude or ChatGPT</span></a></p><p>(Thanks to <a href="https://www.descope.com/blog/post/descope-mcp-server-claude-chatgpt?utm_source=tshapeddev-2026&amp;utm_medium=email&amp;utm_campaign=tshapeddev-2026">Descope</a> for partnering on this post.)</p><div><hr></div><h2>1. One tool, one job</h2><p>The tempting design is the multiplexer: <code>manage_expense(action: "create" | "update" | "submit" | "delete", ...)</code>. </p><p>It feels tidy. You've stacked three decisions, though. </p><p>The model picks the tool, then picks the action, then works out which of the umbrella's parameters apply to <em>this</em> action. Every layer is a place to guess wrong, and the schema, necessarily the union of all modes, can no longer say what's actually required.</p><p>Four sharp tools, each with exactly the parameters that mode needs, turn a three-step guess into one clean selection. </p><p>Same reason your REST API doesn't have a single <code>/do</code> endpoint with an <code>action</code> field.</p><div><hr></div><h2>2. The description is a prompt, not documentation</h2><p>Most tool descriptions read like docstrings: "Fetches expense data".</p><p>Technically true, behaviorally useless. The model uses descriptions to <em>choose</em>, and that sentence doesn't say when this tool wins over its neighbors.</p><p>Write the description as routing guidance:</p><blockquote><p><em>"Get the full details of ONE expense when you already have its ID (from list_expenses or the user). Do NOT use this to search. Use search_expenses for finding expenses by description, date, or amount."</em></p></blockquote><p>Three things in there earn their tokens. </p><blockquote><p>What it does, <strong>when to reach for it</strong>, and <strong>when not to</strong>, with a pointer to the right alternative. </p></blockquote><p>That negative space is the highest-leverage sentence in tool design, because overlapping tools are where selection accuracy goes to die. </p><p>If you can't write the "do not" line without contradicting another tool's, the tools overlap, and one of them should change.</p><div><hr></div><h2>3. Type the parameters like you mean it, then show one call</h2><p>The parameter schema isn't decoration. </p><p>Set <code>strict: true</code> on the tool definition, and the API <em>enforces</em> it, so the model can't hand you a value the schema forbids. That makes the schema the strongest instruction you have. Loose schemas leak guesses:</p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;python&quot;,&quot;nodeId&quot;:null}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-python">from datetime import date
from typing import Literal

from pydantic import BaseModel, ConfigDict, Field, PositiveInt


# &#9940; An invitation to improvise
class LooseParams(BaseModel):
    status: str        # model sends "Approved", "APPROVED", "approved &#9989;"
    amount: str        # "100", "$100", "one hundred"
    expense_date: str  # "tomorrow"


# &#9989; The schema does the instructing
class StrictParams(BaseModel):
    model_config = ConfigDict(extra="forbid")  # additionalProperties: false, which strict mode requires
    status: Literal["pending", "approved", "rejected"]
    amount_cents: PositiveInt                  # the &gt; 0 bound isn't in the strict subset; the SDK checks it client-side
    expense_date: date = Field(description="Posting date, e.g. 2026-07-02")  # emits format "date", which the API enforces</code></pre></div><p>An enum isn't just validation. The model <em>sees</em> the allowed values and picks from them instead of inventing, so correction moves from runtime (parse, fail, retry, pay tokens) to selection time.</p><p>Then add the part a schema can't express. It can say <code>labels</code> is an array of strings; it can't say <em>these two fields travel together</em>, or that your IDs look like <code>USR-12345</code>, or that <code>parent_id</code> only makes sense for sub-tickets. That's where calls go wrong on complex inputs, and it's why tool definitions take an <code>input_examples</code> field. Anthropic measured it: one or two sample calls took accuracy on complex parameter handling from <a href="https://www.anthropic.com/engineering/advanced-tool-use">72% to 90%</a>. For a few lines of JSON, that's the cheapest reliability you'll ever buy.</p><div><hr></div><h2>4. Return errors the model can act on</h2><p>Here's the asymmetry teams miss.</p><p>The tool's <em>return value</em> is part of the conversation too, and the model reasons over it next. A raw stack trace, a bare <code>500</code>, or <code>{"error": true}</code> is a dead end. The model can't fix what it can't interpret, so it flails. It retries the same call, hallucinates an apology, or worse, <em>pretends the call worked</em>.</p><p>Write error returns for the caller who guesses:</p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;json&quot;,&quot;nodeId&quot;:null}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-json">{
  "error": "expense_not_found",
  "message": "No expense with ID 'exp_9x2'. Expense IDs come from list_expenses; call it first if you don't have a current ID.",
  "retryable": false
}</code></pre></div><p>A typed code (so your deterministic layer can count failures), a human explanation of <em>what to do differently</em>, and a retry hint. </p><p>We've fixed entire misbehavior classes by rewriting error messages, with zero prompt changes and zero model changes. The error message <em>is</em> prompt engineering. It just arrives late.</p><p>Parallel calls have their own trap. When the model fires three tools in one turn, all three results go back in a single message, and the tool that failed still needs a result block flagged as an error. Drop it, or split the results across messages, and you quietly teach the model to stop calling tools in parallel at all.</p><div><hr></div><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://thetshaped.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption"><span>Join </span><strong>30,000+ engineers</strong><span> getting one practical issue a week on production-grade React, Node, Architecture, and </span><strong>real AI systems</strong><span>, and leveling up from Mid &#8594; Senior &#8594; Architect.</span></p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div><hr></div><h2>5. Bound every output</h2><p>A tool that returns an unbounded list is a context-window bomb. </p><p><code>list_expenses</code> happily returns 4,000 rows, that JSON lands in the conversation, and the actual instructions drown in table data. Or you blow past the window and silently truncate something that mattered (<a href="https://thetshaped.dev/i/205998753/5-silent-context-window-overflow">failure #5 from the 7 things list</a>).</p><p>So every tool return gets a budget. Default limits (<code>limit: 20</code> unless asked), pagination cursors so the model asks for more <em>deliberately</em>, and summarized shapes.</p><p>The model rarely needs every field of every row. It needs enough to pick the next step, and it can fetch one record in full when it commits.</p><div><hr></div><h2>6. Make destructive tools safe to fumble</h2><p>The model will eventually call a destructive tool at the wrong moment. </p><p>It'll double-fire a payment on a retry, or delete a record off a hallucinated ID. Design for the fumble instead of hoping against it:</p><ul><li><p><strong>Idempotency for the double-fire.</strong> An idempotency key on execute-style tools makes the retry harmless, the same mechanism that keeps <a href="https://thetshaped.dev/p/how-to-stop-charging-customers-twice-practical-idempotency-guide-http-message-queues">payment APIs safe to retry</a>.</p></li><li><p><strong>Two-step commit for the big red buttons.</strong> <code>prepare_refund</code> returns a summary and a confirmation token; <code>execute_refund(token)</code> moves the money. Make that token single-use and short-lived, or the replay you built this to prevent walks right back in. The prepare step also gives a human a checkpoint while everything is still reversible.</p></li><li><p><strong>Validate referenced IDs exist</strong> before acting. An ID in a tool call is a claim until you've checked it.</p></li></ul><div><hr></div><h2>7. Fewer tools beat more tools</h2><p>Every tool you register is another candidate in every selection, and it costs tokens on every call. </p><p>Anthropic's own example is five MCP servers, 58 tools, about <strong>55K tokens of definitions before any work begins</strong>. The accuracy cost is measurable too. On the same benchmark, letting the model <em>search</em> for tools instead of loading them all took one model from 49% to 74%, and a newer one from 79.5% to 88.1%.</p><p>So audit like you'd audit dependencies. Merge near-duplicates, delete the tool nobody's flow needs, move rare admin operations to a separate agent. Then mark the long tail <code>defer_loading: true</code> and register a tool-search tool, so the model sees the handful that matter this turn and pulls in the rest on demand. </p><p>Don't defer everything, though; the search tool itself has to stay loaded or the request errors out.</p><p>One cost detail before you touch that list. Tool definitions sit at the very front of the prompt, ahead of the system prompt and the conversation, so editing them mid-conversation throws away your whole cached prefix. Tool search dodges that by <em>appending</em> what it finds instead of swapping the list.</p><p>When the active list still can't shrink, the domain is genuinely that wide, and that's not a tool problem anymore. It's the signal your agent wants to be several agents, each with a short list again.</p><h2>Conclusion</h2><p>Prompt engineering gets the conference talks, but <strong>tool design</strong> is where agent reliability lives. The contract is enforced, read on every call, and immune to the model's mood.</p><p>None of it is new. It's API design for the strangest client you've ever had, one that reads the contract and guesses the rest. Write it as the guessing depends on it. It does.</p><div><hr></div><h2>&#128204; TL;DR</h2><ul><li><p><strong>One tool, one job.</strong> A <code>manage_expense(action, ...)</code> swiss-army tool turns one selection into three guesses.</p></li><li><p><strong>The description is a prompt.</strong> Say when to use it, and when <em>not</em> to, with a pointer to the right alternative.</p></li><li><p><strong>Type the parameters like you mean it</strong>, then add one example call. Enums plus strict mode make bad calls impossible; Anthropic measured examples taking complex parameter handling from 72% to 90%.</p></li><li><p><strong>Return errors the model can act on.</strong> "Expense exp_123 not found; IDs come from list_expenses" beats a stack trace.</p></li><li><p><strong>Bound every output.</strong> A tool that can return 10,000 rows can eat the conversation.</p></li><li><p><strong>Make destructive tools safe to fumble.</strong> Idempotent where possible, single-use confirmation token where not.</p></li><li><p><strong>Fewer tools beat more tools.</strong> 58 tools across five MCP servers burn ~55K tokens before any work starts.</p></li></ul><div><hr></div><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://thetshaped.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Subscribe to get practical tips on React.js, Node.js, AI Agents, Agentic Systems, and Software Architecture delivered straight to your inbox:</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div class="pullquote"><div class="captioned-image-container"><figure><div class="image-link image2" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!cmMn!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 424w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 848w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 1272w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!cmMn!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png" width="248" height="207.7" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:670,&quot;width&quot;:800,&quot;resizeWidth&quot;:248,&quot;bytes&quot;:257716,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://thetshaped.dev/i/188025481?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb8ebf3ac-ad2f-415d-9e32-111e959c8e4e_800x800.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!cmMn!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 424w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 848w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 1272w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div></div></div></div></figure></div><p><strong>Follow me on <a href="https://www.linkedin.com/in/petarivanovv9/">LinkedIn</a></strong> | <strong><a href="https://x.com/intent/follow?screen_name=petarivanovv9">Twitter(X)</a></strong> |<strong> <a href="https://www.threads.net/@petarivanovv9">Threads</a></strong></p></div><p>Thank you for supporting this newsletter.</p><p>Consider sharing this post with your friends and get rewards.</p><p>You are the best! &#128591;</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!EvJY!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!EvJY!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 424w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 848w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!EvJY!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg" width="800" height="500" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:500,&quot;width&quot;:800,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:83253,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://thetshaped.dev/i/188025481?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!EvJY!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 424w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 848w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button class="pencraft pc-reset pencraft icon-container restack-image buttonBase-tqxFUX" tabindex="0" type="button"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button class="pencraft pc-reset pencraft icon-container view-image buttonBase-tqxFUX" tabindex="0" type="button"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://thetshaped.dev/p/7-rules-tool-definitions-ai-agents-tool-design-software-design-architecture?utm_source=substack&utm_medium=email&utm_content=share&action=share&quot;,&quot;text&quot;:&quot;Share&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://thetshaped.dev/p/7-rules-tool-definitions-ai-agents-tool-design-software-design-architecture?utm_source=substack&utm_medium=email&utm_content=share&action=share"><span>Share</span></a></p><div class="pullquote"><div class="captioned-image-container"><figure><div class="image-link image2" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!UzWz!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 424w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 848w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 1272w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!UzWz!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif" width="48" height="57.6" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/39609b45-f68c-4557-8772-8e2a45182842_500x600.gif&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:600,&quot;width&quot;:500,&quot;resizeWidth&quot;:48,&quot;bytes&quot;:7028,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:&quot;&quot;,&quot;type&quot;:&quot;image/gif&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://thetshaped.dev/i/188785536?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!UzWz!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 424w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 848w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 1272w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 1456w" sizes="auto, 100vw" loading="lazy"></picture><div></div></div></div></figure></div><p style="text-align: center;"><em><strong>Press the Like button if you found this post helpful</strong></em></p></div>]]></content:encoded></item><item><title><![CDATA[Stop Throwing new Error(). It's Lying to Your Logs.]]></title><description><![CDATA[One distinction that sets your HTTP status, your log level, and who gets paged. (6 min)]]></description><link>https://thetshaped.dev/p/domain-errors-are-features-system-errors-are-bugs-error-handling</link><guid isPermaLink="false">https://thetshaped.dev/p/domain-errors-are-features-system-errors-are-bugs-error-handling</guid><pubDate>Sat, 29 Aug 2026 04:19:38 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/f96f4d17-ce65-42b3-9c1a-4f44def8bc98_2912x1632.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://thetshaped.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption"><span data-color="#9900ff" style="color: rgb(153, 0, 255);">Get my Full-Stack JavaScript Architect Bundle for FREE on newsletter signup:</span></p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div><hr></div><p>A user types their email wrong. </p><p>Your API fires back <code>500 Internal Server Error</code>. </p><p>A log line lands at <code>error</code> level, and someone on call gets paged over a typo.</p><p>Except nothing on your side broke. Someone fumbled a form, and your code can&#8217;t tell that apart from the database falling over, because both went out the same door:</p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;typescript&quot;,&quot;nodeId&quot;:null}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-typescript">throw new Error('something went wrong');</code></pre></div><p><strong>When every error looks the same, your app spends its life guessing.</strong></p><p>Is this a 400 or a 500? Should the user see this message? Is this a shrug or a 3 AM page? </p><p>A bare <code>Error</code> can&#8217;t answer any of it, so let&#8217;s give it the one thing it&#8217;s missing.</p><ul><li><p><em><a href="https://thetshaped.dev/p/domain-errors-are-features-system-errors-are-bugs-error-handling/?action=share">Share this post</a> &amp; I&#8217;ll send you some rewards for the referrals.</em></p></li></ul><div><hr></div><h2><strong><a href="https://miro.pxf.io/qWGNky">Get into the flow of work with Miro Flows (Partner)</a></strong></h2><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://miro.pxf.io/qWGNky" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!AMKI!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F479ed91d-41f9-4283-98e1-29488c8c5502_1600x1200.avif 424w, https://substackcdn.com/image/fetch/$s_!AMKI!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F479ed91d-41f9-4283-98e1-29488c8c5502_1600x1200.avif 848w, https://substackcdn.com/image/fetch/$s_!AMKI!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F479ed91d-41f9-4283-98e1-29488c8c5502_1600x1200.avif 1272w, https://substackcdn.com/image/fetch/$s_!AMKI!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F479ed91d-41f9-4283-98e1-29488c8c5502_1600x1200.avif 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!AMKI!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F479ed91d-41f9-4283-98e1-29488c8c5502_1600x1200.avif" width="1456" height="1092" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/479ed91d-41f9-4283-98e1-29488c8c5502_1600x1200.avif&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1092,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:46215,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/avif&quot;,&quot;href&quot;:&quot;https://miro.pxf.io/qWGNky&quot;,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://thetshaped.dev/i/213128205?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F479ed91d-41f9-4283-98e1-29488c8c5502_1600x1200.avif&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!AMKI!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F479ed91d-41f9-4283-98e1-29488c8c5502_1600x1200.avif 424w, https://substackcdn.com/image/fetch/$s_!AMKI!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F479ed91d-41f9-4283-98e1-29488c8c5502_1600x1200.avif 848w, https://substackcdn.com/image/fetch/$s_!AMKI!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F479ed91d-41f9-4283-98e1-29488c8c5502_1600x1200.avif 1272w, https://substackcdn.com/image/fetch/$s_!AMKI!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F479ed91d-41f9-4283-98e1-29488c8c5502_1600x1200.avif 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button class="pencraft pc-reset pencraft icon-container restack-image buttonBase-tqxFUX" tabindex="0" type="button"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button class="pencraft pc-reset pencraft icon-container view-image buttonBase-tqxFUX" tabindex="0" type="button"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p><a href="https://miro.pxf.io/qWGNky">Miro</a> is the collaborative workspace where agents and teams converge to think, decide, and build together.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://miro.pxf.io/qWGNky&quot;,&quot;text&quot;:&quot;Try Miro for free&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://miro.pxf.io/qWGNky"><span>Try Miro for free</span></a></p><p>(Thanks to <a href="https://miro.pxf.io/qWGNky">Miro</a> for partnering on this post.)</p><div><hr></div><h2><strong>Every error is either expected or a surprise</strong></h2><p><strong>Domain errors</strong>: someone broke a rule you <em>expected</em> them to break. Bad input, a forbidden action, a withdrawal bigger than the balance. These aren&#8217;t bugs; they&#8217;re your app working exactly as designed. You saw them coming, so you can say something useful back.</p><p><strong>System errors</strong>: something broke that you <em>didn&#8217;t</em> expect. The DB connection dropped, an upstream API timed out, you ran out of memory. The user can&#8217;t fix any of these, and a polite message won&#8217;t help.</p><p>That&#8217;s the entire distinction, and the moment your code knows which kind it&#8217;s holding, four things it used to guess at become automatic:</p><ul><li><p>the <strong>HTTP status</strong> it returns</p></li><li><p>the <strong>message</strong> the user sees</p></li><li><p>the <strong>level</strong> it logs at</p></li><li><p>whether it <strong>wakes someone up</strong></p></li></ul><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!28I_!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb86148fa-ec40-4337-9370-a560e25096de_1728x824.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!28I_!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb86148fa-ec40-4337-9370-a560e25096de_1728x824.png 424w, https://substackcdn.com/image/fetch/$s_!28I_!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb86148fa-ec40-4337-9370-a560e25096de_1728x824.png 848w, https://substackcdn.com/image/fetch/$s_!28I_!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb86148fa-ec40-4337-9370-a560e25096de_1728x824.png 1272w, https://substackcdn.com/image/fetch/$s_!28I_!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb86148fa-ec40-4337-9370-a560e25096de_1728x824.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!28I_!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb86148fa-ec40-4337-9370-a560e25096de_1728x824.png" width="1456" height="694" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b86148fa-ec40-4337-9370-a560e25096de_1728x824.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:694,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:166823,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://thetshaped.dev/i/213128205?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb86148fa-ec40-4337-9370-a560e25096de_1728x824.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!28I_!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb86148fa-ec40-4337-9370-a560e25096de_1728x824.png 424w, https://substackcdn.com/image/fetch/$s_!28I_!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb86148fa-ec40-4337-9370-a560e25096de_1728x824.png 848w, https://substackcdn.com/image/fetch/$s_!28I_!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb86148fa-ec40-4337-9370-a560e25096de_1728x824.png 1272w, https://substackcdn.com/image/fetch/$s_!28I_!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb86148fa-ec40-4337-9370-a560e25096de_1728x824.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button class="pencraft pc-reset pencraft icon-container restack-image buttonBase-tqxFUX" tabindex="0" type="button"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button class="pencraft pc-reset pencraft icon-container view-image buttonBase-tqxFUX" tabindex="0" type="button"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><h2><strong>The class you extend is the classification</strong></h2><p><span>You only need to answer one question to tell the buckets apart: </span><em>was this expected or not?</em><span> </span></p><p><span>I encode the answer in the type itself. </span></p><p><span>One shared base carries the two things every error needs: a status and a stable code, and the two buckets are two abstract classes on top of it.</span></p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;typescript&quot;,&quot;nodeId&quot;:null}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-typescript">abstract class AppError extends Error {
  abstract readonly status: number // the HTTP status it maps to
  abstract readonly code: string   // a stable, machine-readable code

  constructor(message: string, options?: ErrorOptions) {
    super(message, options) // native Error.cause keeps the original around
    this.name = this.constructor.name
  }
}

// The two buckets, as types.
abstract class DomainError extends AppError {} // expected &#8212; message safe to show
abstract class SystemError extends AppError {} // unexpected &#8212; message stays server-side</code></pre></div><p>Then the actual errors are tiny. A few domain ones, a couple of system ones, and you add more as you need them:</p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;typescript&quot;,&quot;nodeId&quot;:null}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-typescript">class ValidationError extends DomainError {
  readonly status = 400
  readonly code = 'VALIDATION_ERROR'
}

class InsufficientFundsError extends DomainError {
  readonly status = 422
  readonly code = 'INSUFFICIENT_FUNDS'
}

class DatabaseError extends SystemError {
  readonly status = 500
  readonly code = 'DATABASE_ERROR'
}</code></pre></div><p><code>status</code> and <code>code</code> live on the base on purpose. The code that handles these never casts anything or parses a string; it just reads the fields.</p><p>Why a class split and not just a check on <code>status &lt; 500</code>? </p><p>Because status is an HTTP opinion, and these errors outlive HTTP. </p><p>A GraphQL resolver or a queue consumer has no status code to check, but it still has to answer the same question. Real message or generic? Warn or page?</p><div><hr></div><h2><strong>One </strong><code>instanceof</code><strong> routes every error</strong></h2><p>Here's where it pays off. One piece of middleware handles everything, and the logic is short:</p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;typescript&quot;,&quot;nodeId&quot;:null}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-typescript">function errorHandler(err: unknown, req: Request, res: Response, next: NextFunction) {
  // Already streaming a response? We can't change the status now. Let Express close the connection.
  if (res.headersSent) return next(err)

  // Domain error: expected. Tell the user what happened, log it calm.
  if (err instanceof DomainError) {
    logger.warn(err.message, { code: err.code, path: req.path })
    return res.status(err.status).json({
      error: { code: err.code, message: err.message },
    })
  }

  // Everything else is a surprise: a system error we wrapped, or a bug we didn't.
  // Same treatment. Hide the details, log loud, return a 5xx.
  logger.error('Unhandled error', {
    message: err instanceof Error ? err.message : String(err),
    stack: err instanceof Error ? err.stack : undefined,
    path: req.path,
  })
  return res.status(err instanceof SystemError ? err.status : 500).json({
    error: { code: 'INTERNAL_ERROR', message: 'Something went wrong on our end.' },
  })
}</code></pre></div><p>A domain error gets the real message and the right status. <em>Everything else</em> gets the same generic message, because the user can&#8217;t do anything about any of them. </p><p>A wrapped system error only adds its status: an <code>ExternalServiceError</code> can carry a 503 so the client knows to retry later, and an unwrapped bug stays a plain 500.</p><p>No <code>if (err.message.includes('not found'))</code> and no switch statement. One <code>instanceof</code> does the routing.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!2iC0!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F26f3ca39-5f3d-4f17-bbe7-42c2cf08b52d_1668x956.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!2iC0!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F26f3ca39-5f3d-4f17-bbe7-42c2cf08b52d_1668x956.png 424w, https://substackcdn.com/image/fetch/$s_!2iC0!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F26f3ca39-5f3d-4f17-bbe7-42c2cf08b52d_1668x956.png 848w, https://substackcdn.com/image/fetch/$s_!2iC0!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F26f3ca39-5f3d-4f17-bbe7-42c2cf08b52d_1668x956.png 1272w, https://substackcdn.com/image/fetch/$s_!2iC0!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F26f3ca39-5f3d-4f17-bbe7-42c2cf08b52d_1668x956.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!2iC0!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F26f3ca39-5f3d-4f17-bbe7-42c2cf08b52d_1668x956.png" width="1456" height="834" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/26f3ca39-5f3d-4f17-bbe7-42c2cf08b52d_1668x956.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:834,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:162889,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://thetshaped.dev/i/213128205?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F26f3ca39-5f3d-4f17-bbe7-42c2cf08b52d_1668x956.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!2iC0!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F26f3ca39-5f3d-4f17-bbe7-42c2cf08b52d_1668x956.png 424w, https://substackcdn.com/image/fetch/$s_!2iC0!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F26f3ca39-5f3d-4f17-bbe7-42c2cf08b52d_1668x956.png 848w, https://substackcdn.com/image/fetch/$s_!2iC0!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F26f3ca39-5f3d-4f17-bbe7-42c2cf08b52d_1668x956.png 1272w, https://substackcdn.com/image/fetch/$s_!2iC0!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F26f3ca39-5f3d-4f17-bbe7-42c2cf08b52d_1668x956.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button class="pencraft pc-reset pencraft icon-container restack-image buttonBase-tqxFUX" tabindex="0" type="button"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button class="pencraft pc-reset pencraft icon-container view-image buttonBase-tqxFUX" tabindex="0" type="button"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://thetshaped.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption"><span>Join </span><strong>30,000+ engineers</strong><span> getting one practical issue a week on production-grade React, Node, Architecture, and </span><strong>real AI systems</strong><span>, and leveling up from Mid &#8594; Senior &#8594; Architect.</span></p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div><hr></div><h2><strong>Domain errors never page anyone</strong></h2><p>Once errors are split, your logging and alerting rules basically write themselves:</p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;plaintext&quot;,&quot;nodeId&quot;:null}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-plaintext">Error&#9;                  Status&#9;Logs at&#9;  Pages someone?    User sees
ValidationError&#9;           400&#9;          warn&#9;    no&#9;             the real message
NotFoundError&#9;           404&#9;          warn&#9;    no&#9;             the real message
InsufficientFundsError&#9;   422&#9;          warn&#9;    no&#9;             the real message
DatabaseError&#9;           500&#9;          error&#9;    yes&#9;             "Something went wrong"
ExternalServiceError&#9;   503&#9;          error&#9;    yes&#9;             "Something went wrong"
Anything uncaught (a bug)  500&#9;          error&#9;    yes&#9;             "Something went wrong"</code></pre></div><p><span>Domain errors at </span><code>warn</code><span> keep your dashboards honest. </span></p><p><span>You&#8217;re not paging an engineer because someone typed a bad email a thousand times, and when an </span><code>error</code><span>-level line </span><em>does</em><span> show up, something is actually broken, so the alert is worth trusting.</span></p><div><hr></div><h2><strong>Wrap system errors at the edges</strong></h2><p><span>Quick rule of thumb. </span></p><blockquote><p><strong><span>Domain errors come from your service layer and system errors get born at the edges, where your infrastructure wraps them.</span></strong></p></blockquote><p><span> So a raw driver error never leaks up the stack as-is.</span></p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;typescript&quot;,&quot;nodeId&quot;:null}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-typescript">class PostgresUserRepo {
  async findById(id: string): Promise&lt;User | null&gt; {
    try {
      return await this.db.oneOrNone('SELECT * FROM users WHERE id = $1', [id])
    } catch (err) {
      // Wrap it once, here, so the rest of the app only ever sees a DatabaseError.
      throw new DatabaseError(`findById failed for ${id}`, { cause: err })
    }
  }
}</code></pre></div><p>The <code>cause</code> keeps the original error for your logs, but it never travels to the client. </p><p>Your domain code stops thinking about Postgres, and your Postgres code stops making business decisions.</p><p>Watch the names, though. &#8220;Not found&#8221; sounds like one error, but it&#8217;s two. </p><p>A user requesting <code>/users/123</code> that doesn&#8217;t exist is domain; say so, return a 404. Your own code failing to load an ID it wrote five minutes ago is system, because that should be impossible. </p><p>Which base you extend comes from <strong>what you expected, not from what the error is called</strong>.</p><div><hr></div><h2><strong>Two things that&#8217;ll bite you</strong></h2><ol><li><p>Domain messages go straight to the user, so keep secrets out of them.</p></li><li><p>The whole thing rides on <code>instanceof</code>, which only works inside one copy of one module.</p></li></ol><div><hr></div><h2><strong>&#128204; TL;DR</strong></h2><ul><li><p>Every error is <strong>domain</strong> (expected: bad input, broken rule) or <strong>system</strong> (unexpected: DB down, timeout).</p></li><li><p>That one distinction decides HTTP status, user message, log level, and whether anyone gets paged.</p></li><li><p>Encode it in the type: <code>DomainError</code> and <code>SystemError</code></p></li><li><p>One handler routes everything: domain &#8594; real message + right status, logged calm. Everything else &#8594; generic message + 5xx, logged loud.</p></li><li><p>Domain messages reach the client, so keep secrets and internal numbers out of them.</p></li><li><p>Wrap raw errors at the edges (repos, API clients) so a system error is born as a <code>DatabaseError</code>, not a leaked driver stack trace.</p></li></ul><p></p><p></p><p></p><div><hr></div><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://thetshaped.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Subscribe to get practical tips on React.js, Node.js, and Software Architecture delivered straight to your inbox:</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div class="pullquote"><div class="captioned-image-container"><figure><div class="image-link image2" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!cmMn!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 424w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 848w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 1272w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!cmMn!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png" width="248" height="207.7" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:670,&quot;width&quot;:800,&quot;resizeWidth&quot;:248,&quot;bytes&quot;:257716,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://thetshaped.dev/i/188025481?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb8ebf3ac-ad2f-415d-9e32-111e959c8e4e_800x800.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!cmMn!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 424w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 848w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 1272w, https://substackcdn.com/image/fetch/$s_!cmMn!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9aea932b-6248-4324-9ec7-68d0bfed782e_800x670.png 1456w" sizes="auto, 100vw" loading="lazy"></picture><div></div></div></div></figure></div><p><strong>Follow me on <a href="https://www.linkedin.com/in/petarivanovv9/">LinkedIn</a></strong> | <strong><a href="https://x.com/intent/follow?screen_name=petarivanovv9">Twitter(X)</a></strong> |<strong> <a href="https://www.threads.net/@petarivanovv9">Threads</a></strong></p></div><p>Thank you for supporting this newsletter.</p><p>Consider sharing this post with your friends and get rewards.</p><p>You are the best! &#128591;</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!EvJY!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!EvJY!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 424w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 848w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!EvJY!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg" width="800" height="500" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:500,&quot;width&quot;:800,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:83253,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://thetshaped.dev/i/188025481?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!EvJY!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 424w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 848w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!EvJY!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa356e02d-1b11-4eca-81ea-3db29a595cb9_800x500.jpeg 1456w" sizes="auto, 100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button class="pencraft pc-reset pencraft icon-container restack-image buttonBase-tqxFUX" tabindex="0" type="button"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg" class="icon-noB79L"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button class="pencraft pc-reset pencraft icon-container view-image buttonBase-tqxFUX" tabindex="0" type="button"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2 icon-noB79L"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://thetshaped.dev/p/domain-errors-are-features-system-errors-are-bugs-error-handling?utm_source=substack&utm_medium=email&utm_content=share&action=share&quot;,&quot;text&quot;:&quot;Share&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://thetshaped.dev/p/domain-errors-are-features-system-errors-are-bugs-error-handling?utm_source=substack&utm_medium=email&utm_content=share&action=share"><span>Share</span></a></p><div class="pullquote"><div class="captioned-image-container"><figure><div class="image-link image2" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!UzWz!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 424w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 848w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 1272w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 1456w" sizes="auto, 100vw"><img src="https://substackcdn.com/image/fetch/$s_!UzWz!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif" width="48" height="57.6" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/39609b45-f68c-4557-8772-8e2a45182842_500x600.gif&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:600,&quot;width&quot;:500,&quot;resizeWidth&quot;:48,&quot;bytes&quot;:7028,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:&quot;&quot;,&quot;type&quot;:&quot;image/gif&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://thetshaped.dev/i/188785536?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!UzWz!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 424w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 848w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 1272w, https://substackcdn.com/image/fetch/$s_!UzWz!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39609b45-f68c-4557-8772-8e2a45182842_500x600.gif 1456w" sizes="auto, 100vw" loading="lazy"></picture><div></div></div></div></figure></div><p style="text-align: center;"><em><strong>Press the Like button if you found this post helpful</strong></em></p></div>]]></content:encoded></item></channel></rss>