<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: RONI DAS</title>
    <description>The latest articles on DEV Community by RONI DAS (@roni_das_b1b76c5ee6583027).</description>
    <link>https://dev.to/roni_das_b1b76c5ee6583027</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4023784%2F9f177350-df99-4258-8c32-d41c35703ca6.png</url>
      <title>DEV Community: RONI DAS</title>
      <link>https://dev.to/roni_das_b1b76c5ee6583027</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/roni_das_b1b76c5ee6583027"/>
    <language>en</language>
    <item>
      <title>There is no 4B Qwen 3.8. The smallest open model is 27B, and here is what it took to run it on one rented GPU.</title>
      <dc:creator>RONI DAS</dc:creator>
      <pubDate>Wed, 02 Dec 2026 05:46:18 +0000</pubDate>
      <link>https://dev.to/roni_das_b1b76c5ee6583027/there-is-no-4b-qwen-38-the-smallest-open-model-is-27b-and-here-is-what-it-took-to-run-it-on-one-5151</link>
      <guid>https://dev.to/roni_das_b1b76c5ee6583027/there-is-no-4b-qwen-38-the-smallest-open-model-is-27b-and-here-is-what-it-took-to-run-it-on-one-5151</guid>
      <description>&lt;p&gt;&lt;em&gt;Qwen 3.8 has no 2B, 3B or 4B model. I pulled qwen3.8:27b into Ollama on a rented NVIDIA L4: 17.7 GB on disk, 18,004 MiB of GPU memory in use, 18 to 35 tokens a second once loaded.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Where is the Qwen 3.8 you can run on a laptop? I went looking for a 2B or 4B version, the kind that fits next to your browser tabs, and there is none. The Hugging Face listing for the Qwen team shows no Qwen 3.8 model smaller than 27B. The smallest download in Ollama's library, qwen3.8:27b, is about 18 GB.&lt;/p&gt;

&lt;p&gt;So I rented a machine with one graphics card, pulled that 18 GB file, and recorded every command. Below: which Qwen 3.8 models you can download, what the 27B needed in memory, and how fast it answered. The machine cost about 27 US cents.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Qwen 3.8 is
&lt;/h2&gt;

&lt;p&gt;Qwen is a family of large language models (LLMs) from the Qwen team at Alibaba. An LLM is a program that has learned from a huge amount of text and can write, answer questions and write code. Qwen 3.8 is the version of that family that arrived in August 2026.&lt;/p&gt;

&lt;p&gt;Some Qwen models are open-weight. The weights are the numbers a model learned in training; when they are open, you can download them and run the model on your own machine, with no account and no internet connection. Other Qwen models are only a service on Qwen's cloud.&lt;/p&gt;

&lt;p&gt;Qwen 3.8 started on 3 August 2026 with Qwen3.8-Max. Max is Qwen's largest and strongest line, and the Qwen blog post says this is the first time the team opened the weights of a Max-class model. Over the next weeks, three open-weight models arrived on Hugging Face, the website where most open models are shared.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F6nun1orkxbn6gcjwts4h.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F6nun1orkxbn6gcjwts4h.webp" alt="Three open models, very different sizes. Only the smallest one fits one graphics card.*" width="800" height="450"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Three open models, very different sizes. Only the smallest one fits one graphics card.&lt;/em&gt;*&lt;/p&gt;

&lt;h2&gt;
  
  
  The lineup, and why "125B" can be faster than "27B"
&lt;/h2&gt;

&lt;p&gt;A model's size is counted in parameters, the learned numbers inside it. B means billion and T means trillion. Here are the six models Qwen lists for 3.8, from the official model cards and the QwenLM GitHub page, read on 6 October 2026:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Qwen3.8-27B&lt;/strong&gt;: 27B, dense, which means every parameter works on every word. Open weights since 14 August 2026. Licence: Apache 2.0.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Qwen3.8-Flash-Next&lt;/strong&gt;: 125B with 6B active, plus a 51B lookup table. Open since 26 August 2026. Licence: qwen-community-1.0.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Qwen3.8-2.4T-A95B&lt;/strong&gt;: 2.4T with 95B active. Open since 12 August 2026. Licence: qwen3.8-max.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Qwen3.8-Max&lt;/strong&gt;: built on 2.4T-A95B, a cloud service only.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Qwen3.8-Flash&lt;/strong&gt;: built on Flash-Next, a cloud service only.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Qwen3.8-Omni-Flash&lt;/strong&gt;: size not stated, no weights published.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Two of the open models are mixture-of-experts (MoE) models. An MoE model is split into many small expert parts, and only a few experts work on each word. That is what "6B active" means: Flash-Next has 125B parameters, but only about 6B of them do the work for any one word. So it runs faster than its total size suggests, but you still need memory for all 125B.&lt;/p&gt;

&lt;p&gt;The licences matter. A licence is the legal terms for using the weights. Only the 27B uses Apache 2.0, which allows use in paid products. Read the two Qwen licences before you put either big model in a product.&lt;/p&gt;

&lt;p&gt;Flash-Next is a special case. Its card calls it an "experimental preview of the architecture that will underpin Qwen4". The hosted Qwen3.8-Flash, built on it, costs $0.15 per million input tokens and $0.47 per million output tokens, per the Qwen blog. A token is a small piece of text, often part of a word.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is new in 3.8
&lt;/h2&gt;

&lt;p&gt;Three things, from the 27B card.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Thinking, with a dial.&lt;/strong&gt; A thinking model writes out its reasoning first, then gives the answer. That helps on hard problems and costs time. The card says thinking "is on by default and can be disabled per request". A setting called reasoning_effort picks how much it thinks: xhigh, medium or low. Qwen's own default is xhigh. Ollama 0.35.1 listed the levels as false, low, medium and xhigh, and uses medium unless you change it. Ollama's help text mentions high, which this model does not list.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Images and video.&lt;/strong&gt; The card calls the 27B "a native vision-language model that understands images and videos". I gave it a chart to read, below.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Coding and agent work.&lt;/strong&gt; Agents are AI programs that take steps on their own, such as running commands. The 27B uses the Qwen 3.5 design: 64 layers that mix two kinds of attention (the part that lets each word look at the others). It reads 262,144 tokens as built, and about one million with extra settings.&lt;/p&gt;

&lt;p&gt;On the 27B card, the new model beats the older Qwen3.6-27B on all six tests Qwen shows. The biggest jump is OSWorld-Verified, a test of using a computer: 84.3 against 63.9. IFBench, a test of following instructions, went from 69.1 to 79.5. SWE-bench Pro, a test of fixing real GitHub issues, went from 53.5 to 61.7, Terminal Bench 2.1 from 63.4 to 73.0, and LiveCodeBench v6 from 83.9 to 90.3. GPQA Diamond, a science test, barely moved: 87.8 to 89.2.&lt;/p&gt;

&lt;p&gt;Three cautions. Qwen ran these, not me, on its own setup rather than the 4-bit Ollama copy I used. For SWE-bench Pro and several other coding tests, Qwen used Claude Code, Anthropic's coding tool, as the harness (the program that drives the model through each task), and fixed some broken SWE-bench Pro tasks first. Terminal Bench used a different harness, Terminus.&lt;/p&gt;

&lt;h2&gt;
  
  
  Will it fit on your machine?
&lt;/h2&gt;

&lt;p&gt;To run a model, its weights must sit in memory: the graphics card's memory, or on a Mac, the memory that the processor and graphics chip share.&lt;/p&gt;

&lt;p&gt;Qwen itself publishes only 16-bit and 8-bit (FP8) files. Ollama's copy is a 4-bit version called Q4_K_M. Quantization means storing each learned number with fewer bits; Q4_K_M uses about 4 to 5 bits per number instead of 16, so the file is far smaller. Ollama lists qwen3.8:27b at 18 GB. The copy I pulled on 6 October was 17.7 GB.&lt;/p&gt;

&lt;p&gt;My rented NVIDIA L4 card has 23,034 MiB of memory, about 24 GB. Ollama put the whole model on the card and used 18,004 MiB, about 18.9 GB, while answering. That was with a context window of 4,096 tokens. The context window is how much text the model can see at once, and a bigger one needs more memory. I did not measure how much more.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fsnpnxwda8g68p3apyvum.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fsnpnxwda8g68p3apyvum.webp" alt="The middle bar is measured. The bottom three are my reading for common machines, not tests.*" width="800" height="1160"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;The middle bar is measured. The bottom three are my reading for common machines, not tests.&lt;/em&gt;*&lt;/p&gt;

&lt;p&gt;So you need a little more than 18 GB of free memory for the model alone. My MacBook has 24 GB shared by everything, and my guess, which I did not test, is that this is too tight and part of the model would spill onto the slower processor. That is my judgement, not Qwen's: the card gives no memory figures, and its server examples spread the model over 4 graphics cards.&lt;/p&gt;

&lt;p&gt;If 18 GB is too much, the newest small official Qwen models are from Qwen 3.5, the design Qwen 3.8 is built on. In Ollama, qwen3.5:4b-q4_K_M (Apache 2.0) is 3.3 GB and qwen3.5:2b-q4_K_M is 1.9 GB. They are a different, less capable family, not Qwen 3.8. Community-made 1-bit copies of the 27B exist from 6.19 GB, but nobody official has measured their quality.&lt;/p&gt;

&lt;h2&gt;
  
  
  The run: install, pull, inspect
&lt;/h2&gt;

&lt;p&gt;The machine was an AWS g6.2xlarge: 8 virtual CPUs, 32 GiB of memory and one NVIDIA L4. I installed Ollama 0.35.1 with its official install script. &lt;code&gt;ollama pull qwen3.8:27b&lt;/code&gt; downloaded 17.7 GB in about 131 seconds. That was inside Amazon's network, so expect a home connection to take much longer.&lt;/p&gt;

&lt;p&gt;One oddity: my copy had the digest (content ID) aaee06c39dcf, while Ollama's web page listed e118e4d12a70 for the same tag. I do not know why.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ollama show&lt;/code&gt; prints what the model is:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fn77kgpp8h3x4zs1nkyay.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fn77kgpp8h3x4zs1nkyay.webp" alt="A frame from the real recording, drawn by agg, which turns asciinema recordings into images.*" width="800" height="644"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;A frame from the real recording, drawn by agg, which turns asciinema recordings into images.&lt;/em&gt;*&lt;/p&gt;

&lt;p&gt;It reports 27.3 billion parameters, a 262,144-token context, the 4-bit Q4_K_M format, and Ollama 0.32.12 or newer. Under Capabilities it lists vision, tools (it can ask to call functions you provide) and thinking. The sampling settings, temperature 1, top_p 0.95 and top_k 20, match the card's advice for thinking mode. Temperature controls how random the wording is; top_p and top_k limit which words it picks from.&lt;/p&gt;

&lt;h2&gt;
  
  
  Three prompts, real timings
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;A plain answer, thinking off.&lt;/strong&gt; I asked for a three-sentence beginner's explanation of a message queue, with &lt;code&gt;--think=false&lt;/code&gt; (answer straight away) and &lt;code&gt;--verbose&lt;/code&gt; (print timings). The first time took 77.54 seconds in total: 43.03 seconds to load the model onto the card, about 25 seconds to read the prompt and about 9 seconds to write the answer. With the model already loaded, the same prompt took 4.02 seconds: 65 tokens at 17.77 tokens a second.&lt;/p&gt;

&lt;p&gt;One catch. &lt;code&gt;--think=false&lt;/code&gt; only turns thinking off. As far as I can tell, Ollama keeps the model's stored settings (temperature 1) rather than the card's suggested settings for no thinking, which are temperature 0.7, top_p 0.8 and presence_penalty 1.5. Set those yourself if you want them.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A thinking answer.&lt;/strong&gt; Next I asked when a train arrives if it leaves at 09:40 and the trip takes 2 hours 35 minutes, with &lt;code&gt;--think=low&lt;/code&gt;. Ollama prints the reasoning between "Thinking..." and "...done thinking", then the answer.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fd876iko1h0l7oeb9r7fa.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fd876iko1h0l7oeb9r7fa.webp" alt="The thinking run. The first word of the reasoning is cut off because Ollama clears its spinner over it.*" width="799" height="463"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;The thinking run. The first word of the reasoning is cut off because Ollama clears its spinner over it.&lt;/em&gt;*&lt;/p&gt;

&lt;p&gt;It got 12:15, which is right, and wrote 95 tokens at 34.90 tokens a second, about twice the speed of the first. One likely reason is a draft step that guesses a few tokens ahead and keeps the ones the model agrees with, called speculative decoding. The card says the model was trained to predict several tokens at once, &lt;code&gt;ollama show&lt;/code&gt; listed draft_num_predict 4, and Ollama's server log reported speculative decoding. Predictable text like a sum lets more guesses be kept, so do not read these two runs as thinking against no thinking.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Reading a chart.&lt;/strong&gt; To test vision, I drew a bar chart with known values in a short Python script and put the file path in the prompt.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fzw9s8c12kyogfxgl2rc3.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fzw9s8c12kyogfxgl2rc3.webp" alt="The real chart.png the model was given, enlarged 2x here. The model got the 600x400 original.*" width="800" height="533"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;The real chart.png the model was given, enlarged 2x here. The model got the 600x400 original.&lt;/em&gt;*&lt;/p&gt;

&lt;p&gt;It listed all 5 days with the right values. Reading the image took most of the time: 277 tokens of input in 4.1 seconds (the image becomes tokens too), then 29 tokens of answer at 31.52 tokens a second. Total: 5.25 seconds.&lt;/p&gt;

&lt;h2&gt;
  
  
  What these numbers do not tell you
&lt;/h2&gt;

&lt;p&gt;This was four runs of three prompts on one rented L4: the first prompt twice, cold then warm, and the other two once. That is not a benchmark. It shows the model loads, thinks and reads a simple image on a 24 GB card, not how accurate or fast it is under real load.&lt;/p&gt;

&lt;p&gt;I did not run it on a Mac, so I have no Mac speeds. I did not test Flash-Next, the 2.4T model, tool calling, long documents or video. The benchmark scores are Qwen's, and I did not rerun them. I did not try Ollama's 27b-mlx version for Apple Silicon Macs either.&lt;/p&gt;

&lt;p&gt;To serve many users at once, the card recommends SGLang, vLLM or TokenSpeed rather than a desktop tool like Ollama.&lt;/p&gt;

&lt;p&gt;The full page has the full terminal output of every run, the settings checklist, every source, and an FAQ on turning thinking off in each server: &lt;a href="https://www.systemdesign.academy/explained/qwen-3-8?utm_source=devto&amp;amp;utm_medium=crosspost&amp;amp;utm_campaign=qwen-3-8" rel="noopener noreferrer"&gt;Qwen 3.8 explained&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  If you want to go further than one model
&lt;/h2&gt;

&lt;p&gt;A new open model lands every few weeks with a new score table. The method lasts longer: check what fits in memory before you download, time warm and cold runs separately, and test on your own prompts.&lt;/p&gt;

&lt;p&gt;My &lt;strong&gt;AI Engineering&lt;/strong&gt; course teaches exactly that, with labs: how quantization makes a big model fit, when step-by-step thinking helps and what it costs, and how to compare two models fairly on the same test. You pay once and keep it. &lt;a href="https://www.systemdesign.academy/ai-engineering?utm_source=devto&amp;amp;utm_medium=crosspost&amp;amp;utm_campaign=qwen-3-8" rel="noopener noreferrer"&gt;Explore the AI Engineering course&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;A model on one card is the easy part. Putting it behind an API that many people call needs queues, caching, load balancing and capacity planning, and that is what the &lt;strong&gt;System Design Masterclass&lt;/strong&gt; covers, from the basics to full designs of real systems. It is a one-time payment too, with no subscription. &lt;a href="https://www.systemdesign.academy/system-design-course?utm_source=devto&amp;amp;utm_medium=crosspost&amp;amp;utm_campaign=qwen-3-8" rel="noopener noreferrer"&gt;Explore the System Design Masterclass&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This article was first published on &lt;a href="https://www.systemdesign.academy/explained/qwen-3-8?utm_source=devto&amp;amp;utm_medium=crosspost&amp;amp;utm_campaign=qwen-3-8" rel="noopener noreferrer"&gt;systemdesign.academy&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>llm</category>
      <category>ollama</category>
      <category>opensource</category>
      <category>ai</category>
    </item>
    <item>
      <title>KV cache size: the formula, worked out on real models, and how many users fit on one GPU</title>
      <dc:creator>RONI DAS</dc:creator>
      <pubDate>Mon, 23 Nov 2026 05:49:24 +0000</pubDate>
      <link>https://dev.to/roni_das_b1b76c5ee6583027/kv-cache-size-the-formula-worked-out-on-real-models-and-how-many-users-fit-on-one-gpu-1bck</link>
      <guid>https://dev.to/roni_das_b1b76c5ee6583027/kv-cache-size-the-formula-worked-out-on-real-models-and-how-many-users-fit-on-one-gpu-1bck</guid>
      <description>&lt;p&gt;Sooner or later, everyone who hosts a language model asks the same question: how many people can this one GPU serve at the same time? The model's size does not answer it. The answer sits in a much smaller thing that grows with every word of every conversation, the KV cache.&lt;/p&gt;

&lt;p&gt;In this article I work out the KV cache sum the way an engineer needs it: from the model's config file, to the memory one conversation takes, to the number of conversations that fit on a card. Every measured number here comes from a lesson in our Hands-On Inference Engineering course, run on an RTX 4090 we rented on RunPod. I copied them from those runs, so you can check each one.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The short version.&lt;/strong&gt; KV cache per token = 2 × layers × key-value heads × head size × bytes per number, all read from the model's config.json. For Qwen3-8B that is 2 × 36 × 8 × 128 × 2 = 147,456 bytes, 144 KiB a token, so a conversation of 32,768 tokens needs about 4.5 GiB. On a 24 GB RTX 4090, vLLM had 4.56 GiB left for the cache after the weights, room for 33,216 tokens: about 16 conversations of 2,000 tokens at once.&lt;/p&gt;

&lt;h2&gt;
  
  
  Part 1: The formula, worked out on real models
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What is the KV cache, in plain words?
&lt;/h3&gt;

&lt;p&gt;When a model writes a new token, the attention step in every layer looks back at all the earlier tokens. To do that it needs two things for each earlier token. The key lets the new token find an earlier one, and the value is what the earlier token hands over once it is found.&lt;/p&gt;

&lt;p&gt;Working these out again for every new token would waste most of the GPU's time, so the server keeps them in GPU memory. That store of notes is the KV cache. It grows by one entry for every token of every conversation. That is why the cache, and not the model itself, usually decides how many people one GPU can serve at the same moment.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is the KV cache size formula?
&lt;/h3&gt;

&lt;p&gt;Bytes per token = 2 × layers × key-value heads × head size × bytes per number.&lt;/p&gt;

&lt;p&gt;The 2 is there because every layer keeps both a key and a value. The other four numbers are in the model's config.json on Hugging Face:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;num_hidden_layers&lt;/code&gt;, the number of layers&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;num_key_value_heads&lt;/code&gt;, the key-value heads&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;head_dim&lt;/code&gt;, the head size&lt;/li&gt;
&lt;li&gt;the data type, which is 2 bytes for the usual 16-bit formats&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Then multiply by the number of tokens you want to keep. For a whole server, that means every token of every conversation it holds at once.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fvtcqlxox46ajg7qn649d.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fvtcqlxox46ajg7qn649d.webp" alt="Two columns, one per model, each showing the lines read from config.json and the sum worked from them. Qwen3-0.6B: 28 layers, 8 key-value heads, head size 128, 2 bytes, so 114,688 bytes, 112 KiB per token; one chat of 32,768 tokens needs 3,584.0 MiB of notes, against the model's 604 MiB of weights. Qwen3-8B: 36 layers, 8 key-value heads, head size 128, so 147,456 bytes, 144 KiB per token, and 4,608.0 MiB for 32,768 tokens, against 15.27 GiB of weights." width="799" height="672"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;The KV cache per token, worked out from each model's config.json. For the small Qwen3-0.6B, the notes of one long chat outweigh the model itself.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  How big is the KV cache for Qwen3-8B?
&lt;/h3&gt;

&lt;p&gt;Qwen3-8B has 36 layers, 8 key-value heads and a head size of 128, stored in 2 bytes a number. So each token needs 2 × 36 × 8 × 128 × 2 = 147,456 bytes, which is 144 KiB. A conversation of 32,768 tokens therefore needs 4,608 MiB, about 4.5 GiB, on top of the model's weights.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What we measured:&lt;/strong&gt; on the rented RTX 4090, vLLM 0.33 loaded Qwen3-8B's 16-bit weights in 15.27 GiB and set aside 4.56 GiB for the cache, which it reported as room for 33,216 tokens.&lt;/p&gt;

&lt;h3&gt;
  
  
  Can the cache be bigger than the model?
&lt;/h3&gt;

&lt;p&gt;Yes, and for small models it often is. Qwen3-0.6B has 28 layers, 8 key-value heads and a head size of 128, so it keeps 112 KiB for every token. One conversation of 32,768 tokens then needs 3,584 MiB of notes. That is about six times the model's own 604 MiB of weights.&lt;/p&gt;

&lt;h3&gt;
  
  
  Does the sum match what a real server reserves?
&lt;/h3&gt;

&lt;p&gt;It matched exactly in our run. That is worth checking once yourself, because it tells you the formula is not a rough guide. It is what the server really does.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What we measured:&lt;/strong&gt; llama-server on our RTX 4090 reserved 448.00 MiB for 4,096 tokens of Qwen3-0.6B, which is exactly 4,096 × 112 KiB.&lt;/p&gt;

&lt;h2&gt;
  
  
  Part 2: From bytes per token to users per GPU
&lt;/h2&gt;

&lt;p&gt;The question behind every KV cache sum is a capacity question: how many conversations fit on this card at once? It comes down to two divisions, and the server prints the first one for you.&lt;/p&gt;

&lt;h3&gt;
  
  
  How much GPU memory does each user need?
&lt;/h3&gt;

&lt;p&gt;Multiply the bytes per token by the length of that user's conversation. Count the question, the earlier turns and the answer being written. For Qwen3-8B at 144 KiB a token, a conversation of 2,000 tokens needs about 281 MiB, and one of 32,768 tokens needs about 4.5 GiB.&lt;/p&gt;

&lt;p&gt;So the honest answer to "how much memory per user?" is always another question: how long are their conversations? Ten short support chats can cost less than one person pasting in a long document.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why is the cache only 4.56 GiB on a 24 GB card?
&lt;/h3&gt;

&lt;p&gt;Because the cache gets whatever is left after everything else. The RTX 4090 reports 23.52 GiB to programs. vLLM takes 92% of it by default, which is 21.64 GiB. From that it pays for the weights, 15.27 GiB, and its own working space, about 1.8 GiB on a first start. The 4.56 GiB that remains becomes the KV cache.&lt;/p&gt;

&lt;p&gt;That is also why a small change in the share changes the cache a lot. At 80%, the cache in the course's lesson on sizing GPU memory fell to 1.74 GiB, because the weights take the same room whatever the share.&lt;/p&gt;

&lt;h3&gt;
  
  
  How many users can one 24 GB GPU serve?
&lt;/h3&gt;

&lt;p&gt;Divide the cache vLLM reports at start-up by the tokens one conversation holds. For Qwen3-8B in 16 bits on the RTX 4090, the cache held 33,216 tokens. That is enough for about one conversation of 32,768 tokens, 8 of 4,000, or 16 of 2,000, if they all reach full length at the same time.&lt;/p&gt;

&lt;p&gt;vLLM will start more people than that when their questions are short, and pause some of them later if their answers grow and the cache runs out. The sum gives you the safe number, not a hard limit.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What we measured:&lt;/strong&gt; in the course's crowd test, 64 conversations of about 340 tokens filled 66.8% of that cache. 160 filled it completely, and vLLM paused and resumed requests 64 times, but every answer still came back complete.&lt;/p&gt;

&lt;h3&gt;
  
  
  What does "Maximum concurrency" in vLLM's log mean?
&lt;/h3&gt;

&lt;p&gt;It is the cache size divided by the longest conversation the server allows, which you set with &lt;code&gt;--max-model-len&lt;/code&gt;. With a limit of 32,768 tokens and room for 33,216, our server printed 1.01x. It is a worst case: the number of full-length conversations that fit. It is not a limit on how many people vLLM will serve.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why does vLLM count the cache in blocks of 16 tokens?
&lt;/h3&gt;

&lt;p&gt;vLLM stores the cache in small pages of 16 tokens, taken from a shared pool only as a conversation grows. This is PagedAttention. It means a conversation wastes at most part of its last page, instead of reserving room for the longest conversation it might ever have. For Qwen3-8B one page is 16 × 144 KiB = 2.25 MiB, and our 4.56 GiB cache was 2,076 pages.&lt;/p&gt;

&lt;h2&gt;
  
  
  Part 3: What a bigger cache costs in speed
&lt;/h2&gt;

&lt;p&gt;The cache is not only a memory question. Every new token has to read it, so it also sets how fast a long conversation can go.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why do long conversations make the model answer more slowly?
&lt;/h3&gt;

&lt;p&gt;To write each new token, attention reads the notes of every earlier token as well as the model's weights. A longer conversation means more notes to read for every single token, and writing is limited by how fast the GPU's memory can deliver bytes, not by how fast it can do arithmetic.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What we measured:&lt;/strong&gt; on our RTX 4090, Qwen3-8B wrote 164.1 tokens a second with an empty cache and 114.9 tokens a second with 16,384 tokens already in it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fxl3gr01ehxnv1g7otro0.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fxl3gr01ehxnv1g7otro0.webp" alt="A chart of writing speed against the number of tokens already in the KV cache, from llama-bench runs on an RTX 4090. At 0, 2,048, 8,192 and 16,384 tokens, the small model's line falls from 639.4 to 277.8 tokens a second, and the 8B model's line falls from 164.1 to 114.9." width="800" height="604"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;A fuller cache means slower writing. Every new token reads the weights once and every earlier token's notes once.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Does keeping the cache make the second turn of a chat faster?
&lt;/h3&gt;

&lt;p&gt;Yes, because the earlier turns do not have to be read again. In the course's run, the second turn of a conversation reused the notes of 2,480 of its 2,524 prompt tokens. Reading its prompt took 6 ms, where the first turn had needed 94 ms.&lt;/p&gt;

&lt;h2&gt;
  
  
  Part 4: Making the cache smaller
&lt;/h2&gt;

&lt;p&gt;There are two kinds of saving. One is chosen when the model is designed. The other is a setting you can change on your own server tonight.&lt;/p&gt;

&lt;h3&gt;
  
  
  How does grouped-query attention shrink the KV cache?
&lt;/h3&gt;

&lt;p&gt;In grouped-query attention, several query heads share one key and one value. Only keys and values go in the cache, so it shrinks by the size of each group: &lt;code&gt;num_attention_heads&lt;/code&gt; divided by &lt;code&gt;num_key_value_heads&lt;/code&gt; in config.json. Qwen3-8B has 32 query heads in groups of 4, so it keeps 144 KiB per token instead of 576 KiB.&lt;/p&gt;

&lt;p&gt;This is the one mistake I see most often in KV cache answers. Read &lt;code&gt;num_key_value_heads&lt;/code&gt;, not &lt;code&gt;num_attention_heads&lt;/code&gt;, when you do the sum. Using the wrong one makes the result four or eight times too big.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What we measured:&lt;/strong&gt; the same 4.56 GiB that held 33,216 tokens of Qwen3-8B would hold only 8,304 if every head kept its own key and value.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ffelw43i5kzub1z6p471f.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ffelw43i5kzub1z6p471f.webp" alt="Four panels, one for each way of keeping notes, each from a real model's config file. Multi-head attention, Qwen1.5-7B: 512 KiB per token. Grouped-query attention, Qwen3-8B: 32 query heads sharing 8 keys and values in groups of 4, 144 KiB. Multi-query attention, Falcon-7B: one key and value for all heads, 8 KiB. Latent attention, DeepSeek-V3: one compressed note of 512 numbers and a small position key of 64, 68.6 KiB." width="800" height="666"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Four ways a model can keep its notes, and what each costs per token. Sharing keys and values across heads is why a newer 8B model needs far less cache than an older 7B one.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Does an FP8 KV cache double the number of users?
&lt;/h3&gt;

&lt;p&gt;Nearly. &lt;code&gt;--kv-cache-dtype fp8&lt;/code&gt; stores each number in 1 byte instead of 2, so Qwen3-8B needs 73,728 bytes a token instead of 147,456. The numbers are kept more coarsely, so check the quality of the answers on your own test questions before you keep it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What we measured:&lt;/strong&gt; on our RTX 4090 the cache held 59,856 tokens instead of 33,216. That is 1.80 times as many, not quite 2, because that start needed 0.45 GiB more working space.&lt;/p&gt;

&lt;h3&gt;
  
  
  Do smaller weights help the cache too?
&lt;/h3&gt;

&lt;p&gt;Yes, directly. Whatever the weights stop using, the cache gets. The 8-bit copy of Qwen3-8B took 8.9 GiB instead of 15.27 GiB, and its cache held 79,184 tokens, 2.38 times as many as the 16-bit model on the same card.&lt;/p&gt;

&lt;h3&gt;
  
  
  Does a shorter max model length make the cache bigger?
&lt;/h3&gt;

&lt;p&gt;No. &lt;code&gt;--max-model-len&lt;/code&gt; only changes the check vLLM makes at start-up: that one conversation of that length fits. If the full length does not fit, vLLM refuses to start and names the longest length that would. Lowering it lets the server start, but the cache stays the same size.&lt;/p&gt;

&lt;h2&gt;
  
  
  What to take away
&lt;/h2&gt;

&lt;p&gt;Work in tokens. Find the bytes one token costs from config.json. See how much memory is left after the weights and the working space, divide to get the tokens that fit, and divide again by the length of a typical conversation. Use &lt;code&gt;num_key_value_heads&lt;/code&gt;, not the number of attention heads, and check your answer against the cache size the server prints when it starts. If your sum and the server's number agree, you understand your GPU's capacity.&lt;/p&gt;

&lt;p&gt;The full page, with a link to the lesson each number comes from, is on our site: &lt;a href="https://www.systemdesign.academy/kv-cache-size?utm_source=devto&amp;amp;utm_medium=crosspost&amp;amp;utm_campaign=crosspost-1123" rel="noopener noreferrer"&gt;KV cache size: the formula, and how much GPU memory each user needs&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Go deeper: Hands-On Inference Engineering
&lt;/h2&gt;

&lt;p&gt;I teach the runs behind this article in Hands-On Inference Engineering on System Design Academy. You rent an RTX 4090, work out the cache for Qwen3-8B on paper, start vLLM and check your sum against the budget it prints, then fill the cache with real users and watch what happens when it runs out. It suits backend, ML platform and AI Engineering people who plan to host their own models and want to explain every number on their dashboard: &lt;a href="https://www.systemdesign.academy/inference-engineering?utm_source=devto&amp;amp;utm_medium=crosspost&amp;amp;utm_campaign=crosspost-1123" rel="noopener noreferrer"&gt;see the Hands-On Inference Engineering course&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>vllm</category>
      <category>llm</category>
      <category>machinelearning</category>
      <category>gpu</category>
    </item>
    <item>
      <title>Continuous batching explained, with static batching measured beside it on a real GPU</title>
      <dc:creator>RONI DAS</dc:creator>
      <pubDate>Sun, 22 Nov 2026 05:52:07 +0000</pubDate>
      <link>https://dev.to/roni_das_b1b76c5ee6583027/continuous-batching-explained-with-static-batching-measured-beside-it-on-a-real-gpu-kg5</link>
      <guid>https://dev.to/roni_das_b1b76c5ee6583027/continuous-batching-explained-with-static-batching-measured-beside-it-on-a-real-gpu-kg5</guid>
      <description>&lt;p&gt;If you serve a language model to more than one person, batching decides most of your bill. It is also one of the first things an interviewer asks about once a conversation turns to model serving, and the follow-up is always about size: how big a difference it really makes.&lt;/p&gt;

&lt;p&gt;This article answers that with numbers. Each measurement here was taken in a lesson of our Hands-On Inference Engineering course, with vLLM serving on an RTX 4090 we rented on RunPod, and I copied the figures from those runs rather than recalling them.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The short version.&lt;/strong&gt; Writing an answer reads all of the model's weights for every token, so the GPU has arithmetic to spare, and a batch spends it on other people's tokens. A static batch wastes some of it, because short answers leave their places empty until the longest one ends. Continuous batching refills each place at the very next step. It keeps adding people until the seats, the step budget or the KV cache runs out, and then people wait. On our RTX 4090, the same 48 answers took 24.33 seconds in fixed batches of twelve and 16.83 seconds with continuous batching.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fgyu3i7bffcc1nijbr7wa.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fgyu3i7bffcc1nijbr7wa.webp" alt="A timeline of twelve decode steps across five places in a batch, drawn twice. On top, a simple batch: places freed by short answers stay empty until the longest answer ends, 17 empty place-steps. Below, continuous batching: the next person takes a free place at the very next step, 0 empty place-steps." width="800" height="654"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;A simple batch waits for its longest answer; continuous batching fills each free place at once. The answer lengths in this drawing are made up to show the idea; the measured runs come next.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Part 1: What batching is, and why it works
&lt;/h2&gt;

&lt;p&gt;Continuous batching is a better way to batch, so start with why batching helps at all. It comes down to how a GPU writes an answer.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is batching in LLM inference?
&lt;/h3&gt;

&lt;p&gt;Batching means working out the next token for several people in the same step on the GPU. A language model writes one token per pass through its weights, and every pass reads all of those weights from memory. With a batch, that one read writes a token for everyone in it.&lt;/p&gt;

&lt;p&gt;Writing tokens is limited by memory speed, not by arithmetic, so the GPU has spare arithmetic to give. That is why extra people in a batch cost each other so little at first.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What we measured:&lt;/strong&gt; with Qwen3-8B on our RTX 4090, one person got 57 tokens a second and 32 people at once got 1,161 in total, 20.2 times as much, while each person's own speed fell only from 58.1 to 39.9 tokens a second.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why does batching help writing so much more than reading?
&lt;/h3&gt;

&lt;p&gt;A request has two halves. Prefill reads the whole question in one pass, so the GPU's arithmetic is already busy. Decode writes the answer one token per pass, and each pass pays for a full read of the weights to produce a single token, so the arithmetic sits mostly idle. Batching fills that idle arithmetic with other people's tokens.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What we measured:&lt;/strong&gt; on the same card, Qwen3-0.6B read a 256-token prompt at 35,796 tokens a second and wrote at 642.0 tokens a second, about 56 times slower per token.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why does batching make each answer cheaper?
&lt;/h3&gt;

&lt;p&gt;A GPU is paid for by the hour. The cost of a token is the price of the hour divided by the tokens written in that hour, so writing more tokens in the same hour makes each one cheaper. The person pays with a little speed: each answer takes longer than it would alone.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What we measured:&lt;/strong&gt; Qwen3-8B on our RTX 4090 wrote 161 tokens a second for one person and 2,179 for 32 at once, about 13.5 times as many tokens from the same GPU hour, while each person's answer took about 2.4 times longer.&lt;/p&gt;

&lt;h2&gt;
  
  
  Part 2: Static batching against continuous batching
&lt;/h2&gt;

&lt;p&gt;This is the heart of it, and the part interviewers push on most: where a simple batch loses time, and what continuous batching does differently.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is static batching, and what is wrong with it?
&lt;/h3&gt;

&lt;p&gt;Static batching sends a group of requests through together, and the group finishes only when its slowest member does. Answers have very different lengths, so the places freed by short answers sit empty while the longest one keeps writing, and the people waiting in the queue cannot start until the whole group is done.&lt;/p&gt;

&lt;p&gt;It is like a restaurant that seats twelve parties together and seats nobody new until all twelve tables have finished.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What we measured:&lt;/strong&gt; in our run of 48 answers in fixed waves of 12, 47% of the space in the batch sat empty, and the run took 24.33 seconds.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is continuous batching?
&lt;/h3&gt;

&lt;p&gt;Continuous batching decides the batch afresh at every step. Between any two steps, finished requests leave and waiting requests join, as long as their KV cache fits, so the batch stays full and nobody waits for a stranger's long answer. The restaurant seats a new party the moment any table empties.&lt;/p&gt;

&lt;p&gt;You will also hear it called iteration-level scheduling, the name it was given in the Orca paper of 2022, because the decision is made at every iteration of the model rather than once per batch.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What we measured:&lt;/strong&gt; the same 48 answers, 12 at a time, took 16.83 seconds with continuous batching against 24.33 seconds in waves: 929 steps instead of 1,342. The answer lengths alone predicted a ratio of 0.69, and the measured ratio was 0.69.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fqpncyifw4nzg3wdd58ea.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fqpncyifw4nzg3wdd58ea.webp" alt="Two timelines of the same 48 answers on twelve places. Top, static waves: 47% of the space sits empty, 1,342 steps, 24.33 seconds. Bottom, continuous: the bars pack tightly, 929 steps, 16.83 seconds. 16.83 divided by 24.33 is 0.69." width="800" height="720"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;The same 48 answers in waves of twelve, and with continuous batching. The lengths alone predicted the ratio of 0.69, and the real run matched it.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Does vLLM use continuous batching?
&lt;/h3&gt;

&lt;p&gt;Yes, and you cannot switch it off. The scheduler lives in vLLM's engine core process, and before every step it picks the conversations to work on and how many tokens each one gets. The settings you can change only cap the size of a step.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What we measured:&lt;/strong&gt; serving Qwen3-0.6B on the RTX 4090, a single request ran at 359 tokens a second. With 64 requests sharing the card the server produced 12,087 tokens a second, roughly 189 for each person, and the last of the 64 answers arrived 1.06 seconds after they were sent.&lt;/p&gt;

&lt;h3&gt;
  
  
  Can a new question be read in the same step as other people's answers are written?
&lt;/h3&gt;

&lt;p&gt;Yes. A step has a budget of tokens. Every conversation that is writing takes one token of it, and whatever is left can go to reading new questions. Chunked prefill cuts a long question into pieces that fit the leftover budget and reads them over several steps, so the people already being answered keep getting words.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What we measured:&lt;/strong&gt; at the default budget, vLLM read a question of 12,563 tokens in 7 pieces, and none of the eight answers running beside it stalled for longer than 300 ms. When we raised the budget to 16,384, the whole question went through in a single step and those eight answers froze for as long as 1,499 ms.&lt;/p&gt;

&lt;h2&gt;
  
  
  Part 3: The limits: seats, step budget and memory
&lt;/h2&gt;

&lt;p&gt;Continuous batching keeps adding people until something runs out. Three things can run out, and each has its own setting in vLLM.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fqw2hm7j1u3nafzxfano0.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fqw2hm7j1u3nafzxfano0.webp" alt="Three charts on one axis of people at once, 1 to 64, from our sweep on the RTX 4090. Total tokens a second: 57, 109, 211, 397, 713, 1,161, 1,343. Each person's speed falls from 58.1 to 39.9 at 32 and 29.3 at 64. The p99 wait for the first word reaches 8.69 seconds at 64 people, the only level with pauses (59)." width="800" height="723"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;What a bigger batch gives the server and costs each person. At 64 people the KV cache was full and the total rose only 16% more.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  What does max-num-seqs do?
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;--max-num-seqs&lt;/code&gt; sets the most conversations that can be running at once, like seats in a room. Requests beyond it wait in a queue even if memory is free. The default for vLLM 0.33's API server is 256, and 1,024 on H100-class cards of 70 GB or more.&lt;/p&gt;

&lt;p&gt;Lowering it makes each running person faster and everyone else wait. Use it to protect a speed promise, not as a default.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What we measured:&lt;/strong&gt; setting it to 8 cut the time per token from 26.08 to 18.65 ms, but raised the mean wait for the first token from 0.99 to 17.35 seconds and cut throughput 5.1 times.&lt;/p&gt;

&lt;h3&gt;
  
  
  What does max-num-batched-tokens do?
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;--max-num-batched-tokens&lt;/code&gt; is the token budget of one step: the most tokens the model works on at once. The default is 2,048 on GPUs like the RTX 4090 and 8,192 for the API server on H100-class cards. A bigger budget reads long questions in fewer steps, and makes everyone else's words stop for longer while it does.&lt;/p&gt;

&lt;h3&gt;
  
  
  How many requests can one GPU batch at once?
&lt;/h3&gt;

&lt;p&gt;Roughly as many as the KV cache can hold, and fewer if you owe users a certain speed. Take the cache size vLLM prints when it starts and divide it by the length of a typical conversation. Go past that number and the total hardly grows while people begin to queue.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What we measured:&lt;/strong&gt; for Qwen3-8B on an RTX 4090, 33,216 tokens of cache divided by 768-token conversations is about 43. In our sweep 32 people ran with no pauses, and 64 raised the total by only 16% while vLLM paused requests 59 times and the p99 wait for the first word reached 8.69 seconds.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is preemption, and why does it happen?
&lt;/h3&gt;

&lt;p&gt;People are let in while their notes fit, but every answer keeps getting longer as it is written. Sooner or later a running conversation asks for one more page of the KV cache and there is none. vLLM then stops the conversation that joined most recently, gives its pages to the others, and puts it back at the head of the queue. When room comes back, it reads that conversation's text again to rebuild the notes and carries on. Nothing is lost; the paused answers simply finish later.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What we measured:&lt;/strong&gt; thirty-two answers of 1,500 tokens needed 3,104 pages of a cache that had 2,375. vLLM preempted 8 times, and every answer was complete after 40.64 seconds.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fgv1ubse1b6fs6ncmw7h1.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fgv1ubse1b6fs6ncmw7h1.webp" alt="The KV cache's 2,375 pages drawn to scale with 32 conversations of 97 pages each laid end to end: 24 fit, the last 8 hang past the end with a pause sign. Counters from the run: cache 99.8% full at most, 8 preemptions, at most 8 waiting, every answer complete after 40.64 seconds." width="800" height="691"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;When long answers need more pages than the cache has, the newest wait.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  How does PagedAttention help continuous batching?
&lt;/h3&gt;

&lt;p&gt;Continuous batching needs memory that can be handed out and given back at any step. PagedAttention stores each conversation's notes in small pages of 16 tokens, taken from a shared pool as it grows, so a newcomer can start the moment a few pages are free and a finished conversation gives its pages straight back.&lt;/p&gt;

&lt;h2&gt;
  
  
  Part 4: Measuring it honestly
&lt;/h2&gt;

&lt;p&gt;Batching numbers are easy to flatter by accident. Two habits keep them honest.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why must a batching load test use fresh prompts?
&lt;/h3&gt;

&lt;p&gt;Prefix caching is on by default in vLLM, and it remembers the work done on questions it has seen before. Send the same questions twice and the second run is timing the cache instead of the batching. Write new questions for every run, and confirm that &lt;code&gt;vllm:prefix_cache_hits_total&lt;/code&gt; still reads 0 when the run ends.&lt;/p&gt;

&lt;h3&gt;
  
  
  Which numbers should you report for a batching test?
&lt;/h3&gt;

&lt;p&gt;The total tokens a second for the server, each person's own speed, and the wait for the first word at the median and at the 99th percentile. The total tells you the cost; each person's speed tells you what users feel; the slow percentile shows the unlucky users that an average hides. Also count preemptions, because they are the first sign the batch is too big.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What we measured:&lt;/strong&gt; at 64 people on our RTX 4090, the median first word took 1.61 seconds while the p99 took 8.69 seconds.&lt;/p&gt;

&lt;h2&gt;
  
  
  What to take away
&lt;/h2&gt;

&lt;p&gt;A GPU writing an answer is waiting on memory, not on arithmetic, so a batch is nearly free until something runs out. Static batching throws part of that away by making everyone wait for the longest answer. Continuous batching refills a place the moment it empties, and on our card that alone took the same work from 24.33 seconds to 16.83. After that, the job is knowing which of the three limits you will hit first: the seats, the step budget or the KV cache.&lt;/p&gt;

&lt;p&gt;The full page, with a link to the lesson each number comes from, is on our site: &lt;a href="https://www.systemdesign.academy/continuous-batching?utm_source=devto&amp;amp;utm_medium=crosspost&amp;amp;utm_campaign=crosspost-1122" rel="noopener noreferrer"&gt;Continuous batching explained, with static batching measured beside it&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Go deeper: Hands-On Inference Engineering
&lt;/h2&gt;

&lt;p&gt;I teach the runs behind this article in Hands-On Inference Engineering on System Design Academy. You rent the same RTX 4090, push the 48 answers through in fixed waves and then continuously, step the load from 1 person up to 64, and watch the cache fill until vLLM starts pausing requests. It suits backend, ML platform and AI engineering people who plan to host their own models and want to explain every number on their dashboard: &lt;a href="https://www.systemdesign.academy/inference-engineering?utm_source=devto&amp;amp;utm_medium=crosspost&amp;amp;utm_campaign=crosspost-1122" rel="noopener noreferrer"&gt;see the Hands-On Inference Engineering course&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>vllm</category>
      <category>llm</category>
      <category>machinelearning</category>
      <category>performance</category>
    </item>
    <item>
      <title>vLLM interview questions, answered with numbers from a real GPU</title>
      <dc:creator>RONI DAS</dc:creator>
      <pubDate>Sat, 21 Nov 2026 05:45:59 +0000</pubDate>
      <link>https://dev.to/roni_das_b1b76c5ee6583027/vllm-interview-questions-answered-with-numbers-from-a-real-gpu-1h1o</link>
      <guid>https://dev.to/roni_das_b1b76c5ee6583027/vllm-interview-questions-answered-with-numbers-from-a-real-gpu-1h1o</guid>
      <description>&lt;p&gt;When an interview moves to model serving, vLLM comes up fast. The questions sound simple, but the follow-up is always the same: how do you know? An answer with one real number behind it sounds like someone who has run the thing, and an answer without one sounds like someone who read about it.&lt;/p&gt;

&lt;p&gt;So here are the questions I hear most, grouped the way an interviewer usually walks through them. Every number below comes from the lessons of our Hands-On Inference Engineering course, measured with vLLM 0.33 on a rented RTX 4090 on RunPod. Nothing was typed from memory.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The short version.&lt;/strong&gt; vLLM interview questions cover what vLLM is (an open-source server that runs a language model for many people at once on GPUs, in OpenAI's API format), how it splits the GPU's memory between the weights and the KV cache, how PagedAttention and continuous batching keep that memory and the GPU busy, what happens when the cache is full, and the settings that move each number. On a rented RTX 4090, one person got 359 tokens a second and 64 people got 12,087 in total. Be ready to explain why.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fsdt2dond28un1nsf3vyt.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fsdt2dond28un1nsf3vyt.webp" alt="Two charts from four real runs on a rented RTX 4090: total tokens a second rises from 359 for 1 person to 12,087 for 64, while each person slows from 359 to 189 tokens a second." width="800" height="1002"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;What vLLM is for: many people sharing one GPU. Sixty-four people at once got 34 times the total writing, and each of them was only about 1.9 times slower than one person alone.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How to answer a vLLM question well
&lt;/h2&gt;

&lt;p&gt;Start with the one-sentence idea, then name the setting that controls it and the number it moves. Interviewers listen for whether you know the trade: almost every vLLM setting buys speed for one person at the cost of the total, or room in memory at the cost of start-up time or answer quality. If you can say what you would measure to check, you sound like someone who has run it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Part 1: What vLLM is and how it is built
&lt;/h2&gt;

&lt;p&gt;Interviewers usually start here, to see whether you know what vLLM actually does and what runs when you type &lt;code&gt;vllm serve&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. What is vLLM, and why would a team use it?
&lt;/h3&gt;

&lt;p&gt;vLLM is open-source software that serves a language model to many people at once on GPUs. It answers in the same format as OpenAI's API, so an application written for OpenAI can call it after changing the address and the model's name.&lt;/p&gt;

&lt;p&gt;Teams use it because it keeps the GPU busy with many conversations at the same time. Two ideas make that possible: PagedAttention, which stores each conversation's notes in small pages so memory is not wasted, and continuous batching, which lets requests join and leave the running batch at every step.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What we measured:&lt;/strong&gt; on a rented RTX 4090 with Qwen3-0.6B, one request on its own got 359 tokens a second. Sixty-four requests at once got 12,087 tokens a second in total, about 189 each, and all 64 answers were back after 1.06 seconds.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Which processes does vLLM run, and what does each one do?
&lt;/h3&gt;

&lt;p&gt;On a machine with one GPU it runs two. The API server is the &lt;code&gt;vllm serve&lt;/code&gt; command: it speaks HTTP, turns the question into tokens with the model's chat template, and turns the answer's tokens back into text and streams them out. The engine core is the second process: it runs the scheduler and, on one GPU, the worker that runs the model.&lt;/p&gt;

&lt;p&gt;Keeping tokenizing and streaming in the API server lets that work happen while the GPU is busy. With more GPUs, vLLM adds one worker process for each GPU.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What we measured:&lt;/strong&gt; the API server held 1.37 GiB of the computer's memory and none of the GPU's, and the engine core held 22,186 MiB of the GPU.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Is vLLM compatible with the OpenAI API?
&lt;/h3&gt;

&lt;p&gt;Yes. It answers on the same addresses, such as &lt;code&gt;/v1/chat/completions&lt;/code&gt; and &lt;code&gt;/v1/models&lt;/code&gt;, in the same format, and it returns the token counts in the usual usage field. The OpenAI Python library works once you point it at the vLLM server.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What we measured:&lt;/strong&gt; the OpenAI library sent a 35-token question to Qwen3-0.6B on an RTX 4090 and got the first word back after 0.167 seconds.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Why does vLLM take minutes to start, and how do you make it faster?
&lt;/h3&gt;

&lt;p&gt;Before its first answer it loads the weights, compiles the model, builds small GPU programs for your exact card, measures how much working memory it needs and records CUDA graphs, which are replays of whole series of GPU programs that save launch time on every later token. All of that makes later tokens faster.&lt;/p&gt;

&lt;p&gt;The compiled code is saved to disk, so a second start on the same machine is much quicker. In production, download the model before starting, keep the cache folders on a disk that survives restarts, and start new servers a little before you need them. The flag &lt;code&gt;--enforce-eager&lt;/code&gt; skips compiling and CUDA graphs for a fast start, and the server then writes more slowly.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What we measured:&lt;/strong&gt; with Qwen3-8B on an RTX 4090, the first start with every cache deleted took 164 seconds and a warm start took 50. An &lt;code&gt;--enforce-eager&lt;/code&gt; start was ready after 33 seconds but wrote 39.3 tokens a second for one person instead of 57.7, and 1,163 in total for 32 people instead of 1,577.&lt;/p&gt;

&lt;h2&gt;
  
  
  Part 2: Memory, the budget every vLLM answer depends on
&lt;/h2&gt;

&lt;p&gt;Almost every hard vLLM question comes back to GPU memory: what the weights take, what is left for the KV cache, and which setting moves which part.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fj19hvl7hz78cvpufcu5k.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fj19hvl7hz78cvpufcu5k.webp" alt="A column drawn to scale showing the RTX 4090's 23.52 GiB as vLLM counts it: 15.44 GiB of fixed use for weights, 1.63 GiB of working space, 4.56 GiB of KV cache for 33,216 tokens, and 1.88 GiB above the 0.92 budget line left alone." width="800" height="663"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Where the GPU's memory goes when vLLM starts, and which setting moves which part. For Qwen3-8B on an RTX 4090, 4.56 GiB was left for the KV cache, room for 33,216 tokens.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Why does vLLM take almost all of the GPU's memory, and what does gpu-memory-utilization do?
&lt;/h3&gt;

&lt;p&gt;At start-up vLLM claims a fixed share of the card, 0.92 by default in vLLM 0.33, set by &lt;code&gt;--gpu-memory-utilization&lt;/code&gt;. The weights and its working space come out of that share first, and everything left goes to the KV cache, the keys and values the model keeps for every token of every conversation. A bigger cache means more people served at once.&lt;/p&gt;

&lt;p&gt;Because the weights take the same room whatever the share, a small change in the share is a large change in the cache. Lower it only when another program must share the card.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What we measured:&lt;/strong&gt; on a 24 GB RTX 4090, Qwen3-0.6B's weights took 1.12 GiB and the cache got 19.43 GiB, room for 181,936 tokens. For Qwen3-8B, a share of 0.92 gave 33,216 tokens of cache and 0.80 gave 17,456 tokens, leaving 4.71 GiB of the card for something else.&lt;/p&gt;

&lt;h3&gt;
  
  
  6. How many users can one GPU serve with vLLM?
&lt;/h3&gt;

&lt;p&gt;As many conversations as fit in its KV cache at the same moment. Divide the cache size vLLM prints at start-up by the tokens one of your conversations holds at its longest. If you have promised users a speed, the real number is lower, because each person slows down as the batch grows.&lt;/p&gt;

&lt;p&gt;The log line "Maximum concurrency" is a worst case: how many conversations of the full allowed length would fit. It does not limit how many people vLLM serves.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What we measured:&lt;/strong&gt; for Qwen3-8B in 16 bits on an RTX 4090, vLLM had 33,216 tokens of cache: about one conversation of 32,768 tokens, 8 of 4,000 or 16 of 2,000. Its log said Maximum concurrency 1.01x, yet the same server ran 64 conversations of about 340 tokens at once with the cache at most 66.8% full.&lt;/p&gt;

&lt;h3&gt;
  
  
  7. vLLM refuses to start with "KV cache is needed, which is larger than the available KV cache memory". What do you do?
&lt;/h3&gt;

&lt;p&gt;The message means one conversation of the model's full allowed length would not fit in the cache left after the weights. vLLM checks this at start-up and refuses rather than fail later. The usual fix is a shorter &lt;code&gt;--max-model-len&lt;/code&gt;. It leaves the cache the same size and only changes the check.&lt;/p&gt;

&lt;p&gt;The other fixes make real room: raise the memory share, store the weights in fewer bits, or use a GPU with more memory.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What we measured:&lt;/strong&gt; for Qwen3-8B on an RTX 4090, the full 40,960 tokens needed 5.62 GiB with 4.56 GiB available, and vLLM estimated 33,200 as the longest that would fit. With &lt;code&gt;--max-model-len 8192&lt;/code&gt; it started at once.&lt;/p&gt;

&lt;h3&gt;
  
  
  8. Does an fp8 KV cache double the number of tokens vLLM can hold?
&lt;/h3&gt;

&lt;p&gt;Nearly. With &lt;code&gt;--kv-cache-dtype fp8&lt;/code&gt;, each token's keys and values take one byte a number instead of two, so each token needs half the room. The numbers are kept more coarsely, so check the quality of the answers before you keep it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What we measured:&lt;/strong&gt; for Qwen3-8B the notes fell from 147,456 to 73,728 bytes a token. The cache held 59,856 tokens instead of 33,216, 1.80 times as many and not 2, because that start needed 0.45 GiB more working space.&lt;/p&gt;

&lt;h3&gt;
  
  
  9. When is CPU offloading in vLLM worth it?
&lt;/h3&gt;

&lt;p&gt;Only when the model will not fit any other way and speed does not matter. &lt;code&gt;--cpu-offload-gb&lt;/code&gt; keeps part of the weights in the computer's ordinary memory, which makes room on the GPU, but those weights must cross the PCIe link to the GPU for every token. On the RTX 4090 that link moves at most 32 GB a second each way, far slower than the card's own memory.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What we measured:&lt;/strong&gt; offloading 6.02 GiB of Qwen3-8B's weights grew the cache from 33,216 to 76,912 tokens, and one person's answer fell from 56.4 to 2.6 tokens a second.&lt;/p&gt;

&lt;h2&gt;
  
  
  Part 3: The scheduler, who runs in each step
&lt;/h2&gt;

&lt;p&gt;These questions test whether you understand what happens between two steps on the GPU, which is where most latency surprises come from.&lt;/p&gt;

&lt;h3&gt;
  
  
  10. What is PagedAttention, in one answer?
&lt;/h3&gt;

&lt;p&gt;It is how vLLM stores each conversation's KV cache: in small pages of a fixed number of tokens, 16 by default in the version we measured, taken from a shared pool as the conversation grows. A block table records which pages belong to the conversation and in what order, so the pages can sit anywhere in memory and a conversation wastes at most part of its last page.&lt;/p&gt;

&lt;p&gt;Without pages, a server must reserve room for the longest possible conversation for every request, and most of that room is never used.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What we measured:&lt;/strong&gt; on our RTX 4090 with Qwen3-8B, vLLM made 2,076 pages of 2.25 MiB each, 33,216 tokens in all.&lt;/p&gt;

&lt;h3&gt;
  
  
  11. What is continuous batching in vLLM?
&lt;/h3&gt;

&lt;p&gt;The scheduler decides afresh at every step which conversations to work on. A finished conversation leaves and a waiting one joins between any two steps, so the batch never waits for its slowest member. vLLM always works this way.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What we measured:&lt;/strong&gt; the same 48 answers took 24.33 seconds in fixed waves of 12, made on purpose by the client, and 16.83 seconds with continuous batching, 12 at a time.&lt;/p&gt;

&lt;h3&gt;
  
  
  12. What do max-num-seqs and max-num-batched-tokens control?
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;--max-num-seqs&lt;/code&gt; is the most conversations that can run at once, like seats in a room. Requests beyond it wait in a queue even when memory is free. The default for the API server in vLLM 0.33 is 256, and 1,024 on H100-class cards of 70 GB or more.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;--max-num-batched-tokens&lt;/code&gt; is the token budget of one step: one token for each conversation writing its answer, plus pieces of questions being read. The default is 2,048 on GPUs like the RTX 4090 and 8,192 for the API server on H100-class cards.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What we measured:&lt;/strong&gt; setting &lt;code&gt;--max-num-seqs&lt;/code&gt; to 8 cut the time per token from 26.08 to 18.65 ms, but raised the mean wait for the first token from 0.99 to 17.35 seconds and cut throughput 5.1 times.&lt;/p&gt;

&lt;h3&gt;
  
  
  13. What is preemption in vLLM, and is it bad?
&lt;/h3&gt;

&lt;p&gt;When a running conversation needs a new page and none is free, vLLM pauses the conversation that started last, frees its pages, puts it back at the front of the queue, and later reads its text again to rebuild its notes. The answer still comes back complete, only later.&lt;/p&gt;

&lt;p&gt;A few preemptions at a traffic peak are normal. Preemptions all the time mean the server needs more KV cache or fewer people per GPU.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What we measured:&lt;/strong&gt; with 160 people at once the cache reached 100.0% full, vLLM preempted 64 times and up to 62 requests waited. Every answer still came back complete, at 21.9 tokens a second each instead of 43.4 with 64 people.&lt;/p&gt;

&lt;h3&gt;
  
  
  14. Why do streaming answers pause when someone sends a very long prompt, and what fixes it?
&lt;/h3&gt;

&lt;p&gt;The model runs in steps, and a step cannot be interrupted. If one step reads a whole long prompt, every answer being written waits until that step ends. Chunked prefill, on by default in vLLM 0.33, reads a long prompt over several steps in pieces that fit beside the tokens being written for everyone else.&lt;/p&gt;

&lt;p&gt;A bigger step budget reads long prompts in fewer steps, so their first word comes a little sooner, but everyone else's pauses grow. For a chat service, measure the pauses before raising it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fuixqhn57w08azo8auqwu.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fuixqhn57w08azo8auqwu.webp" alt="A chart of the tokens one of eight customers received over 9 seconds: the line climbs steadily, goes flat for 1,499 ms just after a long question arrives at 2.0 s, then climbs again at the same rate." width="800" height="1026"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;One long prompt can freeze everyone else's answer. With a step budget of 16,384 tokens, a 12,563-token question stopped the other eight answers for up to 1,499 ms.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What we measured:&lt;/strong&gt; a 12,563-token question was read in 7 pieces at the default budget, and the eight people being answered never paused for more than 300 ms. With a budget of 16,384 it was read in one step, its first word came only 70 ms sooner, and the others stopped for up to 1,499 ms.&lt;/p&gt;

&lt;h3&gt;
  
  
  15. What is prefix caching, and why might the hit rate be low?
&lt;/h3&gt;

&lt;p&gt;vLLM keeps the KV cache pages of earlier requests and reuses them when a new request starts with exactly the same tokens, so that beginning is not read again. It is on by default in vLLM 0.33.&lt;/p&gt;

&lt;p&gt;Matching starts at the first token and stops at the first page that differs, so anything that changes near the start, such as a timestamp, a request number or a user's name, ruins it. Put the parts that change at the end of the prompt.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What we measured:&lt;/strong&gt; eight questions behind the same 2,958-token beginning found 87% of their prompt tokens in the cache, and their first word came after about 46 ms instead of 326 ms. A clock time at the start of the system message cut the hit rate to 0%; the same line at the end gave 86%.&lt;/p&gt;

&lt;h2&gt;
  
  
  Part 4: Measuring and tuning a vLLM server
&lt;/h2&gt;

&lt;p&gt;Senior interviews ask how you would prove a change helped. These answers show the habits that make a number trustworthy.&lt;/p&gt;

&lt;h3&gt;
  
  
  16. How do you benchmark a vLLM server?
&lt;/h3&gt;

&lt;p&gt;Use &lt;code&gt;vllm bench serve&lt;/code&gt;, which ships with vLLM. Point it at the running server, choose a dataset (random with fixed input and output lengths is the simplest), the number of requests and, for a realistic test, a request rate. It reports throughput, time to first token, time per output token and inter-token latency, with medians and 99th percentiles.&lt;/p&gt;

&lt;p&gt;Always check that failed requests is 0, and know which sampling settings it sent: in vLLM 0.33 it sends no temperature unless you add one.&lt;/p&gt;

&lt;h3&gt;
  
  
  17. Why must a vLLM load test use fresh prompts?
&lt;/h3&gt;

&lt;p&gt;Because prefix caching keeps the notes of questions vLLM has already read. A test that sends the same questions again measures the cache, not the GPU. Give every run its own made-up questions and check that the &lt;code&gt;vllm:prefix_cache_hits_total&lt;/code&gt; counter at &lt;code&gt;/metrics&lt;/code&gt; stays at 0.&lt;/p&gt;

&lt;h3&gt;
  
  
  18. Why does throughput stop rising when you add more users?
&lt;/h3&gt;

&lt;p&gt;Usually because the KV cache is full: the server pauses some people and works their notes out again later, which costs time and writes no new tokens. The arithmetic also stops being free once long questions are read in the same steps.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What we measured:&lt;/strong&gt; with Qwen3-8B on an RTX 4090, going from 32 to 64 people raised the total by only 16%. vLLM paused requests 59 times at 64 people and never below, and the p99 wait for the first word reached 8.69 seconds.&lt;/p&gt;

&lt;h3&gt;
  
  
  19. What do the optimisation levels -O0 to -O3 change in vLLM?
&lt;/h3&gt;

&lt;p&gt;They are presets for how much vLLM prepares before it opens. In vLLM 0.33.0, &lt;code&gt;-O0&lt;/code&gt; compiles nothing and records no CUDA graphs, &lt;code&gt;-O1&lt;/code&gt; compiles and records piecewise graphs only, &lt;code&gt;-O2&lt;/code&gt;, the default, adds full graphs for writing steps, and &lt;code&gt;-O3&lt;/code&gt; is currently the same as &lt;code&gt;-O2&lt;/code&gt;. Use &lt;code&gt;-O0&lt;/code&gt; while you try settings and the default for a server people use.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What we measured:&lt;/strong&gt; with Qwen3-8B on an RTX 4090, &lt;code&gt;-O0&lt;/code&gt; was ready in 40 seconds and wrote 24% slower for one person after the first word.&lt;/p&gt;

&lt;h2&gt;
  
  
  What to take into the interview
&lt;/h2&gt;

&lt;p&gt;If you remember one thing, make it the memory budget. The weights come first, the working space second, and whatever is left is the KV cache. How many people you can serve, when preemption starts, why throughput flattens and what each memory flag really does all follow from that one picture. The scheduler questions are about the other half: what goes into each step, and who waits when one step is too big.&lt;/p&gt;

&lt;p&gt;The full guide, with a link to the lesson each number comes from, is on our site: &lt;a href="https://www.systemdesign.academy/vllm-interview-questions?utm_source=devto&amp;amp;utm_medium=crosspost&amp;amp;utm_campaign=crosspost-1121" rel="noopener noreferrer"&gt;vLLM interview questions, answered with numbers from a real GPU&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Go deeper: Hands-On Inference Engineering
&lt;/h2&gt;

&lt;p&gt;These numbers come from a course I teach on System Design Academy, Hands-On Inference Engineering. In it you install vLLM on a rented RTX 4090, read its memory budget line by line, then change one setting at a time and measure what each one does to speed, memory and waiting time. It is written for backend, ML platform and AI engineers who want to serve their own models and answer these questions from experience: &lt;a href="https://www.systemdesign.academy/inference-engineering?utm_source=devto&amp;amp;utm_medium=crosspost&amp;amp;utm_campaign=crosspost-1121" rel="noopener noreferrer"&gt;see the Hands-On Inference Engineering course&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>vllm</category>
      <category>llm</category>
      <category>machinelearning</category>
      <category>interview</category>
    </item>
    <item>
      <title>SGLang vs vLLM: what is really different inside, and how to compare them fairly</title>
      <dc:creator>RONI DAS</dc:creator>
      <pubDate>Fri, 20 Nov 2026 05:54:20 +0000</pubDate>
      <link>https://dev.to/roni_das_b1b76c5ee6583027/sglang-vs-vllm-what-is-really-different-inside-and-how-to-compare-them-fairly-1703</link>
      <guid>https://dev.to/roni_das_b1b76c5ee6583027/sglang-vs-vllm-what-is-really-different-inside-and-how-to-compare-them-fairly-1703</guid>
      <description>&lt;p&gt;&lt;em&gt;Both engines share almost every big idea in modern LLM serving. The real differences are in how they reuse the KV cache, and most published benchmarks change more than one thing at a time. Here is how I would explain it to a new engineer on the team.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Once a team decides to serve an open model on its own GPUs, the next question is almost always the same: should we run SGLang or vLLM? Most of the answers you find are a single benchmark chart from one team's blog, with no word on how it was measured. That is not much to base a production choice on.&lt;/p&gt;

&lt;p&gt;In this post I go through what the two engines share, where they really differ inside, how to compare them without fooling yourself, and how to choose. One thing up front: every measured number below is a vLLM number, from vLLM 0.33 on a rented RTX 4090 in our course labs. We have not yet run SGLang in the course, so I make no speed claim about it. Where I describe SGLang, it is what its own project describes.&lt;/p&gt;

&lt;h2&gt;
  
  
  What the two engines are
&lt;/h2&gt;

&lt;p&gt;Both are model servers. A model server is a program that loads a language model onto one or more GPUs, keeps it in memory, and answers many people's requests at the same time. Both are free and open source, both are written mainly in Python with fast GPU code underneath, and both answer in the same format as OpenAI's API.&lt;/p&gt;

&lt;p&gt;vLLM came out of the University of California, Berkeley, together with the 2023 paper that introduced PagedAttention. SGLang came out of the LMSYS research group and introduced RadixAttention, a way of reusing the work for prompts that start the same way. The versions our course teaches, checked on 12 November 2026, are vLLM 0.33 and SGLang 0.5.23.&lt;/p&gt;

&lt;h2&gt;
  
  
  They have more in common than the arguments suggest
&lt;/h2&gt;

&lt;p&gt;Almost every big idea in modern serving is in both engines:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Continuous batching&lt;/strong&gt;, where requests join and leave the batch at every step instead of waiting for the whole batch to finish.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Paged storage of the KV cache&lt;/strong&gt;, the notes the model keeps for every token of every conversation.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reuse of shared prompt beginnings&lt;/strong&gt;, so a prompt the server has already read is not read again.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Chunked prefill&lt;/strong&gt;, which reads a long prompt in pieces so it does not hold up everyone else.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Speculative decoding&lt;/strong&gt;, where a fast guesser proposes tokens that the large model checks in one pass.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Tensor parallelism&lt;/strong&gt; across several GPUs, quantized models, and answers forced to follow a JSON schema.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So the choice is rarely about a missing feature. It is about how each engine performs on your workload, and how comfortable your team is running it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Can you switch without changing your code?
&lt;/h2&gt;

&lt;p&gt;Mostly, yes. Both serve OpenAI-compatible addresses such as &lt;code&gt;/v1/chat/completions&lt;/code&gt;, so the OpenAI Python library works with either once you change the address and the model's name. In our vLLM run, the library sent a question to Qwen3-0.6B on an RTX 4090 and got the first word back after 0.167 seconds, with the token counts in the usual usage field.&lt;/p&gt;

&lt;p&gt;What does change is the server side: the command that starts it, the names of its settings, and some extra request fields each engine accepts beyond OpenAI's own. Test those before you switch a live service.&lt;/p&gt;

&lt;h2&gt;
  
  
  The real difference: how each one reuses a prompt it has read before
&lt;/h2&gt;

&lt;p&gt;Both engines keep the notes of a prompt beginning they have already read, so the next request that starts the same way skips that work. They keep the books differently.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F62nrru29t8psj3cedyys.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F62nrru29t8psj3cedyys.webp" alt="A hash chain of page labels: each full page of 16 tokens is labelled with a hash of the label before it plus its own tokens. In the first question all three pages are worked out and kept. In the next question a few tokens near the start differ, so every page after them gets a new label and none is found, even a page whose own tokens are the same." width="800" height="872"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;How vLLM finds a prompt it has read before: a chain of page labels.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;vLLM&lt;/strong&gt; gives each full page of 16 tokens a label made from the label of the page before it and its own tokens. When a new prompt arrives, it reuses pages from the start until the first page that differs, and stops looking there. Because each label depends on the one before, a change on page 1 changes the label of every page after it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;SGLang&lt;/strong&gt; keeps the cached beginnings in a radix tree, a tree in which requests that share a beginning share a branch, and looks up the longest matching branch.&lt;/p&gt;

&lt;p&gt;The goal is the same, and the effect can be large. vLLM's scheme made our first word about 7 times faster on a hit. Eight questions behind the same 2,958-token handbook found 87% of their prompt in the cache, and the first word came after about 46 ms instead of 326 ms.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F9o9umm4w2fpdflva60qu.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F9o9umm4w2fpdflva60qu.webp" alt="A line chart of the wait for the first word, question by question, from four runs on the rented RTX 4090. With nothing found the wait stays near 326 to 330 ms. With the handbook found it drops from the second question onwards to about 46 ms, and 37 ms with the time at the end of the system message." width="800" height="1172"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Reuse made the first word about 7 times faster in our vLLM runs.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  What breaks reuse in either engine
&lt;/h3&gt;

&lt;p&gt;Anything that changes near the start of the prompt: a timestamp, a request number, a user's name, or a shuffled list of tools. Both engines can only reuse a beginning that matches token for token.&lt;/p&gt;

&lt;p&gt;In our vLLM run, a clock time at the start of the system message cut the hit rate from 87% to 0%. The same line at the end of the system message gave 86%. Put the parts that never change first, whichever engine you choose.&lt;/p&gt;

&lt;h2&gt;
  
  
  Both store the KV cache in pages
&lt;/h2&gt;

&lt;p&gt;Neither engine reserves room for the longest possible conversation, which would waste most of the memory. vLLM's PagedAttention uses pages of 16 tokens by default, listed for each conversation in a block table. SGLang keeps cache entries in a shared memory pool indexed by its radix tree.&lt;/p&gt;

&lt;p&gt;On our RTX 4090, vLLM made 2,076 pages for Qwen3-8B. Sixty-four conversations of about 340 tokens used only 66.8% of them at once, where reserving 32,768 tokens for each would have fitted just one.&lt;/p&gt;

&lt;h2&gt;
  
  
  How the main settings map
&lt;/h2&gt;

&lt;p&gt;The ideas match even where the names do not:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;What it controls&lt;/th&gt;
&lt;th&gt;vLLM&lt;/th&gt;
&lt;th&gt;SGLang&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Share of GPU memory the engine may use&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;--gpu-memory-utilization&lt;/code&gt; (0.92 by default in vLLM 0.33)&lt;/td&gt;
&lt;td&gt;&lt;code&gt;--mem-fraction-static&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Most conversations running at once&lt;/td&gt;
&lt;td&gt;&lt;code&gt;--max-num-seqs&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;--max-running-requests&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Token budget of one step&lt;/td&gt;
&lt;td&gt;&lt;code&gt;--max-num-batched-tokens&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;--chunked-prefill-size&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Both also have a scheduling policy setting. Defaults differ between the engines and between versions, so read each engine's start-up log to see how much room it gave the KV cache before you compare anything.&lt;/p&gt;

&lt;h2&gt;
  
  
  Structured output and speculative decoding
&lt;/h2&gt;

&lt;p&gt;Both can force the model's answer to follow a JSON schema or a grammar at serving time, by allowing only the tokens that keep the answer valid. Both can also parse tool calls from the model's output into OpenAI's tool-call format. SGLang also comes with its own frontend language, a way of writing programs that make several model calls with branches and forced formats, which is where its prefix tree pays off. vLLM keeps to the server and the OpenAI-style API, with many settings for parsers and backends.&lt;/p&gt;

&lt;p&gt;Both support speculative decoding. vLLM 0.33 accepts methods from simple n-gram matching to a draft model, EAGLE-3 heads and multi-token prediction, and SGLang supports EAGLE-style guessing too. It helps most when few people are using the GPU. On a busy server the extra checking work can make things slower, so measure it at your real load.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to compare them fairly
&lt;/h2&gt;

&lt;p&gt;Most comparisons online are not wrong on purpose. They just change more than one thing at a time. These are the rules I would hold any benchmark to, including my own.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Keep everything else the same.&lt;/strong&gt; The same model files, the same number format, the same GPU and the same driver. Give both the same share of GPU memory and check in each log how much room the KV cache really got. Send the same workload: the same prompt and answer lengths, the same number of people at once or the same arrival rate, and the same sampling settings.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Use fresh prompts in every run.&lt;/strong&gt; Both engines remember prompts they have read. A test that sends the same questions again measures the prefix cache, not the GPU; our first-word wait fell from 326 ms to 46 ms on a hit. Give every run its own made-up questions, unless reuse is the thing you are measuring, and in vLLM check that &lt;code&gt;vllm:prefix_cache_hits_total&lt;/code&gt; stays at 0 during the test.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Compare second starts, not first starts.&lt;/strong&gt; vLLM compiles the model and measures its working space during the first start. On our card, a first start of Qwen3-8B got 33,216 tokens of KV cache, and the next start with the same settings got 38,000. If one engine is measured cold and the other warm, you are partly comparing start-up work.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Send enough requests for the slow end to mean something.&lt;/strong&gt; The p99 is the value 99 in 100 requests beat, so with a few dozen requests it is simply the slowest one. In our vLLM test with 48 answers, that is exactly what the p99 was. Use hundreds of requests per level, say how many each percentile came from, and check that the failed request count is 0. Know what your tool sends, too: vLLM 0.33's &lt;code&gt;vllm bench serve&lt;/code&gt; sends no temperature unless you add one.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Report more than one number.&lt;/strong&gt; Warm both servers up, run each test several times, and report the spread as well as the best run. Compare the median and p99 wait for the first word, the median and p99 gap between words, and total tokens a second, all at the same load. One number on its own hides the trade.&lt;/p&gt;

&lt;h2&gt;
  
  
  So which one should you pick?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;vLLM is the safer default&lt;/strong&gt; when you want the widest set of supported models, settings and integrations, and the most material to learn from. Many other systems build on it: llm-d, for example, runs vLLM underneath its routing layer on Kubernetes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;SGLang is worth a test&lt;/strong&gt; when your traffic reuses long beginnings heavily: agents that resend the same long instructions and history on every turn, many branches from one prompt, or programs that make several structured calls in a row. That is the kind of work RadixAttention was designed for. If it keeps your promises at a lower cost, the switch is mostly a server change, because your client code speaks the same OpenAI-style API.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Learn the ideas before the engine.&lt;/strong&gt; Prefill and decode, the KV cache, batching, paging and prefix reuse carry over from one engine to the other. Learn one engine deeply, and once you know why each of its settings exists, the other engine's settings read like the same ideas with different names.&lt;/p&gt;

&lt;p&gt;The full guide, with every question and the lesson each number comes from, is on our site: &lt;a href="https://www.systemdesign.academy/sglang-vs-vllm?utm_source=devto&amp;amp;utm_medium=crosspost&amp;amp;utm_campaign=xp-1120" rel="noopener noreferrer"&gt;SGLang vs vLLM: what is different, and how to choose&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Go deeper: Hands-On Inference Engineering
&lt;/h2&gt;

&lt;p&gt;These numbers come from a course I teach on System Design Academy, Hands-On Inference Engineering. You rent the GPU yourself, serve a model with vLLM, learn every setting that changes a number, and then run a fair head-to-head benchmark against SGLang on the same GPU. The SGLang part is still being written, which is why this post makes no speed claim about it. It is written for backend and AI Engineering teams who want to serve their own models: &lt;a href="https://www.systemdesign.academy/inference-engineering?utm_source=devto&amp;amp;utm_medium=crosspost&amp;amp;utm_campaign=xp-1120" rel="noopener noreferrer"&gt;see the Hands-On Inference Engineering course&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>vllm</category>
      <category>llm</category>
      <category>machinelearning</category>
      <category>ai</category>
    </item>
    <item>
      <title>How much GPU memory does an LLM need? The whole sum, worked out on real cards</title>
      <dc:creator>RONI DAS</dc:creator>
      <pubDate>Thu, 19 Nov 2026 05:46:38 +0000</pubDate>
      <link>https://dev.to/roni_das_b1b76c5ee6583027/how-much-gpu-memory-does-an-llm-need-the-whole-sum-worked-out-on-real-cards-4glg</link>
      <guid>https://dev.to/roni_das_b1b76c5ee6583027/how-much-gpu-memory-does-an-llm-need-the-whole-sum-worked-out-on-real-cards-4glg</guid>
      <description>&lt;p&gt;Before a team rents or buys a GPU to serve a language model, someone asks the same question: how much memory does this model need? The answer you usually find online is "the number of parameters times two bytes". That sum is correct, and it is also the reason so many first deployments disappoint. It tells you whether the model loads. It does not tell you how many people the card can serve, and that is the number your users care about.&lt;/p&gt;

&lt;p&gt;In this post I work through the whole sum the way I would at a whiteboard with a new engineer on the team. Every number comes from real starts of vLLM 0.33 on rented GPUs, or from the course's GPU planning game, where each plan was checked on real cards. Nothing here is a rule of thumb that I have not seen hold on a card.&lt;/p&gt;

&lt;h2&gt;
  
  
  Three things share the card
&lt;/h2&gt;

&lt;p&gt;A model being served is not alone in GPU memory. Three things share it.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;The weights.&lt;/strong&gt; These are the numbers the model learned in training. Their size is fixed for a given model and a given number of bits.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The working space.&lt;/strong&gt; This is the memory the model needs for its arithmetic while it runs. vLLM measures it with a trial run when it starts and sets it aside.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The KV cache.&lt;/strong&gt; This holds notes on every token of every conversation the model is answering at that moment. It grows with every person and every token.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The first two are fixed. Only the third grows with your users, so the third decides how many people one card can serve. Keep that in mind, because everything below is really about how much room is left for it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step one: the weights
&lt;/h2&gt;

&lt;p&gt;Multiply the number of parameters by the bytes each one takes: 2 bytes in 16 bits, 1 byte in 8 bits, and about half a byte in 4 bits.&lt;/p&gt;

&lt;p&gt;Qwen3-8B has about 8.2 billion parameters. In 16 bits that is 16.4 GB, which is 15.26 GiB. A GiB is 1,073,741,824 bytes, a little more than a GB, and vLLM counts in GiB, so it is worth doing the sum in the same unit. The sum on paper was close to what the server reported: vLLM's own start-up log counted 15.27 GiB once Qwen3-8B was on the card.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step two: the working space
&lt;/h2&gt;

&lt;p&gt;This one surprises people because it is not one fixed number. On our RTX 4090 with Qwen3-8B it was about 1.80 GiB on a first start, which also compiles the model, and about 1.15 GiB on a later start that reused the compiled program.&lt;/p&gt;

&lt;p&gt;The practical lesson is simple. Read the cache size from vLLM's log on a second start before you plan capacity, not the first.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step three: what is left is the KV cache
&lt;/h2&gt;

&lt;p&gt;Here is the whole budget for Qwen3-8B in 16 bits on one rented RTX 4090 with 24 GB of memory.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fppftdnely56ivz8obihm.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fppftdnely56ivz8obihm.webp" alt="A waterfall of the memory budget for Qwen3-8B in 16 bits on the RTX 4090, drawn to scale in GiB. The card reports 23.52 GiB. vLLM leaves 8% alone, 1.88 GiB, leaving its share of 21.64 GiB. The weights take 15.26 GiB and the working space about 1.80 GiB. The last bar, about 4.6 GiB, is the KV cache, room for roughly 33,000 tokens." width="800" height="564"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The card reported 23.52 GiB to vLLM. By default vLLM 0.33 uses 92% of it, which is 21.64 GiB, and leaves the rest alone. Take away 15.27 GiB of weights and about 1.8 GiB of working space, and the KV cache got 4.56 GiB in vLLM's log. (The figure above is our sizing script's version of the same sum, which lands within a few hundred tokens of the log.)&lt;/p&gt;

&lt;p&gt;How many tokens is that? For Qwen3-8B, every token of every conversation needs 144 KiB of notes. At 144 KiB a token, 4.56 GiB is room for 33,216 tokens, which vLLM keeps as 2,076 pages of 16 tokens each.&lt;/p&gt;

&lt;p&gt;Now turn tokens into people. That is enough for about one conversation of 32,768 tokens, or 8 of 4,000, or 16 of 2,000, if they all reach their full length at the same time. So the model fits on a 24 GB card, with modest room for conversations. The weights took most of the card; the users get what is left.&lt;/p&gt;

&lt;p&gt;This is also why two cards that both hold the weights can serve very different numbers of people. The weights take the same room whether one person or a hundred are asking. Every extra person adds their own notes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Every setting moves the cache, and only the cache
&lt;/h2&gt;

&lt;p&gt;Once you see that the weights and working space are paid first, every tuning knob becomes easy to reason about. Whatever you change, the KV cache absorbs the difference.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F9icrpq70j5ivpfy8gcuq.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F9icrpq70j5ivpfy8gcuq.webp" alt="Four columns drawn to one scale in GiB, one for each way of serving Qwen3-8B. 16 bits at 92% on the RTX 4090: 33,216 tokens. 16 bits at 80%: 12,656 tokens. 8-bit weights at 92%: 79,184 tokens. 16 bits at 92% on a 44 GiB card, on paper: 170,560 tokens." width="800" height="711"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The share of the card.&lt;/strong&gt; The &lt;code&gt;--gpu-memory-utilization&lt;/code&gt; setting is 0.92 by default in vLLM 0.33. Because the weights and working space take the same room whatever the share, the whole change comes out of the cache. On our card, going from 0.92 to 0.80 cut the cache from 4.56 GiB to 1.74 GiB, and the room for conversations from 33,216 tokens to 12,656. A small change in the share is a large change in capacity.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Weights in 8 bits.&lt;/strong&gt; Whatever the weights give up goes straight to the cache. Qwen3-8B in 8 bits (FP8) took 8.9 GiB instead of 15.27 GiB, and the cache grew to 10.88 GiB, room for 79,184 tokens instead of 33,216. That is about 2.38 times as many. Fewer bits can change the quality of the answers, so test the smaller copy on your own questions before you rely on it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The cache itself in 8 bits.&lt;/strong&gt; With &lt;code&gt;--kv-cache-dtype fp8&lt;/code&gt;, each token's notes take half the bytes: 73,728 instead of 147,456 for Qwen3-8B. In our run the cache held 59,856 tokens instead of 33,216. That is 1.80 times as many rather than 2, because that start needed a little more working space. Check answer quality on long conversations before you keep it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A bigger card.&lt;/strong&gt; The fourth column is on paper, drawn dashed: the same model on a card with 44 GiB available gets 170,560 tokens of cache. A bigger card helps more than its size suggests, because only the cache grows.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why vLLM sometimes refuses to start
&lt;/h2&gt;

&lt;p&gt;A common first error looks like a memory problem but is really a check. At start-up, vLLM makes sure that at least one conversation of the longest allowed length fits in the cache, and refuses to start if it does not. Qwen3-8B allows 40,960 tokens, which needs 5.62 GiB of cache, and our card had 4.56 GiB.&lt;/p&gt;

&lt;p&gt;The fix is to start with a shorter &lt;code&gt;--max-model-len&lt;/code&gt;, such as 8,192. That does not make the cache any bigger. It only changes the check, and it means no single conversation can grow past that length.&lt;/p&gt;

&lt;h2&gt;
  
  
  Big models: fitting is not the same as serving
&lt;/h2&gt;

&lt;p&gt;For a 70B model the question changes from "how many people fit?" to "does the model fit at all with room for anyone?"&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A 70B model on one 80 GB card.&lt;/strong&gt; Llama 3.1 70B in 8 bits loaded into 67.7 GiB on one H100 80 GB. That left 0.07 GiB for the KV cache. vLLM refused to start with room for 2,560 tokens, so the card could not hold even one ordinary conversation, let alone sixteen people. The model fitted. It could not serve.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The same 70B on two A100s.&lt;/strong&gt; The fix is to split the model across two cards with tensor parallelism, which divides each of the model's large weight tables between the GPUs so that every layer is shared out. In vLLM that is &lt;code&gt;--tensor-parallel-size 2&lt;/code&gt;. The two cards talk at every step, so they need a fast link between them; our A100 80 GB SXM cards were joined by NVLink. Llama 3.1 70B in 8 bits then took 33.89 GiB on each card, and the KV cache got 37.48 GiB in all. Sixteen people at once got a new word every 48 ms each, inside the game's promise of 60 ms. One person alone got a word every 26.5 ms.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Two gaming cards instead.&lt;/strong&gt; You can split a model across two RTX 4090s the same way, but it is much slower than it looks on paper. Those cards talk over PCIe, which moves at most 32 GB a second each way on that card. On cards without NVLink it is usually better to run separate copies of a smaller model, one per card, and split the people between them.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A 120B model on one 80 GB card.&lt;/strong&gt; This works when the model is stored in very few bits from the start. OpenAI's gpt-oss-120b is published with its expert layers in MXFP4, a 4-bit format with one shared scale for every 32 numbers. On one A100 80 GB it loaded in 66.14 GiB and left 4.6 GiB for the KV cache. It is also a mixture-of-experts model, where only some groups of weights work on each token, which is why it writes quickly for its size: 32 people at once got a word every 29 ms each in our run.&lt;/p&gt;

&lt;h2&gt;
  
  
  CPU offload makes room, and makes every token wait
&lt;/h2&gt;

&lt;p&gt;When memory is short, the tempting fix is to keep part of the weights in the computer's ordinary memory instead of on the GPU.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F83l13m4qqbrlvui7she0.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F83l13m4qqbrlvui7she0.webp" alt="A hand-drawn architecture of CPU offload on one RTX 4090. 6.02 GiB of weights sit in the computer's memory and cross PCIe, at most 32 GB a second, to the GPU's cores for every token, while the other 9.25 GiB stay in GPU memory at 919 GB a second. One person got 2.6 tokens a second, and one token took 388 ms offloaded against 17.4 ms without." width="800" height="1000"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With &lt;code&gt;--cpu-offload-gb 6&lt;/code&gt;, vLLM moved 6.02 GiB of Qwen3-8B's weights off our RTX 4090, and the cache grew from 33,216 to 76,912 tokens. That looks like a win until you time it. One person's answer fell from 56.4 to 2.6 tokens a second, about 22 times slower, because the offloaded weights cross the PCIe link for every token. Use it only when the model will not fit any other way and speed does not matter.&lt;/p&gt;

&lt;h2&gt;
  
  
  Two reasons the picture is better than it first looks
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Newer models need less memory per user.&lt;/strong&gt; In grouped-query attention, several query heads share one key and one value, and only keys and values go in the cache. Qwen3-8B shares each one among 4 heads, so it keeps 144 KiB a token; the older Qwen1.5-7B keeps 512 KiB. The same memory therefore holds about 3.6 times as many tokens of the newer model, even though it has more parameters. You can check any model by comparing &lt;code&gt;num_key_value_heads&lt;/code&gt; with &lt;code&gt;num_attention_heads&lt;/code&gt; in its &lt;code&gt;config.json&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;vLLM does not reserve room for the longest conversation.&lt;/strong&gt; It stores the cache in pages of 16 tokens, taken one at a time as a conversation grows, so a conversation wastes at most part of its last page. This is called PagedAttention. On our card, 64 people with conversations of about 340 tokens used only 66.8% of the cache at once, where reserving 32,768 tokens for each of them would have fitted only one.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to check the number for your own model
&lt;/h2&gt;

&lt;p&gt;Start the model with vLLM and read its start-up log. It prints the memory the weights took, the KV cache it set aside, the number of tokens that fit, and a maximum concurrency figure, which is how many conversations of the full allowed length would fit at once. Divide the tokens by the length of your own users' conversations for a realistic count.&lt;/p&gt;

&lt;p&gt;So the short version is this. Count three things: the weights, the working space and the KV cache. The first two are fixed, so subtract them from the share of the card your server uses, and whatever is left is the room for conversations. Divide that by the notes one token needs and by the length of your users' conversations, and you know how many people fit. When nothing is left, use fewer bits or more GPUs.&lt;/p&gt;

&lt;p&gt;The full guide, with every question and the lesson each number comes from, is on our site: &lt;a href="https://www.systemdesign.academy/gpu-memory-for-llm?utm_source=devto&amp;amp;utm_medium=crosspost&amp;amp;utm_campaign=gpu-memory-for-llm" rel="noopener noreferrer"&gt;How much GPU memory does an LLM need?&lt;/a&gt;. If you want a first estimate before you rent anything, the free &lt;a href="https://www.systemdesign.academy/tools/gpu-memory-calculator?utm_source=devto&amp;amp;utm_medium=crosspost&amp;amp;utm_campaign=gpu-memory-for-llm" rel="noopener noreferrer"&gt;GPU memory calculator&lt;/a&gt; does the same sum in the browser.&lt;/p&gt;

&lt;h2&gt;
  
  
  Go deeper: Hands-On Inference Engineering
&lt;/h2&gt;

&lt;p&gt;These numbers come from a course I teach on System Design Academy, Hands-On Inference Engineering. In it you rent the GPU yourself, start vLLM with real models, read its memory budget line by line, and change each setting to see how many people fit. It goes on into PagedAttention, the scheduler, quantization, speculative decoding and serving at scale, each one run and measured on a rented GPU, and it is written for backend and AI Engineering teams who want to serve their own models: &lt;a href="https://www.systemdesign.academy/inference-engineering?utm_source=devto&amp;amp;utm_medium=crosspost&amp;amp;utm_campaign=gpu-memory-for-llm" rel="noopener noreferrer"&gt;see the Hands-On Inference Engineering course&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>llm</category>
      <category>gpu</category>
      <category>machinelearning</category>
      <category>ai</category>
    </item>
    <item>
      <title>Why Microsoft 365 stayed down for more than a day when its traffic never grew</title>
      <dc:creator>RONI DAS</dc:creator>
      <pubDate>Sat, 14 Nov 2026 11:39:50 +0000</pubDate>
      <link>https://dev.to/roni_das_b1b76c5ee6583027/why-microsoft-365-stayed-down-for-more-than-a-day-when-its-traffic-never-grew-ipa</link>
      <guid>https://dev.to/roni_das_b1b76c5ee6583027/why-microsoft-365-stayed-down-for-more-than-a-day-when-its-traffic-never-grew-ipa</guid>
      <description>&lt;p&gt;On Thursday 12 November 2026, at 16:17 UTC, people began losing access to their Outlook mail. The service behind it is Exchange Online, the part of Microsoft 365 that stores and delivers Outlook mail, calendars and contacts. On the US East Coast it was 11:17 in the morning; in India it was 21:47. Twelve hours later it was still not fixed, and Microsoft had listed 18 other services as affected, from Teams calendars to Copilot and Intune.&lt;/p&gt;

&lt;p&gt;The most interesting thing Microsoft said came at 19:18 UTC, about three hours in. The load was not coming from more users. Traffic had not grown. Servers were dropping out, and the ones left behind could not carry the work. This article walks through what Microsoft said, update by update, what I saw when I probed its servers during the outage, and why an overload like this can keep a service down long after the first cause is found. It ends with the safeguards I would add to my own services because of it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where things stood when the explainer was last checked
&lt;/h2&gt;

&lt;p&gt;At 00:31 UTC on 14 November, Microsoft listed the incident as recovering, not yet closed. It said it continued to observe sustained service recovery across the affected environments, that it was monitoring for an extended period because of how severe the impact was, and that it was working through the backlog of email that built up during the outage before calling the issue resolved. It also said it believed most of the affected scenarios in the other Microsoft 365 services had been addressed. Microsoft had not published a root cause, and it has said it will publish a Post-Incident Report. Nothing below is a claim about the final cause.&lt;/p&gt;

&lt;h2&gt;
  
  
  What happened
&lt;/h2&gt;

&lt;p&gt;At 16:17 UTC Microsoft opened incident EX1550543: users may be unable to connect to their mailboxes in Exchange Online through multiple connection methods. The methods it named are the ones nearly everybody uses: Outlook in a browser, Outlook installed on a computer (which talks to Exchange with Microsoft's own MAPI protocol), the mail apps on phones (which use Exchange ActiveSync) and programs that read mail through Microsoft's web API (REST). Microsoft also said email delivery may be delayed.&lt;/p&gt;

&lt;p&gt;Two hours later, at 18:26 UTC, Microsoft opened a second notice, MO1550821, for the rest of Microsoft 365. By the morning of 13 November that notice listed 18 services besides Exchange Online, and Microsoft's status page for consumers said Outlook.com, OneDrive, OneNote, To Do, Teams and Microsoft 365 for the web were affected for personal accounts too.&lt;/p&gt;

&lt;p&gt;Microsoft posted 23 updates on the Exchange incident in its first twelve hours. It paused all deployments and configuration changes, moved traffic between regions, restarted and rebuilt servers, tried a different health check, began reverting a recent network change and started blocking some traffic on purpose. At 03:34 UTC on 13 November it said there was "no indication this is the result of malicious activity".&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"We've determined that the elevated load isn't driven by an overall increase in traffic, but by a large number of servers being unexpectedly unavailable, which is shifting load onto the remaining infrastructure."&lt;/p&gt;

&lt;p&gt;Microsoft, incident EX1550543, update at 19:18 UTC on 12 November 2026&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Why one mail service took 18 others with it
&lt;/h2&gt;

&lt;p&gt;Exchange Online is where Microsoft 365 keeps every user's mail, calendar and contacts. Many other services call Exchange when they need that data. Teams asks Exchange for your calendar when you schedule a meeting. Copilot asks it for your email when you ask a question about your inbox. Microsoft Graph, the API that thousands of company apps use, passes mail and calendar requests through to Exchange. Defender for Office 365 checks mail and links that flow through Exchange.&lt;/p&gt;

&lt;p&gt;So when Exchange stopped answering, every feature that needed it failed too, and Microsoft listed them service by service. The useful detail is in what kept working. Microsoft wrote that most of Teams' core functions, including calls, meetings, group chats and channel messages, were not affected, and that files stored directly in SharePoint still opened. Those features do not need Exchange. Engineers call the set of things that break when one part fails its blast radius, and this outage drew it clearly.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fmky68z9sxeqcqxz9msni.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fmky68z9sxeqcqxz9msni.webp" alt="Exchange Online in the middle, servers overloaded, with dashed lines to the 18 services Microsoft listed as affected, including Teams, Copilot, Graph API, Intune, Planner, Purview and SharePoint" width="800" height="947"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Microsoft listed 18 services whose features that interact with Exchange Online were affected. The list is parsed from Microsoft's status feed on 13 November 2026.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;That is the first design lesson. A service that many others depend on needs the strongest protection against overload, because its failure is everyone's failure. And each service that depends on it should be built so that the parts which do not need it keep working. Teams' core chat and calls kept working because they do not need Exchange; the chat features that do, such as adding people to new chats, failed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Twelve hours of updates, read as an engineer would
&lt;/h2&gt;

&lt;p&gt;Microsoft's explanation changed as it learned more, and the order of the changes is the story.&lt;/p&gt;

&lt;p&gt;At 18:19 UTC Microsoft reported elevated load across several regions. At 19:18 it corrected itself: traffic had not grown, but many servers were unexpectedly unavailable, so the rest carried their load. At 19:43 it found why they were unavailable: they were failing automated health checks and being taken out of service, possibly because of a recent change to the part that processes those checks. At 20:56 a different health check method brought some servers back.&lt;/p&gt;

&lt;p&gt;Then it got harder. At 22:00 Microsoft said its actions had "not provided the anticipated relief" and that a core process on the servers was overwhelmed. At 23:59 it said servers it had returned to service "can become unresponsive again once they begin receiving traffic". At 01:06 on 13 November it named the process: the one "responsible for establishing secure connections". The same update said it was reverting a recent network configuration change that correlated with the start of the outage.&lt;/p&gt;

&lt;p&gt;By 04:10 Microsoft was "adjusting traffic protocols, redistributing traffic, and applying strategic traffic blocking measures", with partial recovery that was not lasting. At 04:57 it added that its own routing services were sending a surge of calls to the backend servers, partly because of the traffic moves it had made during the event.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fa4rsyr3vp4cnhtg4k5ya.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fa4rsyr3vp4cnhtg4k5ya.webp" alt="Timeline of ten Microsoft updates in UTC, from the incident opening at 16:20 through failing health checks, an overwhelmed secure connection process and traffic blocking at 04:10" width="800" height="1200"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Ten of the 23 updates on EX1550543, shortened to one line each. Microsoft records the impact as starting at 16:17 and posted its first update at 16:20.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Here is how those updates fit together. A health check is a small test a load balancer runs against each server, such as "answer this request within two seconds". A server that fails is taken out of rotation so users are not sent to a broken machine. That is good when one server is sick. It is dangerous when the check itself is wrong, or when servers fail it only because they are busy. Each server removed pushes more load onto the rest, they get slower, and they fail the check too. Microsoft's 19:57 update says it took steps "to prevent additional servers from being removed from service", which is exactly the brake on that loop.&lt;/p&gt;

&lt;p&gt;Microsoft has not said this was the cause, and its final report may say something different. But several of its steps, stopping removals, bringing servers back slowly and limiting and blocking traffic, are the textbook answer to this kind of loop.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Microsoft's servers did when I connected
&lt;/h2&gt;

&lt;p&gt;While the outage was still on, I probed Microsoft's servers from a home internet connection in India. No account and no password: I sent only the connection handshake and, where that succeeded, one anonymous request with no login, the same thing a browser sends before it shows a login page.&lt;/p&gt;

&lt;p&gt;First, one host at a time, I checked two steps separately. Step one is the TCP connection, the basic "can I reach you" handshake. Step two is the TLS handshake, where the client says hello and the server must answer before the client can send any request. For outlook.office365.com, TCP opened and the TLS handshake did not finish in 15 seconds. For graph.microsoft.com, both finished in under a third of a second.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ ./handshake_check.sh
== outlook.office365.com (40.104.146.194)  05:03:21Z
   TCP connect to 40.104.146.194:443: open
   TLS done after 0.000000s, HTTP 000, total 15.069710s
   curl exit 28: TLS handshake did not finish in 15 s
== graph.microsoft.com (20.190.175.24)  05:03:36Z
   TCP connect to 20.190.175.24:443: open
   TLS done after 0.171402s, HTTP 301, total 0.273097s

$ python3 summarize_probes.py
9 rounds, 04:54 to 05:41 UTC, 13 Nov
  outlook.office365.com          72 attempts,  46 addresses: tls-timeout 61, tcp-timeout 11
  outlook.office.com             61 attempts,  47 addresses: tls-timeout 41, tcp-timeout 19, refused 1
  outlook.live.com               53 attempts,  41 addresses: tls-timeout 36, tcp-timeout 15, reset 1, refused 1
  graph.microsoft.com            27 attempts,   6 addresses: http-301 27
  login.microsoftonline.com      72 attempts,  20 addresses: http-302 72
  teams.microsoft.com            18 attempts,   2 addresses: http-302 18
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Second, in nine rounds, I asked DNS for every address of six Microsoft hosts and tried each address in turn: TCP, then TLS, then one plain request with no login. From 04:54 to 05:41 UTC, DNS gave me more than 40 different addresses for each of the three Outlook hosts, and not one of 186 attempts got as far as an HTTP reply. 138 took the TCP connection and then did not finish the TLS handshake within 6 seconds, 45 did not even open TCP within 6 seconds, 2 refused the connection and 1 reset it. Graph, Microsoft sign-in and Teams answered all 117 of their 117 attempts.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fb3e86sdqwx4gd4zcahsy.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fb3e86sdqwx4gd4zcahsy.webp" alt="Grid of small squares, one per server address per round, for six hosts over 9 rounds. The three Outlook hosts are almost all TLS timeouts and none answered; Graph, sign-in and Teams answered every time" width="800" height="827"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Each square is one address DNS returned for that host in one round. One home connection in India, so people elsewhere may have reached healthier servers.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;What does that tell us? A server that accepts TCP but never answers the TLS hello is alive enough for its operating system to take the connection, but the program that should do the secure handshake is not getting to it. That is consistent with what Microsoft described at 01:06 UTC, though from outside I cannot see which machine stalled. It also means a load balancer that only checks whether TCP opens would still call these servers healthy.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why an overloaded service can stay down after the first cause is fixed
&lt;/h2&gt;

&lt;p&gt;The hardest line in Microsoft's updates is the one from 23:59 UTC: servers put back into service went down again once traffic reached them. One pattern that fits this is what researchers call a metastable failure: a system that stays broken on its own, even after whatever broke it has gone away. Microsoft has not said this is what happened, and its later updates suggest the trigger may still have been present.&lt;/p&gt;

&lt;p&gt;In a metastable failure the first trigger, whatever it was, knocks capacity down. Then the system's own reactions keep it down. Users and apps retry, the retries add load, the overloaded servers waste time on requests that time out, and they complete less real work than before. Removing the trigger does not help, because the extra load is now coming from the outage itself.&lt;/p&gt;

&lt;p&gt;To make this concrete I wrote a small model in Python. It is not Microsoft's system; the numbers are mine. One backend can serve 1,000 requests a second, users send 800, and every failed request is retried up to three times. Past its capacity the server wastes work on requests that time out, so it completes fewer of them as the load grows, which is how real overloaded servers behave. For one minute I cut capacity to 300, then restored it fully.&lt;/p&gt;

&lt;p&gt;With plain retries the server never recovers in the 15 minutes the model runs. Up to four tries per request turn 800 requests a second into about 2,500, and the server serves only about 16% of them for good. With exponential backoff and jitter it is stuck at the same level in this model, because almost every request was failing, so the same number of retries still arrived, just spread out. Only when the front door also turns away everything above 900 requests a second, at once and cheaply, does the server catch up. It is back to serving everything 24 seconds after capacity returns.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fp6tps69zhg43o6pn8awa.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fp6tps69zhg43o6pn8awa.webp" alt="Line chart of the share of requests served per second over 450 seconds. Retrying at once and backing off with jitter both stay stuck at 16 percent after the capacity cut; backing off plus blocking extra traffic at the front door recovers to 100 percent 24 seconds after the fix" width="800" height="987"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;In this toy model, only turning traffic away lets the server recover. Backoff alone did not help here because nearly every request was failing.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Backoff still matters. It stops retries from all arriving in the same second and gives a recovering server room. But backoff on its own is not guaranteed to bring the load under capacity. Something has to say no. That is load shedding: reject work early and cheaply, before the server spends effort on it, so the work it does accept finishes. Microsoft's step of limiting which traffic reached the affected servers, and bringing it back gradually, works the same way.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"When these factors are combined, an overload creates its own feedback loop that results in overload as a steady state."&lt;/p&gt;

&lt;p&gt;Amazon Builders' Library, "Using load shedding to avoid overload"&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  How to design your own service so this does not take you down
&lt;/h2&gt;

&lt;p&gt;You probably do not run anything as large as Exchange Online. But you almost certainly depend on something like it, and you run services that others depend on. These are the safeguards this outage argues for, in the order I would add them.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Never let health checks remove most of your servers.&lt;/strong&gt; Set a limit, for example: never take more than a third of a pool out of rotation because of failed checks. If more than that fails at once, the check or a shared dependency is the likely problem, not the servers. Some load balancers do this by failing open: below a minimum share of healthy servers they ignore the checks and use every server (Envoy calls it the panic threshold; AWS target groups have minimum healthy settings).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Check the real path, but keep the check cheap.&lt;/strong&gt; A check that only opens TCP would have called Microsoft's stuck servers healthy. A check that runs a full mailbox query will fail on busy servers and remove them. Test the step users need (here, completing the TLS handshake), and keep it light.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Shed load at the front door.&lt;/strong&gt; Give each server a limit on concurrent work and reject anything above it at once with a clear "try later" (HTTP 503 with Retry-After). A fast rejection costs little; a slow timeout costs a full request of work. If setting up connections is what overloads, as Microsoft described here, limit new connections at the edge before the TLS handshake, not after it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cap retries in every client you own.&lt;/strong&gt; Use exponential backoff with jitter, and a retry budget: stop retrying when, say, more than 10% of recent calls are already retries. Three retries at every layer of a deep call chain multiply load many times over.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Bring capacity back slowly.&lt;/strong&gt; When servers return, admit a small share of traffic first and raise it step by step. Every client reconnecting at once is a second outage. Microsoft did this in specific locations, starting with a narrower set of connections.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Contain the blast radius in your own product.&lt;/strong&gt; If your app reads mail or calendars through Microsoft Graph, decide now what the screen shows when that call fails: a cached copy, a short message, or the rest of the page without that panel.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Freeze changes during an incident.&lt;/strong&gt; Microsoft paused all deployments and configuration changes about three hours in. Make that an automatic step in your incident process, so nobody changes two things at once while you are still learning what broke.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  What I could and could not check
&lt;/h2&gt;

&lt;p&gt;Microsoft's updates are quoted from two places: its public status feeds at status.cloud.microsoft, which I saved every five minutes from 04:48 UTC on 13 November, and its Service Health record for EX1550543 and MO1550821, which the University of Pennsylvania's mail team republishes word for word with UTC times. I did not use news reports for any fact here. The probes are real but narrow: one home connection, six hosts, no login. The retry model is a model with numbers I chose, written to explain the mechanism, not to reproduce Microsoft's system. Microsoft's own explanation changed during the day, and its final report may change it again.&lt;/p&gt;

&lt;h2&gt;
  
  
  Read the full explainer
&lt;/h2&gt;

&lt;p&gt;This article is adapted from the full explainer on System Design Academy, &lt;a href="https://www.systemdesign.academy/explained/microsoft-365-outage" rel="noopener noreferrer"&gt;Microsoft 365 outage, 12 November 2026: why Outlook and Exchange went down, and what else broke&lt;/a&gt;, which has the complete timeline, the sources and the practical steps for users and admins, and which I update as Microsoft publishes more. If you want to learn how to design services that shed load instead of falling over, and recover once the cause is gone, the &lt;a href="https://www.systemdesign.academy/system-design-course?utm_source=devto&amp;amp;utm_medium=crosspost&amp;amp;utm_campaign=m365-outage" rel="noopener noreferrer"&gt;System Design course&lt;/a&gt; covers health checks, retry and backoff, load shedding, bulkheads, graceful degradation and incident management, each as its own lesson with diagrams and a quiz.&lt;/p&gt;

</description>
      <category>microsoft</category>
      <category>devops</category>
      <category>systemdesign</category>
      <category>cloud</category>
    </item>
    <item>
      <title>How Canada's new Real-Time Rail moves one payment, and what it teaches about backend design</title>
      <dc:creator>RONI DAS</dc:creator>
      <pubDate>Thu, 12 Nov 2026 06:57:44 +0000</pubDate>
      <link>https://dev.to/roni_das_b1b76c5ee6583027/how-canadas-new-real-time-rail-moves-one-payment-and-what-it-teaches-about-backend-design-1479</link>
      <guid>https://dev.to/roni_das_b1b76c5ee6583027/how-canadas-new-real-time-rail-moves-one-payment-and-what-it-teaches-about-backend-design-1479</guid>
      <description>&lt;p&gt;On 9 November 2026 Payments Canada announced that Canada's Real-Time Rail is live. Banks, credit unions and payment companies connected to it can now exchange, clear and settle payments in real time, 24 hours a day, every day of the year. The launch notice is here: &lt;a href="https://www.payments.ca/canada-launches-real-time-rail-bringing-247-payment-infrastructure-national-economy" rel="noopener noreferrer"&gt;https://www.payments.ca/canada-launches-real-time-rail-bringing-247-payment-infrastructure-national-economy&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Most news coverage will talk about faster money. I want to talk about the engineering, because a system that moves money in real time and cannot take it back has to be designed with a lot of care. The same care applies to any backend that does something it cannot undo: charging a card, shipping an order, sending an email, deleting a record.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F3gngpo1bi8505di2fhwy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F3gngpo1bi8505di2fhwy.png" alt="One payment on Canada's Real-Time Rail: you, a check, your bank, the rail, their bank, and the status coming back" width="799" height="382"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  A few words first
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Exchange&lt;/strong&gt;: the payment message travels from one bank to another.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Clearing&lt;/strong&gt;: the banks work out what each one now owes the other.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Settlement&lt;/strong&gt;: the money really moves between the banks' accounts. After this, it is done.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Irrevocable&lt;/strong&gt;: once final, the sender cannot pull the payment back on its own.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;ISO 20022&lt;/strong&gt;: the shared message format every bank on the rail uses. Each message type has a short name, like &lt;code&gt;pacs.008&lt;/code&gt; for "send this payment".&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Participant&lt;/strong&gt;: a bank or payment firm connected to the rail. Only participants can send messages on it.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What actually changed
&lt;/h2&gt;

&lt;p&gt;Payments Canada's own FAQ puts it in two sentences: "For years, Canada has had fast exchange via existing money movement systems. With the launch of the Real-Time Rail, Canada will have both real-time exchange and real-time clearing and settlement in a single system."&lt;/p&gt;

&lt;p&gt;So the message was already fast. What is new is that the money between the two banks now settles payment by payment, as it happens, instead of being settled outside that real-time path. Once a payment has settled, the FAQ says the person receiving it will generally have access to the funds within 60 seconds.&lt;/p&gt;

&lt;p&gt;And it is final. In the FAQ's words: "RTR payments are final and irrevocable, except in circumstances specified in the RTR Rules." If something went wrong, the sending bank may ask for a return, but the decision rests with the receiving bank.&lt;/p&gt;

&lt;h2&gt;
  
  
  The life of one payment
&lt;/h2&gt;

&lt;p&gt;Here is the path, from Payments Canada's ISO 20022 message guide:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;You press send in your banking app.&lt;/li&gt;
&lt;li&gt;Checks run (more on these below).&lt;/li&gt;
&lt;li&gt;Your bank sends a &lt;code&gt;pacs.008&lt;/code&gt;, the "customer credit transfer" message, to the RTR Exchange.&lt;/li&gt;
&lt;li&gt;The rail checks the message. If it passes, the rail gives it a &lt;strong&gt;Clearing System Reference&lt;/strong&gt; and a time stamp. That time stamp starts a &lt;strong&gt;timer&lt;/strong&gt; the rail keeps for this payment.&lt;/li&gt;
&lt;li&gt;The rail sends the &lt;code&gt;pacs.008&lt;/code&gt;, now carrying that reference, on to the receiving bank.&lt;/li&gt;
&lt;li&gt;Status reports (&lt;code&gt;pacs.002&lt;/code&gt;) flow back as the payment moves to final.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;One detail is worth pausing on. The ISO 20022 format allows many payments in one message. The rail does not: "the RTR will only allow a single payment in each respective ISO 20022 message." The number of transactions is fixed at 1. My reading of why: one payment per message means a bad message, a rejection or a time-out touches exactly one payment, and every status report is about exactly one payment.&lt;/p&gt;

&lt;h2&gt;
  
  
  What the bank hears back
&lt;/h2&gt;

&lt;p&gt;Every answer is a &lt;code&gt;pacs.002&lt;/code&gt; with one status code. The guide lists five:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fjkmm0wscbn718uaqqhpt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fjkmm0wscbn718uaqqhpt.png" alt="The pacs.002 status codes: ACWP, ACSP, ACSC, RJCT, PDNG" width="800" height="1000"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Code&lt;/th&gt;
&lt;th&gt;Who sets it&lt;/th&gt;
&lt;th&gt;Meaning&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;ACWP&lt;/td&gt;
&lt;td&gt;receiving bank&lt;/td&gt;
&lt;td&gt;it accepts the payment&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;ACSP&lt;/td&gt;
&lt;td&gt;the rail only&lt;/td&gt;
&lt;td&gt;the payment is declared final, and the banks are now bound to settle it&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;ACSC&lt;/td&gt;
&lt;td&gt;the rail only&lt;/td&gt;
&lt;td&gt;final and settled&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;RJCT&lt;/td&gt;
&lt;td&gt;the rail or the receiving bank&lt;/td&gt;
&lt;td&gt;rejected, always with a reason&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;PDNG&lt;/td&gt;
&lt;td&gt;the rail only&lt;/td&gt;
&lt;td&gt;still in flight, and only sent as an answer to a status question&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Notice the split. The receiving bank can say "I accept", but only the rail can say "this is final". One party owns the decision that cannot be undone.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why the fraud checks come first
&lt;/h2&gt;

&lt;p&gt;If a payment cannot be pulled back after it settles, there is only one place left to stop a bad one: before it moves. Canada is, in Payments Canada's words, "the first G7 country to deploy a real-time payment system with mandatory centralized fraud services for use at launch". There are four:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Confirmation of payee&lt;/strong&gt;: you can see the account holder's identity before you press send. On day one this is available for consumer accounts.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A safety score&lt;/strong&gt;: a real-time risk score on every payment, "before a payment is authorized".&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A risk list&lt;/strong&gt;: accounts already tied to confirmed fraud can be flagged.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A fraud reporting and intelligence platform&lt;/strong&gt;: one view of fraud trends across the whole rail.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These run alongside each bank's own fraud controls. There is also a hard cap: at launch, one payment can carry at most $100,000.&lt;/p&gt;

&lt;h2&gt;
  
  
  The reply never came. Send again, or ask?
&lt;/h2&gt;

&lt;p&gt;This is the part I would most like every backend engineer to read.&lt;/p&gt;

&lt;p&gt;Your bank sent a payment. The status report never arrived. Maybe the network dropped it. Did the payment go through or not?&lt;/p&gt;

&lt;p&gt;The tempting move is to send it again. On an irreversible system that is dangerous. If the first one went through, the person is paid twice, and getting the money back depends on the other bank agreeing to return it. The message format even has a "Possible Duplicate" marker, but the rail's guide says that if it is filled in it "will have no impact on the RTR processing".&lt;/p&gt;

&lt;p&gt;So the rail gives banks a message whose only job is to ask: &lt;code&gt;pacs.028&lt;/code&gt;, the payment status request. The rail answers with a &lt;code&gt;pacs.002&lt;/code&gt;. If the payment is still moving, the answer is &lt;code&gt;PDNG&lt;/code&gt;. And the guide plans for exactly the lost-reply case: the bank should use the rail's Clearing System Reference if it has it, and "should this message not be received", it uses the original identifiers from its own &lt;code&gt;pacs.008&lt;/code&gt; instead.&lt;/p&gt;

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

&lt;h2&gt;
  
  
  Five rules to copy into your own backend
&lt;/h2&gt;

&lt;p&gt;These are my reading of the design, not Payments Canada's words:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;One item per message&lt;/strong&gt; when each item can fail on its own. It keeps failures and status reports small and clear.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A unique id on every request&lt;/strong&gt;, known to both sides. Without it you cannot ask "what happened to this one?".&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A timer on every step in flight.&lt;/strong&gt; Nothing should wait forever.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Ask before you retry.&lt;/strong&gt; Give callers a cheap way to check whether an action already happened, and use it after any time-out.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Check before the step you cannot undo, and cap it.&lt;/strong&gt; Validation, fraud or abuse checks and a hard limit belong in front of the irreversible step, not after it.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;None of these are special to payments. They are what makes retries safe in an order service, a booking system or a job queue, too.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;Payments Canada launch notice, 9 Nov 2026: &lt;a href="https://www.payments.ca/canada-launches-real-time-rail-bringing-247-payment-infrastructure-national-economy" rel="noopener noreferrer"&gt;https://www.payments.ca/canada-launches-real-time-rail-bringing-247-payment-infrastructure-national-economy&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Real-Time Rail FAQ and fraud services pages on payments.ca&lt;/li&gt;
&lt;li&gt;RTR ISO 20022 message specification companion document: &lt;a href="https://www.payments.ca/sites/default/files/RTR_ISO_20022_message_specification_companion_document.pdf" rel="noopener noreferrer"&gt;https://www.payments.ca/sites/default/files/RTR_ISO_20022_message_specification_companion_document.pdf&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Where to go next
&lt;/h2&gt;

&lt;p&gt;Waiting for a reply is the heart of this whole story: what the caller does while it waits, and what it does when the wait runs out. If you want to go one step further, I wrote a free lesson on exactly that on systemdesign.academy: &lt;a href="https://www.systemdesign.academy/courses/foundation/lessons/core-fundamentals/synchronous-processing?utm_source=devto&amp;amp;utm_medium=article&amp;amp;utm_campaign=devto-canada-rtr-1112" rel="noopener noreferrer"&gt;synchronous processing and what to do when the wait runs out&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>payments</category>
      <category>systemdesign</category>
      <category>backend</category>
      <category>distributedsystems</category>
    </item>
    <item>
      <title>10 LangGraph and LangChain Features Most Agent Tutorials Still Don't Show You</title>
      <dc:creator>RONI DAS</dc:creator>
      <pubDate>Thu, 05 Nov 2026 12:29:41 +0000</pubDate>
      <link>https://dev.to/roni_das_b1b76c5ee6583027/10-langgraph-and-langchain-features-most-agent-tutorials-still-dont-show-you-4lfg</link>
      <guid>https://dev.to/roni_das_b1b76c5ee6583027/10-langgraph-and-langchain-features-most-agent-tutorials-still-dont-show-you-4lfg</guid>
      <description>&lt;p&gt;&lt;em&gt;Ten small features that decide whether your agent survives its first week in production. Every snippet below was run, and every output is copied from the terminal.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Most agent tutorials I read this year still teach the same thing. Import &lt;code&gt;create_react_agent&lt;/code&gt;, give it two tools, ask it about the weather, done.&lt;/p&gt;

&lt;p&gt;That function is now deprecated. In LangGraph 1.2.20, calling it prints this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;create_react_agent has been moved to `langchain.agents`. Please update your import to `from langchain.agents import create_agent`. Deprecated in LangGraph V1.0 to be removed in V2.0.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The bigger problem is not the import. It is that the tutorial stops at the happy path. Real agents loop forever, call flaky APIs, run on models that get retired, and sometimes try to delete a table. The demo never shows what happens then.&lt;/p&gt;

&lt;p&gt;LangChain and LangGraph have answers for most of this now, and they are small. First, two terms you will see all the way through:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;An &lt;strong&gt;agent&lt;/strong&gt; is a model calling tools in a loop until the task is done.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Middleware&lt;/strong&gt; is code that wraps that loop. It can look at every model call and every tool call, and change or stop it. LangChain's &lt;code&gt;create_agent&lt;/code&gt; ships with built-in middleware, and you can write your own.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Items 1 to 5 use &lt;code&gt;create_agent&lt;/code&gt; and a real model (&lt;code&gt;gpt-4o-mini&lt;/code&gt;; the whole set cost well under one cent). Items 6 to 10 are LangGraph runtime features and need no model at all. Versions: langchain 1.4.9, langchain-core 1.7.6, langgraph 1.2.20, langchain-openai 1.7.4, Python 3.13.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Call limits, so a stuck agent stops by itself
&lt;/h2&gt;

&lt;p&gt;An agent that polls "is it done yet?" will happily poll forever. Each loop is a paid model call.&lt;/p&gt;

&lt;p&gt;What people usually write is a &lt;code&gt;for i in range(10)&lt;/code&gt; around their own loop, or nothing at all and a prayer.&lt;/p&gt;

&lt;p&gt;The better way is two lines of middleware:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;langchain.agents&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;create_agent&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;langchain.agents.middleware&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;ModelCallLimitMiddleware&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ToolCallLimitMiddleware&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;langchain.tools&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;tool&lt;/span&gt;


&lt;span class="nd"&gt;@tool&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;check_deploy_status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;service&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;Check whether a deploy has finished.&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;service&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;: still rolling out. Check again.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;  &lt;span class="c1"&gt;# never finishes
&lt;/span&gt;

&lt;span class="n"&gt;agent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;create_agent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;openai:gpt-4o-mini&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;tools&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;check_deploy_status&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="n"&gt;system_prompt&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Keep checking until the deploy finishes, then report.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;middleware&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="nc"&gt;ModelCallLimitMiddleware&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;run_limit&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;exit_behavior&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;end&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="nc"&gt;ToolCallLimitMiddleware&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tool_name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;check_deploy_status&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;run_limit&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="p"&gt;],&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;agent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;invoke&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;messages&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;user&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Is the payments deploy done?&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)]})&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;messages&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;type&lt;/span&gt;&lt;span class="si"&gt;:&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nf"&gt;str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;)[&lt;/span&gt;&lt;span class="si"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;90&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tool_calls&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;name&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When I ran it, the agent did this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;human: Is the payments deploy done?
   ai: check_deploy_status
 tool: payments: still rolling out. Check again.
   ai: check_deploy_status
 tool: payments: still rolling out. Check again.
   ai: check_deploy_status
 tool: payments: still rolling out. Check again.
   ai: check_deploy_status
 tool: Tool call limit exceeded. Do not call 'check_deploy_status' again.
   ai: Model call limits exceeded: run limit (4/4)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The tool limit fired first and told the model to stop calling that tool. The model tried once more anyway (models are optimists), and then the model limit ended the run cleanly.&lt;/p&gt;

&lt;p&gt;This helps any agent that pays per call. On-call agents that poll deploys are the classic case.&lt;/p&gt;

&lt;p&gt;Experienced engineers like it because the limit lives outside the prompt. "Please do not call this more than three times" is a request. Middleware is a rule.&lt;/p&gt;

&lt;p&gt;The trade-off: &lt;code&gt;exit_behavior="end"&lt;/code&gt; returns a polite message, not an error, so your code must check for it. If you would rather fail loudly, use &lt;code&gt;exit_behavior="error"&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Tool retry with backoff, without writing the loop yourself
&lt;/h2&gt;

&lt;p&gt;Internal services drop connections. If the agent gives up on the first error, the user gets "I could not reach the runbook" for a problem that would have fixed itself in 200 ms.&lt;/p&gt;

&lt;p&gt;People usually wrap every tool body in &lt;code&gt;try/except&lt;/code&gt; and a &lt;code&gt;time.sleep&lt;/code&gt;, and copy that block into every tool.&lt;br&gt;
&lt;/p&gt;

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

&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;langchain.agents&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;create_agent&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;langchain.agents.middleware&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;ToolRetryMiddleware&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;langchain.tools&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;tool&lt;/span&gt;

&lt;span class="n"&gt;attempts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;


&lt;span class="nd"&gt;@tool&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;search_runbook&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;Search the on-call runbook.&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
    &lt;span class="n"&gt;attempts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;monotonic&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;attempts&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;ConnectionError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;runbook service reset the connection&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Runbook: restart the consumer, then check lag on the orders topic.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;


&lt;span class="n"&gt;agent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;create_agent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;openai:gpt-4o-mini&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;tools&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;search_runbook&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="n"&gt;middleware&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="nc"&gt;ToolRetryMiddleware&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;max_retries&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;retry_on&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;ConnectionError&lt;/span&gt;&lt;span class="p"&gt;,),&lt;/span&gt;
            &lt;span class="n"&gt;initial_delay&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mf"&gt;0.2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;backoff_factor&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mf"&gt;2.0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;jitter&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;False&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;],&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;agent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;invoke&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;messages&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;user&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Orders consumer is stuck. What does the runbook say?&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)]})&lt;/span&gt;
&lt;span class="n"&gt;gaps&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;round&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;zip&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;attempts&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;attempts&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:])]&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;tool attempts:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;attempts&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;| gaps between attempts (s):&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;gaps&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;answer:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;messages&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is the run. The tool failed twice on purpose:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tool attempts: 3 | gaps between attempts (s): [0.2, 0.41]
answer: The runbook suggests the following steps:

1. Restart the orders consumer.
2. Check the lag on the orders topic.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The gaps double (0.2 s, then 0.41 s) because of &lt;code&gt;backoff_factor=2.0&lt;/code&gt;. The model never saw the two failures.&lt;/p&gt;

&lt;p&gt;Use it for anything that goes over a network, such as RAG retrieval or a ticketing API.&lt;/p&gt;

&lt;p&gt;Experienced engineers prefer this because retry rules sit in one place, and &lt;code&gt;retry_on&lt;/code&gt; limits retries to errors that are actually temporary.&lt;/p&gt;

&lt;p&gt;The trade-off: only retry tools that are safe to run twice. Retrying "search" is fine. Retrying "charge the card" without an idempotency key (a unique ID that makes a repeat request harmless) is how a customer pays three times. When retries run out, the default &lt;code&gt;on_failure="continue"&lt;/code&gt; hands the error text to the model; &lt;code&gt;"error"&lt;/code&gt; stops the run instead.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Model fallback, for the day your model is retired
&lt;/h2&gt;

&lt;p&gt;Models get retired, and your agent is not told. While writing this post I found that &lt;code&gt;gpt-4.1-nano&lt;/code&gt; now returns a 404 on my account. Any agent pinned to it is down.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;langchain.agents&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;create_agent&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;langchain.agents.middleware&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;ModelFallbackMiddleware&lt;/span&gt;

&lt;span class="c1"&gt;# gpt-4.1-nano was retired on this account: every call to it returns a 404.
&lt;/span&gt;&lt;span class="n"&gt;agent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;create_agent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;openai:gpt-4.1-nano&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;tools&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[],&lt;/span&gt;
    &lt;span class="n"&gt;middleware&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nc"&gt;ModelFallbackMiddleware&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;openai:gpt-4o-mini&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)],&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;agent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;invoke&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;messages&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;user&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;In five words: what is a dead-letter queue?&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)]})&lt;/span&gt;
&lt;span class="n"&gt;reply&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;messages&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;answered by:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;reply&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;response_metadata&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;model_name&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;answer:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;reply&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And the output from a real run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;answered by: gpt-4o-mini-2024-07-18
answer: Message handling for processing failures.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The primary failed. The middleware tried the next model in the list and the user still got an answer.&lt;/p&gt;

&lt;p&gt;This is useful for support bots that must answer, and for provider outages and rate limits.&lt;/p&gt;

&lt;p&gt;Experienced engineers like it because the fallback list is plain config. You can put a model from a second provider in it.&lt;/p&gt;

&lt;p&gt;The trade-off: I read the source. It falls back on any exception, not only on outages. A bug in your request will also be quietly "fixed" by the second model, so log which model answered (as the snippet does). The fallback model may also behave differently with your prompt, so test it on its own.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Human approval per tool call, with interrupt_mode="per_call"
&lt;/h2&gt;

&lt;p&gt;Some tools should never run without a person saying yes: restarts, scaling, refunds. The older human-in-the-loop setup paused once per model turn and expected one combined answer for all calls in that turn, matched by position.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;interrupt_mode="per_call"&lt;/code&gt; (new in langchain 1.4.6) gives each tool call its own pause and its own ID. An &lt;strong&gt;interrupt&lt;/strong&gt; is LangGraph's way of pausing a run and saving it, so it needs a &lt;strong&gt;checkpointer&lt;/strong&gt; (the component that saves the run's state).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;langchain.agents&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;create_agent&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;langchain.agents.middleware&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;HumanInTheLoopMiddleware&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;langchain.tools&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;tool&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;langgraph.checkpoint.memory&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;InMemorySaver&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;langgraph.types&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Command&lt;/span&gt;


&lt;span class="nd"&gt;@tool&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;restart_service&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;Restart a production service.&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; restarted&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;


&lt;span class="nd"&gt;@tool&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;scale_service&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;replicas&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;Change the replica count of a production service.&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; scaled to &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;replicas&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;


&lt;span class="n"&gt;agent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;create_agent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;openai:gpt-4o-mini&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;tools&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;restart_service&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;scale_service&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="n"&gt;middleware&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="nc"&gt;HumanInTheLoopMiddleware&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;interrupt_on&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;restart_service&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;scale_service&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;allowed_decisions&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;approve&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;reject&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]},&lt;/span&gt;
            &lt;span class="p"&gt;},&lt;/span&gt;
            &lt;span class="n"&gt;interrupt_mode&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;per_call&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="n"&gt;checkpointer&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nc"&gt;InMemorySaver&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;config&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;configurable&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;thread_id&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;incident-42&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;}}&lt;/span&gt;

&lt;span class="n"&gt;out&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;agent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;invoke&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;messages&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;user&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Restart checkout and scale search to 40 replicas. Do both in parallel.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)]},&lt;/span&gt;
    &lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;version&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;v2&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;answers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;intr&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;interrupts&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;intr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;PAUSED:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;type&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;name&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;args&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;name&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;scale_service&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;answers&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;intr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;id&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;type&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;reject&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;message&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;40 is too many. Max is 12.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;answers&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;intr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;id&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;type&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;approve&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;out&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;agent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;invoke&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Command&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;resume&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;answers&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;version&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;v2&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;messages&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;type&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;tool&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;TOOL RESULT:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;intr&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;interrupts&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;PAUSED AGAIN:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;intr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;name&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;intr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;args&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is what happened when I ran it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;PAUSED: tool_approval restart_service {'name': 'checkout'}
PAUSED: tool_approval scale_service {'name': 'search', 'replicas': 40}
TOOL RESULT: restart_service -&amp;gt; checkout restarted
TOOL RESULT: scale_service -&amp;gt; User rejected the tool call for `scale_service` with reason: 40 is too many. Max is 12.
PAUSED AGAIN: scale_service {'name': 'search', 'replicas': 12}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The approved restart ran. The rejected scale did not. The model read my reason and came back with 12 replicas, which paused again for approval. That second pause is the part I liked most.&lt;/p&gt;

&lt;p&gt;This is useful for any agent that can change production, from on-call bots with write access to refund flows.&lt;/p&gt;

&lt;p&gt;Experienced engineers prefer per-call answers because each answer is keyed by its interrupt ID and checked against a schema before it is saved. An answer cannot quietly land on the wrong call.&lt;/p&gt;

&lt;p&gt;The trade-off: the source docstring warns that this middleware must be listed before tool retry or error middleware, and that middleware listed before it runs again on resume. Also, people approve most prompts without reading them, so keep approvals for the few tools that matter.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Your own wrap_tool_call middleware
&lt;/h2&gt;

&lt;p&gt;Built-ins will not cover your rules. Say your SQL agent must never write.&lt;/p&gt;

&lt;p&gt;People usually put "never run DELETE" in the system prompt.&lt;br&gt;
&lt;/p&gt;

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

&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;langchain.agents&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;create_agent&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;langchain.agents.middleware&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;wrap_tool_call&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;langchain.messages&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;ToolMessage&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;langchain.tools&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;tool&lt;/span&gt;


&lt;span class="nd"&gt;@tool&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;run_sql&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;Run a SQL query against the orders database.&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;3 rows&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;


&lt;span class="nd"&gt;@wrap_tool_call&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;sql_guard&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;handler&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;query&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tool_call&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;args&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;query&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;""&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;upper&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nf"&gt;any&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;word&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;query&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;word&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;DROP&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;DELETE&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;TRUNCATE&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;UPDATE&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)):&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;BLOCKED: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;ToolMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Blocked: this agent has read-only access. Use SELECT only.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;tool_call_id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tool_call&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;id&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;start&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;perf_counter&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;handler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# run the real tool
&lt;/span&gt;    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;RAN: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; in &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;perf_counter&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="si"&gt;:&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; ms&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;


&lt;span class="n"&gt;agent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;create_agent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;openai:gpt-4o-mini&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;tools&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;run_sql&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;middleware&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;sql_guard&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;agent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;invoke&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;messages&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[(&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;user&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Delete all rows in the orders table where status=&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;test&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;. Use the exact SQL DELETE statement.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)]})&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;FINAL:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;messages&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I asked it to delete test orders. It printed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;BLOCKED: DELETE FROM ORDERS WHERE STATUS='TEST';
FINAL: I am unable to execute a DELETE statement as I have read-only access to the database. However, you can use the following SQL statement to delete the rows:

```sql
DELETE FROM orders WHERE status='test';
```

Please execute this statement in your database management interface.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The tool never ran. Then the model helpfully suggested I run the DELETE myself, which is a good reminder that the guard protects the tool, not the user.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;wrap_tool_call&lt;/code&gt; gets the request and a &lt;code&gt;handler&lt;/code&gt;. Call &lt;code&gt;handler(request)&lt;/code&gt; to run the tool, or return your own &lt;code&gt;ToolMessage&lt;/code&gt; to skip it. The same hook is a good place for audit logs and for redacting secrets.&lt;/p&gt;

&lt;p&gt;Experienced engineers like it because a code check runs every time, and a prompt instruction runs when the model feels like it.&lt;/p&gt;

&lt;p&gt;The trade-off: my word list is naive. It would also block &lt;code&gt;SELECT last_updated FROM orders&lt;/code&gt;, because "UPDATE" is inside the column name. The real fix is a read-only database user. Use the middleware as a second layer, not the only one.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Durability modes: exit, async, sync
&lt;/h2&gt;

&lt;p&gt;LangGraph saves a &lt;strong&gt;checkpoint&lt;/strong&gt; (a snapshot of the run's state) as the graph moves. The &lt;code&gt;durability&lt;/code&gt; argument decides when that write happens. To test it I killed the process inside the third step, the way an out-of-memory kill would, then tried to resume.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;operator&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;sqlite3&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;subprocess&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;typing&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Annotated&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;TypedDict&lt;/span&gt;

&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;langgraph.checkpoint.sqlite&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;SqliteSaver&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;langgraph.errors&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;EmptyInputError&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;langgraph.graph&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;END&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;START&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;StateGraph&lt;/span&gt;


&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;State&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;TypedDict&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;done&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Annotated&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;operator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;add&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;step&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;State&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;open_ticket&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;KILL&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;1&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;_exit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# the pod is OOM-killed: no exception, no cleanup
&lt;/span&gt;        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;done&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;]}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;run&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;build&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;StateGraph&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;State&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;fetch_logs&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;summarise&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;open_ticket&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt;
        &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add_node&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;step&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add_edge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;START&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;fetch_logs&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add_edge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;fetch_logs&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;summarise&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add_edge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;summarise&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;open_ticket&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add_edge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;open_ticket&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;END&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;compile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;checkpointer&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nc"&gt;SqliteSaver&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sqlite3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;check_same_thread&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;False&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;


&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;  &lt;span class="c1"&gt;# child process: run the graph, die at open_ticket
&lt;/span&gt;    &lt;span class="n"&gt;mode&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="nf"&gt;build&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;invoke&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;done&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[]},&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;configurable&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;thread_id&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;t1&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;}},&lt;/span&gt; &lt;span class="n"&gt;durability&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;mode&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;exit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;mode&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;exit&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;async&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;sync&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt;
    &lt;span class="n"&gt;db&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;/tmp/durability-&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;mode&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;.db&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;exists&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;remove&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;subprocess&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;executable&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;__file__&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mode&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;KILL&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;1&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;
    &lt;span class="n"&gt;graph&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;build&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;config&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;configurable&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;thread_id&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;t1&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;}}&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;mode&lt;/span&gt;&lt;span class="si"&gt;:&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; after kill: saved=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;graph&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get_state&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;values&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;done&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;KILL&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;0&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;ran&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;u&lt;/span&gt;&lt;span class="p"&gt;)[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;u&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;graph&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stream&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;stream_mode&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;updates&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;mode&lt;/span&gt;&lt;span class="si"&gt;:&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; resume re-ran: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;ran&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;  final=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;graph&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get_state&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;values&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;done&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="n"&gt;EmptyInputError&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;exc&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;mode&lt;/span&gt;&lt;span class="si"&gt;:&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; after resume: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nf"&gt;type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;exc&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;__name__&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;exc&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;KILL&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;1&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output, copied from the terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; exit after kill: saved=None
 exit after resume: EmptyInputError: Received no input for __start__
async after kill: saved=[]
async resume re-ran: ['fetch_logs', 'summarise', 'open_ticket']  final=['fetch_logs', 'summarise', 'open_ticket']
 sync after kill: saved=['fetch_logs', 'summarise']
 sync resume re-ran: ['open_ticket']  final=['fetch_logs', 'summarise', 'open_ticket']
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;exit&lt;/code&gt; saves only when the run ends. A hard kill means it never ended. Nothing was saved, and the thread looked as if it had never run. The docs warn about exactly this.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;async&lt;/code&gt; writes in the background while the next step runs. My steps take microseconds, so the writes had not landed when the process died. All three steps ran again. With slower steps the gap is smaller, but it is not zero.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;sync&lt;/code&gt; writes before every next step. Only the step that crashed ran again.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A note on my first attempt: I raised a Python exception instead of killing the process. All three modes kept their progress, because an exception still lets the run end in an orderly way. Test durability with a real kill.&lt;/p&gt;

&lt;p&gt;Use &lt;code&gt;sync&lt;/code&gt; for steps with side effects (opening tickets, sending emails). Use &lt;code&gt;exit&lt;/code&gt; for short, cheap runs you can simply start again.&lt;/p&gt;

&lt;p&gt;Experienced engineers prefer to choose this per call instead of accepting the default without knowing it.&lt;/p&gt;

&lt;p&gt;The trade-off: &lt;code&gt;sync&lt;/code&gt; is the slowest, because every step waits for a database write.&lt;/p&gt;

&lt;h2&gt;
  
  
  7. Node caching with CachePolicy
&lt;/h2&gt;

&lt;p&gt;RAG pipelines run the same retrieval for the same question all day. Each run is an embedding call plus a vector search.&lt;/p&gt;

&lt;p&gt;People usually add &lt;code&gt;functools.lru_cache&lt;/code&gt; to the retrieval function, which lives in one process and knows nothing about the graph.&lt;br&gt;
&lt;/p&gt;

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

&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;langgraph.cache.memory&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;InMemoryCache&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;langgraph.graph&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;END&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;START&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;StateGraph&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;langgraph.types&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;CachePolicy&lt;/span&gt;


&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;State&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;TypedDict&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;question&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;
    &lt;span class="n"&gt;docs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;retrieve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;State&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;1.5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# pretend: embedding call + vector search
&lt;/span&gt;    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;docs&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;doc about &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;question&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]}&lt;/span&gt;


&lt;span class="n"&gt;builder&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;StateGraph&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;State&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add_node&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;retrieve&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;retrieve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;cache_policy&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nc"&gt;CachePolicy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ttl&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;300&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;key_func&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;lambda&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;question&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;strip&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;lower&lt;/span&gt;&lt;span class="p"&gt;()),&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add_edge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;START&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;retrieve&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add_edge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;retrieve&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;END&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;graph&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;compile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cache&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nc"&gt;InMemoryCache&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;q&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;What is our refund policy?&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;  what is our REFUND policy?&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;How do I reset 2FA?&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt;
    &lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;perf_counter&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;chunks&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;graph&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stream&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;question&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="n"&gt;stream_mode&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;updates&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;perf_counter&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="si"&gt;:&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;s  &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;chunks&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I asked three questions and timed each one:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1.51s  {'retrieve': {'docs': ['doc about What is our refund policy?']}}
0.00s  {'retrieve': {'docs': ['doc about What is our refund policy?']}, '__metadata__': {'cached': True}}
1.51s  {'retrieve': {'docs': ['doc about How do I reset 2FA?']}}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The second question differs only in spaces and case, so &lt;code&gt;key_func&lt;/code&gt; mapped it to the same key: 0.00 s instead of 1.51 s, marked &lt;code&gt;'cached': True&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Use it for retrieval or any other expensive step that is pure (same input, same output).&lt;/p&gt;

&lt;p&gt;Experienced engineers like that the cache is per node with its own TTL (time to live, how long an entry stays valid).&lt;/p&gt;

&lt;p&gt;The trade-off is visible in the output. The cached result still says "What is our refund policy?" with the first question's exact text. If your &lt;code&gt;key_func&lt;/code&gt; throws away something that matters, you serve the wrong answer very quickly. &lt;code&gt;InMemoryCache&lt;/code&gt; also disappears on restart; &lt;code&gt;SqliteCache&lt;/code&gt; exists for that.&lt;/p&gt;

&lt;h2&gt;
  
  
  8. Per-node timeouts and an error_handler that returns a Command
&lt;/h2&gt;

&lt;p&gt;A CRM API that hangs will hang your agent with it. LangGraph 1.2 added &lt;code&gt;timeout=&lt;/code&gt; per node and &lt;code&gt;error_handler=&lt;/code&gt;, a function that runs after a node has failed all its retries. It can return a &lt;strong&gt;Command&lt;/strong&gt;, an object that updates state and says which node to go to next. &lt;code&gt;set_node_defaults&lt;/code&gt; sets all of this once for the whole graph.&lt;br&gt;
&lt;/p&gt;

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

&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;langgraph.errors&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;NodeError&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;langgraph.graph&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;END&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;START&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;StateGraph&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;langgraph.types&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Command&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;RetryPolicy&lt;/span&gt;

&lt;span class="n"&gt;calls&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;lookup_customer&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;


&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;State&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;TypedDict&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;ticket&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;
    &lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;


&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;lookup_customer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;State&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;calls&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;lookup_customer&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;asyncio&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# the CRM API hangs
&lt;/span&gt;    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;status&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;found customer&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;


&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;draft_reply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;State&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;status&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;status&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt; -&amp;gt; reply drafted&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;


&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;escalate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;State&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;status&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;status&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt; -&amp;gt; sent to a human&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;


&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;on_failure&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;State&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;NodeError&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Command&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;Command&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;update&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;status&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; failed (&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nf"&gt;type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;__name__&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;)&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="n"&gt;goto&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;escalate&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="n"&gt;graph&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nc"&gt;StateGraph&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;State&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set_node_defaults&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;timeout&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mf"&gt;1.0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;retry_policy&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nc"&gt;RetryPolicy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;max_attempts&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;initial_interval&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mf"&gt;0.1&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="n"&gt;error_handler&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;on_failure&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add_node&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;lookup_customer&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;lookup_customer&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add_node&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;draft_reply&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;draft_reply&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add_node&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;escalate&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;escalate&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add_edge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;START&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;lookup_customer&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add_edge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;lookup_customer&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;draft_reply&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add_edge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;draft_reply&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;END&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add_edge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;escalate&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;END&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;compile&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;perf_counter&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;asyncio&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;graph&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ainvoke&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;ticket&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;T-1093&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;status&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;""&lt;/span&gt;&lt;span class="p"&gt;}))&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;took &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;perf_counter&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="si"&gt;:&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;s, CRM attempts: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;calls&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;lookup_customer&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;status:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;status&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The run took a few seconds:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;took 3.0s, CRM attempts: 2
status: lookup_customer failed (NodeTimeoutError) -&amp;gt; sent to a human
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two attempts, each cut at one second, then the handler moved the ticket to a human instead of crashing.&lt;/p&gt;

&lt;p&gt;My first version failed at compile time:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ValueError: Node timeouts are only supported for async nodes because sync Python execution cannot be safely cancelled in-process. Node '__default_error_handler__' is sync.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The docs do say that &lt;code&gt;timeout&lt;/code&gt; defaults apply to error-handler nodes too, and timeouts only work on async code. So when you set a default timeout, your handler must be &lt;code&gt;async def&lt;/code&gt; as well. The error message names the node, which helps.&lt;/p&gt;

&lt;p&gt;This is useful for support bots that call slow vendor APIs, and for any graph that should degrade instead of crash.&lt;/p&gt;

&lt;p&gt;Experienced engineers prefer it because the recovery path is a real node in the graph, visible and testable.&lt;/p&gt;

&lt;p&gt;The trade-off: timeouts need async nodes, and a node that blocks with &lt;code&gt;time.sleep&lt;/code&gt; will not be cut on time. Defaults are also not inherited by subgraphs.&lt;/p&gt;

&lt;h2&gt;
  
  
  9. stream(version="v2") and GraphOutput.interrupts
&lt;/h2&gt;

&lt;p&gt;With several stream modes at once, the old stream gave you tuples whose shape depended on the options. And &lt;code&gt;invoke&lt;/code&gt; mixed the pause information into your state under a &lt;code&gt;__interrupt__&lt;/code&gt; key.&lt;br&gt;
&lt;/p&gt;

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

&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;langgraph.checkpoint.memory&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;InMemorySaver&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;langgraph.config&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;get_stream_writer&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;langgraph.graph&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;END&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;START&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;StateGraph&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;langgraph.types&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;interrupt&lt;/span&gt;


&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;State&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;TypedDict&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;refund&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;
    &lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;check_order&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;State&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="nf"&gt;get_stream_writer&lt;/span&gt;&lt;span class="p"&gt;()({&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;progress&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;order found, paid by card&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;status&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;checked&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;approve_refund&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;State&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;answer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;interrupt&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;question&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Approve refund of &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;refund&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; INR?&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;status&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;refund &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;answer&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;


&lt;span class="n"&gt;builder&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;StateGraph&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;State&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add_node&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;check_order&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;check_order&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add_node&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;approve_refund&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;approve_refund&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add_edge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;START&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;check_order&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add_edge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;check_order&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;approve_refund&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add_edge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;approve_refund&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;END&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;graph&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;compile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;checkpointer&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nc"&gt;InMemorySaver&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;--- stream, version v2: every part has the same shape&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;cfg&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;configurable&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;thread_id&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;a&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;}}&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;part&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;graph&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stream&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;refund&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1200&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;status&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;""&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="n"&gt;cfg&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                         &lt;span class="n"&gt;stream_mode&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;updates&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;custom&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;version&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;v2&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;part&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;type&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;|&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;part&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;data&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;--- invoke, default&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;old&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;graph&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;invoke&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;refund&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1200&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;status&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;""&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;configurable&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;thread_id&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;b&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;}})&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;old&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;--- invoke, version v2&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;new&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;graph&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;invoke&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;refund&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1200&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;status&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;""&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;configurable&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;thread_id&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;c&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;}},&lt;/span&gt; &lt;span class="n"&gt;version&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;v2&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;value:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;interrupts:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;interrupts&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The printed result shows both shapes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;--- stream, version v2: every part has the same shape
custom | {'progress': 'order found, paid by card'}
updates | {'check_order': {'status': 'checked'}}
updates | {'__interrupt__': (Interrupt(value={'question': 'Approve refund of 1200 INR?'}, id='f2226891b56fbadf838dbeb04bc84ee5', response_schema=None),)}
--- invoke, default
{'refund': 1200, 'status': 'checked', '__interrupt__': [Interrupt(value={'question': 'Approve refund of 1200 INR?'}, id='19c1d978c90c7be7cf56b97568b45f4b', response_schema=None)]}
--- invoke, version v2
value: {'refund': 1200, 'status': 'checked'}
interrupts: [{'question': 'Approve refund of 1200 INR?'}]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With &lt;code&gt;version="v2"&lt;/code&gt;, every stream part is a dict with &lt;code&gt;type&lt;/code&gt;, &lt;code&gt;ns&lt;/code&gt; and &lt;code&gt;data&lt;/code&gt;, and &lt;code&gt;invoke&lt;/code&gt; returns a &lt;code&gt;GraphOutput&lt;/code&gt; with &lt;code&gt;.value&lt;/code&gt; (your state) and &lt;code&gt;.interrupts&lt;/code&gt; (the pauses) kept apart.&lt;/p&gt;

&lt;p&gt;Use it in chat UIs that show progress, and in any code that checks "did the run pause for approval?"&lt;/p&gt;

&lt;p&gt;Experienced engineers like it because one shape means one parser, and the state no longer carries a key you never put there.&lt;/p&gt;

&lt;p&gt;The trade-off: it is opt-in. Without &lt;code&gt;version="v2"&lt;/code&gt;, &lt;code&gt;invoke&lt;/code&gt; still returns the old shape, so a codebase can easily end up with both.&lt;/p&gt;

&lt;h2&gt;
  
  
  10. Store for long-term memory, not the checkpointer
&lt;/h2&gt;

&lt;p&gt;A user tells the bot their language and timezone on Monday. On Friday, in a new chat, the bot has forgotten.&lt;/p&gt;

&lt;p&gt;People usually try to keep one thread alive forever. The checkpointer is short-term memory for one thread. The &lt;strong&gt;Store&lt;/strong&gt; is long-term memory shared across threads, organised by namespace.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;operator&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;dataclasses&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;dataclass&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;typing&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Annotated&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;TypedDict&lt;/span&gt;

&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;langgraph.checkpoint.memory&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;InMemorySaver&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;langgraph.graph&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;END&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;START&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;StateGraph&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;langgraph.runtime&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Runtime&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;langgraph.store.memory&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;InMemoryStore&lt;/span&gt;


&lt;span class="nd"&gt;@dataclass&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Context&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;user_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;


&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;State&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;TypedDict&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;messages&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Annotated&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;operator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;add&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;known_prefs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;dict&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;chat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;State&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;runtime&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Runtime&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;Context&lt;/span&gt;&lt;span class="p"&gt;]):&lt;/span&gt;
    &lt;span class="n"&gt;ns&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;users&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;runtime&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;user_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;last&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;messages&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;last&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;startswith&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;remember:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;last&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;removeprefix&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;remember:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;strip&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;runtime&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;store&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;put&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ns&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;value&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;
    &lt;span class="n"&gt;prefs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;value&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;runtime&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;store&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;search&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ns&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;known_prefs&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;prefs&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;


&lt;span class="n"&gt;builder&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;StateGraph&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;State&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;context_schema&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;Context&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add_node&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;chat&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;chat&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add_edge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;START&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;chat&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add_edge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;chat&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;END&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;graph&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;compile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;checkpointer&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nc"&gt;InMemorySaver&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;store&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nc"&gt;InMemoryStore&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;

&lt;span class="n"&gt;ctx&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Context&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user_id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;priya&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;monday&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;configurable&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;thread_id&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;monday-chat&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;}}&lt;/span&gt;
&lt;span class="n"&gt;friday&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;configurable&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;thread_id&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;friday-chat&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;}}&lt;/span&gt;

&lt;span class="n"&gt;graph&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;invoke&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;messages&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;remember: language=Python&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]},&lt;/span&gt; &lt;span class="n"&gt;monday&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;graph&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;invoke&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;messages&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;remember: timezone=IST&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]},&lt;/span&gt; &lt;span class="n"&gt;monday&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;out&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;graph&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;invoke&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;messages&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;hi again&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]},&lt;/span&gt; &lt;span class="n"&gt;friday&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Friday thread messages (checkpointer):&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;messages&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Friday thread prefs (store):         &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;known_prefs&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Monday thread messages:              &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;graph&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get_state&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;monday&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;values&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;messages&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="n"&gt;other&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;graph&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;invoke&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;messages&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;hello&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]},&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;configurable&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;thread_id&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;x&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;}},&lt;/span&gt;
                     &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nc"&gt;Context&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user_id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;arjun&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Another user&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;s prefs:                &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;known_prefs&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is what each thread could see:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Friday thread messages (checkpointer): ['hi again']
Friday thread prefs (store):          {'language': 'Python', 'timezone': 'IST'}
Monday thread messages:               ['remember: language=Python', 'remember: timezone=IST']
Another user's prefs:                 {}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Friday's thread has only its own message, but the preferences from Monday are there. Another user sees nothing, because the namespace includes the user ID.&lt;/p&gt;

&lt;p&gt;This is useful for support bots that remember a customer, and assistants that learn preferences.&lt;/p&gt;

&lt;p&gt;Experienced engineers keep the two apart because thread history grows without limit, while the Store holds only what you chose to keep.&lt;/p&gt;

&lt;p&gt;The trade-off: the Store never forgets on its own. You decide what goes in, how to delete it when a user asks, and where it lives. &lt;code&gt;InMemoryStore&lt;/code&gt; is gone on restart; use a database-backed store in production.&lt;/p&gt;

&lt;h2&gt;
  
  
  One I did not include
&lt;/h2&gt;

&lt;p&gt;langchain 1.4 has built-in MCP support (&lt;code&gt;langchain.mcp&lt;/code&gt;, still beta) for loading tools from MCP servers. It needs a running server, so it did not fit the "every snippet runs on its own" rule here. That one can wait for a follow-up post.&lt;/p&gt;

&lt;h2&gt;
  
  
  Which one would you add?
&lt;/h2&gt;

&lt;p&gt;That is my ten. Most of them are a few lines, and most of them are the difference between a demo and something you can leave running overnight.&lt;/p&gt;

&lt;p&gt;Which feature would you add to this list? Tell me in the comments.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Versions used: langchain 1.4.9, langchain-core 1.7.6, langgraph 1.2.20, langchain-openai 1.7.4, Python 3.13.15. I write about building and running AI systems at systemdesign.academy.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>ai</category>
      <category>langchain</category>
      <category>langgraph</category>
    </item>
    <item>
      <title>OpenAI dots: how an always-on AI agent keeps working after you close the chat</title>
      <dc:creator>RONI DAS</dc:creator>
      <pubDate>Sun, 04 Oct 2026 10:48:09 +0000</pubDate>
      <link>https://dev.to/roni_das_b1b76c5ee6583027/openai-dots-how-an-always-on-ai-agent-keeps-working-after-you-close-the-chat-5gh5</link>
      <guid>https://dev.to/roni_das_b1b76c5ee6583027/openai-dots-how-an-always-on-ai-agent-keeps-working-after-you-close-the-chat-5gh5</guid>
      <description>&lt;p&gt;On 29 September 2026, at DevDay, OpenAI launched dots. A dot is a ChatGPT agent with its own cloud computer, and it keeps working after you close the chat. I spent today reading everything OpenAI published about them, and then I asked the question I always ask as an engineer: how would I build something like this myself?&lt;/p&gt;

&lt;p&gt;One thing first. I could not use a dot. They are a hosted feature on paid plans with no local version, so there was nothing for me to run or measure. Every statement about dots below comes from OpenAI's launch post, its safety post and two help pages, read on 4 October 2026. The design part is general design, not OpenAI's internals, which it has not published.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Frokxlkzi3714b74n23o0.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Frokxlkzi3714b74n23o0.webp" alt="A chat waits. A dot keeps working. Two lanes: you ask, you leave, it waits. You give it a goal, you leave, it keeps working." width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What a dot is
&lt;/h2&gt;

&lt;p&gt;An agent is an AI model that does more than answer. It takes actions one after another, such as opening a web page, editing a file or drafting a message, to reach a goal you gave it.&lt;/p&gt;

&lt;p&gt;In a normal chat, the model answers and then waits for your next message. A dot does not wait. OpenAI calls dots "always-on agents", says they run on GPT-6 Astra, and says they can connect to over 4,000 apps through plugins. You can talk to yours in ChatGPT on desktop, web and mobile, and in Slack and Microsoft Teams.&lt;/p&gt;

&lt;p&gt;A dot can start work without a new message from you in two ways:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It runs reminders and recurring tasks you scheduled.&lt;/li&gt;
&lt;li&gt;It does what OpenAI calls "proactive research": it reads the apps you connected, looks for ways to help and saves private notes.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The second one is the interesting part. OpenAI says those research tasks use read-only tools, enforced in code, that "cannot directly send messages to other people, change content in connected apps, or control a browser or desktop". Anything the dot then wants to do goes through the normal approval rules.&lt;/p&gt;

&lt;h2&gt;
  
  
  Who can use one
&lt;/h2&gt;

&lt;p&gt;At launch, OpenAI's help page lists Pro users outside the European Economic Area, Switzerland and the UK, Business Premium in all supported regions, and Enterprise, Edu and Healthcare as a beta that stays off until an admin turns it on. Your first dot is included on Pro and Business Premium.&lt;/p&gt;

&lt;h2&gt;
  
  
  What OpenAI says keeps it in check
&lt;/h2&gt;

&lt;p&gt;An agent that can act on your accounts can also act wrongly on them. OpenAI's own safety post says a misunderstood request "could lead it to change the wrong file or share information you intended to keep private". Here is what it says it built around that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;A separate computer.&lt;/strong&gt; Each dot works on its own cloud computer. Access to your laptop starts turned off.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Passwords kept away from the model.&lt;/strong&gt; For supported sign-ins the model is paused while you type into a secure form, and the password goes straight to the dot's browser.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Your rules per kind of action.&lt;/strong&gt; Take action without asking, take action if pre-approved, ask first, or hand off to you.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;You do the most sensitive steps.&lt;/strong&gt; Changing a password or moving money always needs you to take over.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A check before actions run.&lt;/strong&gt; A separate checker, which OpenAI calls auto-review, looks at some planned actions first, for example who an email is going to and what it says.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Two things deserve a careful read. First, memory: according to OpenAI's privacy FAQ, you cannot currently view or delete one dot memory at a time, and disconnecting an app does not delete what the dot already learned from it. Deleting the whole dot is the way to delete its context.&lt;/p&gt;

&lt;p&gt;Second, prompt injection. That means hidden instructions inside a web page, email or document that try to trick the agent into doing something you never asked for. OpenAI does not claim to have solved it. In its own words: "These protections help reduce the risk of malicious instructions causing an unwanted action, but they do not eliminate it."&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F1v3t23s2dtbtdlfl7um4.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F1v3t23s2dtbtdlfl7um4.webp" alt="Where the trust boundary goes: untrusted web pages and emails feed a model inside a sandbox, while the credential vault, the action checker and the token broker sit outside it in a trusted control plane." width="800" height="834"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How I would build an always-on agent
&lt;/h2&gt;

&lt;p&gt;Here is the general design I would use for any agent that keeps working after the user leaves. Again, this is not OpenAI's system.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Make the task durable.&lt;/strong&gt; A web request lives for seconds. An agent task can run for days. So I store the task as a workflow: a list of steps, with each step's result saved before the next one starts. This is called durable execution, and engines such as Temporal are built for it. Closing the chat stops nothing, because the task never lived in the chat.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Save a checkpoint after every step.&lt;/strong&gt; A checkpoint is the saved result of a finished step. After a crash, a new machine reads the last checkpoint and carries on, so a crash costs one step, not the whole job. Steps that change the outside world, like sending an email, must be idempotent: running them twice has the same effect as running them once. An idempotency key, a unique ID sent with the action, lets the other side ignore a repeat.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Give it its own sandbox.&lt;/strong&gt; A sandbox is a virtual machine or container walled off from other users and from your own systems. I run the parts that enforce the rules somewhere else, so a confused or tricked agent cannot switch them off.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Connect apps with narrow, short-lived tokens.&lt;/strong&gt; A token is a temporary key limited to scopes such as "read my calendar". I keep tokens outside the sandbox in a small service that hands out only what one step needs. Background jobs get read-only scopes, the same idea OpenAI describes for proactive research.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. Put a gate in front of every risky action.&lt;/strong&gt; When an action needs a yes, the workflow saves its state and sleeps until the person answers. A sleeping workflow costs almost nothing, so waiting a whole day for an approval is affordable.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;6. Put a budget on everything.&lt;/strong&gt; Always-on does not mean the model is always running. Most of the time the agent is waiting for a trigger: a message, a schedule or an app event. I cap each task's steps, tokens and time, and stop a task that keeps getting its actions refused.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fs231dqrbczur2512gsqw.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fs231dqrbczur2512gsqw.webp" alt="A general always-on agent: triggers start or resume a workflow engine, which runs one step in a sandbox, sends the proposed action through an approval gate, and saves every result to a checkpoint store." width="800" height="1306"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What I took away
&lt;/h2&gt;

&lt;p&gt;The model is the least surprising part of dots. The real change is where the work lives. In a chat, the work lives in your browser tab, so it ends when you leave. In an always-on agent, the work lives in a workflow engine, one checkpoint at a time, and the chat is just one of several ways to talk to it.&lt;/p&gt;

&lt;p&gt;That also tells you where the risk is. Everything the model reads is untrusted, so anything that must not be fooled, such as passwords, rules and tokens, has to live outside the box the model runs in. OpenAI's design points the same way, and it is honest that this reduces the risk rather than removing it.&lt;/p&gt;

&lt;p&gt;I wrote a longer version with the full lifecycle diagram, the plan table, a FAQ and links to every OpenAI page I used: &lt;a href="https://www.systemdesign.academy/explained/openai-dots" rel="noopener noreferrer"&gt;OpenAI dots, explained&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>llm</category>
      <category>openai</category>
      <category>systemdesign</category>
    </item>
    <item>
      <title>Searching Inventory You Don't Own: How MakeMyTrip-Style Travel Search Works</title>
      <dc:creator>RONI DAS</dc:creator>
      <pubDate>Fri, 25 Sep 2026 08:32:18 +0000</pubDate>
      <link>https://dev.to/roni_das_b1b76c5ee6583027/searching-inventory-you-dont-own-how-makemytrip-style-travel-search-works-1018</link>
      <guid>https://dev.to/roni_das_b1b76c5ee6583027/searching-inventory-you-dont-own-how-makemytrip-style-travel-search-works-1018</guid>
      <description>&lt;p&gt;MakeMyTrip sold $10.39 billion of travel in the year to March 2026, according to its SEC filing. That was $5.83 billion of flights, and $2.66 billion of hotels and holiday packages. Almost none of it is its own. The seats belong to airlines. The rooms belong to hotels and suppliers. Each of them has its own systems and its own speed. Their prices change by the minute.&lt;/p&gt;

&lt;p&gt;Yet you expect a list of flights in about a second. And you expect the price you clicked to be the price you pay. Those two wishes pull in opposite directions. The way an online travel agency handles that is one of the most useful patterns in system design. It works for any site that sells what it does not own.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why you cannot just ask everyone
&lt;/h2&gt;

&lt;p&gt;Here is the obvious design. When someone searches, ask every airline and hotel supplier for live prices. Merge the answers and show them. It is correct, and it does not work.&lt;/p&gt;

&lt;p&gt;Supplier calls are slow. Many have rate limits, and some cost money per call. Travellers search, compare and come back several times before they book. So searches outnumber bookings by ten to a hundred times. That is my estimate, but anyone who has planned a trip knows it is true. Ask live for every search, and the system is slow and costly. It is also only as fast as the slowest supplier.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cache the read, check again at the write
&lt;/h2&gt;

&lt;p&gt;The answer is to split the system into two paths. Each path is allowed to be wrong in a different way.&lt;/p&gt;

&lt;p&gt;The search path is about speed and cost. It answers from a cache of recent prices and free seats or rooms. The cache key is the shape of the search. For flights, it is route and date. For hotels, it is city, dates and guests. MakeMyTrip's engineers have written about this. With caching, they answer repeated searches from different users in a few milliseconds. On a cache miss, the system asks the suppliers and saves the answer for next time. Popular routes and cities are refreshed in the background, before anyone asks. So the searches most people make are fast and fairly fresh.&lt;/p&gt;

&lt;p&gt;The booking path is about being right. A traveller picks an option. The booking service asks that one supplier for the live price and whether it is still free. This is called a re-price. If the price is the same, booking goes on. If it went up, the traveller sees the new price before paying. If the option is gone, they go back to the results.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F83p99jeflm7cjwdt25om.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F83p99jeflm7cjwdt25om.webp" alt="An isometric diagram. The MakeMyTrip app searches saved results kept by route and date. Only on a miss do saved results ask airlines and hotels. Booking always asks airlines and hotels for the live price." width="800" height="603"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is the whole trick. All the checking happens in one live call per booking. It is not spread across millions of searches. The cache is allowed to be wrong only because the re-price step exists.&lt;/p&gt;

&lt;h2&gt;
  
  
  Do not wait for the slowest supplier
&lt;/h2&gt;

&lt;p&gt;Sometimes a search does have to ask suppliers live. Then it asks them all at the same time, each with its own time limit. When the search runs out of time, it returns what it has. Late answers are saved in the cache for the next search. Say a supplier keeps timing out. It trips a circuit breaker and is skipped for a while. So its bad day does not become everyone's bad day.&lt;/p&gt;

&lt;p&gt;The trade is completeness for speed. Now and then, one option from a slow supplier is missing. That is far better than every traveller waiting for it.&lt;/p&gt;

&lt;h2&gt;
  
  
  "No rooms available" can be a data bug
&lt;/h2&gt;

&lt;p&gt;This is my favourite detail. It comes from MakeMyTrip's own engineering writing. They found that many city searches showed few or zero hotels on the listing pages. The reasons were not only real shortages in peak season. Some of it was missing rates in their own stock system, for the number of guests asked for. For example, there was no rate loaded for three adults in a room that could hold them.&lt;/p&gt;

&lt;p&gt;I would bring that lesson to any search system. Find out where "no results" comes from before you assume the world ran out. Part of the fix is better data from suppliers. Part is smarter room choice. Say no single room has a rate for that many guests. Then offer two rooms that fit the group together.&lt;/p&gt;

&lt;h2&gt;
  
  
  Personal results in 44 milliseconds
&lt;/h2&gt;

&lt;p&gt;The last piece sits beside search. It shows you the hotels you looked at a minute ago, while you are still browsing. MakeMyTrip described this in an April 2026 case study with Databricks. The pipeline used to run in small batches, with one to two seconds of delay. The slowest 1% took over a minute.&lt;/p&gt;

&lt;p&gt;They moved it to Spark Real-Time Mode, reading clicks from Kafka. They kept each user's recently viewed hotels in Aerospike, and served them from Redis. The typical delay dropped from about 1.23 seconds to 44 milliseconds. The slowest 1% dropped to about 500 milliseconds. Clicks rose 7%.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F4rhtch5rlg0x4kfzm4t7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F4rhtch5rlg0x4kfzm4t7.png" alt="A hand-drawn pipeline. App clicks flow into Kafka, then Spark in real-time mode. Aerospike keeps each user's last viewed hotels. Redis serves them to the app. A note says the delay fell from 1.23 seconds to 44 ms and clicks rose 7%." width="800" height="1140"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The design lesson: this personal store is separate from the search path. It is small per user, always updating, and read with one key lookup.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I would say in an interview
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Split a fast, cached search path from an exact booking path.&lt;/li&gt;
&lt;li&gt;Key the cache by the shape of the search. Refresh popular searches ahead of time. Give fast-changing stock shorter cache times.&lt;/li&gt;
&lt;li&gt;Ask suppliers at the same time, with time limits and circuit breakers. Return what arrives in time.&lt;/li&gt;
&lt;li&gt;Re-price with the one chosen supplier before taking money, and show any change.&lt;/li&gt;
&lt;li&gt;Use a booking ID as the idempotency key for the supplier and payment calls.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;MakeMyTrip has not published the full design of its flight and hotel search. So parts of this are a standard design for this kind of site, built to fit its published facts. The full version, with the data model, trade-offs and every source, is in the &lt;a href="https://www.systemdesign.academy/interview/design-makemytrip" rel="noopener noreferrer"&gt;MakeMyTrip system design walkthrough&lt;/a&gt;. The same problem of selling stock you do not own shows up at &lt;a href="https://www.systemdesign.academy/interview/design-airbnb" rel="noopener noreferrer"&gt;Airbnb&lt;/a&gt;. And redBus, which MakeMyTrip also owns, adds the seat-holding side of it in the &lt;a href="https://www.systemdesign.academy/interview/design-redbus" rel="noopener noreferrer"&gt;redBus design&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>systemdesign</category>
      <category>caching</category>
      <category>architecture</category>
      <category>microservices</category>
    </item>
    <item>
      <title>Two People Tap Seat 14 at Once: Designing redBus Seat Locks</title>
      <dc:creator>RONI DAS</dc:creator>
      <pubDate>Fri, 25 Sep 2026 08:32:14 +0000</pubDate>
      <link>https://dev.to/roni_das_b1b76c5ee6583027/two-people-tap-seat-14-at-once-designing-redbus-seat-locks-34pi</link>
      <guid>https://dev.to/roni_das_b1b76c5ee6583027/two-people-tap-seat-14-at-once-designing-redbus-seat-locks-34pi</guid>
      <description>&lt;p&gt;Two people tap seat 14 on the same bus at the same second. What happens?&lt;/p&gt;

&lt;p&gt;That one question decides most seat-booking interviews. It does not matter if the product is a bus, a train, a cinema or a concert. I want to walk through it using redBus. redBus makes the question harder in a way most booking systems do not: the seats are not its own.&lt;/p&gt;

&lt;h2&gt;
  
  
  The numbers
&lt;/h2&gt;

&lt;p&gt;MakeMyTrip owns redBus. In its SEC filing, it reported $501.9 million of bus ticket bookings for April to June 2026. The number of tickets was up 23.9% on the same quarter a year earlier. A March 2026 case study by an observability company that works with redBus gives another number. It says redBus sells more than 100 million tickets a year, across India, Southeast Asia and Latin America.&lt;/p&gt;

&lt;p&gt;100 million tickets a year is only about 3 bookings a second on average. That sounds easy, and that is exactly why this problem fools people. The hard part is not volume. A typical bus has somewhere around 30 to 50 seats. Take a popular Friday night bus before a festival. Hundreds of people are fighting over a handful of rows in a database.&lt;/p&gt;

&lt;h2&gt;
  
  
  The race
&lt;/h2&gt;

&lt;p&gt;Here is the simple version. The booking service reads "seat 14 is free". Then it writes "seat 14 is held". Two customers can both read "free" before either one writes. Both think they have the seat.&lt;/p&gt;

&lt;p&gt;The fix is to make the check and the change one step:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;seats&lt;/span&gt;
   &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="k"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'held'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;hold_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="n"&gt;hold_expires_at&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;now&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;interval&lt;/span&gt; &lt;span class="s1"&gt;'8 minutes'&lt;/span&gt;
 &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;trip_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;seat_no&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;
   &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'available'&lt;/span&gt;
        &lt;span class="k"&gt;OR&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'held'&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;hold_expires_at&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;now&lt;/span&gt;&lt;span class="p"&gt;()));&lt;/span&gt;
&lt;span class="c1"&gt;-- 0 rows updated: someone else has the seat&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I ran this on PostgreSQL, with two sessions racing for the same seat. The first one holds it. The second waits for the first to finish. Then the database checks the condition again, on the updated row. It finds the seat already held, and changes nothing. The app can tell the second customer right away: that seat was just taken, pick another.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F2w5275l2v36apukobyhk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F2w5275l2v36apukobyhk.png" alt="A hand-drawn sketch. Asha and Bala both tap seat 14. The seat service sends one update to a PostgreSQL seats table that holds the seat only if it is free. Asha's update changes one row, so Asha gets the seat. Bala's changes zero rows, so Bala is asked to pick another seat." width="800" height="883"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Split the seat data by trip. Then this fight only ever involves one bus's 30 to 50 rows. It stays fast, even when that bus is the most wanted trip of the weekend.&lt;/p&gt;

&lt;h2&gt;
  
  
  A hold, not a lock
&lt;/h2&gt;

&lt;p&gt;Notice what the update does not do. It does not take a database lock and keep it while the customer pays. A payment with a bank OTP in the middle can take minutes. Keeping a real lock that long would be a disaster.&lt;/p&gt;

&lt;p&gt;Instead, the seat moves into a "held" state with an expiry time. If the customer pays, it becomes "booked". If they close the app, the hold simply runs out. The condition above treats a held seat whose time has passed as free. So the system stays correct even if a cleanup job runs late. The cleanup job is only there to tidy up.&lt;/p&gt;

&lt;p&gt;How long should the hold be? Too short, and honest customers who are slow at the OTP screen lose their seat. Too long, and seats stay off sale for people who have already left. On a sold-out bus, that looks like "no seats" while seats sit unpaid. A few minutes, with the countdown shown to the customer, is the usual balance.&lt;/p&gt;

&lt;h2&gt;
  
  
  The awkward case: paid after the hold ran out
&lt;/h2&gt;

&lt;p&gt;The customer paid. But the bank's confirmation came after the hold ran out, and someone else now holds the seat. The system must never give one seat to two people. So the late payment cannot just book it.&lt;/p&gt;

&lt;p&gt;The booking tries to confirm. The conditional update fails, because the hold no longer matches. The booking moves to "payment received, seat lost". The customer gets a refund automatically. Ideally, they are also offered another seat on the same bus. This is an expected state with a planned way out. It is not an error that leaves someone charged with no ticket.&lt;/p&gt;

&lt;h2&gt;
  
  
  One seat, many sellers
&lt;/h2&gt;

&lt;p&gt;This is the part that is special to redBus. According to Wikipedia, redBus runs BOGDS. It is a cloud service where bus operators manage their buses. It also runs SeatSeller, which gives bus seats to other agents to sell. So the same seat can be sold in three places. It can be sold on the redBus app, by a travel agent, or at the operator's own counter.&lt;/p&gt;

&lt;p&gt;If each seller kept its own copy of the seat map, they would sell seat 14 three times. Some operators use redBus's own software. For them, every seller goes through the same seat data and the same conditional hold. So there is one truth. Other operators use their own booking software. For them, the truth lives in their system. So a hold on redBus must also be placed there before the customer pays. And the booking must be confirmed there before the ticket is issued. That is slower, and it can fail. So each operator connection needs its own timeout and circuit breaker. One slow operator must never stall booking for everyone else.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I would say in an interview
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Split a cached search path, which is mostly reads, from an exact booking path.&lt;/li&gt;
&lt;li&gt;Hold seats with a conditional update that has an expiry. Never use a long database lock.&lt;/li&gt;
&lt;li&gt;Take payment during the hold. Refund automatically if the payment lands after it.&lt;/li&gt;
&lt;li&gt;Keep one source of truth per seat, across every place that sells it.&lt;/li&gt;
&lt;li&gt;Put each outside operator behind its own timeout and circuit breaker.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;redBus has not published how its booking system is built inside. So this is a standard seat-booking design that fits the public facts. It is not a description of its code. The full version, with the data model, trade-offs and sources, is in the &lt;a href="https://www.systemdesign.academy/interview/design-redbus" rel="noopener noreferrer"&gt;redBus system design walkthrough&lt;/a&gt;. The same seat problem shows up at &lt;a href="https://www.systemdesign.academy/interview/design-irctc" rel="noopener noreferrer"&gt;IRCTC&lt;/a&gt;, at far larger scale during Tatkal. It also shows up at &lt;a href="https://www.systemdesign.academy/interview/design-bookmyshow" rel="noopener noreferrer"&gt;BookMyShow&lt;/a&gt; for cinema seats.&lt;/p&gt;

</description>
      <category>systemdesign</category>
      <category>concurrency</category>
      <category>database</category>
      <category>interview</category>
    </item>
  </channel>
</rss>
