<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Resilience on Robin Su</title><link>https://robinsujob.github.io/tags/resilience/</link><description>Recent content in Resilience on Robin Su</description><generator>Hugo -- gohugo.io</generator><language>en-us</language><lastBuildDate>Thu, 22 Oct 2026 00:00:00 +0800</lastBuildDate><atom:link href="https://robinsujob.github.io/tags/resilience/index.xml" rel="self" type="application/rss+xml"/><item><title>Building E2B Sandbox Resilience: Lessons from Nomad's Heartbeat Mechanism</title><link>https://robinsujob.github.io/p/e2b-sandbox-resilience-nomad-heartbeats/</link><pubDate>Thu, 22 Oct 2026 00:00:00 +0800</pubDate><guid>https://robinsujob.github.io/p/e2b-sandbox-resilience-nomad-heartbeats/</guid><description>&lt;img src="https://robinsujob.github.io/" alt="Featured image of post Building E2B Sandbox Resilience: Lessons from Nomad's Heartbeat Mechanism" /&gt;&lt;p&gt;Running a self-hosted E2B cluster is about more than getting Firecracker sandboxes to start. We also need to consider how to keep them stable once they host long-running workloads. As a single cluster supports more sandboxes, resilience becomes increasingly important. &lt;strong&gt;Our architecture needs to protect existing runtime state and user connections while limiting the impact on capacity for new sandboxes, so that a local interruption does not cause unnecessary disruption elsewhere.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;This article starts with a single Nomad server becoming unavailable. It describes what we learned about Server / Client heartbeats and the changes we explored to task preservation, node failure detection, and blast-radius control.&lt;/p&gt;
&lt;p&gt;In this self-hosted environment, three Nomad servers form the control plane. The failure affected only the VM hosting one of them; the Clients running sandboxes and the other nodes remained healthy. Our initial expectation was straightforward: the remaining servers would keep the control plane available, Clients would switch to another healthy server, and workloads would continue running.&lt;/p&gt;
&lt;p&gt;The tests revealed something counterintuitive: &lt;strong&gt;only one server was lost, yet Orchestrator and other tasks on healthy Nomad Client hosts were replaced.&lt;/strong&gt; To understand why, we first needed to examine how Nomad servers and Clients use heartbeats to maintain contact, and what Nomad does with existing tasks when those heartbeats stop arriving.&lt;/p&gt;
&lt;h2 id="how-a-single-server-failure-affects-an-e2b-deployment"&gt;How a Single Server Failure Affects an E2B Deployment
&lt;/h2&gt;&lt;p&gt;First, consider the relationship between E2B and Nomad. In this deployment, Nomad schedules and manages services such as the API, Orchestrator, session-proxy, and client-proxy. Orchestrator, in turn, manages Firecracker sandboxes on its host. &lt;strong&gt;Sandbox creation and state management are handled by Orchestrator instances deployed on sandbox nodes and managed by Nomad.&lt;/strong&gt;&lt;/p&gt;
&lt;pre class="mermaid" style="visibility:hidden"&gt;flowchart TB
 accTitle: Control and access paths in self-hosted E2B
 accDescr: Nomad manages infrastructure services, while Orchestrator manages sandboxes. Sandbox traffic does not pass through Nomad servers for every request.
 servers["3 x Nomad servers&lt;br/&gt;Leader / Follower"]

 subgraph host["Sandbox host"]
 agent["Nomad Client agent"]
 orchestrator["Orchestrator"]
 proxy["session-proxy"]
 sandboxes["Firecracker sandboxes&lt;br/&gt;Original processes / memory / files"]
 agent --&gt;|"Run tasks"| orchestrator
 agent --&gt;|"Run tasks"| proxy
 orchestrator --&gt;|"Create and manage"| sandboxes
 proxy --&gt;|"Forward workload requests"| sandboxes
 end

 agent -.-&gt;|"Node.UpdateStatus"| servers
 servers -.-&gt;|"Heartbeat response / allocation state"| agent
 user["E2B SDK / user"] --&gt; entry["client-proxy&lt;br/&gt;Shared proxy entry point"]
 entry --&gt; proxy&lt;/pre&gt;&lt;p&gt;A Nomad Client is the agent running on a host. It communicates with servers and executes tasks. When Nomad schedules a task group onto a Client node, the resulting execution instance is called an &lt;strong&gt;allocation&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;To observe whether a single server failure could trigger task replacement, we forcibly stopped one non-Leader server without configuring a disconnect retention policy. The other two servers maintained quorum, and we continued observing for approximately 11 minutes.&lt;/p&gt;
&lt;p&gt;Tracking the identities of the original allocations, we recorded the following replacements for selected tasks:&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th&gt;Task&lt;/th&gt;
 &lt;th style="text-align: right"&gt;Original allocations replaced / original total&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td&gt;Orchestrator&lt;/td&gt;
 &lt;td style="text-align: right"&gt;&lt;strong&gt;7 / 20&lt;/strong&gt;&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;session-proxy&lt;/td&gt;
 &lt;td style="text-align: right"&gt;&lt;strong&gt;7 / 20&lt;/strong&gt;&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;client-proxy&lt;/td&gt;
 &lt;td style="text-align: right"&gt;&lt;strong&gt;2 / 3&lt;/strong&gt;&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;The experiment showed that &lt;strong&gt;the control plane can instruct a Client to abandon its original tasks even when the Client host has neither failed nor restarted.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;For an E2B cluster, this is an important detail. Orchestrator manages sandbox runtime resources and participates in sandbox creation and state management. An unnecessary replacement can therefore affect sandboxes that are already running.&lt;/p&gt;
&lt;h2 id="the-unexpected-sequence-tasks-stop-after-heartbeats-recover"&gt;The Unexpected Sequence: Tasks Stop After Heartbeats Recover
&lt;/h2&gt;&lt;p&gt;Using the platform&amp;rsquo;s confirmation that the server had stopped as the observation baseline, we aligned Nomad Client logs with workload events:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;At approximately 21 seconds, we had captured a successful heartbeat from the Client to a healthy server.&lt;/li&gt;
&lt;li&gt;At approximately 39 seconds, the original task began &lt;code&gt;Killing&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;At approximately 44 seconds, the original application stream closed.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The node and allocation states showed that &lt;strong&gt;the Client executed a previously made stop decision only after reconnecting and receiving an update.&lt;/strong&gt; The overall sequence was:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;span class="lnt"&gt;4
&lt;/span&gt;&lt;span class="lnt"&gt;5
&lt;/span&gt;&lt;span class="lnt"&gt;6
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Heartbeat expires
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;-&amp;gt; Server updates node status and triggers a scheduling evaluation
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;-&amp;gt; Scheduler marks the original allocation as stop / lost
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;-&amp;gt; Client reconnects and retrieves the allocation update
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;-&amp;gt; Local task executes Killing
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;-&amp;gt; Replacement allocation starts
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;h2 id="understanding-nomad-heartbeats-how-node-failure-detection-affects-workloads"&gt;Understanding Nomad Heartbeats: How Node Failure Detection Affects Workloads
&lt;/h2&gt;&lt;p&gt;With this question in mind, we examined Nomad&amp;rsquo;s GitHub source code and official documentation. We traced the following call chain:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Client sends a heartbeat -&amp;gt; RPC selects a server -&amp;gt; Follower forwards the request -&amp;gt; Leader renews the heartbeat timer -&amp;gt; Expiration updates node status -&amp;gt; Scheduler processes allocations -&amp;gt; Client applies task updates.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The sequence diagram below provides the full picture before we walk through the code. Its timestamps are illustrative and start from the last successfully processed heartbeat at the Leader. The log timestamps above start from the platform&amp;rsquo;s stop confirmation, so the two timelines do not correspond directly.&lt;/p&gt;
&lt;pre class="mermaid" style="visibility:hidden"&gt;sequenceDiagram
 accTitle: From heartbeat expiration to termination of the original task
 accDescr: t=0 is the last successful heartbeat at the Leader. With an illustrative TTL of 15 seconds and grace of 10 seconds, expiration occurs at about 25 seconds. If connectivity returns at 40 seconds, the Client can still execute a stop plan created earlier. These are illustrative, not measured, timestamps.
 autonumber
 participant Time as Relative timeline
 participant C as Nomad Client
 participant F as Follower
 participant L as Leader
 participant S as Scheduler
 participant T as Original task

 Note over C,T: Illustrative: TTL=15s, grace=10s&lt;br/&gt;t=0: last successful heartbeat at Leader&lt;br/&gt;Not measured or to scale, scheduling delay omitted
 Note over Time: 0s&lt;br/&gt;Last successful heartbeat
 C-&gt;&gt;F: Node.UpdateStatus
 F-&gt;&gt;L: t=0: Forward heartbeat and renew timer
 L--&gt;&gt;F: Return dynamic TTL=15s
 F--&gt;&gt;C: Heartbeat succeeds

 Note over Time: 5s
 Note over F: t=5s: Assume the server VM becomes unavailable
 Note over Time: About 15s
 C-xF: t~15s: Next heartbeat RPC does not return promptly
 Note over Time: About 25s&lt;br/&gt;15s TTL + 10s grace
 Note over L: t~25s (15+10): Heartbeat budget expires
 L-&gt;&gt;L: Update node status
 L-&gt;&gt;S: Trigger node-update evaluation
 Note over Time: About 25s+
 S-&gt;&gt;L: t~25s+: Submit stop plan for original allocation

 Note over Time: 40s&lt;br/&gt;Assumed recovery point
 C-&gt;&gt;L: t=40s: Assume heartbeat succeeds through a healthy server
 C-&gt;&gt;L: Retrieve allocation updates
 L--&gt;&gt;C: Original allocation is already marked to stop
 Note over Time: 40s+
 C-&gt;&gt;T: t=40s+: Local runner executes Killing
 Note over C,T: The stop plan predates reconnection&lt;br/&gt;A recovered heartbeat does not automatically cancel it&lt;/pre&gt;&lt;p&gt;The following sections walk through the key code paths. The snippets have been simplified.&lt;/p&gt;
&lt;h3 id="1-client-heartbeats-report-node-status"&gt;1. Client Heartbeats Report Node Status
&lt;/h3&gt;&lt;p&gt;On the Client, &lt;a class="link" href="https://github.com/hashicorp/nomad/blob/bf24b0dd97430d3b364b68b1f7375e85e585778f/client/client.go#L1870-L1923" target="_blank" rel="noopener"
 &gt;&lt;code&gt;registerAndHeartbeat()&lt;/code&gt;&lt;/a&gt; handles registration and periodic heartbeats. The actual request is constructed in &lt;a class="link" href="https://github.com/hashicorp/nomad/blob/bf24b0dd97430d3b364b68b1f7375e85e585778f/client/client.go#L2236-L2274" target="_blank" rel="noopener"
 &gt;&lt;code&gt;updateNodeStatus()&lt;/code&gt;&lt;/a&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;span class="lnt"&gt;4
&lt;/span&gt;&lt;span class="lnt"&gt;5
&lt;/span&gt;&lt;span class="lnt"&gt;6
&lt;/span&gt;&lt;span class="lnt"&gt;7
&lt;/span&gt;&lt;span class="lnt"&gt;8
&lt;/span&gt;&lt;span class="lnt"&gt;9
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;:=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;structs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;NodeUpdateStatusRequest&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;NodeID&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;NodeID&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;structs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;NodeStatusReady&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;var&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;resp&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;structs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;NodeUpdateResponse&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;:=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;RPC&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;Node.UpdateStatus&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nx"&gt;resp&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;!=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;nil&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;heartbeatTTL&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;resp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;HeartbeatTTL&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;The request reports the &lt;strong&gt;NodeID and node status&lt;/strong&gt;, not the application health of a particular Job. After a successful response, the Client uses &lt;code&gt;HeartbeatTTL&lt;/code&gt; to schedule its next heartbeat. After a failure, the heartbeat loop schedules a retry and triggers server discovery.&lt;/p&gt;
&lt;p&gt;Multiple tasks on the same Client therefore share the node heartbeat. &lt;code&gt;Node.UpdateAlloc&lt;/code&gt; reports allocation state, while Orchestrator&amp;rsquo;s gRPC health check measures application health. Neither replaces this Client-to-Server heartbeat.&lt;/p&gt;
&lt;h3 id="2-one-server-per-rpc-attempt-switching-paths-waits-for-the-call-to-return"&gt;2. One Server per RPC Attempt: Switching Paths Waits for the Call to Return
&lt;/h3&gt;&lt;p&gt;Next, consider the Client&amp;rsquo;s &lt;a class="link" href="https://github.com/hashicorp/nomad/blob/bf24b0dd97430d3b364b68b1f7375e85e585778f/client/rpc.go#L83-L146" target="_blank" rel="noopener"
 &gt;&lt;code&gt;rpc()&lt;/code&gt;&lt;/a&gt;. The key sequence, simplified, is:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;span class="lnt"&gt;4
&lt;/span&gt;&lt;span class="lnt"&gt;5
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;server&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;:=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;servers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;FindServer&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;rpcErr&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;:=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;connPool&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;RPC&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Region&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Addr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;method&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;reply&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;rpcErr&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;!=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;nil&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;servers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;NotifyFailedServer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;server&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;The Client first selects a server and then makes the RPC through the connection pool. &lt;strong&gt;Only after the current call returns an error&lt;/strong&gt; does it report the server failure and decide whether to retry at the RPC layer, based on the error type and remaining budget. If the error is not safe to retry automatically, it returns to the heartbeat loop above.&lt;/p&gt;
&lt;p&gt;The important delay here is how long it takes an RPC to return an error after a connection fails. Under the &lt;a class="link" href="https://developer.hashicorp.com/nomad/docs/configuration/rpc" target="_blank" rel="noopener"
 &gt;default configuration&lt;/a&gt; and &lt;a class="link" href="https://github.com/hashicorp/yamux/blob/v0.1.2/session.go#L327-L379" target="_blank" rel="noopener"
 &gt;yamux implementation&lt;/a&gt;, when an established connection silently fails, the Keepalive interval is 30 seconds, and a Ping waits approximately 10 seconds for a Pong. Assuming the Ping write does not block and no Ping was already in flight before the failure, detecting the failure can take roughly 10-40 seconds, depending on the Keepalive phase.&lt;/p&gt;
&lt;p&gt;The retry budget is also checked only after the underlying call returns; it cannot interrupt that call early. This creates a possible race: &lt;strong&gt;the connection is still waiting to confirm a failure while the Leader&amp;rsquo;s node heartbeat budget has already expired.&lt;/strong&gt;&lt;/p&gt;
&lt;h3 id="3-heartbeats-can-pass-through-a-follower-but-the-leader-renews-the-timer"&gt;3. Heartbeats Can Pass Through a Follower, but the Leader Renews the Timer
&lt;/h3&gt;&lt;p&gt;When a server receives the request, it first passes through the forwarding check in &lt;a class="link" href="https://github.com/hashicorp/nomad/blob/bf24b0dd97430d3b364b68b1f7375e85e585778f/nomad/node_endpoint.go#L655-L678" target="_blank" rel="noopener"
 &gt;&lt;code&gt;Node.UpdateStatus()&lt;/code&gt;&lt;/a&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;done&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;:=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;srv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forward&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;Node.UpdateStatus&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;reply&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;done&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;&lt;a class="link" href="https://github.com/hashicorp/nomad/blob/bf24b0dd97430d3b364b68b1f7375e85e585778f/nomad/rpc.go#L603-L618" target="_blank" rel="noopener"
 &gt;&lt;code&gt;forward()&lt;/code&gt;&lt;/a&gt; determines whether the current server should handle the request. If it is not the Leader, it forwards the request to the Leader.&lt;/p&gt;
&lt;p&gt;The Client does not need to contact the Leader directly every time, but its selected first-hop server must deliver the request promptly. &lt;strong&gt;If the Follower stops responding and the Client cannot switch paths in time, a healthy Leader may still fail to receive that Client&amp;rsquo;s heartbeat.&lt;/strong&gt;&lt;/p&gt;
&lt;h3 id="4-the-leader-waits-for-the-dynamic-ttl-plus-grace"&gt;4. The Leader Waits for the Dynamic TTL Plus Grace
&lt;/h3&gt;&lt;p&gt;Once the heartbeat successfully reaches the Leader, it calls &lt;a class="link" href="https://github.com/hashicorp/nomad/blob/bf24b0dd97430d3b364b68b1f7375e85e585778f/nomad/heartbeat.go#L93-L135" target="_blank" rel="noopener"
 &gt;&lt;code&gt;resetHeartbeatTimer()&lt;/code&gt;&lt;/a&gt;. The function calculates an interval based on cluster size and then adds random jitter:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;span class="lnt"&gt;4
&lt;/span&gt;&lt;span class="lnt"&gt;5
&lt;/span&gt;&lt;span class="lnt"&gt;6
&lt;/span&gt;&lt;span class="lnt"&gt;7
&lt;/span&gt;&lt;span class="lnt"&gt;8
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;ttl&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;:=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;helper&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;RateScaledInterval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;h&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;srv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;MaxHeartbeatsPerSecond&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;h&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;srv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;MinHeartbeatTTL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;h&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;heartbeatTimers&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;ttl&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;+=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;helper&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;RandomStagger&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ttl&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;h&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resetHeartbeatTimerLocked&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;ttl&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="nx"&gt;h&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;srv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;HeartbeatGrace&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;ttl&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;nil&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;&lt;strong&gt;The returned &lt;code&gt;ttl&lt;/code&gt; determines the Client&amp;rsquo;s next heartbeat interval, while the Leader sets its expiration timer to &lt;code&gt;ttl + HeartbeatGrace&lt;/code&gt;.&lt;/strong&gt; &lt;code&gt;heartbeat_grace&lt;/code&gt; adds time for the Server to wait for the next successful heartbeat.&lt;/p&gt;
&lt;p&gt;Following the &lt;a class="link" href="https://developer.hashicorp.com/nomad/docs/configuration/server#client-heartbeats" target="_blank" rel="noopener"
 &gt;official heartbeat documentation&lt;/a&gt;, the normal path can be understood as:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;span class="lnt"&gt;4
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Reference node expiration time
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; = Time when the Leader last successfully processed a heartbeat and reset its timer
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; + Dynamic heartbeat TTL for that round
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; + heartbeat_grace
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;In this small cluster, the normal TTL was approximately 10-20 seconds. With the default 10-second grace, the Leader&amp;rsquo;s budget before declaring a Client&amp;rsquo;s heartbeat expired was approximately 20-30 seconds.&lt;/p&gt;
&lt;h3 id="5-timer-expiration-changes-node-status-before-triggering-scheduling"&gt;5. Timer Expiration Changes Node Status Before Triggering Scheduling
&lt;/h3&gt;&lt;p&gt;When the timer expires, the Leader runs &lt;a class="link" href="https://github.com/hashicorp/nomad/blob/bf24b0dd97430d3b364b68b1f7375e85e585778f/nomad/heartbeat.go#L137-L181" target="_blank" rel="noopener"
 &gt;&lt;code&gt;invalidateHeartbeat()&lt;/code&gt;&lt;/a&gt;. The key node-status branch is:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;span class="lnt"&gt;4
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Status&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;structs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;NodeStatusDown&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;canDisconnect&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;hasPendingReconnects&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Status&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;structs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;NodeStatusDisconnected&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;Whether the node enters &lt;code&gt;disconnected&lt;/code&gt; depends on whether its allocations support reconnection and remain within their recovery windows. Not every heartbeat expiration leads to the same state transition.&lt;/p&gt;
&lt;p&gt;The &lt;a class="link" href="https://github.com/hashicorp/nomad/blob/bf24b0dd97430d3b364b68b1f7375e85e585778f/nomad/node_endpoint.go#L816-L873" target="_blank" rel="noopener"
 &gt;node-status update path&lt;/a&gt; then persists the necessary state changes through Raft and creates scheduling evaluations for the relevant transitions. The scheduler uses the Job type and disconnect policy to decide which allocations to retain temporarily, which to mark &lt;code&gt;lost&lt;/code&gt;, and whether to create replacements.&lt;/p&gt;
&lt;p&gt;For example, &lt;a class="link" href="https://github.com/hashicorp/nomad/blob/bf24b0dd97430d3b364b68b1f7375e85e585778f/scheduler/reconciler/reconcile_cluster.go#L1097-L1127" target="_blank" rel="noopener"
 &gt;stop calculations for service allocations&lt;/a&gt; handle the lost set, while the &lt;a class="link" href="https://github.com/hashicorp/nomad/blob/bf24b0dd97430d3b364b68b1f7375e85e585778f/scheduler/reconciler/reconcile_node.go#L185-L263" target="_blank" rel="noopener"
 &gt;node reconciliation logic for system Jobs&lt;/a&gt; distinguishes reconnectable &lt;code&gt;unknown&lt;/code&gt; allocations from those that need to be treated as lost.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;A heartbeat timeout does not mean the Leader directly sends a kill signal to an application process.&lt;/strong&gt; Node-status updates and scheduling plans sit between those events, and that is where the following configuration changes can intervene.&lt;/p&gt;
&lt;h3 id="6-after-reconnecting-the-client-applies-an-allocation-update-that-already-exists"&gt;6. After Reconnecting, the Client Applies an Allocation Update That Already Exists
&lt;/h3&gt;&lt;p&gt;The Client retrieves server-side allocation changes through &lt;a class="link" href="https://github.com/hashicorp/nomad/blob/bf24b0dd97430d3b364b68b1f7375e85e585778f/client/client.go#L2482-L2545" target="_blank" rel="noopener"
 &gt;&lt;code&gt;watchAllocations()&lt;/code&gt;&lt;/a&gt;. Once an update reaches the local runner, &lt;a class="link" href="https://github.com/hashicorp/nomad/blob/bf24b0dd97430d3b364b68b1f7375e85e585778f/client/allocrunner/alloc_runner.go#L1078-L1101" target="_blank" rel="noopener"
 &gt;&lt;code&gt;handleAllocUpdate()&lt;/code&gt;&lt;/a&gt; checks whether the allocation is transitioning to a terminal state:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;span class="lnt"&gt;4
&lt;/span&gt;&lt;span class="lnt"&gt;5
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;stopping&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;:=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;ar&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Alloc&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;TerminalStatus&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;update&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;TerminalStatus&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;ar&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setAlloc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;update&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;stopping&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;ar&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;killTasks&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;The terminal-state check also includes the Server&amp;rsquo;s desired state. Even if the original task is still running, the local runner stops it if the received update already requires termination.&lt;/p&gt;
&lt;p&gt;This connects the cause and effect seen in the logs: &lt;strong&gt;the Client did not switch heartbeat paths before its budget expired, so the Server updated node status and the scheduler created a stop plan. When the Client later reconnected, it applied the update and stopped the original task. Replacement means a new allocation starts, not that a task restarts within the original allocation.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The community has also discussed similar symptoms in &lt;a class="link" href="https://github.com/hashicorp/nomad/issues/17974" target="_blank" rel="noopener"
 &gt;hashicorp/nomad#17974&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Understanding this chain reveals two intervention points: &lt;strong&gt;allow more time to switch heartbeat paths before declaring a node disconnected, or give the original tasks a chance to reconnect after that declaration.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id="solution-1-preserve-original-tasks-and-access-paths-with-disconnect"&gt;Solution 1: Preserve Original Tasks and Access Paths with disconnect
&lt;/h2&gt;&lt;p&gt;For E2B, the first priority is to avoid unnecessarily replacing Orchestrator on a healthy Client because of a brief control-plane disconnection.&lt;/p&gt;
&lt;p&gt;We used Nomad&amp;rsquo;s &lt;code&gt;disconnect&lt;/code&gt; policy to separate &amp;ldquo;detect that the node is disconnected&amp;rdquo; from &amp;ldquo;immediately abandon its original tasks.&amp;rdquo; The configuration belongs in an existing Job&amp;rsquo;s &lt;strong&gt;task group&lt;/strong&gt;, for example:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;span class="lnt"&gt;4
&lt;/span&gt;&lt;span class="lnt"&gt;5
&lt;/span&gt;&lt;span class="lnt"&gt;6
&lt;/span&gt;&lt;span class="lnt"&gt;7
&lt;/span&gt;&lt;span class="lnt"&gt;8
&lt;/span&gt;&lt;span class="lnt"&gt;9
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-hcl" data-lang="hcl"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;group&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;orchestrator&amp;#34;&lt;/span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;disconnect&lt;/span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt; lost_after&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;50s&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt; replace&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;false&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt; reconcile&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;keep_original&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; }&lt;span class="c1"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt; # Keep the existing task, network, and other definitions unchanged.
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;Orchestrator, session-proxy, and client-proxy should each be evaluated and configured in their respective groups where protection is needed.&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th&gt;Setting&lt;/th&gt;
 &lt;th&gt;What it addresses&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;code&gt;lost_after = &amp;quot;50s&amp;quot;&lt;/code&gt;&lt;/td&gt;
 &lt;td&gt;Provides a bounded reconnection window after an allocation enters &lt;code&gt;unknown&lt;/code&gt;&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;code&gt;replace = false&lt;/code&gt;&lt;/td&gt;
 &lt;td&gt;Prevents immediate replacement during the disconnect retention phase&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;code&gt;reconcile = &amp;quot;keep_original&amp;quot;&lt;/code&gt;&lt;/td&gt;
 &lt;td&gt;Prefers the original instance during applicable reconnection reconciliation&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;Explicitly setting &lt;code&gt;replace = false&lt;/code&gt; prevents an immediate replacement from being created within the retention window. The corresponding &lt;a class="link" href="https://github.com/hashicorp/nomad/blob/bf24b0dd97430d3b364b68b1f7375e85e585778f/scheduler/reconciler/filters.go#L527-L544" target="_blank" rel="noopener"
 &gt;scheduler check&lt;/a&gt; is:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;isDisconnecting&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;alloc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ReplaceOnDisconnect&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;The other distinction is when the window starts. &lt;a class="link" href="https://github.com/hashicorp/nomad/blob/bf24b0dd97430d3b364b68b1f7375e85e585778f/nomad/structs/alloc.go#L756-L779" target="_blank" rel="noopener"
 &gt;&lt;code&gt;Allocation.Expired()&lt;/code&gt;&lt;/a&gt; calculates expiration from the allocation&amp;rsquo;s most recent transition into &lt;code&gt;unknown&lt;/code&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;lastUnknown&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;:=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;LastUnknown&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;expiry&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;:=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;lastUnknown&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;tg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetDisconnectLostAfter&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;expiry&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Sub&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;now&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;lt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;Therefore, &lt;code&gt;lost_after = &amp;quot;50s&amp;quot;&lt;/code&gt; does not mean waiting 50 seconds from the server&amp;rsquo;s failure, and it is not equivalent to &lt;code&gt;heartbeat_grace = &amp;quot;50s&amp;quot;&lt;/code&gt;. Assuming a TTL of 15 seconds and a grace of 10 seconds, the approximate sequence is:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;span class="lnt"&gt;4
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;t = 0 Last successful heartbeat at the Leader
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;t ~ 25s Heartbeat expires; node disconnection handling begins
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;t ~ 25s+ Relevant allocation enters unknown
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;t ~ (25+50)=75s+ If recovery has not completed, the 50-second retention window expires
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;h3 id="disconnect-sequence-retain-the-original-task-then-wait-for-reconnection"&gt;disconnect Sequence: Retain the Original Task, Then Wait for Reconnection
&lt;/h3&gt;&lt;p&gt;The following diagram assumes that the original task on a healthy Client is still running. It shows how the &lt;code&gt;50s / false / keep_original&lt;/code&gt; policy handles a brief disconnection:&lt;/p&gt;
&lt;pre class="mermaid" style="visibility:hidden"&gt;sequenceDiagram
 accTitle: How disconnect retains the original task after node disconnection
 accDescr: t=0 is the last successful heartbeat. With an illustrative TTL of 15 seconds and grace of 10 seconds, expiration occurs at about 25 seconds. A separate 50-second window starts when the allocation enters unknown. The example either reconnects at 40 seconds or proceeds to expiration handling after approximately 75 seconds. These are not measured timestamps.
 autonumber
 participant Time as Relative timeline
 participant C as Nomad Client
 participant L as Leader
 participant S as Scheduler
 participant T as Original task

 Note over C,T: Illustrative: TTL=15s, grace=10s, lost_after=50s&lt;br/&gt;t=0: last successful heartbeat at Leader&lt;br/&gt;Not measured or to scale, scheduling delay omitted
 Note over Time: 0s&lt;br/&gt;Last successful heartbeat
 C-&gt;&gt;L: t=0: Last successful heartbeat through a server
 L--&gt;&gt;C: Return dynamic TTL=15s
 Note over Time: 5s
 Note over C,L: t=5s: Assume the original first-hop server fails
 Note over Time: About 15s
 C-xL: t~15s: Heartbeat through the original path does not succeed
 Note over Time: About 25s&lt;br/&gt;Heartbeat budget expires
 Note over L: t~25s (15+10): Heartbeat budget expires
 L-&gt;&gt;L: Mark node as disconnected
 L-&gt;&gt;S: Trigger scheduling evaluation
 Note over Time: tU ~ 25s+&lt;br/&gt;Retention window starts
 S-&gt;&gt;L: tU~25s+: Mark original allocation as unknown
 Note over L,S: Start a separate 50s window at tU&lt;br/&gt;Expiration: tU+50s~75s+
 Note over S,T: replace = false: No immediate replacement
 Note over T: Original task keeps running on the healthy host

 alt Assume connectivity returns at t=40s within the window
 Note over Time: 40s&lt;br/&gt;Still within the retention window
 C-&gt;&gt;L: Restore heartbeat through another healthy server
 C-&gt;&gt;L: Synchronize original allocation runtime state
 L-&gt;&gt;S: Trigger reconnection reconciliation
 S-&gt;&gt;L: Retain original instance under keep_original
 Note over T: t=40s+: Preserve task identity without a disconnection-induced restart
 else Still disconnected at tU+50s~75s+
 Note over Time: tU + 50s ~ 75s+&lt;br/&gt;Retention window expires
 S-&gt;&gt;L: Window expires, proceed to lost and subsequent recovery
 end&lt;/pre&gt;&lt;p&gt;This does not extend the node&amp;rsquo;s heartbeat budget. Instead, it delays the decision to abandon the original allocation: &lt;strong&gt;enter &lt;code&gt;unknown&lt;/code&gt;, avoid immediate replacement, and wait for the original instance to reconnect.&lt;/strong&gt; The node may still not be &lt;code&gt;ready&lt;/code&gt; during this period, and retaining the original instance is no longer guaranteed after the window expires.&lt;/p&gt;
&lt;p&gt;Protection also needs to follow the user access path. Preserving Orchestrator alone does not necessarily preserve an existing connection. If client-proxy is replaced, the sandbox can remain alive while the connection breaks. We therefore included session-proxy and client-proxy in the validation.&lt;/p&gt;
&lt;p&gt;In one test with a 50-second disconnect window, Nomad Clients did briefly enter &lt;code&gt;disconnected&lt;/code&gt;. The observed &lt;code&gt;unknown&lt;/code&gt; intervals for their original allocations were approximately 1.7-31.6 seconds, followed by recovery within the window:&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th&gt;Validation item&lt;/th&gt;
 &lt;th&gt;Observation&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td&gt;Orchestrator, session-proxy, and client-proxy&lt;/td&gt;
 &lt;td&gt;Original allocations preserved; no additional task restarts&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Original sandbox process identities, memory, and files&lt;/td&gt;
 &lt;td&gt;10 / 10 preserved; final independent checks completed&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Original HTTP NDJSON streams&lt;/td&gt;
 &lt;td&gt;23 / 23 preserved&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Short requests during the formal observation period&lt;/td&gt;
 &lt;td&gt;12,581 requests, 0 errors&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;With disconnect configured, &lt;strong&gt;the Clients briefly lost control-plane contact, but their original tasks remained in place until reconnection, without being replaced.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id="beyond-task-preservation-how-node-status-affects-sandbox-creation-capacity"&gt;Beyond Task Preservation: How Node Status Affects Sandbox Creation Capacity
&lt;/h2&gt;&lt;p&gt;&lt;code&gt;disconnect&lt;/code&gt; preserves original tasks; it does not prevent a Client from being declared disconnected after its heartbeat expires. In other words, &lt;strong&gt;Orchestrator and existing sandboxes can remain running without the node remaining eligible to receive new sandbox placements.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;In this E2B implementation, the API normally synchronizes node information from Nomad every 20 seconds, selecting nodes in the sandbox node pool whose status is &lt;code&gt;ready&lt;/code&gt;. The core filter is:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;:=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nx"&gt;nomadapi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;QueryOptions&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Filter&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;`Status == &amp;#34;ready&amp;#34; and NodePool == &amp;#34;default&amp;#34;`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;If the query succeeds but a previously known node no longer appears in the results, the API marks it &lt;code&gt;unhealthy&lt;/code&gt;, affecting subsequent placement.&lt;/p&gt;
&lt;p&gt;The public E2B runtime also has an implementation that discovers Orchestrators through allocations. Its &lt;a class="link" href="https://github.com/e2b-dev/runtime/blob/afaaeaef96aae86f65ce60bf38fe083e7638a28e/packages/shared/pkg/clusters/discovery/nomad.go#L29-L57" target="_blank" rel="noopener"
 &gt;discovery filter&lt;/a&gt; requires &lt;code&gt;ClientStatus == &amp;quot;running&amp;quot;&lt;/code&gt;. Its &lt;a class="link" href="https://github.com/e2b-dev/runtime/blob/afaaeaef96aae86f65ce60bf38fe083e7638a28e/packages/api/internal/orchestrator/cache.go#L195-L211" target="_blank" rel="noopener"
 &gt;API synchronization logic&lt;/a&gt; also removes an original instance from the local node pool when that instance no longer appears in valid discovery results.&lt;/p&gt;
&lt;p&gt;The two implementations filter different objects, but they highlight the same issue: &lt;strong&gt;placement decisions depend on Nomad&amp;rsquo;s state, not only on whether the original Orchestrator process is alive.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Under a high sandbox creation rate, some healthy hosts may continue running existing sandboxes while temporarily being unable to share the load of new requests. Creation pressure shifts to the remaining eligible nodes. If they lack sufficient capacity, creation can fail, queue, or time out.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;This is fundamentally a blast-radius problem.&lt;/strong&gt; The failure originates in a single server and directly disrupts communication for some Clients. Node state then affects API placement, transferring pressure to other healthy nodes. &lt;code&gt;disconnect&lt;/code&gt; contains the &amp;ldquo;destroy original tasks&amp;rdquo; path, but it does not automatically contain the &amp;ldquo;exclude healthy capacity&amp;rdquo; and &amp;ldquo;overload the remaining nodes&amp;rdquo; paths.&lt;/p&gt;
&lt;h2 id="solution-2-reduce-false-node-failure-detections-with-heartbeat_grace"&gt;Solution 2: Reduce False Node Failure Detections with heartbeat_grace
&lt;/h2&gt;&lt;p&gt;&lt;code&gt;disconnect&lt;/code&gt; addresses the situation after a node has been declared disconnected: do not abandon its original tasks immediately. Since node state also affects creation capacity, we need to look one step earlier. Can we give the Client enough time to switch paths before declaring it disconnected?&lt;/p&gt;
&lt;p&gt;This is where the Server-side &lt;strong&gt;&lt;code&gt;heartbeat_grace&lt;/code&gt;&lt;/strong&gt; setting applies:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-hcl" data-lang="hcl"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;server&lt;/span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt; heartbeat_grace&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;50s&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;It adds time beyond the dynamic TTL. Changing the default grace from 10 to 50 seconds adds 40 seconds of headroom. Combined with the normal TTL of approximately 10-20 seconds, the reference budget becomes roughly 60-70 seconds from the last successful heartbeat.&lt;/p&gt;
&lt;h3 id="heartbeat_grace-sequence-allow-more-switching-time-before-declaring-a-node-disconnected"&gt;heartbeat_grace Sequence: Allow More Switching Time Before Declaring a Node Disconnected
&lt;/h3&gt;&lt;p&gt;Unlike disconnect, this changes how long the Leader waits for the next successful heartbeat. The example still assumes that the same Leader remains healthy throughout:&lt;/p&gt;
&lt;pre class="mermaid" style="visibility:hidden"&gt;sequenceDiagram
 accTitle: How heartbeat_grace provides switching time before node failure detection
 accDescr: t=0 is the last successful heartbeat. With an illustrative TTL of 15 seconds and grace of 50 seconds, expiration is at 65 seconds. A heartbeat restored at 40 seconds prevents this disconnection. If no heartbeat succeeds by 65 seconds, failure handling begins. These are illustrative, not measured, timestamps.
 autonumber
 participant Time as Relative timeline
 participant C as Nomad Client
 participant F as Original Follower
 participant L as Leader
 participant T as Original task

 Note over C,T: Illustrative: TTL=15s, grace=50s&lt;br/&gt;t=0: last successful heartbeat at Leader&lt;br/&gt;Not measured or to scale, scheduling delay omitted
 Note over Time: 0s&lt;br/&gt;Last successful heartbeat
 C-&gt;&gt;F: Node.UpdateStatus
 F-&gt;&gt;L: t=0: Forward heartbeat and renew timer
 L-&gt;&gt;L: Set expiration to t=65s (15+50)
 L--&gt;&gt;F: Return dynamic TTL=15s
 F--&gt;&gt;C: Schedule next heartbeat using the dynamic TTL

 Note over Time: 5s
 Note over F: t=5s: Assume the server fails
 Note over Time: About 15s
 C-xF: t~15s: Next heartbeat RPC does not return promptly
 Note over C: Wait for failure detection and switch to a healthy server
 Note over Time: About 25s&lt;br/&gt;Expiration with default grace
 Note over L: t~25s: Default grace would have expired&lt;br/&gt;The extended budget still runs until t=65s

 alt Assume heartbeat recovers at t=40s, before t=65s
 Note over Time: 40s&lt;br/&gt;Before the 65s expiration point
 C-&gt;&gt;L: Complete heartbeat through a healthy server
 L-&gt;&gt;L: Reset the TTL + grace timer
 Note over L,T: No node disconnection or task replacement triggered by this heartbeat failure
 else Still no heartbeat at t=65s, or the Client has actually failed
 Note over Time: About 65s&lt;br/&gt;15s TTL + 50s grace
 Note over L: t~65s (15+50): Heartbeat budget expires
 L-&gt;&gt;L: t~65s+: Declare node failure and begin subsequent task handling
 end&lt;/pre&gt;&lt;p&gt;Across two complete server-stop tests with grace set to 50 seconds and no disconnect policy on the key tasks, none of the 20 surviving Clients in either run was observed entering &lt;code&gt;down&lt;/code&gt; or &lt;code&gt;disconnected&lt;/code&gt;, and no original tasks were replaced. This matched the expected behavior: &lt;strong&gt;if switching completes before the budget expires, there is no need to enter the subsequent disconnection and task-handling paths.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;heartbeat_grace&lt;/code&gt; gives the Leader more time to wait.&lt;/strong&gt; If connectivity recovers in time, that heartbeat failure does not lead to a node-disconnection decision.&lt;/p&gt;
&lt;p&gt;However, the cost becomes clearer when a Client actually fails. When a Server fails but the Client is healthy, we want to wait a little longer. When the Client&amp;rsquo;s host really has failed, we want to detect it quickly and stop placing work there. The same heartbeat signal cannot guarantee both &amp;ldquo;never falsely mark a healthy node down&amp;rdquo; and &amp;ldquo;immediately detect a real failure.&amp;rdquo; A longer grace delays real Client-failure detection at the Nomad heartbeat layer, although application health checks may exclude the failed node earlier.&lt;/p&gt;
&lt;p&gt;From a blast-radius perspective, the two adjustments place the cost at different points:&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th&gt;Adjustment&lt;/th&gt;
 &lt;th&gt;Benefit&lt;/th&gt;
 &lt;th&gt;Cost and boundary&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td&gt;Moderately increase &lt;code&gt;heartbeat_grace&lt;/code&gt;&lt;/td&gt;
 &lt;td&gt;Give the Leader more time for the next heartbeat, reduce false failure detections on healthy Clients, and lower the risk of unnecessary task replacement or exclusion from the creation pool&lt;/td&gt;
 &lt;td&gt;Real Client failures also become visible later at the Nomad heartbeat layer&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Shorter grace + targeted &lt;code&gt;disconnect&lt;/code&gt;&lt;/td&gt;
 &lt;td&gt;Expose disconnection sooner after a real Client failure while giving original tasks a short reconnection window&lt;/td&gt;
 &lt;td&gt;During a Server failure, some healthy Clients may still temporarily leave the creation pool, shifting pressure to the remaining nodes&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;h2 id="summary-turning-heartbeat-findings-into-sandbox-resilience"&gt;Summary: Turning Heartbeat Findings into Sandbox Resilience
&lt;/h2&gt;&lt;p&gt;This investigation started with a simple question: why were tasks on healthy hosts rebuilt when only one Server was lost? Following the logs and code revealed a chain of architectural decisions that affect workloads: &lt;strong&gt;whether a heartbeat arrives in time, whether the node is declared disconnected, whether the original tasks are retained, and whether the layer above still treats the node as available capacity.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;For E2B, we need to protect three goals separately:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Existing sandbox runtime state&lt;/strong&gt;: Separate node disconnection from immediately abandoning original tasks, leaving room to recover from brief control-plane failures.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Existing user connections&lt;/strong&gt;: Cover Orchestrator, session-proxy, and client-proxy rather than only verifying that the sandbox still exists.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Creation capacity and real-failure recovery&lt;/strong&gt;: Observe both the erroneous exclusion of healthy nodes and delays in detecting failed nodes or replacing tasks, while maintaining headroom on the remaining capacity.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Ultimately, resilience does not mean every failure causes zero interruption. It means preserving valuable runtime state during short disconnections, keeping recovery bounded when a real failure occurs, and limiting the knock-on effects to a level the workload can tolerate.&lt;/p&gt;
&lt;h2 id="references"&gt;References
&lt;/h2&gt;&lt;ul&gt;
&lt;li&gt;&lt;a class="link" href="https://developer.hashicorp.com/nomad/docs/configuration/server#heartbeat_grace" target="_blank" rel="noopener"
 &gt;Nomad Server Configuration: heartbeat_grace&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://developer.hashicorp.com/nomad/docs/configuration/server#client-heartbeats" target="_blank" rel="noopener"
 &gt;Nomad Client Heartbeats&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://developer.hashicorp.com/nomad/docs/job-specification/disconnect" target="_blank" rel="noopener"
 &gt;Nomad disconnect Specification&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://github.com/hashicorp/nomad/blob/bf24b0dd97430d3b364b68b1f7375e85e585778f/client/client.go#L1870-L1923" target="_blank" rel="noopener"
 &gt;Nomad Client Heartbeat Scheduling&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://github.com/hashicorp/nomad/blob/bf24b0dd97430d3b364b68b1f7375e85e585778f/nomad/heartbeat.go#L56-L181" target="_blank" rel="noopener"
 &gt;Nomad Leader Heartbeat Timers and Expiration&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://github.com/hashicorp/nomad/blob/bf24b0dd97430d3b364b68b1f7375e85e585778f/nomad/structs/alloc.go#L756-L779" target="_blank" rel="noopener"
 &gt;Nomad Allocation Disconnect Expiration&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://github.com/e2b-dev/runtime/blob/afaaeaef96aae86f65ce60bf38fe083e7638a28e/packages/shared/pkg/clusters/discovery/nomad.go#L29-L57" target="_blank" rel="noopener"
 &gt;E2B Runtime: Nomad Discovery Filters&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://github.com/e2b-dev/runtime/blob/afaaeaef96aae86f65ce60bf38fe083e7638a28e/packages/api/internal/orchestrator/cache.go#L53-L120" target="_blank" rel="noopener"
 &gt;E2B Runtime: API Node Synchronization&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://github.com/hashicorp/nomad/issues/17974" target="_blank" rel="noopener"
 &gt;hashicorp/nomad#17974: Non-Leader Connectivity Problems and Allocation Restarts&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</description></item><item><title>Building Resilient Azure AI Architecture with API Management Gateway (Part 2) — Retry Observability and Alerting</title><link>https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/</link><pubDate>Wed, 29 Apr 2026 00:00:00 +0800</pubDate><guid>https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/</guid><description>&lt;img src="https://robinsujob.github.io/" alt="Featured image of post Building Resilient Azure AI Architecture with API Management Gateway (Part 2) — Retry Observability and Alerting" /&gt;&lt;p&gt;In the previous article &lt;a class="link" href="../azure-apim-ai-resilience/" &gt;&lt;em&gt;Building Resilient Azure AI Architecture with API Management Gateway&lt;/em&gt;&lt;/a&gt;, we built a resilient gateway architecture for multiple Microsoft Foundry resources using Azure API Management — Backend Load Balancing Pool for load distribution, and a &lt;code&gt;retry&lt;/code&gt; policy that automatically switches to healthy instances when backends return 4xx/5xx errors. This mechanism makes backend failures nearly invisible to clients.&lt;/p&gt;
&lt;p&gt;But &amp;ldquo;invisible&amp;rdquo; is precisely the challenge for operations teams: &lt;strong&gt;The Retry mechanism, while protecting clients, also hides the details of intermediate failures.&lt;/strong&gt; When a request goes through 3 retries and ultimately returns 200, operations staff have no way of knowing why the first two attempts failed — was it a 429 rate limit? A 404 deployment not found? Or a 500 server error? Which backend had the issue? What did the error body say?&lt;/p&gt;
&lt;p&gt;This article builds on the previous architecture to achieve complete Retry observability through &lt;strong&gt;Application Insights Diagnostic Logs&lt;/strong&gt;, &lt;strong&gt;without modifying any Policy code&lt;/strong&gt;, helping developers and operations teams:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;📋 &lt;strong&gt;Record detailed information for each Retry failure&lt;/strong&gt; — including the failed backend address, HTTP status code, error response body, and APIM request ID&lt;/li&gt;
&lt;li&gt;🔍 &lt;strong&gt;Reconstruct complete Retry chains via KQL queries&lt;/strong&gt; — using operation_Id to correlate all retry records for the same request&lt;/li&gt;
&lt;li&gt;🔔 &lt;strong&gt;Configure automated alerts&lt;/strong&gt; — notify the operations team via email when Retry failure counts exceed thresholds&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="1-architecture-overview"&gt;1. Architecture Overview
&lt;/h3&gt;&lt;p&gt;This article adds an Application Insights observability layer on top of the APIM + Foundry resilient architecture from the previous article. No changes to existing APIM Policies or Backend configurations are needed — Retry monitoring and alerting are achieved entirely through Diagnostic Logs configuration.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Components from the previous article (unchanged):&lt;/strong&gt;&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th&gt;Component&lt;/th&gt;
 &lt;th&gt;Role&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;strong&gt;Azure API Management&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;Unified gateway, executing routing, Retry, and load balancing policies&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;strong&gt;Backend Load Balancing Pool&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;Pool of multiple Foundry resources, distributing requests via Round-Robin&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;&lt;strong&gt;New components in this article:&lt;/strong&gt;&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th&gt;Component&lt;/th&gt;
 &lt;th&gt;Role&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;strong&gt;Application Insights&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;Receives APIM native telemetry (&lt;code&gt;requests&lt;/code&gt; / &lt;code&gt;dependencies&lt;/code&gt;), recording details of each Retry backend call&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;strong&gt;Log Alert&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;Automatically triggers alert notifications based on KQL queries when failure counts exceed thresholds&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;strong&gt;Action Group&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;Defines alert actions (Email/SMS/Webhook)&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;Core design principles:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Zero code changes&lt;/strong&gt; — No Policy modifications needed, fully implemented through APIM Diagnostic Logs configuration&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Native Telemetry&lt;/strong&gt; — APIM&amp;rsquo;s &lt;code&gt;dependencies&lt;/code&gt; table automatically records each &lt;code&gt;forward-request&lt;/code&gt; call (including intermediate Retry failures), combined with Backend Response Headers to log and Body bytes configuration to capture error details and Foundry request IDs&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Full Retry chain visibility&lt;/strong&gt; — All Retry attempts for the same request share the same &lt;code&gt;operation_Id&lt;/code&gt;, making it easy to reconstruct the complete retry chain in KQL&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proactive alerting&lt;/strong&gt; — Log Alert rules automatically detect failure trends and promptly notify the operations team&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The following steps will guide you through building the complete Retry observability solution:&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th&gt;Section&lt;/th&gt;
 &lt;th&gt;Action&lt;/th&gt;
 &lt;th&gt;Goal&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td&gt;2&lt;/td&gt;
 &lt;td&gt;Create Application Insights&lt;/td&gt;
 &lt;td&gt;Set up log storage and query infrastructure&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;3&lt;/td&gt;
 &lt;td&gt;Bind APIM Logger&lt;/td&gt;
 &lt;td&gt;Associate APIM with App Insights&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;4&lt;/td&gt;
 &lt;td&gt;Configure API Diagnostic Logs&lt;/td&gt;
 &lt;td&gt;Enable App Insights logging for the target API, configure Verbosity, Backend Response Headers and Body&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;5&lt;/td&gt;
 &lt;td&gt;Verify Data&lt;/td&gt;
 &lt;td&gt;Confirm Retry records in the dependencies table in App Insights&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;6&lt;/td&gt;
 &lt;td&gt;Configure Alert Rules&lt;/td&gt;
 &lt;td&gt;Create Log Alert + Action Group for automated email notifications&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;h3 id="2-create-an-application-insights-instance"&gt;2. Create an Application Insights Instance
&lt;/h3&gt;&lt;p&gt;Create a workspace-based Application Insights instance in the same subscription and region as APIM to receive APIM telemetry data.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;2-1) In the Azure Portal search bar, type Application Insights and select the &lt;strong&gt;Application Insights&lt;/strong&gt; service&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;img class="gallery-image" data-flex-basis="401px" data-flex-grow="167" height="1408" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/2-1-search-application-insights.png" srcset="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/2-1-search-application-insights_hu_acf1cf99e17b7e8b.png 800w, https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/2-1-search-application-insights_hu_61fc3fb0044de7d7.png 1600w, https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/2-1-search-application-insights.png 2357w" width="2357"&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;2-2) Click &lt;strong&gt;+ Create&lt;/strong&gt;, fill in the following key configurations and complete creation&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;img class="gallery-image" data-flex-basis="232px" data-flex-grow="96" height="1442" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/2-2-create-app-insights.png" srcset="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/2-2-create-app-insights_hu_2128c60a8f4cd365.png 800w, https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/2-2-create-app-insights.png 1396w" width="1396"&gt;&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th&gt;Configuration&lt;/th&gt;
 &lt;th&gt;Recommended Value&lt;/th&gt;
 &lt;th&gt;Description&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td&gt;Subscription&lt;/td&gt;
 &lt;td&gt;Same as APIM&lt;/td&gt;
 &lt;td&gt;Keep in the same subscription for easier management&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Resource group&lt;/td&gt;
 &lt;td&gt;&lt;code&gt;apim-ai-hub&lt;/code&gt;&lt;/td&gt;
 &lt;td&gt;Same resource group as APIM&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Name&lt;/td&gt;
 &lt;td&gt;&lt;code&gt;ai-hub-app-insight&lt;/code&gt;&lt;/td&gt;
 &lt;td&gt;Specify an Application Insights instance name&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Region&lt;/td&gt;
 &lt;td&gt;&lt;code&gt;Japan East&lt;/code&gt;&lt;/td&gt;
 &lt;td&gt;Same region as APIM to reduce data transmission latency&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Workspace&lt;/td&gt;
 &lt;td&gt;Default&lt;/td&gt;
 &lt;td&gt;System automatically creates a Log Analytics workspace, or select one you&amp;rsquo;ve created&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;

 &lt;blockquote&gt;
 &lt;p&gt;&lt;strong&gt;📝 Note:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Application Insights is workspace-based, with underlying data stored in a Log Analytics workspace. A workspace is automatically associated during creation — no manual creation needed.&lt;/p&gt;

 &lt;/blockquote&gt;
&lt;h3 id="3-bind-apim-logger"&gt;3. Bind APIM Logger
&lt;/h3&gt;&lt;p&gt;Associate the Application Insights instance created in the previous step with APIM, enabling APIM to send telemetry data to App Insights.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;3-1) Navigate to the APIM instance → Left menu &lt;strong&gt;Monitoring&lt;/strong&gt; → &lt;strong&gt;Application Insights&lt;/strong&gt; → Click &lt;strong&gt;+ Add&lt;/strong&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;img class="gallery-image" data-flex-basis="440px" data-flex-grow="183" height="1451" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/3-1-bind-apim-logger.png" srcset="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/3-1-bind-apim-logger_hu_cf14459b12773009.png 800w, https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/3-1-bind-apim-logger_hu_8a6c1a171bfb24d4.png 1600w, https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/3-1-bind-apim-logger_hu_77c3530864ca1fd1.png 2400w, https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/3-1-bind-apim-logger.png 2665w" width="2665"&gt;&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th&gt;Configuration&lt;/th&gt;
 &lt;th&gt;Recommended Value&lt;/th&gt;
 &lt;th&gt;Description&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td&gt;Application Insights instance&lt;/td&gt;
 &lt;td&gt;&lt;code&gt;ai-hub-app-insight&lt;/code&gt;&lt;/td&gt;
 &lt;td&gt;Select the Application Insights instance just created&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;☐ Use as default and enable basic logging for all APIs&lt;/td&gt;
 &lt;td&gt;&lt;strong&gt;Unchecked&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;When checked, this Logger becomes the global default, enabling basic logging for all APIs. Can reduce throughput by 40-50% under high traffic (&amp;gt;1000 req/s). Recommended to enable per-API in Step 4&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;☐ Add availability monitor&lt;/td&gt;
 &lt;td&gt;&lt;strong&gt;Unchecked&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;When checked, App Insights periodically validates whether the APIM gateway endpoint is responding, with results displayed in App Insights&amp;rsquo; Availability panel. Not needed for this scenario&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;h3 id="4-configure-api-diagnostic-logs"&gt;4. Configure API Diagnostic Logs
&lt;/h3&gt;&lt;p&gt;Enable Application Insights logging for the target AI API, configuring sampling rate, error recording, Backend Response, and other parameters.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;4-1) Navigate to APIM → APIs → Select target API → Switch to &lt;strong&gt;Settings&lt;/strong&gt; tab → Scroll down to &lt;strong&gt;Diagnostic Logs&lt;/strong&gt; area, click Enable&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;img class="gallery-image" data-flex-basis="370px" data-flex-grow="154" height="1715" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/4-1-api-settings-diagnostic-logs.png" srcset="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/4-1-api-settings-diagnostic-logs_hu_6a90258f10a6380d.png 800w, https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/4-1-api-settings-diagnostic-logs_hu_79eb34df8dfd1a1c.png 1600w, https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/4-1-api-settings-diagnostic-logs_hu_7fe7c175d99acd5b.png 2400w, https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/4-1-api-settings-diagnostic-logs.png 2647w" width="2647"&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;4-2) Select the &lt;strong&gt;Application Insights&lt;/strong&gt; tab, check &lt;strong&gt;Enable&lt;/strong&gt;, and configure with the following parameters&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;img class="gallery-image" data-flex-basis="267px" data-flex-grow="111" height="1212" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/4-2-diagnostic-logs-config.png" srcset="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/4-2-diagnostic-logs-config_hu_872049444957e930.png 800w, https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/4-2-diagnostic-logs-config.png 1350w" width="1350"&gt;&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th&gt;Configuration&lt;/th&gt;
 &lt;th&gt;Recommended Value&lt;/th&gt;
 &lt;th&gt;Description&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;strong&gt;Destination&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;&lt;code&gt;ai-hub-app-insight&lt;/code&gt;&lt;/td&gt;
 &lt;td&gt;Select the bound App Insights instance. ⚠️ If it shows &amp;ldquo;No App Insights were configured&amp;rdquo;, go back to Step 3 to bind the Logger first&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;strong&gt;Sampling (%)&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;&lt;code&gt;100&lt;/code&gt;&lt;/td&gt;
 &lt;td&gt;Sampling rate, 100 for full recording (can be adjusted to 25-50 for production)&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;strong&gt;Always log errors&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;✅ Enable&lt;/td&gt;
 &lt;td&gt;Ensures error requests are not filtered by sampling — all 4xx/5xx are recorded even when Sampling &amp;lt; 100%&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;strong&gt;Log client IP address&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;✅ Enable&lt;/td&gt;
 &lt;td&gt;Records caller IP, useful for troubleshooting&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;strong&gt;Support custom metrics&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;Unchecked&lt;/td&gt;
 &lt;td&gt;Only needed when using the &lt;code&gt;emit-metric&lt;/code&gt; policy, not used in this article&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;strong&gt;Verbosity&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;⚠️ &lt;code&gt;Information&lt;/code&gt;&lt;/td&gt;
 &lt;td&gt;Controls the logging level for native telemetry, &lt;strong&gt;recommended to select Information or Verbose&lt;/strong&gt; (see explanation below)&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;strong&gt;Correlation protocol&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;&lt;code&gt;W3C&lt;/code&gt;&lt;/td&gt;
 &lt;td&gt;Modern standard, supports cross-service distributed tracing using &lt;code&gt;traceparent&lt;/code&gt; header to correlate frontend and backend requests&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;&lt;strong&gt;Verbosity Configuration Notes:&lt;/strong&gt;&lt;/p&gt;

 &lt;blockquote&gt;
 &lt;p&gt;&lt;strong&gt;❗ Important:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Verbosity should be set to &lt;code&gt;Information&lt;/code&gt; or &lt;code&gt;Verbose&lt;/code&gt;, not &lt;code&gt;Error&lt;/code&gt;.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Verbosity controls the logging level for the entire request chain&lt;/strong&gt; — successful final requests (200) are treated as information level, failed requests (4xx/5xx) are treated as error level.&lt;/p&gt;

 &lt;/blockquote&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th&gt;Verbosity&lt;/th&gt;
 &lt;th&gt;Final Request Successful (200)&lt;/th&gt;
 &lt;th&gt;Final Request Failed (4xx/5xx)&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;strong&gt;Information&lt;/strong&gt; ✅&lt;/td&gt;
 &lt;td&gt;✅ &lt;code&gt;requests&lt;/code&gt; recorded + ✅ All &lt;code&gt;dependencies&lt;/code&gt; recorded (including intermediate Retry failures)&lt;/td&gt;
 &lt;td&gt;✅ &lt;code&gt;requests&lt;/code&gt; recorded + ✅ All &lt;code&gt;dependencies&lt;/code&gt; recorded&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;strong&gt;Error&lt;/strong&gt; ❌&lt;/td&gt;
 &lt;td&gt;❌ &lt;strong&gt;Nothing recorded&lt;/strong&gt; (both request and dependencies are lost)&lt;/td&gt;
 &lt;td&gt;✅ &lt;code&gt;requests&lt;/code&gt; recorded + ✅ Associated &lt;code&gt;dependencies&lt;/code&gt; recorded&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;

 &lt;blockquote&gt;
 &lt;p&gt;&lt;strong&gt;⚠️ Warning:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;When Verbosity=Error:&lt;/strong&gt; If 2 out of 3 Retry attempts fail (404) but the 3rd attempt succeeds (200), the entire request chain &lt;strong&gt;will not appear in any table&lt;/strong&gt; — including those 2 intermediate failed dependency records. This creates a blind spot for Retry observability.&lt;/p&gt;
&lt;p&gt;Selecting &lt;code&gt;Information&lt;/code&gt; ensures that regardless of whether the final result is success or failure, all Retry processes are fully recorded.&lt;/p&gt;

 &lt;/blockquote&gt;
&lt;p&gt;&lt;strong&gt;Additional settings — Backend Response Configuration:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;This is the key configuration for achieving Retry observability. Check &lt;strong&gt;Backend Response&lt;/strong&gt;, configure Headers to log and Body bytes — APIM will include this information in the &lt;code&gt;Properties&lt;/code&gt; field of the &lt;code&gt;dependencies&lt;/code&gt; table.&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th&gt;Configuration&lt;/th&gt;
 &lt;th&gt;Recommended Value&lt;/th&gt;
 &lt;th&gt;Description&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;strong&gt;Headers to log&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;&lt;code&gt;apim-request-id&lt;/code&gt;&lt;/td&gt;
 &lt;td&gt;Records the request tracking ID returned by Foundry, used for locating backend requests when submitting support tickets to Microsoft&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;strong&gt;Body bytes to log&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;&lt;code&gt;1024&lt;/code&gt;&lt;/td&gt;
 &lt;td&gt;Records the first 1KB of response body, capturing error JSON details (e.g., &lt;code&gt;DeploymentNotFound&lt;/code&gt;, &lt;code&gt;Too Many Requests&lt;/code&gt;, etc.)&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;

 &lt;blockquote&gt;
 &lt;p&gt;&lt;strong&gt;📝 Note:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Headers to log and Body bytes apply to &lt;strong&gt;all sampled requests&lt;/strong&gt; (including successful ones), without distinguishing between success and failure. This data appears in two App Insights tables:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;requests&lt;/code&gt; table&lt;/strong&gt; — Records the final request returned to the client (frontend perspective)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;dependencies&lt;/code&gt; table&lt;/strong&gt; — Records each &lt;code&gt;forward-request&lt;/code&gt; call (backend perspective, &lt;strong&gt;including each intermediate Retry attempt&lt;/strong&gt;)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The &lt;code&gt;dependencies&lt;/code&gt; table is the core data source for Retry observability.&lt;/p&gt;

 &lt;/blockquote&gt;
&lt;p&gt;Click &lt;strong&gt;Save&lt;/strong&gt; to complete the configuration.&lt;/p&gt;

 &lt;blockquote&gt;
 &lt;p&gt;&lt;strong&gt;⚠️ Warning:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Configuration changes take &lt;strong&gt;several minutes&lt;/strong&gt; to propagate to APIM gateway instances. If you test immediately and see no data, please wait 5 minutes before checking.&lt;/p&gt;

 &lt;/blockquote&gt;
&lt;h3 id="5-verify-data"&gt;5. Verify Data
&lt;/h3&gt;&lt;p&gt;After configuration is complete, send a few test requests that will trigger Retry (e.g., call a non-existent model deployment), wait approximately 5 minutes, then verify data in Application Insights.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;5-1) Navigate to Application Insights → Monitoring → ① Logs, ② Switch to Tables view, ③ Find the &lt;code&gt;dependencies&lt;/code&gt; table under the Application Insights category and click Run&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;img class="gallery-image" data-flex-basis="357px" data-flex-grow="149" height="1691" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/5-1-dependencies-retry-records.png" srcset="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/5-1-dependencies-retry-records_hu_9e2b1c9bfe29f3e2.png 800w, https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/5-1-dependencies-retry-records_hu_2d9636b048ff0ea2.png 1600w, https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/5-1-dependencies-retry-records_hu_8f8d132eb0a3836c.png 2400w, https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/5-1-dependencies-retry-records.png 2522w" width="2522"&gt;&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;dependencies&lt;/code&gt; table generates an independent record for &lt;strong&gt;each &lt;code&gt;forward-request&lt;/code&gt; call&lt;/strong&gt; within the &lt;code&gt;retry&lt;/code&gt; block — including intermediate failed Retry attempts and the final successful request. In the results on the right, you can see multiple records pointing to different backends (e.g., &lt;code&gt;ai-hub-swedencentral-02&lt;/code&gt;, &lt;code&gt;ai-hub-eastus2-01&lt;/code&gt;, etc.), all belonging to the same request&amp;rsquo;s Retry chain.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;5-2) Expand a single record to view detailed fields and customDimensions&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;img class="gallery-image" data-flex-basis="380px" data-flex-grow="158" height="1779" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/5-2-dependency-custom-dimensions.png" srcset="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/5-2-dependency-custom-dimensions_hu_3ec02de0c7557683.png 800w, https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/5-2-dependency-custom-dimensions_hu_da9d711ed95de465.png 1600w, https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/5-2-dependency-custom-dimensions_hu_4b398a8a1c40a260.png 2400w, https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/5-2-dependency-custom-dimensions.png 2824w" width="2824"&gt;&lt;/p&gt;
&lt;p&gt;After expanding, you can see the complete information of the dependency record, with key troubleshooting fields highlighted:&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th&gt;Field&lt;/th&gt;
 &lt;th&gt;Description&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;strong&gt;&lt;code&gt;target&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;Full address of the requested backend&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;strong&gt;&lt;code&gt;resultCode&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;HTTP status code&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;code&gt;duration&lt;/code&gt;&lt;/td&gt;
 &lt;td&gt;Call duration&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;code&gt;success&lt;/code&gt;&lt;/td&gt;
 &lt;td&gt;Whether successful&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;strong&gt;&lt;code&gt;Response-Body&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;Success/error response JSON details&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;strong&gt;&lt;code&gt;Response-apim-request-id&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;Foundry-side request ID (different for each Retry), useful for submitting troubleshooting tickets&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;

 &lt;blockquote&gt;
 &lt;p&gt;&lt;strong&gt;💡 Tip:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Use &lt;code&gt;operation_Id&lt;/code&gt; to link all Retry records for the same request together, reconstructing the complete retry chain. &lt;code&gt;Response-apim-request-id&lt;/code&gt; can be used to locate specific backend requests when submitting support tickets to Microsoft.&lt;/p&gt;

 &lt;/blockquote&gt;
&lt;h3 id="6-configure-alert-rules"&gt;6. Configure Alert Rules
&lt;/h3&gt;&lt;p&gt;Automatically notify the operations team via email when Retry failure counts exceed thresholds. Alert rules are created on &lt;strong&gt;Application Insights&lt;/strong&gt; (not APIM).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;6-1) Write and verify KQL query on the Logs page&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Navigate to Application Insights → Monitoring → &lt;strong&gt;Logs&lt;/strong&gt;, ① create a new query tab, ② switch to &lt;strong&gt;KQL mode&lt;/strong&gt;, ③ enter the following query and ④ click &lt;strong&gt;Run&lt;/strong&gt; to verify results:&lt;/p&gt;
&lt;p&gt;&lt;img class="gallery-image" data-flex-basis="375px" data-flex-grow="156" height="1172" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-1-kql-query-in-logs.png" srcset="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-1-kql-query-in-logs_hu_642be3aedad56c4.png 800w, https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-1-kql-query-in-logs_hu_cc2065b85fea4e56.png 1600w, https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-1-kql-query-in-logs.png 1836w" width="1836"&gt;&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;span class="lnt"&gt;4
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-fallback" data-lang="fallback"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;dependencies
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;| where success == false
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;| where resultCode in (&amp;#34;429&amp;#34;, &amp;#34;500&amp;#34;, &amp;#34;502&amp;#34;, &amp;#34;503&amp;#34;, &amp;#34;504&amp;#34;) or toint(resultCode) &amp;gt;= 400
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;| summarize FailureCount = count()
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;Adjust the status code filter as needed:&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th&gt;Scenario&lt;/th&gt;
 &lt;th&gt;Filter Condition&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td&gt;All failures&lt;/td&gt;
 &lt;td&gt;&lt;code&gt;where success == false&lt;/code&gt;&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Rate limiting only (429)&lt;/td&gt;
 &lt;td&gt;&lt;code&gt;where resultCode == &amp;quot;429&amp;quot;&lt;/code&gt;&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;5xx server errors only&lt;/td&gt;
 &lt;td&gt;&lt;code&gt;where toint(resultCode) &amp;gt;= 500&lt;/code&gt;&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;429 + 5xx&lt;/td&gt;
 &lt;td&gt;&lt;code&gt;where resultCode == &amp;quot;429&amp;quot; or toint(resultCode) &amp;gt;= 500&lt;/code&gt;&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Exclude 404&lt;/td&gt;
 &lt;td&gt;&lt;code&gt;where success == false and resultCode != &amp;quot;404&amp;quot;&lt;/code&gt;&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;Confirm the query returns the expected FailureCount value.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;6-2) Click the &lt;strong&gt;&lt;code&gt;...&lt;/code&gt;&lt;/strong&gt; menu in the top right → &lt;strong&gt;+ New alert rule&lt;/strong&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;img class="gallery-image" data-flex-basis="342px" data-flex-grow="142" height="647" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-2-new-alert-rule.png" srcset="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-2-new-alert-rule_hu_cc3b6550b8d4b269.png 800w, https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-2-new-alert-rule.png 922w" width="922"&gt;&lt;/p&gt;
&lt;p&gt;The system will automatically carry over the current KQL query into the Alert rule&amp;rsquo;s Condition configuration.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;6-3) Confirm query and Measurement configuration on the Condition tab&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;img class="gallery-image" data-flex-basis="267px" data-flex-grow="111" height="1473" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-3-alert-condition-measurement.png" srcset="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-3-alert-condition-measurement_hu_581e6c3507c648d5.png 800w, https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-3-alert-condition-measurement_hu_918929b7c5602c7b.png 1600w, https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-3-alert-condition-measurement.png 1642w" width="1642"&gt;&lt;/p&gt;
&lt;p&gt;Three key configurations highlighted in the screenshot:&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th&gt;Annotation&lt;/th&gt;
 &lt;th&gt;Configuration&lt;/th&gt;
 &lt;th&gt;Value&lt;/th&gt;
 &lt;th&gt;Description&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td&gt;①&lt;/td&gt;
 &lt;td&gt;&lt;strong&gt;Search query&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;KQL from previous step (auto-populated)&lt;/td&gt;
 &lt;td&gt;Contains &lt;code&gt;success == false&lt;/code&gt; + status code filter + &lt;code&gt;summarize FailureCount = count()&lt;/code&gt;&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;②&lt;/td&gt;
 &lt;td&gt;&lt;strong&gt;Measure&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;&lt;code&gt;FailureCount&lt;/code&gt;&lt;/td&gt;
 &lt;td&gt;⚠️ Must select &lt;code&gt;FailureCount&lt;/code&gt; (auto-detected from KQL&amp;rsquo;s summarize). If it defaults to &lt;code&gt;Table rows&lt;/code&gt;, switch manually&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;③&lt;/td&gt;
 &lt;td&gt;&lt;strong&gt;Aggregation granularity&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;&lt;code&gt;3 hours&lt;/code&gt;&lt;/td&gt;
 &lt;td&gt;Aggregation granularity — aggregates FailureCount within this time range into a single data point for evaluation&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;Other settings keep defaults:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Query type&lt;/strong&gt;: Aggregated logs&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Aggregation type&lt;/strong&gt;: Total (sum of FailureCount)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;6-4) Scroll down to configure Alert logic&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;img class="gallery-image" data-flex-basis="358px" data-flex-grow="149" height="1113" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-4-alert-logic-threshold.png" srcset="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-4-alert-logic-threshold_hu_e8c567663cf0217.png 800w, https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-4-alert-logic-threshold_hu_10bae5d7c67dab9d.png 1600w, https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-4-alert-logic-threshold.png 1661w" width="1661"&gt;&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th&gt;Configuration&lt;/th&gt;
 &lt;th&gt;Value&lt;/th&gt;
 &lt;th&gt;Description&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td&gt;Threshold type&lt;/td&gt;
 &lt;td&gt;&lt;strong&gt;Static&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;Use fixed threshold&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Operator&lt;/td&gt;
 &lt;td&gt;&lt;strong&gt;Greater than&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;Trigger when FailureCount exceeds threshold&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Threshold value&lt;/td&gt;
 &lt;td&gt;&lt;code&gt;10&lt;/code&gt;&lt;/td&gt;
 &lt;td&gt;Adjust based on business tolerance&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Frequency of evaluation&lt;/td&gt;
 &lt;td&gt;&lt;code&gt;15 minutes&lt;/code&gt;&lt;/td&gt;
 &lt;td&gt;Evaluate every 15 minutes&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;

 &lt;blockquote&gt;
 &lt;p&gt;&lt;strong&gt;📝 Note:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;📊 The &lt;strong&gt;Preview&lt;/strong&gt; chart below displays the FailureCount trend and threshold line (dashed), helping you assess whether the threshold setting is reasonable. Areas where the blue solid line exceeds the dashed threshold line indicate periods that would trigger alerts.&lt;/p&gt;
&lt;p&gt;💰 Estimated monthly cost is approximately &lt;strong&gt;$0.50 USD&lt;/strong&gt; (depends on evaluation frequency).&lt;/p&gt;

 &lt;/blockquote&gt;
&lt;p&gt;Click &lt;strong&gt;Next: Actions &amp;gt;&lt;/strong&gt; to proceed.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;6-5) Configure alert actions on the Actions tab&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;If the Portal displays the &lt;strong&gt;Use quick actions (preview)&lt;/strong&gt; option (new UI), you can complete Action Group creation directly on this page without creating one separately:&lt;/p&gt;
&lt;p&gt;&lt;img class="gallery-image" data-flex-basis="387px" data-flex-grow="161" height="1521" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-5-quick-actions-preview.png" srcset="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-5-quick-actions-preview_hu_e24a82964c1c43b7.png 800w, https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-5-quick-actions-preview_hu_32781c44552b9774.png 1600w, https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-5-quick-actions-preview_hu_ef08194da7af03db.png 2400w, https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-5-quick-actions-preview.png 2453w" width="2453"&gt;&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th&gt;Annotation&lt;/th&gt;
 &lt;th&gt;Configuration&lt;/th&gt;
 &lt;th&gt;Description&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td&gt;①&lt;/td&gt;
 &lt;td&gt;&lt;strong&gt;Use quick actions (preview)&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;Select this option, and the Portal will automatically create an Action Group&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;②&lt;/td&gt;
 &lt;td&gt;&lt;strong&gt;Quick actions panel&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;Fill in Action group name, Display name, check Email and enter email address → &lt;strong&gt;Save&lt;/strong&gt;&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;③&lt;/td&gt;
 &lt;td&gt;&lt;strong&gt;Email subject&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;Custom alert email subject, e.g., &lt;code&gt;AI Hub Retry Alert&lt;/code&gt;&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;

 &lt;blockquote&gt;
 &lt;p&gt;&lt;strong&gt;📝 Note:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;If the Portal doesn&amp;rsquo;t show the quick actions option (legacy UI), select &lt;strong&gt;Use action groups&lt;/strong&gt; → &lt;strong&gt;+ Create action group&lt;/strong&gt;, and follow steps 6-6 ~ 6-8 below to create one manually.&lt;/p&gt;

 &lt;/blockquote&gt;
&lt;p&gt;&lt;strong&gt;6-6) Fill in Action Group basic information&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;img class="gallery-image" data-flex-basis="449px" data-flex-grow="187" height="843" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-6-action-group-basics.png" srcset="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-6-action-group-basics_hu_7bf86028e786560d.png 800w, https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-6-action-group-basics.png 1578w" width="1578"&gt;&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th&gt;Configuration&lt;/th&gt;
 &lt;th&gt;Value&lt;/th&gt;
 &lt;th&gt;Description&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td&gt;Region&lt;/td&gt;
 &lt;td&gt;&lt;strong&gt;Global&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;Recommended to select Global for Action Groups&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Action group name&lt;/td&gt;
 &lt;td&gt;&lt;code&gt;ai-hub-retry-alert-action&lt;/code&gt;&lt;/td&gt;
 &lt;td&gt;Unique identifier&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Display name&lt;/td&gt;
 &lt;td&gt;&lt;code&gt;AI-Hub-Retry&lt;/code&gt;&lt;/td&gt;
 &lt;td&gt;Limited to 12 characters, displayed in notifications&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;&lt;strong&gt;6-7) Switch to the Notifications tab and configure email notifications&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;img class="gallery-image" data-flex-basis="519px" data-flex-grow="216" height="1056" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-7-action-group-notifications.png" srcset="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-7-action-group-notifications_hu_f73f8c20a5afca11.png 800w, https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-7-action-group-notifications_hu_7e3cabadfe52490a.png 1600w, https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-7-action-group-notifications.png 2286w" width="2286"&gt;&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th&gt;Configuration&lt;/th&gt;
 &lt;th&gt;Value&lt;/th&gt;
 &lt;th&gt;Description&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td&gt;Notification type&lt;/td&gt;
 &lt;td&gt;&lt;strong&gt;Email/SMS message/Push/Voice&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;Select notification method&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Name&lt;/td&gt;
 &lt;td&gt;Recipient name&lt;/td&gt;
 &lt;td&gt;Notification recipient identifier&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Email&lt;/td&gt;
 &lt;td&gt;✅ Check and enter email&lt;/td&gt;
 &lt;td&gt;Alert email recipient address&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;

 &lt;blockquote&gt;
 &lt;p&gt;&lt;strong&gt;📝 Note:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The Actions tab allows configuration of richer automated actions (Automation Runbook, Azure Function, Logic App, Webhook, etc.). These are not used in this article — simply skip.&lt;/p&gt;
&lt;p&gt;&lt;img class="gallery-image" data-flex-basis="421px" data-flex-grow="175" height="816" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-8-action-group-actions.png" srcset="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-8-action-group-actions_hu_c23fee3f12422516.png 800w, https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-8-action-group-actions.png 1434w" width="1434"&gt;&lt;/p&gt;

 &lt;/blockquote&gt;
&lt;p&gt;&lt;strong&gt;6-8) Switch to the Review + create tab, confirm configuration and click &lt;strong&gt;Create&lt;/strong&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;img class="gallery-image" data-flex-basis="282px" data-flex-grow="117" height="1348" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-8-action-group-review-create.png" srcset="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-8-action-group-review-create_hu_ed0653e8c8c38395.png 800w, https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-8-action-group-review-create.png 1584w" width="1584"&gt;&lt;/p&gt;
&lt;p&gt;After creation, you&amp;rsquo;ll receive an Azure confirmation email indicating you&amp;rsquo;ve been added to the Action Group:&lt;/p&gt;
&lt;p&gt;&lt;img class="gallery-image" data-flex-basis="333px" data-flex-grow="138" height="849" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-8-action-group-email-confirm.png" srcset="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-8-action-group-email-confirm_hu_113108bed65e44e4.png 800w, https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-8-action-group-email-confirm.png 1180w" width="1180"&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;6-9) Return to the Alert rule&amp;rsquo;s Actions page, confirm the Action Group is associated, and fill in the email subject&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;img class="gallery-image" data-flex-basis="279px" data-flex-grow="116" height="1351" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-9-alert-actions-email-subject.png" srcset="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-9-alert-actions-email-subject_hu_4deac364262f2076.png 800w, https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-9-alert-actions-email-subject.png 1576w" width="1576"&gt;&lt;/p&gt;
&lt;p&gt;Enter a custom alert email subject in the &lt;strong&gt;Email subject&lt;/strong&gt; field, e.g., &lt;code&gt;AI Hub Retry Alert&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;6-10) Switch to the Details tab and fill in Alert rule basic information&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;img class="gallery-image" data-flex-basis="282px" data-flex-grow="117" height="1355" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-10-alert-details.png" srcset="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-10-alert-details_hu_dbcef4146041b689.png 800w, https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-10-alert-details.png 1595w" width="1595"&gt;&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th&gt;Configuration&lt;/th&gt;
 &lt;th&gt;Value&lt;/th&gt;
 &lt;th&gt;Description&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td&gt;Severity&lt;/td&gt;
 &lt;td&gt;&lt;strong&gt;2 - Warning&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;Alert severity level&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Alert rule name&lt;/td&gt;
 &lt;td&gt;&lt;code&gt;AI-Hub-Alert&lt;/code&gt;&lt;/td&gt;
 &lt;td&gt;Alert rule name&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Region&lt;/td&gt;
 &lt;td&gt;&lt;code&gt;Japan East&lt;/code&gt;&lt;/td&gt;
 &lt;td&gt;Same region as App Insights&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Identity&lt;/td&gt;
 &lt;td&gt;&lt;strong&gt;Default&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;Use default identity&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;&lt;strong&gt;6-11) Switch to Review + create to confirm all configurations, click &lt;strong&gt;Create&lt;/strong&gt; to complete&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;img class="gallery-image" data-flex-basis="218px" data-flex-grow="91" height="1342" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-11-alert-review-create.png" srcset="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-11-alert-review-create_hu_a50e8bf7c746406e.png 800w, https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-11-alert-review-create.png 1222w" width="1222"&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;6-12) Verify alert email&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Approximately 15 minutes after creation, when FailureCount exceeds the threshold (10), you&amp;rsquo;ll receive an alert email:&lt;/p&gt;
&lt;p&gt;&lt;img class="gallery-image" data-flex-basis="326px" data-flex-grow="136" height="1123" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-12-alert-email-notification.png" srcset="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-12-alert-email-notification_hu_633484e3ccfa6737.png 800w, https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-12-alert-email-notification.png 1529w" width="1529"&gt;&lt;/p&gt;
&lt;p&gt;The email contains key information:&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th&gt;Field&lt;/th&gt;
 &lt;th&gt;Value&lt;/th&gt;
 &lt;th&gt;Description&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td&gt;Alert name&lt;/td&gt;
 &lt;td&gt;&lt;code&gt;AI-Hub-Alert&lt;/code&gt;&lt;/td&gt;
 &lt;td&gt;Name of the triggered alert rule&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Severity&lt;/td&gt;
 &lt;td&gt;Warning&lt;/td&gt;
 &lt;td&gt;Alert level&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Metric value&lt;/td&gt;
 &lt;td&gt;15&lt;/td&gt;
 &lt;td&gt;Actual FailureCount&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Threshold&lt;/td&gt;
 &lt;td&gt;10&lt;/td&gt;
 &lt;td&gt;Configured threshold&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Fired time&lt;/td&gt;
 &lt;td&gt;Specific time (UTC)&lt;/td&gt;
 &lt;td&gt;When the alert was triggered&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;Click &lt;strong&gt;View alert details&lt;/strong&gt; in the email to jump directly to Azure Portal for details; &lt;strong&gt;View search results&lt;/strong&gt; to view the KQL query results that triggered the alert.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;6-13) (Optional) Fine-tune alert parameters&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;After the alert rule is created, navigate to Application Insights → Monitoring → &lt;strong&gt;Alerts&lt;/strong&gt; → &lt;strong&gt;Alert rules&lt;/strong&gt; → Select the corresponding rule → Click the &lt;strong&gt;Edit&lt;/strong&gt; button:&lt;/p&gt;
&lt;p&gt;&lt;img class="gallery-image" data-flex-basis="712px" data-flex-grow="297" height="606" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-13-alert-edit-button.png" srcset="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-13-alert-edit-button_hu_caf882434c1c375a.png 800w, https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-13-alert-edit-button_hu_3f71a1ceb886ff91.png 1600w, https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-13-alert-edit-button.png 1800w" width="1800"&gt;&lt;/p&gt;
&lt;p&gt;In the editing page&amp;rsquo;s &lt;strong&gt;Details tab → Advanced options&lt;/strong&gt;, fine-tune further:&lt;/p&gt;
&lt;p&gt;&lt;img class="gallery-image" data-flex-basis="452px" data-flex-grow="188" height="791" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-13-alert-advanced-mute.png" srcset="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-13-alert-advanced-mute_hu_42206efbc0e4a856.png 800w, https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-13-alert-advanced-mute.png 1491w" width="1491"&gt;&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th&gt;Configuration&lt;/th&gt;
 &lt;th&gt;Description&lt;/th&gt;
 &lt;th&gt;Recommended Value&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;strong&gt;Mute actions&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;Enter a silent period after triggering, during which no repeat notifications are sent&lt;/td&gt;
 &lt;td&gt;✅ Check&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;strong&gt;Mute actions for&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;Silent duration — no notifications during this period even if conditions persist&lt;/td&gt;
 &lt;td&gt;&lt;code&gt;3 hours&lt;/code&gt; (adjust as needed)&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;strong&gt;Automatically resolve alerts&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;Automatically change alert status from Fired to Resolved when conditions are no longer met&lt;/td&gt;
 &lt;td&gt;Check as needed&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;

 &lt;blockquote&gt;
 &lt;p&gt;&lt;strong&gt;💡 Tip:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;If the evaluation frequency is 15 minutes and Mute is not configured, &lt;strong&gt;an email notification will be sent every 15 minutes&lt;/strong&gt; while alert conditions persist. Configuring Mute prevents repeated notifications after the first alert trigger, avoiding email flooding.&lt;/p&gt;
&lt;p&gt;Additionally, you can adjust &lt;strong&gt;Aggregation granularity&lt;/strong&gt;, &lt;strong&gt;Threshold value&lt;/strong&gt;, &lt;strong&gt;Frequency of evaluation&lt;/strong&gt;, and other parameters on the editing page to optimize alert sensitivity.&lt;/p&gt;

 &lt;/blockquote&gt;
&lt;h3 id="7-appendix-advanced-kql-queries-and-client-ip-configuration-optional"&gt;7. Appendix: Advanced KQL Queries and Client IP Configuration (Optional)
&lt;/h3&gt;&lt;h4 id="7-1-enable-real-client-ip-recording"&gt;7-1) Enable Real Client IP Recording
&lt;/h4&gt;&lt;p&gt;Application Insights &lt;strong&gt;does not store client IP addresses by default&lt;/strong&gt; (privacy protection), and the &lt;code&gt;client_IP&lt;/code&gt; field displays as &lt;code&gt;0.0.0.0&lt;/code&gt;. Disable IP masking via CLI to record real IPs:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;span class="lnt"&gt;4
&lt;/span&gt;&lt;span class="lnt"&gt;5
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;az resource update &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --resource-group &amp;lt;resource-group&amp;gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --name &amp;lt;app-insights-name&amp;gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --resource-type &lt;span class="s2"&gt;&amp;#34;Microsoft.Insights/components&amp;#34;&lt;/span&gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --set properties.DisableIpMasking&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nb"&gt;true&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
 &lt;blockquote&gt;
 &lt;p&gt;&lt;strong&gt;⚠️ Warning:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Disabling IP masking will record real client IP addresses. Please ensure this complies with your organization&amp;rsquo;s privacy requirements.&lt;/p&gt;
&lt;p&gt;📖 Reference: Geolocation and IP address handling: &lt;a class="link" href="https://learn.microsoft.com/azure/azure-monitor/app/ip-collection" target="_blank" rel="noopener"
 &gt;https://learn.microsoft.com/azure/azure-monitor/app/ip-collection&lt;/a&gt;&lt;/p&gt;

 &lt;/blockquote&gt;
&lt;h4 id="7-2-correlate-client-ip-with-retry-failure-records"&gt;7-2) Correlate Client IP with Retry Failure Records
&lt;/h4&gt;&lt;p&gt;The &lt;code&gt;dependencies&lt;/code&gt; table itself does not contain client IP (it records APIM → backend outbound calls). You need to JOIN with the &lt;code&gt;requests&lt;/code&gt; table via &lt;code&gt;operation_Id&lt;/code&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt; 1
&lt;/span&gt;&lt;span class="lnt"&gt; 2
&lt;/span&gt;&lt;span class="lnt"&gt; 3
&lt;/span&gt;&lt;span class="lnt"&gt; 4
&lt;/span&gt;&lt;span class="lnt"&gt; 5
&lt;/span&gt;&lt;span class="lnt"&gt; 6
&lt;/span&gt;&lt;span class="lnt"&gt; 7
&lt;/span&gt;&lt;span class="lnt"&gt; 8
&lt;/span&gt;&lt;span class="lnt"&gt; 9
&lt;/span&gt;&lt;span class="lnt"&gt;10
&lt;/span&gt;&lt;span class="lnt"&gt;11
&lt;/span&gt;&lt;span class="lnt"&gt;12
&lt;/span&gt;&lt;span class="lnt"&gt;13
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-fallback" data-lang="fallback"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;dependencies
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;| where timestamp &amp;gt; ago(1h)
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;| where success == false
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;| join kind=leftouter (
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; requests 
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; | project operation_Id, req_client_IP = client_IP
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; ) on operation_Id
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;| project timestamp, target, resultCode, 
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; ClientIP = req_client_IP,
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; ErrorBody = tostring(customDimensions[&amp;#34;Response-Body&amp;#34;]),
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; APIMRequestId = tostring(customDimensions[&amp;#34;Response-apim-request-id&amp;#34;]),
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; operation_Id
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;| order by timestamp desc
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
 &lt;blockquote&gt;
 &lt;p&gt;&lt;strong&gt;📝 Note:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Both &lt;code&gt;requests&lt;/code&gt; and &lt;code&gt;dependencies&lt;/code&gt; tables have a &lt;code&gt;client_IP&lt;/code&gt; field, but with different meanings:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;requests.client_IP&lt;/code&gt;&lt;/strong&gt; — IP of the client calling APIM (real client)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;dependencies.client_IP&lt;/code&gt;&lt;/strong&gt; — APIM gateway&amp;rsquo;s outbound IP (not the client)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;When JOINing, use renaming (e.g., &lt;code&gt;req_client_IP&lt;/code&gt;) to avoid field conflicts.&lt;/p&gt;

 &lt;/blockquote&gt;
&lt;h4 id="7-3-more-useful-kql-queries"&gt;7-3) More Useful KQL Queries
&lt;/h4&gt;&lt;p&gt;&lt;strong&gt;Reconstruct complete Retry chain by operation_Id (including the successful attempt):&lt;/strong&gt;&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;span class="lnt"&gt;4
&lt;/span&gt;&lt;span class="lnt"&gt;5
&lt;/span&gt;&lt;span class="lnt"&gt;6
&lt;/span&gt;&lt;span class="lnt"&gt;7
&lt;/span&gt;&lt;span class="lnt"&gt;8
&lt;/span&gt;&lt;span class="lnt"&gt;9
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-fallback" data-lang="fallback"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;dependencies
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;| where timestamp &amp;gt; ago(1h)
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;| summarize CallCount = count(),
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; Backends = make_list(target),
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; StatusCodes = make_list(resultCode),
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; Durations = make_list(duration)
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; by operation_Id
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;| where CallCount &amp;gt; 1
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;| order by CallCount desc
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;&lt;strong&gt;Failure trend chart:&lt;/strong&gt;&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;span class="lnt"&gt;4
&lt;/span&gt;&lt;span class="lnt"&gt;5
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-fallback" data-lang="fallback"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;dependencies
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;| where timestamp &amp;gt; ago(24h)
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;| where success == false
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;| summarize FailureCount = count() by bin(timestamp, 5m), target
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;| render timechart
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;&lt;strong&gt;For alerting (grouped by backend + status code):&lt;/strong&gt;&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;span class="lnt"&gt;4
&lt;/span&gt;&lt;span class="lnt"&gt;5
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-fallback" data-lang="fallback"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;dependencies
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;| where timestamp &amp;gt; ago(2h)
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;| where success == false
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;| summarize FailureCount = count() by target, resultCode
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;| where FailureCount &amp;gt; 0
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;h3 id="summary"&gt;Summary
&lt;/h3&gt;&lt;p&gt;Through this article&amp;rsquo;s implementation, we&amp;rsquo;ve built a complete Retry observability solution on top of the resilient gateway architecture from the previous article:&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th&gt;Capability&lt;/th&gt;
 &lt;th&gt;Implementation&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;strong&gt;Each Retry failure recorded&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;APIM native &lt;code&gt;dependencies&lt;/code&gt; table automatically records each &lt;code&gt;forward-request&lt;/code&gt; call&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;strong&gt;Error Body recording&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;Backend Response Body bytes=1024, error JSON details appear in Properties&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;strong&gt;Request tracking IDs&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;&lt;code&gt;operation_Id&lt;/code&gt; (unchanged across Retries) + &lt;code&gt;Response-apim-request-id&lt;/code&gt; (Foundry-side ID)&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;strong&gt;Call duration&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;Native &lt;code&gt;DurationMs&lt;/code&gt; field, precise to milliseconds&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;strong&gt;KQL queries&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;Support aggregation analysis by backend, status code, and time dimensions&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;strong&gt;Automated alerting&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;Log Alert rules + Action Group email notifications&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;For users who have already deployed the resilient architecture from the previous article, this article &lt;strong&gt;requires no Policy code changes&lt;/strong&gt; — complete Retry observability is achieved solely through APIM Diagnostic Logs configuration (binding App Insights + &lt;strong&gt;setting Verbosity to Information&lt;/strong&gt; + enabling Backend Response Headers/Body).&lt;/p&gt;
&lt;p&gt;Those errors hidden by the Retry mechanism are now clearly recorded in Application Insights&amp;rsquo; &lt;code&gt;dependencies&lt;/code&gt; table, ready for operations staff to review and address.&lt;/p&gt;
&lt;h3 id="references"&gt;References
&lt;/h3&gt;&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th&gt;Document&lt;/th&gt;
 &lt;th&gt;Link&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td&gt;How to integrate Azure API Management with Azure Application Insights&lt;/td&gt;
 &lt;td&gt;&lt;a class="link" href="https://learn.microsoft.com/azure/api-management/api-management-howto-app-insights" target="_blank" rel="noopener"
 &gt;https://learn.microsoft.com/azure/api-management/api-management-howto-app-insights&lt;/a&gt;&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Diagnostic - Create Or Update REST API&lt;/td&gt;
 &lt;td&gt;&lt;a class="link" href="https://learn.microsoft.com/rest/api/apimanagement/diagnostic/create-or-update" target="_blank" rel="noopener"
 &gt;https://learn.microsoft.com/rest/api/apimanagement/diagnostic/create-or-update&lt;/a&gt;&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Application Insights telemetry data model&lt;/td&gt;
 &lt;td&gt;&lt;a class="link" href="https://learn.microsoft.com/azure/azure-monitor/app/data-model-complete" target="_blank" rel="noopener"
 &gt;https://learn.microsoft.com/azure/azure-monitor/app/data-model-complete&lt;/a&gt;&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Geolocation and IP address handling (IP masking)&lt;/td&gt;
 &lt;td&gt;&lt;a class="link" href="https://learn.microsoft.com/azure/azure-monitor/app/ip-collection" target="_blank" rel="noopener"
 &gt;https://learn.microsoft.com/azure/azure-monitor/app/ip-collection&lt;/a&gt;&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Subscriptions in Azure API Management&lt;/td&gt;
 &lt;td&gt;&lt;a class="link" href="https://learn.microsoft.com/azure/api-management/api-management-subscriptions" target="_blank" rel="noopener"
 &gt;https://learn.microsoft.com/azure/api-management/api-management-subscriptions&lt;/a&gt;&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Tutorial: Create a log search alert for an Azure resource&lt;/td&gt;
 &lt;td&gt;&lt;a class="link" href="https://learn.microsoft.com/azure/azure-monitor/alerts/tutorial-log-alert" target="_blank" rel="noopener"
 &gt;https://learn.microsoft.com/azure/azure-monitor/alerts/tutorial-log-alert&lt;/a&gt;&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Create and manage action groups&lt;/td&gt;
 &lt;td&gt;&lt;a class="link" href="https://learn.microsoft.com/azure/azure-monitor/alerts/action-groups" target="_blank" rel="noopener"
 &gt;https://learn.microsoft.com/azure/azure-monitor/alerts/action-groups&lt;/a&gt;&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Kusto Query Language (KQL) overview&lt;/td&gt;
 &lt;td&gt;&lt;a class="link" href="https://learn.microsoft.com/azure/data-explorer/kusto/query/" target="_blank" rel="noopener"
 &gt;https://learn.microsoft.com/azure/data-explorer/kusto/query/&lt;/a&gt;&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Performance implications and log sampling&lt;/td&gt;
 &lt;td&gt;&lt;a class="link" href="https://learn.microsoft.com/azure/api-management/api-management-howto-app-insights#performance-implications-and-log-sampling" target="_blank" rel="noopener"
 &gt;https://learn.microsoft.com/azure/api-management/api-management-howto-app-insights#performance-implications-and-log-sampling&lt;/a&gt;&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Azure Monitor pricing&lt;/td&gt;
 &lt;td&gt;&lt;a class="link" href="https://azure.microsoft.com/pricing/details/monitor/" target="_blank" rel="noopener"
 &gt;https://azure.microsoft.com/pricing/details/monitor/&lt;/a&gt;&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;</description></item><item><title>Building Resilient Azure AI Architecture with API Management Gateway</title><link>https://robinsujob.github.io/p/azure-apim-ai-resilience/</link><pubDate>Fri, 03 Apr 2026 00:00:00 +0800</pubDate><guid>https://robinsujob.github.io/p/azure-apim-ai-resilience/</guid><description>&lt;img src="https://robinsujob.github.io/" alt="Featured image of post Building Resilient Azure AI Architecture with API Management Gateway" /&gt;&lt;p&gt;As enterprise demand for AI capabilities grows, organizations are deeply integrating Microsoft Foundry model services into core business systems. As AI moves from experimentation to production, &lt;strong&gt;high availability, elastic scaling, unified governance, and security controls&lt;/strong&gt; become unavoidable themes.&lt;/p&gt;
&lt;p&gt;In production, a single subscription instance fails to meet requirements for &lt;strong&gt;continuity, stability, and throughput&lt;/strong&gt;. A mature architecture consolidates resources across subscriptions, &lt;strong&gt;aggregating AI quotas&lt;/strong&gt; to maximize throughput, with traffic distribution, automatic failover, and unified access control.&lt;/p&gt;
&lt;p&gt;Azure API Management (APIM), as Azure&amp;rsquo;s native API gateway, is naturally suited for this role. By deploying APIM as the unified entry point, combined with the following capabilities, we can build an &lt;strong&gt;enterprise-grade, highly resilient AI architecture&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;🔀 &lt;strong&gt;Backend Load Balancing Pool&lt;/strong&gt; — Load balancing and failover across subscriptions and Foundry instances, aggregating distributed quotas&lt;/li&gt;
&lt;li&gt;🔐 &lt;strong&gt;Managed Identity&lt;/strong&gt; — Zero-key secure authentication between APIM and backend AI services&lt;/li&gt;
&lt;li&gt;🛡️ &lt;strong&gt;Unified API Governance&lt;/strong&gt; — Request routing, traffic control, log auditing, and policy management at the gateway layer&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This article walks through a complete hands-on example: from resource deployment and identity configuration to load balancing policies, building a production-ready resilient gateway architecture targeting multiple Microsoft Foundry resources.&lt;/p&gt;
&lt;h2 id="architecture-overview"&gt;Architecture Overview
&lt;/h2&gt;&lt;p&gt;&lt;img alt="1-Architecture" class="gallery-image" data-flex-basis="289px" data-flex-grow="120" height="683" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/azure-apim-ai-resilience/images/1-Architecture-en.png" srcset="https://robinsujob.github.io/p/azure-apim-ai-resilience/images/1-Architecture-en_hu_10ff4595295ffe34.png 800w, https://robinsujob.github.io/p/azure-apim-ai-resilience/images/1-Architecture-en.png 823w" width="823"&gt;&lt;/p&gt;
&lt;p&gt;This architecture uses a &lt;strong&gt;centralized API gateway + multi-subscription backend pattern&lt;/strong&gt;, with APIM as the unified entry point, distributing requests round-robin to Foundry instances across multiple subscriptions and regions.&lt;/p&gt;
&lt;p&gt;Core design principles:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Unified Entry Point&lt;/strong&gt; — Clients only need the APIM endpoint; the pool uses Round-Robin by default&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Simplified Security Authentication&lt;/strong&gt; — Zero-key communication via Managed Identity; clients only need a single API Key&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Cross-Subscription Quota Aggregation&lt;/strong&gt; — Aggregate quotas across subscriptions into a unified capacity pool&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Cross-Region Disaster Recovery&lt;/strong&gt; — APIM automatically routes to healthy instances when a region becomes unavailable&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;APIM&amp;rsquo;s Policy engine also provides flexible traffic control:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Pinned Routing&lt;/strong&gt; — via &lt;code&gt;X-AI-Foundry-Target&lt;/code&gt; header&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Auto Failover&lt;/strong&gt; — Auto-retry with exponential backoff on 4xx/5xx&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Request Tracing&lt;/strong&gt; — &lt;code&gt;X-AI-Foundry-Backend&lt;/code&gt; response header for tracing&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Architecture setup steps:&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th&gt;Section&lt;/th&gt;
 &lt;th&gt;Action&lt;/th&gt;
 &lt;th&gt;Goal&lt;/th&gt;
 &lt;th&gt;Platform&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td&gt;1&lt;/td&gt;
 &lt;td&gt;Create APIM instance&lt;/td&gt;
 &lt;td&gt;Set up the unified gateway entry point and enable Managed Identity&lt;/td&gt;
 &lt;td&gt;Azure Portal&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;2&lt;/td&gt;
 &lt;td&gt;Create Foundry resources&lt;/td&gt;
 &lt;td&gt;Build the backend resource pool across multiple subscriptions and regions&lt;/td&gt;
 &lt;td&gt;Azure Portal&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;3&lt;/td&gt;
 &lt;td&gt;Deploy AI models&lt;/td&gt;
 &lt;td&gt;Enable AI service capability on backend resources (using Sora2 as an example)&lt;/td&gt;
 &lt;td&gt;Foundry Portal&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;4&lt;/td&gt;
 &lt;td&gt;Configure RBAC&lt;/td&gt;
 &lt;td&gt;Grant APIM Managed Identity permission to access backends&lt;/td&gt;
 &lt;td&gt;Azure Portal&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;5&lt;/td&gt;
 &lt;td&gt;Configure Backend + LB Pool&lt;/td&gt;
 &lt;td&gt;Register backends and create the load balancing pool&lt;/td&gt;
 &lt;td&gt;Azure Portal (APIM)&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;6&lt;/td&gt;
 &lt;td&gt;Create API + Policy&lt;/td&gt;
 &lt;td&gt;Define the API entry point and configure routing and failover&lt;/td&gt;
 &lt;td&gt;Azure Portal (APIM)&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;7&lt;/td&gt;
 &lt;td&gt;Test and validate&lt;/td&gt;
 &lt;td&gt;Verify load balancing and pinned backend routing&lt;/td&gt;
 &lt;td&gt;curl / Postman&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;hr&gt;
&lt;h2 id="create-apim-instance"&gt;Create APIM Instance
&lt;/h2&gt;&lt;p&gt;Create an API Management instance in Azure Portal, selecting v2 Tier, and enable &lt;strong&gt;System Assigned Managed Identity&lt;/strong&gt; during creation.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Portal Steps:&lt;/strong&gt; Search &lt;code&gt;API Management Service&lt;/code&gt; → + Create → Fill in configuration → Enable on the Managed identity tab → Review + install&lt;/p&gt;
&lt;p&gt;&lt;img class="gallery-image" data-flex-basis="185px" data-flex-grow="77" height="1506" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/azure-apim-ai-resilience/images/2-3-basics-config.png" srcset="https://robinsujob.github.io/p/azure-apim-ai-resilience/images/2-3-basics-config_hu_14e08ccbc5610987.png 800w, https://robinsujob.github.io/p/azure-apim-ai-resilience/images/2-3-basics-config.png 1163w" width="1163"&gt;&lt;/p&gt;
&lt;p&gt;&lt;img class="gallery-image" data-flex-basis="475px" data-flex-grow="198" height="562" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/azure-apim-ai-resilience/images/2-4-managed-identity.png" srcset="https://robinsujob.github.io/p/azure-apim-ai-resilience/images/2-4-managed-identity_hu_4043f457949cc774.png 800w, https://robinsujob.github.io/p/azure-apim-ai-resilience/images/2-4-managed-identity.png 1114w" width="1114"&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;CLI Alternative:&lt;/strong&gt;&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt; 1
&lt;/span&gt;&lt;span class="lnt"&gt; 2
&lt;/span&gt;&lt;span class="lnt"&gt; 3
&lt;/span&gt;&lt;span class="lnt"&gt; 4
&lt;/span&gt;&lt;span class="lnt"&gt; 5
&lt;/span&gt;&lt;span class="lnt"&gt; 6
&lt;/span&gt;&lt;span class="lnt"&gt; 7
&lt;/span&gt;&lt;span class="lnt"&gt; 8
&lt;/span&gt;&lt;span class="lnt"&gt; 9
&lt;/span&gt;&lt;span class="lnt"&gt;10
&lt;/span&gt;&lt;span class="lnt"&gt;11
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;az rest --method PUT &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --url &lt;span class="s2"&gt;&amp;#34;https://management.azure.com/subscriptions/&amp;lt;subscription-id&amp;gt;/resourceGroups/&amp;lt;resource-group&amp;gt;/providers/Microsoft.ApiManagement/service/&amp;lt;apim-name&amp;gt;?api-version=2024-05-01&amp;#34;&lt;/span&gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --body &lt;span class="s1"&gt;&amp;#39;{
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="s1"&gt; &amp;#34;location&amp;#34;: &amp;#34;&amp;lt;region&amp;gt;&amp;#34;,
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="s1"&gt; &amp;#34;sku&amp;#34;: { &amp;#34;name&amp;#34;: &amp;#34;&amp;lt;BasicV2|StandardV2|PremiumV2&amp;gt;&amp;#34;, &amp;#34;capacity&amp;#34;: 1 },
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="s1"&gt; &amp;#34;identity&amp;#34;: { &amp;#34;type&amp;#34;: &amp;#34;SystemAssigned&amp;#34; },
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="s1"&gt; &amp;#34;properties&amp;#34;: {
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="s1"&gt; &amp;#34;publisherName&amp;#34;: &amp;#34;&amp;lt;organization-name&amp;gt;&amp;#34;,
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="s1"&gt; &amp;#34;publisherEmail&amp;#34;: &amp;#34;&amp;lt;admin-email&amp;gt;&amp;#34;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="s1"&gt; }
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="s1"&gt; }&amp;#39;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;&lt;strong&gt;After Creation:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Status: &lt;strong&gt;Online&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Record &lt;strong&gt;Gateway URL&lt;/strong&gt; (for example: &lt;code&gt;https://&amp;lt;apim-name&amp;gt;.azure-api.net&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Record the Managed Identity &lt;strong&gt;Object (principal) ID&lt;/strong&gt; for RBAC&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img class="gallery-image" data-flex-basis="728px" data-flex-grow="303" height="644" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/azure-apim-ai-resilience/images/2-6-apim-dashboard.png" srcset="https://robinsujob.github.io/p/azure-apim-ai-resilience/images/2-6-apim-dashboard_hu_2b05d081fb5fa317.png 800w, https://robinsujob.github.io/p/azure-apim-ai-resilience/images/2-6-apim-dashboard_hu_e21fb9797c99f7a2.png 1600w, https://robinsujob.github.io/p/azure-apim-ai-resilience/images/2-6-apim-dashboard.png 1956w" width="1956"&gt;&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id="create-foundry-resources"&gt;Create Foundry Resources
&lt;/h2&gt;&lt;p&gt;Create Foundry resources across multiple subscriptions and regions, each with a corresponding project.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Portal Steps:&lt;/strong&gt; Search &lt;code&gt;Microsoft Foundry&lt;/code&gt; → Create a resource → Fill in Subscription / Region / Name / Project name → Create&lt;/p&gt;
&lt;p&gt;&lt;img class="gallery-image" data-flex-basis="220px" data-flex-grow="91" height="1512" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/azure-apim-ai-resilience/images/3-3-create-foundry-resource.png" srcset="https://robinsujob.github.io/p/azure-apim-ai-resilience/images/3-3-create-foundry-resource_hu_e040e90bd75b1db3.png 800w, https://robinsujob.github.io/p/azure-apim-ai-resilience/images/3-3-create-foundry-resource.png 1386w" width="1386"&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;CLI Alternative:&lt;/strong&gt;&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt; 1
&lt;/span&gt;&lt;span class="lnt"&gt; 2
&lt;/span&gt;&lt;span class="lnt"&gt; 3
&lt;/span&gt;&lt;span class="lnt"&gt; 4
&lt;/span&gt;&lt;span class="lnt"&gt; 5
&lt;/span&gt;&lt;span class="lnt"&gt; 6
&lt;/span&gt;&lt;span class="lnt"&gt; 7
&lt;/span&gt;&lt;span class="lnt"&gt; 8
&lt;/span&gt;&lt;span class="lnt"&gt; 9
&lt;/span&gt;&lt;span class="lnt"&gt;10
&lt;/span&gt;&lt;span class="lnt"&gt;11
&lt;/span&gt;&lt;span class="lnt"&gt;12
&lt;/span&gt;&lt;span class="lnt"&gt;13
&lt;/span&gt;&lt;span class="lnt"&gt;14
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;az cognitiveservices account create &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --subscription &amp;lt;subscription-id&amp;gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --resource-group &amp;lt;resource-group&amp;gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --name &amp;lt;foundry-resource-name&amp;gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --location &amp;lt;region-code&amp;gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --kind AIServices --sku S0 &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --custom-domain &amp;lt;foundry-resource-name&amp;gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --allow-project-management &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;az cognitiveservices account project create &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --subscription &amp;lt;subscription-id&amp;gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --name &amp;lt;foundry-resource-name&amp;gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --resource-group &amp;lt;resource-group&amp;gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --project-name &amp;lt;project-name&amp;gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --location &amp;lt;region-code&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;&lt;strong&gt;Example Resource Distribution:&lt;/strong&gt;&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th&gt;Subscription&lt;/th&gt;
 &lt;th&gt;Region&lt;/th&gt;
 &lt;th&gt;Resource Name&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td&gt;Subscription A&lt;/td&gt;
 &lt;td&gt;East US 2&lt;/td&gt;
 &lt;td&gt;foundry-eastus2-01&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Subscription A&lt;/td&gt;
 &lt;td&gt;Sweden Central&lt;/td&gt;
 &lt;td&gt;foundry-swedencentral-01&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Subscription B&lt;/td&gt;
 &lt;td&gt;East US 2&lt;/td&gt;
 &lt;td&gt;foundry-eastus2-02&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Subscription B&lt;/td&gt;
 &lt;td&gt;Sweden Central&lt;/td&gt;
 &lt;td&gt;foundry-swedencentral-02&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;hr&gt;
&lt;h2 id="deploy-models"&gt;Deploy Models
&lt;/h2&gt;&lt;p&gt;Deploy AI models in each Foundry resource via the Foundry Portal.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Portal Steps:&lt;/strong&gt; In &lt;a class="link" href="https://ai.azure.com/nextgen/" target="_blank" rel="noopener"
 &gt;Foundry Portal&lt;/a&gt;, select the target project → Build → Models → Deploy a base model → Search for a model (for example &lt;code&gt;sora-2&lt;/code&gt;) → Deploy → Default settings&lt;/p&gt;
&lt;p&gt;&lt;img class="gallery-image" data-flex-basis="491px" data-flex-grow="204" height="924" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/azure-apim-ai-resilience/images/4-2-search-sora2.png" srcset="https://robinsujob.github.io/p/azure-apim-ai-resilience/images/4-2-search-sora2_hu_e7087db313cdf721.png 800w, https://robinsujob.github.io/p/azure-apim-ai-resilience/images/4-2-search-sora2_hu_956f92a0935b1b90.png 1600w, https://robinsujob.github.io/p/azure-apim-ai-resilience/images/4-2-search-sora2.png 1891w" width="1891"&gt;&lt;/p&gt;
&lt;p&gt;&lt;img class="gallery-image" data-flex-basis="455px" data-flex-grow="189" height="1006" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/azure-apim-ai-resilience/images/4-5-playground.png" srcset="https://robinsujob.github.io/p/azure-apim-ai-resilience/images/4-5-playground_hu_b6ecf169c4389d93.png 800w, https://robinsujob.github.io/p/azure-apim-ai-resilience/images/4-5-playground_hu_50733a65436bad42.png 1600w, https://robinsujob.github.io/p/azure-apim-ai-resilience/images/4-5-playground.png 1911w" width="1911"&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;CLI Alternative:&lt;/strong&gt;&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt; 1
&lt;/span&gt;&lt;span class="lnt"&gt; 2
&lt;/span&gt;&lt;span class="lnt"&gt; 3
&lt;/span&gt;&lt;span class="lnt"&gt; 4
&lt;/span&gt;&lt;span class="lnt"&gt; 5
&lt;/span&gt;&lt;span class="lnt"&gt; 6
&lt;/span&gt;&lt;span class="lnt"&gt; 7
&lt;/span&gt;&lt;span class="lnt"&gt; 8
&lt;/span&gt;&lt;span class="lnt"&gt; 9
&lt;/span&gt;&lt;span class="lnt"&gt;10
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;az cognitiveservices account deployment create &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --subscription &amp;lt;subscription-id&amp;gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --resource-group &amp;lt;resource-group&amp;gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --name &amp;lt;foundry-resource-name&amp;gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --deployment-name &amp;lt;deployment-name&amp;gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --model-name &amp;lt;model-name&amp;gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --model-version &amp;lt;model-version&amp;gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --model-format &amp;lt;model-format&amp;gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --sku-name &amp;lt;sku-name&amp;gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --sku-capacity &amp;lt;capacity&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
 &lt;blockquote&gt;
 &lt;p&gt;Model names, versions, and SKUs vary by region. See: &lt;a class="link" href="https://learn.microsoft.com/azure/foundry/foundry-models/concepts/models-sold-directly-by-azure" target="_blank" rel="noopener"
 &gt;Models and Region Availability&lt;/a&gt;&lt;/p&gt;

 &lt;/blockquote&gt;
&lt;hr&gt;
&lt;h2 id="configure-rbac"&gt;Configure RBAC
&lt;/h2&gt;&lt;p&gt;Grant the &lt;code&gt;Cognitive Services OpenAI User&lt;/code&gt; role to APIM&amp;rsquo;s Managed Identity on &lt;strong&gt;each Foundry resource&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Portal Steps:&lt;/strong&gt; Go to the Foundry resource → Access control (IAM) → + Add → Add role assignment → Search &lt;code&gt;Cognitive Services OpenAI User&lt;/code&gt; → In Members choose Managed identity → Select the API Management service → Review + assign&lt;/p&gt;
&lt;p&gt;&lt;img class="gallery-image" data-flex-basis="296px" data-flex-grow="123" height="1042" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/azure-apim-ai-resilience/images/5-3-select-role.png" srcset="https://robinsujob.github.io/p/azure-apim-ai-resilience/images/5-3-select-role_hu_7d310acac9aba761.png 800w, https://robinsujob.github.io/p/azure-apim-ai-resilience/images/5-3-select-role.png 1287w" width="1287"&gt;&lt;/p&gt;
&lt;p&gt;&lt;img class="gallery-image" data-flex-basis="378px" data-flex-grow="157" height="1166" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/azure-apim-ai-resilience/images/5-4-select-managed-identity.png" srcset="https://robinsujob.github.io/p/azure-apim-ai-resilience/images/5-4-select-managed-identity_hu_d617d34d874084d4.png 800w, https://robinsujob.github.io/p/azure-apim-ai-resilience/images/5-4-select-managed-identity_hu_66bfce957da67b8d.png 1600w, https://robinsujob.github.io/p/azure-apim-ai-resilience/images/5-4-select-managed-identity.png 1840w" width="1840"&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;CLI Alternative:&lt;/strong&gt;&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt; 1
&lt;/span&gt;&lt;span class="lnt"&gt; 2
&lt;/span&gt;&lt;span class="lnt"&gt; 3
&lt;/span&gt;&lt;span class="lnt"&gt; 4
&lt;/span&gt;&lt;span class="lnt"&gt; 5
&lt;/span&gt;&lt;span class="lnt"&gt; 6
&lt;/span&gt;&lt;span class="lnt"&gt; 7
&lt;/span&gt;&lt;span class="lnt"&gt; 8
&lt;/span&gt;&lt;span class="lnt"&gt; 9
&lt;/span&gt;&lt;span class="lnt"&gt;10
&lt;/span&gt;&lt;span class="lnt"&gt;11
&lt;/span&gt;&lt;span class="lnt"&gt;12
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;# Get APIM Managed Identity Principal ID&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;az apim show &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --subscription &amp;lt;subscription-id&amp;gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --name &amp;lt;apim-name&amp;gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --resource-group &amp;lt;resource-group&amp;gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --query &lt;span class="s2"&gt;&amp;#34;identity.principalId&amp;#34;&lt;/span&gt; -o tsv
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;# Assign role for each Foundry resource&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;az role assignment create &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --assignee &amp;lt;principal-id&amp;gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --role &lt;span class="s2"&gt;&amp;#34;Cognitive Services OpenAI User&amp;#34;&lt;/span&gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --scope /subscriptions/&amp;lt;subscription-id&amp;gt;/resourceGroups/&amp;lt;resource-group&amp;gt;/providers/Microsoft.CognitiveServices/accounts/&amp;lt;foundry-resource-name&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
 &lt;blockquote&gt;
 &lt;p&gt;&lt;code&gt;Cognitive Services OpenAI User&lt;/code&gt; is the minimum permission role—allows inference calls but not resource configuration changes.&lt;/p&gt;

 &lt;/blockquote&gt;
&lt;hr&gt;
&lt;h2 id="configure-backend--lb-pool"&gt;Configure Backend + LB Pool
&lt;/h2&gt;&lt;h3 id="create-backend"&gt;Create Backend
&lt;/h3&gt;&lt;p&gt;Create a Backend for each Foundry resource with Managed Identity authentication.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Get OpenAI Endpoint:&lt;/strong&gt; On the project Home page in &lt;a class="link" href="https://ai.azure.com/nextgen/" target="_blank" rel="noopener"
 &gt;Foundry Portal&lt;/a&gt;, find &lt;strong&gt;Azure OpenAI endpoint&lt;/strong&gt;, or use the CLI:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;span class="lnt"&gt;4
&lt;/span&gt;&lt;span class="lnt"&gt;5
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;az cognitiveservices account show &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --subscription &amp;lt;subscription-id&amp;gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --resource-group &amp;lt;resource-group&amp;gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --name &amp;lt;foundry-resource-name&amp;gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --query &lt;span class="s2"&gt;&amp;#34;properties.endpoints.\&amp;#34;OpenAI Language Model Instance API\&amp;#34;&amp;#34;&lt;/span&gt; -o tsv
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;&lt;strong&gt;Portal Steps:&lt;/strong&gt; APIM → Backends → + Create new backend → Fill in Name / Runtime URL → Managed Identity tab → Client identity = System assigned → Resource ID = &lt;code&gt;https://cognitiveservices.azure.com&lt;/code&gt; → Create&lt;/p&gt;
&lt;p&gt;&lt;img class="gallery-image" data-flex-basis="299px" data-flex-grow="124" height="1647" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/azure-apim-ai-resilience/images/6-3-create-backend.png" srcset="https://robinsujob.github.io/p/azure-apim-ai-resilience/images/6-3-create-backend_hu_6b4ff1c24b7d40ce.png 800w, https://robinsujob.github.io/p/azure-apim-ai-resilience/images/6-3-create-backend_hu_3c50fe44b4ab8742.png 1600w, https://robinsujob.github.io/p/azure-apim-ai-resilience/images/6-3-create-backend.png 2052w" width="2052"&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Key Configuration:&lt;/strong&gt;&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th&gt;Configuration&lt;/th&gt;
 &lt;th&gt;Example Value&lt;/th&gt;
 &lt;th&gt;Description&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td&gt;Name&lt;/td&gt;
 &lt;td&gt;&lt;code&gt;&amp;lt;foundry-resource-name&amp;gt;&lt;/code&gt;&lt;/td&gt;
 &lt;td&gt;Recommended to match the Foundry resource name&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Runtime URL&lt;/td&gt;
 &lt;td&gt;&lt;code&gt;https://&amp;lt;foundry-resource-name&amp;gt;.openai.azure.com/openai/v1&lt;/code&gt;&lt;/td&gt;
 &lt;td&gt;Foundry OpenAI endpoint (+ &lt;code&gt;openai/v1&lt;/code&gt;)&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Client identity&lt;/td&gt;
 &lt;td&gt;System assigned identity&lt;/td&gt;
 &lt;td&gt;Use APIM system-assigned managed identity&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Resource ID&lt;/td&gt;
 &lt;td&gt;&lt;code&gt;https://cognitiveservices.azure.com&lt;/code&gt;&lt;/td&gt;
 &lt;td&gt;Token audience, &lt;strong&gt;fixed value&lt;/strong&gt;&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;&lt;strong&gt;CLI Alternative:&lt;/strong&gt;&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt; 1
&lt;/span&gt;&lt;span class="lnt"&gt; 2
&lt;/span&gt;&lt;span class="lnt"&gt; 3
&lt;/span&gt;&lt;span class="lnt"&gt; 4
&lt;/span&gt;&lt;span class="lnt"&gt; 5
&lt;/span&gt;&lt;span class="lnt"&gt; 6
&lt;/span&gt;&lt;span class="lnt"&gt; 7
&lt;/span&gt;&lt;span class="lnt"&gt; 8
&lt;/span&gt;&lt;span class="lnt"&gt; 9
&lt;/span&gt;&lt;span class="lnt"&gt;10
&lt;/span&gt;&lt;span class="lnt"&gt;11
&lt;/span&gt;&lt;span class="lnt"&gt;12
&lt;/span&gt;&lt;span class="lnt"&gt;13
&lt;/span&gt;&lt;span class="lnt"&gt;14
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;az rest --method PUT &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --url &lt;span class="s2"&gt;&amp;#34;https://management.azure.com/subscriptions/&amp;lt;subscription-id&amp;gt;/resourceGroups/&amp;lt;resource-group&amp;gt;/providers/Microsoft.ApiManagement/service/&amp;lt;apim-name&amp;gt;/backends/&amp;lt;backend-name&amp;gt;?api-version=2024-05-01&amp;#34;&lt;/span&gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --body &lt;span class="s1"&gt;&amp;#39;{
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="s1"&gt; &amp;#34;properties&amp;#34;: {
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="s1"&gt; &amp;#34;url&amp;#34;: &amp;#34;https://&amp;lt;foundry-resource-name&amp;gt;.openai.azure.com/openai/v1&amp;#34;,
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="s1"&gt; &amp;#34;protocol&amp;#34;: &amp;#34;http&amp;#34;,
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="s1"&gt; &amp;#34;credentials&amp;#34;: {
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="s1"&gt; &amp;#34;managedIdentity&amp;#34;: {
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="s1"&gt; &amp;#34;clientId&amp;#34;: null,
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="s1"&gt; &amp;#34;resource&amp;#34;: &amp;#34;https://cognitiveservices.azure.com&amp;#34;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="s1"&gt; }
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="s1"&gt; }
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="s1"&gt; }
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="s1"&gt; }&amp;#39;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;h3 id="create-lb-pool"&gt;Create LB Pool
&lt;/h3&gt;&lt;p&gt;&lt;strong&gt;Portal Steps:&lt;/strong&gt; APIM → Backends → Load balancer tab → + Create new pool → Fill in Pool Name → Select all Backends → Send requests evenly (Round-robin) → Create&lt;/p&gt;
&lt;p&gt;&lt;img class="gallery-image" data-flex-basis="375px" data-flex-grow="156" height="774" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/azure-apim-ai-resilience/images/6-6-create-pool.png" srcset="https://robinsujob.github.io/p/azure-apim-ai-resilience/images/6-6-create-pool_hu_3af9a6228e0fc588.png 800w, https://robinsujob.github.io/p/azure-apim-ai-resilience/images/6-6-create-pool.png 1212w" width="1212"&gt;&lt;/p&gt;

 &lt;blockquote&gt;
 &lt;p&gt;Portal provides two options: &lt;strong&gt;Send requests evenly&lt;/strong&gt; (Round-robin) or &lt;strong&gt;Customize weight and priority&lt;/strong&gt;.&lt;/p&gt;

 &lt;/blockquote&gt;
&lt;hr&gt;
&lt;h2 id="create-api--policy"&gt;Create API + Policy
&lt;/h2&gt;&lt;h3 id="create-api"&gt;Create API
&lt;/h3&gt;&lt;p&gt;&lt;strong&gt;Portal Steps:&lt;/strong&gt; APIM → APIs → + Add API → HTTP → Fill in Display name / API URL suffix (&lt;code&gt;openai/v1&lt;/code&gt;) → Leave Web service URL empty → Create&lt;/p&gt;
&lt;p&gt;Then add Operations: create &lt;code&gt;/*&lt;/code&gt; wildcard path for POST, GET, and DELETE.&lt;/p&gt;

 &lt;blockquote&gt;
 &lt;p&gt;This example uses wildcard &lt;code&gt;/*&lt;/code&gt; to proxy all requests. For finer control, create operations for specific paths like &lt;code&gt;/videos/*&lt;/code&gt; for Sora2.&lt;/p&gt;

 &lt;/blockquote&gt;
&lt;h3 id="configure-policy"&gt;Configure Policy
&lt;/h3&gt;&lt;p&gt;Select All operations → click &lt;code&gt;&amp;lt;/&amp;gt;&lt;/code&gt; in Inbound processing → paste the policy below, update the pool name → Save&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt; 1
&lt;/span&gt;&lt;span class="lnt"&gt; 2
&lt;/span&gt;&lt;span class="lnt"&gt; 3
&lt;/span&gt;&lt;span class="lnt"&gt; 4
&lt;/span&gt;&lt;span class="lnt"&gt; 5
&lt;/span&gt;&lt;span class="lnt"&gt; 6
&lt;/span&gt;&lt;span class="lnt"&gt; 7
&lt;/span&gt;&lt;span class="lnt"&gt; 8
&lt;/span&gt;&lt;span class="lnt"&gt; 9
&lt;/span&gt;&lt;span class="lnt"&gt;10
&lt;/span&gt;&lt;span class="lnt"&gt;11
&lt;/span&gt;&lt;span class="lnt"&gt;12
&lt;/span&gt;&lt;span class="lnt"&gt;13
&lt;/span&gt;&lt;span class="lnt"&gt;14
&lt;/span&gt;&lt;span class="lnt"&gt;15
&lt;/span&gt;&lt;span class="lnt"&gt;16
&lt;/span&gt;&lt;span class="lnt"&gt;17
&lt;/span&gt;&lt;span class="lnt"&gt;18
&lt;/span&gt;&lt;span class="lnt"&gt;19
&lt;/span&gt;&lt;span class="lnt"&gt;20
&lt;/span&gt;&lt;span class="lnt"&gt;21
&lt;/span&gt;&lt;span class="lnt"&gt;22
&lt;/span&gt;&lt;span class="lnt"&gt;23
&lt;/span&gt;&lt;span class="lnt"&gt;24
&lt;/span&gt;&lt;span class="lnt"&gt;25
&lt;/span&gt;&lt;span class="lnt"&gt;26
&lt;/span&gt;&lt;span class="lnt"&gt;27
&lt;/span&gt;&lt;span class="lnt"&gt;28
&lt;/span&gt;&lt;span class="lnt"&gt;29
&lt;/span&gt;&lt;span class="lnt"&gt;30
&lt;/span&gt;&lt;span class="lnt"&gt;31
&lt;/span&gt;&lt;span class="lnt"&gt;32
&lt;/span&gt;&lt;span class="lnt"&gt;33
&lt;/span&gt;&lt;span class="lnt"&gt;34
&lt;/span&gt;&lt;span class="lnt"&gt;35
&lt;/span&gt;&lt;span class="lnt"&gt;36
&lt;/span&gt;&lt;span class="lnt"&gt;37
&lt;/span&gt;&lt;span class="lnt"&gt;38
&lt;/span&gt;&lt;span class="lnt"&gt;39
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-xml" data-lang="xml"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nt"&gt;&amp;lt;policies&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;lt;inbound&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;lt;base&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="c"&gt;&amp;lt;!-- Read request header so callers can pin a backend --&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;lt;set-variable&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;target&amp;#34;&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;@(context.Request.Headers.GetValueOrDefault(&amp;#34;&lt;/span&gt;&lt;span class="err"&gt;X-AI-Foundry-Target&amp;#34;,&lt;/span&gt; &lt;span class="err"&gt;&amp;#34;&amp;#34;))&amp;#34;&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="c"&gt;&amp;lt;!-- Backend load balancing pool name --&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;lt;set-variable&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;pool_name&amp;#34;&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;&amp;lt;your-ai-hub-pool-name&amp;gt;&amp;#34;&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;lt;choose&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;lt;when&lt;/span&gt; &lt;span class="na"&gt;condition=&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;@(context.Variables.GetValueOrDefault&amp;lt;string&amp;gt;(&amp;#34;&lt;/span&gt;&lt;span class="err"&gt;target&amp;#34;)&lt;/span&gt; &lt;span class="err"&gt;!=&lt;/span&gt; &lt;span class="err"&gt;&amp;#34;&amp;#34;)&amp;#34;&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;lt;set-backend-service&lt;/span&gt; &lt;span class="na"&gt;backend-id=&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;@(context.Variables.GetValueOrDefault&amp;lt;string&amp;gt;(&amp;#34;&lt;/span&gt;&lt;span class="err"&gt;target&amp;#34;))&amp;#34;&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;lt;/when&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;lt;otherwise&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;lt;set-backend-service&lt;/span&gt; &lt;span class="na"&gt;backend-id=&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;@((string)context.Variables[&amp;#34;&lt;/span&gt;&lt;span class="err"&gt;pool_name&amp;#34;])&amp;#34;&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;lt;/otherwise&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;lt;/choose&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;lt;/inbound&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;lt;outbound&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;lt;base&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;lt;set-header&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;X-AI-Foundry-Backend&amp;#34;&lt;/span&gt; &lt;span class="na"&gt;exists-action=&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;override&amp;#34;&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;lt;value&amp;gt;&lt;/span&gt;@(context.Request.Url.Host.Split(&amp;#39;.&amp;#39;)[0])&lt;span class="nt"&gt;&amp;lt;/value&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;lt;/set-header&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;lt;/outbound&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;lt;on-error&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;lt;base&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;lt;/on-error&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;lt;backend&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;lt;choose&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;lt;when&lt;/span&gt; &lt;span class="na"&gt;condition=&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;@(context.Variables.GetValueOrDefault&amp;lt;string&amp;gt;(&amp;#34;&lt;/span&gt;&lt;span class="err"&gt;target&amp;#34;)&lt;/span&gt; &lt;span class="err"&gt;!=&lt;/span&gt; &lt;span class="err"&gt;&amp;#34;&amp;#34;)&amp;#34;&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;lt;forward-request&lt;/span&gt; &lt;span class="na"&gt;buffer-request-body=&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;true&amp;#34;&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;lt;/when&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;lt;otherwise&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;lt;retry&lt;/span&gt; &lt;span class="na"&gt;condition=&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;@(context.Response == null || context.Response.StatusCode &amp;gt;= 400)&amp;#34;&lt;/span&gt; &lt;span class="na"&gt;count=&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;3&amp;#34;&lt;/span&gt; &lt;span class="na"&gt;interval=&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;1&amp;#34;&lt;/span&gt; &lt;span class="na"&gt;delta=&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;1&amp;#34;&lt;/span&gt; &lt;span class="na"&gt;max-interval=&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;5&amp;#34;&lt;/span&gt; &lt;span class="na"&gt;first-fast-retry=&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;true&amp;#34;&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;lt;set-backend-service&lt;/span&gt; &lt;span class="na"&gt;backend-id=&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;@((string)context.Variables[&amp;#34;&lt;/span&gt;&lt;span class="err"&gt;pool_name&amp;#34;]&lt;/span&gt; &lt;span class="err"&gt;)&amp;#34;&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;lt;forward-request&lt;/span&gt; &lt;span class="na"&gt;buffer-request-body=&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;true&amp;#34;&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;lt;/retry&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;lt;/otherwise&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;lt;/choose&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;lt;/backend&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nt"&gt;&amp;lt;/policies&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;&lt;img class="gallery-image" data-flex-basis="370px" data-flex-grow="154" height="1848" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/azure-apim-ai-resilience/images/7-5-policy-editor-en.png" srcset="https://robinsujob.github.io/p/azure-apim-ai-resilience/images/7-5-policy-editor-en_hu_bf4c944bd3751b2f.png 800w, https://robinsujob.github.io/p/azure-apim-ai-resilience/images/7-5-policy-editor-en_hu_82283df76f27d876.png 1600w, https://robinsujob.github.io/p/azure-apim-ai-resilience/images/7-5-policy-editor-en_hu_fc16f38167941c1d.png 2400w, https://robinsujob.github.io/p/azure-apim-ai-resilience/images/7-5-policy-editor-en.png 2851w" width="2851"&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Policy Logic:&lt;/strong&gt;&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th&gt;Area&lt;/th&gt;
 &lt;th&gt;Function&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;strong&gt;inbound&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;Read &lt;code&gt;X-AI-Foundry-Target&lt;/code&gt;; if present, route to the specified backend, otherwise use the LB Pool round-robin&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;strong&gt;outbound&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;Return the actual backend name in the &lt;code&gt;X-AI-Foundry-Backend&lt;/code&gt; response header&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;strong&gt;backend&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;Forward directly for pinned routing; in pool mode, auto-retry and switch backend on failure (up to 3 attempts with exponential backoff)&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;hr&gt;
&lt;h2 id="test-and-validate"&gt;Test and Validate
&lt;/h2&gt;&lt;p&gt;&lt;strong&gt;Prerequisites:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Gather the following before testing:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Get Gateway URL: APIM → Overview → Gateway URL&lt;/li&gt;
&lt;li&gt;Get API Key: APIM → Subscriptions → Built-in all-access → Show keys → Primary key&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img class="gallery-image" data-flex-basis="506px" data-flex-grow="211" height="1116" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/azure-apim-ai-resilience/images/8-2-subscription-key.png" srcset="https://robinsujob.github.io/p/azure-apim-ai-resilience/images/8-2-subscription-key_hu_73484fdc0f89d8c9.png 800w, https://robinsujob.github.io/p/azure-apim-ai-resilience/images/8-2-subscription-key_hu_a8585cc5e1155571.png 1600w, https://robinsujob.github.io/p/azure-apim-ai-resilience/images/8-2-subscription-key.png 2355w" width="2355"&gt;&lt;/p&gt;
&lt;h3 id="test-1-load-balancing"&gt;Test 1: Load Balancing
&lt;/h3&gt;&lt;p&gt;Send consecutive requests without &lt;code&gt;X-AI-Foundry-Target&lt;/code&gt;, and observe &lt;code&gt;X-AI-Foundry-Backend&lt;/code&gt; rotating across backends:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;curl -D - -o /dev/null --location &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s1"&gt;&amp;#39;https://&amp;lt;apim-gateway-url&amp;gt;/openai/v1/videos&amp;#39;&lt;/span&gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --header &lt;span class="s1"&gt;&amp;#39;Ocp-Apim-Subscription-Key: &amp;lt;your-subscription-key&amp;gt;&amp;#39;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;Expected: &lt;code&gt;X-AI-Foundry-Backend&lt;/code&gt; changes with each request, confirming Round-robin is working.&lt;/p&gt;
&lt;p&gt;&lt;img class="gallery-image" data-flex-basis="295px" data-flex-grow="123" height="1961" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/azure-apim-ai-resilience/images/8-3-round-robin-test.png" srcset="https://robinsujob.github.io/p/azure-apim-ai-resilience/images/8-3-round-robin-test_hu_7f93d3fde652e814.png 800w, https://robinsujob.github.io/p/azure-apim-ai-resilience/images/8-3-round-robin-test_hu_a47e59e6a4cc2c60.png 1600w, https://robinsujob.github.io/p/azure-apim-ai-resilience/images/8-3-round-robin-test_hu_54b55a659d650932.png 2400w, https://robinsujob.github.io/p/azure-apim-ai-resilience/images/8-3-round-robin-test.png 2415w" width="2415"&gt;&lt;/p&gt;
&lt;h3 id="test-2-pinned-backend-routing"&gt;Test 2: Pinned Backend Routing
&lt;/h3&gt;&lt;p&gt;Use &lt;code&gt;X-AI-Foundry-Target&lt;/code&gt; header to pin to a specific backend:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;span class="lnt"&gt;4
&lt;/span&gt;&lt;span class="lnt"&gt;5
&lt;/span&gt;&lt;span class="lnt"&gt;6
&lt;/span&gt;&lt;span class="lnt"&gt;7
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;curl -s -D - -o /dev/null --location &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s1"&gt;&amp;#39;https://&amp;lt;apim-gateway-url&amp;gt;/openai/v1/responses&amp;#39;&lt;/span&gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --header &lt;span class="s1"&gt;&amp;#39;Content-Type: application/json&amp;#39;&lt;/span&gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --header &lt;span class="s1"&gt;&amp;#39;Ocp-Apim-Subscription-Key: &amp;lt;your-subscription-key&amp;gt;&amp;#39;&lt;/span&gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --header &lt;span class="s1"&gt;&amp;#39;X-AI-Foundry-Target: &amp;lt;backend-name&amp;gt;&amp;#39;&lt;/span&gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --data &lt;span class="s1"&gt;&amp;#39;{&amp;#34;model&amp;#34;: &amp;#34;&amp;lt;model-name&amp;gt;&amp;#34;, &amp;#34;input&amp;#34;: &amp;#34;Hello&amp;#34;}&amp;#39;&lt;/span&gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;|&lt;/span&gt; grep -i X-AI-Foundry-Backend
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;Expected: &lt;code&gt;X-AI-Foundry-Backend&lt;/code&gt; consistently returns the specified backend name.&lt;/p&gt;
&lt;p&gt;&lt;img class="gallery-image" data-flex-basis="423px" data-flex-grow="176" height="1056" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/azure-apim-ai-resilience/images/8-4-pinned-backend-test.png" srcset="https://robinsujob.github.io/p/azure-apim-ai-resilience/images/8-4-pinned-backend-test_hu_dfc5dea219569ee5.png 800w, https://robinsujob.github.io/p/azure-apim-ai-resilience/images/8-4-pinned-backend-test_hu_f665edd6a0ac1aeb.png 1600w, https://robinsujob.github.io/p/azure-apim-ai-resilience/images/8-4-pinned-backend-test.png 1865w" width="1865"&gt;&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id="faq"&gt;FAQ
&lt;/h2&gt;&lt;p&gt;&lt;strong&gt;Q: Why not use APIM Backend&amp;rsquo;s Circuit Breaker?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;This architecture targets load balancing and quota aggregation. Circuit Breaker would &amp;ldquo;break&amp;rdquo; on backend failure, but a 429 on one model shouldn&amp;rsquo;t block other models on the same backend. Simple failover via &lt;code&gt;retry&lt;/code&gt; + &lt;code&gt;set-backend-service&lt;/code&gt; is more appropriate.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Q: Is this architecture only for Sora2?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;No. It applies to any Foundry / Azure OpenAI model service (Responses API, Chat Completions, Embeddings, Image Generation, etc.)—just point the Backend URL to the corresponding endpoint.&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id="reference-documentation"&gt;Reference Documentation
&lt;/h2&gt;&lt;ul&gt;
&lt;li&gt;Microsoft Foundry Overview: &lt;a class="link" href="https://learn.microsoft.com/azure/foundry/what-is-foundry" target="_blank" rel="noopener"
 &gt;https://learn.microsoft.com/azure/foundry/what-is-foundry&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Foundry RBAC: &lt;a class="link" href="https://learn.microsoft.com/azure/foundry-classic/openai/how-to/role-based-access-control" target="_blank" rel="noopener"
 &gt;https://learn.microsoft.com/azure/foundry-classic/openai/how-to/role-based-access-control&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;APIM v2 Tier: &lt;a class="link" href="https://learn.microsoft.com/azure/api-management/v2-service-tiers-overview" target="_blank" rel="noopener"
 &gt;https://learn.microsoft.com/azure/api-management/v2-service-tiers-overview&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Backends &amp;amp; LB Pool: &lt;a class="link" href="https://learn.microsoft.com/azure/api-management/backends" target="_blank" rel="noopener"
 &gt;https://learn.microsoft.com/azure/api-management/backends&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Managed Identity: &lt;a class="link" href="https://learn.microsoft.com/azure/api-management/api-management-howto-use-managed-service-identity" target="_blank" rel="noopener"
 &gt;https://learn.microsoft.com/azure/api-management/api-management-howto-use-managed-service-identity&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Retry Policy: &lt;a class="link" href="https://learn.microsoft.com/azure/api-management/retry-policy" target="_blank" rel="noopener"
 &gt;https://learn.microsoft.com/azure/api-management/retry-policy&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Video Generation / Sora2: &lt;a class="link" href="https://learn.microsoft.com/azure/foundry/openai/concepts/video-generation" target="_blank" rel="noopener"
 &gt;https://learn.microsoft.com/azure/foundry/openai/concepts/video-generation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Models &amp;amp; Regions: &lt;a class="link" href="https://learn.microsoft.com/azure/foundry/foundry-models/concepts/models-sold-directly-by-azure" target="_blank" rel="noopener"
 &gt;https://learn.microsoft.com/azure/foundry/foundry-models/concepts/models-sold-directly-by-azure&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Error Codes: &lt;a class="link" href="https://learn.microsoft.com/azure/foundry/openai/supported-languages#error-handling" target="_blank" rel="noopener"
 &gt;https://learn.microsoft.com/azure/foundry/openai/supported-languages#error-handling&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;</description></item></channel></rss>