<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: MUHAMMAD ZAIN UL ABIDIN</title>
    <description>The latest articles on DEV Community by MUHAMMAD ZAIN UL ABIDIN (@zforzain2000).</description>
    <link>https://dev.to/zforzain2000</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2867509%2F69a6aab1-b9d6-4e31-bb93-685ce7b2042d.jpg</url>
      <title>DEV Community: MUHAMMAD ZAIN UL ABIDIN</title>
      <link>https://dev.to/zforzain2000</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/zforzain2000"/>
    <language>en</language>
    <item>
      <title>Multi-Tenant SaaS With NestJS and Postgres RLS: What Held Up</title>
      <dc:creator>MUHAMMAD ZAIN UL ABIDIN</dc:creator>
      <pubDate>Tue, 24 Nov 2026 06:24:05 +0000</pubDate>
      <link>https://dev.to/zforzain2000/multi-tenant-saas-with-nestjs-and-postgres-rls-what-held-up-30na</link>
      <guid>https://dev.to/zforzain2000/multi-tenant-saas-with-nestjs-and-postgres-rls-what-held-up-30na</guid>
      <description>&lt;p&gt;I have built multi-tenant SaaS the way most teams build it first: a tenant_id column, a where clause in every query, and a quiet prayer that nobody forgets one. The Healthcare Ireland platform I worked on made the stakes concrete - Org, Branch and Department isolation across a healthcare system, with Socket.io rooms, Prisma tenant middleware, and a BullMQ document pipeline on top. One missing filter there is not a bug report. It is a patient's data on another organisation's screen. The setup that held up, across that platform and the ComplianceBot and recruitment systems after it, is shared tables with Postgres row-level security doing the enforcing. This post is that setup, the traps that break it, and the exact point where you leave it.&lt;/p&gt;

&lt;h2&gt;
  
  
  The short answer: shared tables, RLS, and a hybrid escape hatch
&lt;/h2&gt;

&lt;p&gt;Start with the verdict, because the rest of this post is the evidence for it. For most B2B SaaS, put every tenant in the same tables, give every tenant-owned table a tenant_id column, and enforce the boundary with Postgres row-level security. Run one schema, one migration, one connection pool. Take the tenant from a verified JWT, set it per transaction, and let the database refuse any row that does not belong to it. When a customer contract demands residency, a dedicated database, or a hard isolation guarantee, move that one customer up to a silo and keep everyone else in the pool. The hybrid model is the escape hatch, not the default.&lt;/p&gt;



&lt;h2&gt;
  
  
  The three models, and what each one charges you
&lt;/h2&gt;

&lt;p&gt;Every multi-tenant architecture is one of three models, and the choice is a bill, not a belief. I have seen all three in production, and the honest way to compare them is to price the same work three times.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;Shared tables + RLS&lt;/th&gt;
&lt;th&gt;Schema per tenant&lt;/th&gt;
&lt;th&gt;Database per tenant&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Isolation&lt;/td&gt;
&lt;td&gt;Logical, enforced by the database policy&lt;/td&gt;
&lt;td&gt;Stronger, one namespace per tenant&lt;/td&gt;
&lt;td&gt;Strongest, physically separate&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Migrations per schema change&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;Once per tenant&lt;/td&gt;
&lt;td&gt;Once per tenant&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Connection pooling&lt;/td&gt;
&lt;td&gt;One pool, everyone&lt;/td&gt;
&lt;td&gt;One pool, fragmented by search_path&lt;/td&gt;
&lt;td&gt;One pool per tenant, or reconnects&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;New tenant costs&lt;/td&gt;
&lt;td&gt;One INSERT into tenants&lt;/td&gt;
&lt;td&gt;DDL plus a full migration&lt;/td&gt;
&lt;td&gt;Provision, migrate, back up&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Per-tenant restore&lt;/td&gt;
&lt;td&gt;Hard - rows, not a database&lt;/td&gt;
&lt;td&gt;Schema-level, awkward&lt;/td&gt;
&lt;td&gt;Straightforward&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Cross-tenant leak risk&lt;/td&gt;
&lt;td&gt;Near-zero with RLS, high without it&lt;/td&gt;
&lt;td&gt;Low, a wrong search_path still crosses&lt;/td&gt;
&lt;td&gt;Zero&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Fits&lt;/td&gt;
&lt;td&gt;Most B2B SaaS&lt;/td&gt;
&lt;td&gt;Mid-market with stricter requirements&lt;/td&gt;
&lt;td&gt;Enterprise, regulated, residency&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The shared model wins on every operational axis and loses on exactly one: the boundary is logical, so it depends on the policy being right. That is a real risk, and the rest of this post is about making the policy unbreakable rather than pretending the risk does not exist.&lt;/p&gt;

&lt;p&gt;Schema-per-tenant looks like a clean middle ground until the tenant count grows. Every schema change is N jobs, one failed migration is schema drift, and past roughly 500 schemas the pg_catalog metadata bloat starts eating the buffer cache. For Prisma specifically the break comes earlier: one generated client per schema means the model breaks at roughly 200 to 300 tenants. A database per tenant is the strongest isolation and the worst migration story - backups, monitoring and pools all multiply by the customer count, and onboarding a customer becomes provisioning instead of an INSERT.&lt;/p&gt;

&lt;p&gt;The tiered answer is the one I would defend to a founder: start most tenants on shared tables, and move a tenant up when they ask for exclusivity or their traffic starts impacting others. Build the tenant-resolution layer as an abstraction over the data source early enough that the application code does not change at all when a client moves. That is not a compromise. It is the only model where the default stays cheap and the exception stays possible.&lt;/p&gt;

&lt;h2&gt;
  
  
  RLS in five lines: the policy, the role, and the setting
&lt;/h2&gt;

&lt;p&gt;Row-level security is a filter the database adds to every query, based on who is asking. The whole setup is five lines of SQL, and every line earns its place.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="c1"&gt;-- 1. Every tenant-owned table carries its owner&lt;/span&gt;
&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;invoices&lt;/span&gt; &lt;span class="k"&gt;ADD&lt;/span&gt; &lt;span class="k"&gt;COLUMN&lt;/span&gt; &lt;span class="n"&gt;tenant_id&lt;/span&gt; &lt;span class="kt"&gt;uuid&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;-- 2. Turn policies on, and force them even for the table owner&lt;/span&gt;
&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;invoices&lt;/span&gt; &lt;span class="no"&gt;ENABLE&lt;/span&gt; &lt;span class="k"&gt;ROW&lt;/span&gt; &lt;span class="no"&gt;LEVEL&lt;/span&gt; &lt;span class="no"&gt;SECURITY&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;invoices&lt;/span&gt; &lt;span class="no"&gt;FORCE&lt;/span&gt; &lt;span class="k"&gt;ROW&lt;/span&gt; &lt;span class="no"&gt;LEVEL&lt;/span&gt; &lt;span class="no"&gt;SECURITY&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;-- 3. One policy, reads and writes, on the app role&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="no"&gt;POLICY&lt;/span&gt; &lt;span class="n"&gt;tenant_isolation&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;invoices&lt;/span&gt;
  &lt;span class="k"&gt;TO&lt;/span&gt; &lt;span class="n"&gt;tenant_app&lt;/span&gt;
  &lt;span class="k"&gt;USING&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tenant_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;NULLIF&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;current_setting&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'app.tenant_id'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;''&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;uuid&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;WITH&lt;/span&gt; &lt;span class="k"&gt;CHECK&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tenant_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;NULLIF&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;current_setting&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'app.tenant_id'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;''&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;uuid&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Three things in that policy deserve a second look, because each one is a bug I have seen shipped.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;USING filters, WITH CHECK validates.&lt;/strong&gt; USING decides which rows a query can see and modify. WITH CHECK decides which rows an insert or update may create. Without WITH CHECK, an insert with another tenant's id succeeds quietly. Both halves are required; a policy with only USING is half a boundary.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The role matters more than the policy.&lt;/strong&gt; Superusers and roles with BYPASSRLS always bypass row security, and table owners normally do too. If your app connects as postgres, or as the user that ran the migrations, every policy you just wrote is silently ignored. The fix is two roles: a migration role that owns the tables, and an app role - NOSUPERUSER, NOBYPASSRLS, not the owner - with only SELECT, INSERT, UPDATE and DELETE grants. FORCE ROW LEVEL SECURITY makes the policy apply to the owner as well, which is the belt to the role's braces.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The tenant is set per transaction, never per connection.&lt;/strong&gt; &lt;code&gt;set_config('app.tenant_id', id, true)&lt;/code&gt; with the third argument true is transaction-local: it resets at commit or rollback. A plain SET stays on the connection and leaks into the next request that borrows it from the pool. With a connection pool, transaction-local is not a preference. It is the whole point.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="c1"&gt;-- Inside the transaction, before the first query:&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="nf"&gt;set_config&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'app.tenant_id'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'123e4567-e89b-12d3-a456-426614174000'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And the NULLIF guard in the policy is not decoration. After a SET and then a RESET in a session, &lt;code&gt;current_setting(..., true)&lt;/code&gt; returns an empty string, not NULL, and &lt;code&gt;''::uuid&lt;/code&gt; throws. NULLIF turns that into a NULL that matches no rows instead of an error your users see. Two independent write-ups of this exact trap - one tested on Postgres 18.6, one on 18.7 - both hit the same error, which is how you know it bites real teams.&lt;/p&gt;

&lt;h2&gt;
  
  
  The traps that break RLS in production
&lt;/h2&gt;

&lt;p&gt;The policy above is five lines. The production system around it is where the leaks live. These are the five I check for in review, in the order I would check them.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. The app connects as the owner
&lt;/h3&gt;

&lt;p&gt;This is the most common failure and the quietest. Migrations run as a role that owns the tables, the app reuses the same connection string for convenience, and RLS never applies to a single query. Nothing errors. Nothing logs. The policies exist and do nothing. The test that catches it is one line: query as the app role with no tenant set and confirm zero rows come back. If rows come back, your role is wrong, not your policy.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. A session SET leaks across the pool
&lt;/h3&gt;

&lt;p&gt;The tenant setting is connection state, and connections are shared. A plain &lt;code&gt;SET app.tenant_id = ...&lt;/code&gt; on a pooled connection survives the request and hands the next borrower the previous tenant's rows. The fix is the transaction-local set from the section above, and the test is two requests on one pooled connection with different tenants. The second request must see only its own rows. If you use PgBouncer in transaction mode, the same rule applies - transaction-local settings are the ones that survive pooling correctly.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. The tenant comes from a header the client controls
&lt;/h3&gt;

&lt;p&gt;The tenant id must come from a verified source: the subdomain, which DNS routed before your app saw the request, or the JWT, which you signed. Never the request body, never a query param, never a raw header. A tenant header is a claim, not proof of access, and the middleware must check the caller's membership in that tenant before it trusts the value. I have seen a session cookie outlive a subdomain change after a tenant rename; the middleware that refuses the request instead of guessing is the middleware that earns its keep.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Views and foreign keys ignore the boundary
&lt;/h3&gt;

&lt;p&gt;Two Postgres behaviours surprise teams that test only SELECT on base tables. First, a view runs with its owner's rights unless you set &lt;code&gt;security_invoker = true&lt;/code&gt; (Postgres 15 and later), so a view over a protected table happily returns every tenant's rows to a caller who should see one tenant's. Second, foreign-key checks ignore RLS: with &lt;code&gt;Comment.noteId -&amp;gt; Note.id&lt;/code&gt; alone, one tenant can reference another tenant's row. The fix for the second is composite foreign keys on &lt;code&gt;(id, tenantId)&lt;/code&gt; between tenant tables.&lt;/p&gt;

&lt;p&gt;And the write path has its own hole, one that was only just patched. CVE-2026-120154 let a role with INSERT, UPDATE and applicable SELECT privileges iteratively use &lt;code&gt;INSERT ... ON CONFLICT DO UPDATE&lt;/code&gt; to read rows that row-level-security policies should hide. It was fixed in the November 12, 2026 releases - 18.7, 17.12, 16.16, 15.20 and 14.25, a 52-CVE security drop that also ended Postgres 14 support. The lesson is not "patch Postgres", though you should - 18.7 is the current stable minor and 19.0 went generally available on November 19. The lesson is that a passing SELECT isolation test is insufficient: the write path and conflict handling need tenant-negative tests too. If your test suite only asserts that tenant A cannot SELECT tenant B's rows, you have tested the easy half.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. The cache and the queue do not know about tenants
&lt;/h3&gt;

&lt;p&gt;The database is not the only layer that can leak. I have read the post-mortem shape of this one more than once: the cache key in front of a query did not include the tenant, and for about fifteen minutes one customer's dashboard rendered snippets from another customer's data. The code looked correct, the query had a where clause, the user was authenticated, the role was right - and the cache key did not know about tenants.&lt;/p&gt;

&lt;p&gt;The same hole lives in background jobs. Cron tasks, BullMQ workers, anything outside an HTTP request has no request-scoped context, so a tenant service that reads from the request blows up - or worse, silently runs with no tenant at all. The fix is explicit: the tenant id travels in the job payload, and the worker wraps the run in a &lt;code&gt;runWithTenant(tenantId, ...)&lt;/code&gt; call that sets the context before the first query. The BullMQ post on this site builds the retry and dead-letter side of that story; the tenant id in the payload is the multi-tenancy half. Search the codebase for cache keys and job payloads missing a tenant before launch, not after the first incident.&lt;/p&gt;

&lt;h2&gt;
  
  
  The NestJS wiring: resolve once, carry everywhere
&lt;/h2&gt;

&lt;p&gt;The NestJS shape that held up for me has three pieces, and the order matters: resolve the tenant once at the edge, carry it with AsyncLocalStorage, and enforce it below the call sites.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Resolve once, at the edge.&lt;/strong&gt; Middleware runs first, so by the time guards, interceptors or controllers touch the request, the tenant context is already there. The middleware reads the subdomain or the verified JWT, checks membership, and refuses the request on mismatch. Refusing is cheap. Guessing is the bug.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Carry it with AsyncLocalStorage.&lt;/strong&gt; The middleware sets the tenant, and AsyncLocalStorage threads it through repositories, listeners, queue workers and scheduled jobs - everywhere &lt;code&gt;req&lt;/code&gt; no longer reaches. Request-scoped providers do this too, technically, but they tear down the DI graph per request, and the overhead is measurable once you are past a few thousand requests per second. ALS is close to free, and the deliberate choice is to throw when the store is missing: returning null and trusting downstream code to handle it is how you end up with cross-tenant queries. Fail closed, not open.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;AsyncLocalStorage&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s2"&gt;"node:async_hooks"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;tenantContext&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;AsyncLocalStorage&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;tenantId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getTenantId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;store&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;tenantContext&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getStore&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;store&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"No tenant in context - refusing to query"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;store&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tenantId&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Enforce it below the call sites.&lt;/strong&gt; Prisma's schema language has no syntax for policies, so the RLS SQL lives in a migration - create an empty one with &lt;code&gt;prisma migrate dev --create-only&lt;/code&gt; and write the SQL yourself. Then a Prisma Client Extension applies the tenant filter to every model that has a &lt;code&gt;tenantId&lt;/code&gt;, reading the active tenant from the AsyncLocalStorage context. The old &lt;code&gt;$use&lt;/code&gt; middleware hook is deprecated; new code uses &lt;code&gt;$extends&lt;/code&gt; with a typed &lt;code&gt;query&lt;/code&gt; component scoped per model and operation. Because the rewrite happens inside the client, a developer who calls &lt;code&gt;findMany()&lt;/code&gt; with no &lt;code&gt;where&lt;/code&gt; still gets a tenant-scoped query. That is the application layer doing its job. RLS underneath is the second, independent boundary for the raw query, the bug in the merge logic, and the reporting script - the paths the extension cannot see.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;PrismaClient&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s2"&gt;"@prisma/client"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;prisma&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;PrismaClient&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;$extends&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nv"&gt;$allModels&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nv"&gt;$allOperations&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;operation&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;query&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;tenantId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;getTenantId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="c1"&gt;// Models without tenantId (the tenants table itself) pass through.&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;scoped&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;where&lt;/span&gt;&lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="kt"&gt;object&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;where&lt;/span&gt;
          &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;where&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;where&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;object&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;where&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;tenantId&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
          &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;where&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;tenantId&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;scoped&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two honesty notes on that snippet. First, it is simplified: a production extension scopes per model, skips the tenants table and any genuinely global tables, and handles nested writes and &lt;code&gt;updateMany&lt;/code&gt; explicitly - the merge logic is the part you test hardest. Second, if you would rather not own the extension, the ecosystem has caught up: &lt;code&gt;@nestarc/tenancy&lt;/code&gt; (0.16.4, November 2026) carries the authorised tenant id through AsyncLocalStorage and sets the Postgres transaction-local context for Prisma, with an RLS-first design and a &lt;code&gt;check&lt;/code&gt;/&lt;code&gt;doctor&lt;/code&gt; command that verifies roles and policies at startup. I have not run it in production, so I am naming it as an option, not a recommendation - but the shape it encodes is exactly the shape above.&lt;/p&gt;

&lt;p&gt;One version note before you copy the stack: Prisma 7.10.1 is the stable line as of this week. Prisma 8 is still a release candidate with no general-availability date announced - &lt;code&gt;npm install prisma&lt;/code&gt; already installs the RC, so pin your version deliberately. The code above is written against Prisma 7, NestJS 11 and Postgres 18.7, the combination that was verified end to end in October and November 2026.&lt;/p&gt;

&lt;h2&gt;
  
  
  When to leave the shared pool
&lt;/h2&gt;

&lt;p&gt;The shared model is the default, not a religion. Three triggers move a tenant up, and each one has a receipt you can point at.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A contract says so.&lt;/strong&gt; Data residency, a dedicated database, a custom backup retention window, a hard isolation guarantee in an enterprise agreement. As a rule of thumb, this is the high-ACV enterprise tier - one practitioner guide puts the line at roughly $25,000 a year - and the figure is a judgment call, not a measurement. The point is the direction: the customer paying for a silo is the customer whose contract names one.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A noisy neighbour.&lt;/strong&gt; One tenant does 10x the volume of everyone else and the shared pool feels it. Before you move them, check the cheaper fixes: per-tenant rate limits, a separate read replica for reporting, statement timeouts per tenant. A silo is the last resort for a performance problem, not the first.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A per-tenant restore you cannot do from rows.&lt;/strong&gt; If a customer can demand their data back as a database, or a restore that must not touch anyone else, rows in a shared table make that a project instead of a command. This is the one trigger that is genuinely hard to retrofit, so name it in the design doc before launch even if no customer needs it yet.&lt;/p&gt;

&lt;p&gt;None of these triggers is a reason to start on silos. Starting on silos means paying the migration, backup and pooling bill for every customer from day one, including the ones who will never ask. Start shared, keep the resolution layer abstract, and move tenants up one at a time with the contract or the measurement as the receipt.&lt;/p&gt;

&lt;h2&gt;
  
  
  The launch checklist, as six checks
&lt;/h2&gt;

&lt;p&gt;Everything above compresses into six checks. Run them on your real app, in order, before the first paying tenant lands.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;RLS is enabled AND forced on every tenant-owned table.&lt;/strong&gt; ENABLE plus FORCE, in the same migration as the table. A table added next month needs the same two lines.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The app connects as a non-owner role.&lt;/strong&gt; No SUPERUSER, no BYPASSRLS, not the owner. Query as the app role with no tenant set: zero rows, or the role is wrong.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The tenant is set per transaction.&lt;/strong&gt; &lt;code&gt;set_config(..., true)&lt;/code&gt;, never a plain SET. Two requests on one pooled connection, different tenants, each seeing only its own rows.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Every policy uses the NULLIF guard.&lt;/strong&gt; &lt;code&gt;NULLIF(current_setting('app.tenant_id', true), '')::uuid&lt;/code&gt; - the empty-string trap is the error your users see if you skip it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Tenant-negative tests cover the write path.&lt;/strong&gt; Not just SELECT: updates, deletes, inserts into another tenant, ON CONFLICT probes, cross-tenant foreign keys. CVE-2026-120154 is the reason this item exists.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cache keys and job payloads carry the tenant.&lt;/strong&gt; A cache key without a tenant is a leak with a TTL. A BullMQ job without a tenant in the payload is a worker running blind.&lt;/li&gt;
&lt;/ol&gt;



&lt;h2&gt;
  
  
  My verdict
&lt;/h2&gt;

&lt;p&gt;Build on shared tables with Postgres RLS, a Prisma client extension on top, and the tenant resolved once from a verified JWT and carried in AsyncLocalStorage. That is the setup I would defend for any B2B SaaS whose customers are roughly the same shape: one schema, one migration, one pool, and a database that refuses foreign rows even when the application is wrong.&lt;/p&gt;

&lt;p&gt;The conditions that would flip it: a customer contract that names residency or a dedicated database - then that customer gets a silo and nobody else does. A tenant whose traffic starves the pool - then rate-limit and isolate that tenant first, and move them only if the measurements say so. A restore requirement that needs a whole database per customer - then the silo model from the start, with automated provisioning, because hand-run silos are a schema-drift incident waiting for its first schema change.&lt;/p&gt;

&lt;p&gt;What I would not do, in any of those cases, is the thing I did on my first multi-tenant build: trust the where clause. A where clause is a request. A policy is a contract. The difference between them is the afternoon your support team spends explaining to a healthcare organisation why another organisation's data was on their screen.&lt;/p&gt;

&lt;p&gt;Not sure which model fits your SaaS? Answer four questions and I will place you:&lt;/p&gt;



&lt;p&gt;If you are building a multi-tenant SaaS and want the isolation layer, the tenant resolution, and the launch checks built as one system instead of bolted on one feature at a time, that is the layer I build for teams. &lt;a href="https://dev.to/#contact"&gt;Tell me about your project&lt;/a&gt; and I will tell you honestly whether you need shared tables, a hybrid tier, or nothing at all.&lt;/p&gt;

&lt;p&gt;Related reading: &lt;a href="https://dev.to/blog/bullmq-nestjs-retries-idempotency-dead-letter-queue"&gt;BullMQ in NestJS: Retries, Idempotency and Dead-Letter Queues&lt;/a&gt; for the job layer this post's tenant payload rides on, &lt;a href="https://dev.to/blog/scaling-socketio-rooms-nestjs-redis-adapter"&gt;Scaling Socket.io Rooms in NestJS With the Redis Adapter&lt;/a&gt; for the real-time rooms the Healthcare Ireland platform runs on, and &lt;a href="https://dev.to/blog/zod-as-a-contract"&gt;Zod as a Contract, Not a Comment&lt;/a&gt; for the schema-first discipline the same systems use at every boundary. My case studies live at &lt;a href="https://dev.to/work/healthcare-ireland"&gt;Healthcare Ireland&lt;/a&gt; and &lt;a href="https://dev.to/work/compliancebot-ai"&gt;ComplianceBot AI&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  FAQ
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Is shared-schema multi-tenancy safe enough for paying customers?
&lt;/h3&gt;

&lt;p&gt;Yes, when the isolation is enforced in two independent layers: a Prisma client extension that scopes every query to the tenant, and Postgres row-level security that refuses foreign rows even when the application layer is wrong. One layer alone is a convention; two layers is a guarantee. Add tenant-negative tests on the write path and you have the setup I would defend to a customer.&lt;/p&gt;

&lt;h3&gt;
  
  
  Does RLS slow down queries?
&lt;/h3&gt;

&lt;p&gt;The policy is applied by the query planner, not as a post-filter, so rows from other tenants are never read from disk. The cost is the predicate itself, which a composite index leading with tenant_id keeps cheap. What does cost you is a missing index: without one, the policy turns a tenant-scoped lookup into a sequential scan, so every tenant-owned table needs that index.&lt;/p&gt;

&lt;h3&gt;
  
  
  Should I use Prisma Client Extensions or rely on RLS alone?
&lt;/h3&gt;

&lt;p&gt;Use both. The extension makes the common path correct and ergonomic: a developer calling findMany with no where clause still gets a tenant-scoped query. RLS is the backstop for the paths the extension cannot see: raw queries, a bug in the merge logic, a reporting script. The extension is convenience; RLS is the guarantee. Neither replaces the other.&lt;/p&gt;

&lt;h3&gt;
  
  
  What happens when one tenant outgrows the shared pool?
&lt;/h3&gt;

&lt;p&gt;You move that one tenant up a tier. Build the tenant-resolution layer as an abstraction over the data source from day one, so the application code does not change when a tenant moves from shared tables to a schema or a dedicated database. The trigger is a contract (residency, isolation guarantee) or a measured problem (one tenant's traffic starving the rest), never a feeling.&lt;/p&gt;

&lt;h3&gt;
  
  
  How do I migrate from schema-per-tenant to shared tables?
&lt;/h3&gt;

&lt;p&gt;It is a data migration, not a rewrite: add tenant_id to every table, backfill it from the schema name, enable RLS with the policy, then cut reads over behind a flag and verify with tenant-negative tests before you drop the old schemas. The hard part is the same as any migration - proving the new path returns exactly the old rows - so keep both paths live until the tests pass on production data.&lt;/p&gt;

&lt;h3&gt;
  
  
  Does Supabase change any of this?
&lt;/h3&gt;

&lt;p&gt;No - it leans on it. Supabase's data API relies on RLS for authorization, which is why ComplianceBot runs its tenant boundary there. The same rules apply: a non-owner app role, the tenant set per transaction from a verified JWT, policies with USING and WITH CHECK, and tests that try to read across tenants. Supabase removes the hosting, not the thinking.&lt;/p&gt;

&lt;h2&gt;
  
  
  The tools I would actually use
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Tool&lt;/th&gt;
&lt;th&gt;What it is&lt;/th&gt;
&lt;th&gt;What I use it for&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;PostgreSQL 18.7&lt;/td&gt;
&lt;td&gt;Open-source database, free&lt;/td&gt;
&lt;td&gt;RLS policies, the tenant boundary, one schema and one pool&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;NestJS 11&lt;/td&gt;
&lt;td&gt;TypeScript backend framework, free and open source&lt;/td&gt;
&lt;td&gt;Middleware, AsyncLocalStorage tenant context, the API layer&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Prisma 7.10.1&lt;/td&gt;
&lt;td&gt;TypeScript ORM, free and open source&lt;/td&gt;
&lt;td&gt;Client Extensions for tenant scoping, migrations for the RLS SQL&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;PgBouncer&lt;/td&gt;
&lt;td&gt;Connection pooler, free and open source&lt;/td&gt;
&lt;td&gt;Transaction-mode pooling that respects transaction-local settings&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Supabase&lt;/td&gt;
&lt;td&gt;Hosted Postgres with an RLS-first data API, free tier available&lt;/td&gt;
&lt;td&gt;The managed path - ComplianceBot runs its tenant boundary here&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;{/* MONETIZATION NOTE: Postgres, NestJS, Prisma, PgBouncer and Supabase's free tier have no affiliate program to swap in. The money path for this post is freelance/consulting leads: founders and hiring managers evaluating a multi-tenant build hire the author, which is why the CTA points at /#contact. */}&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>nestjs</category>
      <category>postgres</category>
      <category>prisma</category>
    </item>
    <item>
      <title>How to Test Your AI Coding Agent With Stripe Evals</title>
      <dc:creator>MUHAMMAD ZAIN UL ABIDIN</dc:creator>
      <pubDate>Fri, 20 Nov 2026 08:24:58 +0000</pubDate>
      <link>https://dev.to/zforzain2000/how-to-test-your-ai-coding-agent-with-stripe-evals-453h</link>
      <guid>https://dev.to/zforzain2000/how-to-test-your-ai-coding-agent-with-stripe-evals-453h</guid>
      <description>&lt;p&gt;Your coding agent's Stripe integration compiles, the tests pass, and the demo charges a test card. Mine did too, and that told me almost nothing. On November 19, Stripe published the measurement I did not have: across 69 realistic payment scenarios and 14 model and harness pairs, agents averaged 78.1% of the required criteria - but only 29.4% of runs passed every must-have criterion. The other 70% were almost right, and in payments, almost right is a double charge, a wrong split, or an order shipped after a refund. Stripe also open-sourced the harness, Forge, so you can run the same test on your own agent before a customer runs it for you. This post is the how-to: what the benchmark measures, how to run it step by step, what it costs, and how to read the one number that actually predicts production.&lt;/p&gt;

&lt;h2&gt;
  
  
  The 29.4% is the only number in the launch that matters
&lt;/h2&gt;

&lt;p&gt;Stripe reports two scores, and the difference between them is the whole story.&lt;/p&gt;

&lt;p&gt;The headline score, the "integration success rate," is a mean: every criterion in a case is graded separately, MUST and SHOULD together, and the run gets the fraction it satisfied. A scenario with 7 MUST criteria and 1 SHOULD where the agent satisfies 6 scores 75%. That is partial credit, and partial credit is how the pooled average reaches a comfortable 78.1%.&lt;/p&gt;

&lt;p&gt;The strict pass rate removes the comfort. A case passes only if &lt;strong&gt;every MUST criterion passes&lt;/strong&gt; - binary, no fractions. Under strict grading, the same runs collapse to 29.4%. Stripe's own summary is blunt: many models "create almost correct implementations and have difficulty with the last mile."&lt;/p&gt;

&lt;p&gt;Here is what the last mile looks like in practice. In the &lt;code&gt;payments-radar-review-fulfillment&lt;/code&gt; scenario, the agent must hold orders flagged for manual fraud review and release them only when the review is approved. Opus 5.5 checked only that &lt;code&gt;PaymentIntent.status === 'succeeded'&lt;/code&gt; and never checked whether the order was still eligible for fulfillment - which means an order can ship after it was refunded or disputed. I read that case's rubric myself in the open-source dataset: 8 MUST criteria, including "Release a reviewed order only when the authoritative review is approved and the payment/order is still eligible for fulfillment" and "MUST NOT treat every &lt;code&gt;review.closed&lt;/code&gt; event as approval." The code that failed it would pass a demo, a compile check, and most test suites. It fails the rubric, and it would fail a real customer.&lt;/p&gt;

&lt;p&gt;That is why I test agents this way now, and why you should too: &lt;strong&gt;a payment integration is not 78% correct. It is correct, or it moves money wrongly.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The scoreboard (interactive on zainvibes.com - here are the strict numbers that matter):&lt;/strong&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Model (harness)&lt;/th&gt;
&lt;th&gt;Average, no plugin -&amp;gt; plugin&lt;/th&gt;
&lt;th&gt;Strict pass, no plugin -&amp;gt; plugin&lt;/th&gt;
&lt;th&gt;Cost per run&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;GPT-6 Astra (Codex)&lt;/td&gt;
&lt;td&gt;85.5% -&amp;gt; 90.0%&lt;/td&gt;
&lt;td&gt;41.9% -&amp;gt; 52.9%&lt;/td&gt;
&lt;td&gt;$2.93 -&amp;gt; $3.63&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;GPT-6.1 Sol (Codex)&lt;/td&gt;
&lt;td&gt;85.6% -&amp;gt; 89.6%&lt;/td&gt;
&lt;td&gt;39.0% -&amp;gt; 50.7%&lt;/td&gt;
&lt;td&gt;$0.51 -&amp;gt; $0.61&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Opus 5.5 (Claude Code)&lt;/td&gt;
&lt;td&gt;83.5% -&amp;gt; 86.2%&lt;/td&gt;
&lt;td&gt;35.3% -&amp;gt; 36.0%&lt;/td&gt;
&lt;td&gt;$2.77 -&amp;gt; $3.15&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Fable 5.2 (Claude Code)&lt;/td&gt;
&lt;td&gt;81.1% -&amp;gt; 85.2%&lt;/td&gt;
&lt;td&gt;28.4% -&amp;gt; 43.2%&lt;/td&gt;
&lt;td&gt;$5.76 -&amp;gt; $5.35&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Sonnet 5.5 (Claude Code)&lt;/td&gt;
&lt;td&gt;78.0% -&amp;gt; 82.6%&lt;/td&gt;
&lt;td&gt;29.4% -&amp;gt; 33.1%&lt;/td&gt;
&lt;td&gt;$1.07 -&amp;gt; $1.16&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;GPT-6 Luna (Codex)&lt;/td&gt;
&lt;td&gt;72.0% -&amp;gt; 79.1%&lt;/td&gt;
&lt;td&gt;17.3% -&amp;gt; 29.1%&lt;/td&gt;
&lt;td&gt;$0.05 -&amp;gt; $0.07&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Gemini 3.8 Flash (OpenCode)&lt;/td&gt;
&lt;td&gt;64.8% -&amp;gt; 72.7%&lt;/td&gt;
&lt;td&gt;11.2% -&amp;gt; 21.6%&lt;/td&gt;
&lt;td&gt;$2.21 -&amp;gt; $2.67&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Pooled across all 14 pairs: 78.1% average score, 29.4% strict pass rate (no-plugin arm).&lt;/p&gt;

&lt;h2&gt;
  
  
  What Stripe evals actually measures
&lt;/h2&gt;

&lt;p&gt;The benchmark is a conversation, not a unit test. Each of the 69 scenarios hands the coding agent a short, realistic request - build a hosted checkout, implement metered billing, manage per-seat subscriptions, split marketplace payments - and then a second agent role-plays the developer. The roleplayer (Stripe's harness calls her Ava) derives a private set of two to four needs from the scenario, never shows the full list, and asks follow-ups one at a time, the way a real client does. She never writes code and never sees the grading rubric. The agent works in an isolated workspace with its normal tools and web search, over multiple turns, up to a 45-minute timeout per case.&lt;/p&gt;

&lt;p&gt;The 69 cases split into 24 one-time payments, 26 subscriptions, and 19 Connect (marketplaces and platforms). Every case carries a rubric of criteria labelled with RFC 2119 language: MUST (required for a valid integration), SHOULD (recommended), and in the harness, OPTIONAL (recorded, not scored). Judges grade each criterion separately, from two evidence sources only - the session transcript and the workspace snapshot. The agent's private reasoning is excluded. Stripe used GPT-6.1 Sol as both roleplayer and judge across all runs, and probed for judge bias by re-grading with Opus: scores moved in lockstep, with Opus uniformly more lenient, so the rankings are not a home-team artifact.&lt;/p&gt;

&lt;p&gt;Each agent ran every case twice, in two arms - with and without the Stripe agent plugin - for 276 total runs per agent. Three harnesses were tested: Claude Code, Codex, and OpenCode, across 14 model pairings from Haiku 5.5 to GPT-6 Astra.&lt;/p&gt;

&lt;p&gt;One piece of context before you quote the numbers anywhere: Stripe ran an earlier, smaller benchmark in March 2026 (11 environments; that is where the "Opus 4.5 scored 92%" figure you may have seen comes from). The November evals are the public, expanded version - 69 cases, a roleplayer, strict grading, and a harness you can run. Do not mix the two sets of numbers; some launch coverage already has.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to run it: Forge, step by step
&lt;/h2&gt;

&lt;p&gt;The harness is called &lt;strong&gt;Forge&lt;/strong&gt; (github.com/stripe/forge, Apache-2.0). I cloned, installed, and built it while writing this post, and validated the sample experiment end to end. I did not run a scored eval: that needs an Anthropic key, an OpenAI key, and Docker, and it makes billable model calls - none of which I spend on a benchmark run without it being the point. What follows is the exact path, with the one step that costs nothing marked clearly, so you can verify your setup before you spend a cent.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 0: What you need
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Node.js 22.13 or newer, and pnpm 11.6.0&lt;/li&gt;
&lt;li&gt;Docker that can run linux/amd64 containers (the agent runs inside one)&lt;/li&gt;
&lt;li&gt;An Anthropic API key for the coding agent (Claude Code), and an OpenAI API key for Ava and the judge, which default to an OpenAI model&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;jq&lt;/code&gt;, for reading the results&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Step 1: Clone and build
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git clone https://github.com/stripe/forge.git
&lt;span class="nb"&gt;cd&lt;/span&gt; forge
corepack &lt;span class="nb"&gt;enable&lt;/span&gt; &lt;span class="c"&gt;# skip if pnpm 11.6.0 is already installed&lt;/span&gt;
pnpm install --frozen-lockfile
pnpm build
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The build took under a minute on my machine. One real-world note: on my Node 24 setup, corepack did not provide pnpm, and the README's fallback worked - &lt;code&gt;npm install -g pnpm@11.6.0&lt;/code&gt;. If &lt;code&gt;pnpm&lt;/code&gt; is not found after &lt;code&gt;corepack enable&lt;/code&gt;, that is the fix, not a Forge bug.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 2: Validate the sample experiment (free, no keys, no Docker)
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;node dist/cli.js validate --experiment release/experiment.yaml
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the step I ran, and it is the step that makes the how-to honest. My output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nl"&gt;"dataset"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nl"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="s2"&gt;"stripe-integration-public-evals"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nl"&gt;"version"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="s2"&gt;"1.0.0"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nl"&gt;"experiment"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nl"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="s2"&gt;"stripe-integration-public-evals/baseline"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nl"&gt;"version"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="s2"&gt;"0.5.0"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nl"&gt;"run_count"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nl"&gt;"valid"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The sample experiment pins the public dataset (v1.0.0), one case (&lt;code&gt;payments-onetime&lt;/code&gt;, a hosted Checkout integration), the baseline treatment, and Claude Code running &lt;code&gt;claude-sonnet-4-6&lt;/code&gt;. If your validate does not print &lt;code&gt;"valid":true&lt;/code&gt;, stop and fix the setup here - this check exists so the paid steps below are not your debugger.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 3: Run one case
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="kd"&gt;export&lt;/span&gt; &lt;span class="nv"&gt;ANTHROPIC_API_KEY&lt;/span&gt;&lt;span class="o"&gt;=&amp;lt;&lt;/span&gt;your key&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="kd"&gt;export&lt;/span&gt; &lt;span class="nv"&gt;OPENAI_API_KEY&lt;/span&gt;&lt;span class="o"&gt;=&amp;lt;&lt;/span&gt;your key&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;

node dist/cli.js run \
  --experiment release/experiment.yaml \
  --credential-env ANTHROPIC_API_KEY \
  --credential-env OPENAI_API_KEY \
  --output ./results --run-id payments-001 &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; run-summary.json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two details worth knowing before you run it. First, Forge reads a key from your environment only when you grant it with &lt;code&gt;--credential-env&lt;/code&gt; - keys are never ambient. Second, the first run builds the Docker runtime image, so it needs network access and takes longer; progress goes to stderr. Exit code 0 means the run finished and the report was saved - whatever the score was.&lt;/p&gt;

&lt;p&gt;Budget honestly. Stripe's published cost per run ranges from $0.05 (GPT-6 Luna, no plugin) to $6.13 (Fable 5.1, with plugin), and a single case takes roughly 5 to 28 minutes of agent time depending on the model. One case is a cheap afternoon experiment. The full matrix - 69 cases, 2 repetitions, 2 arms, several models - is a line item.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 4: Read the strict number, not the headline
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;jq .evaluation &lt;span class="s2"&gt;"$(jq -r .location run-summary.json)/runs/payments-001/result.json"&lt;/span&gt;

jq &lt;span class="s1"&gt;'.cases[] | {case_id, treatment, agent, completed, errored, judge_errors, pass_rate, criterion_scores}'&lt;/span&gt; \
  &lt;span class="s2"&gt;"$(jq -r .report run-summary.json)"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each run's &lt;code&gt;result.json&lt;/code&gt; carries an &lt;code&gt;evaluation&lt;/code&gt;: a score (the fraction of valid MUST and SHOULD judgments that passed), a status, and one entry per criterion with the judge's explanation. The aggregated &lt;code&gt;report.json&lt;/code&gt; separates what the launch coverage blurred: &lt;code&gt;criterion_scores&lt;/code&gt; reports the &lt;strong&gt;required&lt;/strong&gt; set (MUST only - your strict pass rate) apart from the &lt;strong&gt;recommended&lt;/strong&gt; set (MUST plus SHOULD). Read the required set first. The recommended set will flatter your agent; the required set is the production prediction.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 5: Add the plugin arm and run it again
&lt;/h3&gt;

&lt;p&gt;The comparison that produced the launch's second headline - every agent improving with the plugin - is a treatment in the experiment file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;treatments&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;baseline&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;setup&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="na"&gt;stripe-plugin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;setup&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;forge/plugin&lt;/span&gt;
        &lt;span class="na"&gt;with&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
          &lt;span class="na"&gt;plugin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;stripe&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add that treatment, list both in the experiment's matrix, and re-run the same case. One caveat from Forge's own docs, and it matters if you publish comparisons: the plugin action installs the marketplace's current version (&lt;code&gt;stripe@claude-plugins-official&lt;/code&gt;) with &lt;strong&gt;no version pin&lt;/strong&gt; - identical experiment files on different dates can install different plugin bytes. Record the run date next to any numbers you share.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The run checklist, in short:&lt;/strong&gt; (1) clone and build Forge; (2) validate the sample experiment - free, no keys, no Docker; (3) grant keys per run with &lt;code&gt;--credential-env&lt;/code&gt; and set the budget first; (4) run one baseline case end to end; (5) read the strict (required) pass rate, not the headline score; (6) add the plugin arm and re-run the same case.&lt;/p&gt;

&lt;h2&gt;
  
  
  What the results say when you read them strictly
&lt;/h2&gt;

&lt;p&gt;Sorted by the strict, with-plugin numbers (the scoreboard above has the full table), three patterns matter for your own stack decisions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The leaders are Codex models, and even they pass barely half.&lt;/strong&gt; GPT-6 Astra reaches a 52.9% strict pass rate with the plugin, GPT-6.1 Sol 50.7%. The best Claude Code pairing, Fable 5.2, gets to 43.2%. Read that again: the best agent Stripe could test, with Stripe's own plugin helping it, still fails almost half of realistic payment scenarios on at least one must-have criterion. Anyone shipping agent-written checkout code unreviewed is accepting those odds with real money.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Subscriptions are where agents go to die.&lt;/strong&gt; In Stripe's per-category table, subscriptions is the weakest category for nearly every pair - the best score there is Sol's 84% average (with plugin), and GLM 5.3 manages 56%. One-time payments are the strongest category, and Connect gains the most from the plugin. If your agent builds billing - trials, proration, per-seat changes, test clocks - it is working in exactly the category where the measured failure rate is highest. Weight your review accordingly.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Cost and quality barely correlate.&lt;/strong&gt; The cheapest run on the board, GPT-6 Luna at $0.05, posts a 29.1% strict rate with the plugin - better than several pairings that cost forty times more. The most expensive, Fable 5.1 at $6.13 with the plugin, reaches 36.4% strict. And the slowest agent, Minimax M3.1 Flash at roughly 28 minutes per eval, lands mid-pack. Price buys you the top of the table (Astra, Sol) or it buys you nothing; the middle is a lottery. This is the second reason to run the eval yourself: the right model for your scenarios is an empirical question, and Forge is how you answer it for the price of a few runs.&lt;/p&gt;

&lt;h2&gt;
  
  
  The plugin is the cheapest points you will ever buy
&lt;/h2&gt;

&lt;p&gt;Whatever else you take from this post, install the Stripe agent plugin in the agent you already use. It bundles Stripe-authored skills with MCP tools - an implementation planner, documentation search, and API lookup - and the measured effect is not subtle:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Every one of the 14 pairs scored higher with it&lt;/strong&gt;, by 2.7 to 7.9 points on average score.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Outdated parameter references fell from 7% to 1.3%&lt;/strong&gt; of the parameters in API calls. Stripe's example: without the plugin, GPT-6.1 Sol wrote the recently removed &lt;code&gt;payment_method_types: ['card']&lt;/code&gt;; with it, the model writes &lt;code&gt;automatic_payment_methods: { enabled: true }&lt;/code&gt; and gets Dynamic Payment Methods. This is the pretraining-staleness problem, measured and then mostly fixed by giving the agent current, authoritative docs at the moment of writing.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Web searches per eval fell 96%&lt;/strong&gt; (1.26 to 0.05 pooled) and page fetches fell 63% - the agent stops researching Stripe on the open web and uses the plugin's guidance instead. Behaviour differs by model in an interesting way: Fable 5.1 made 53% &lt;em&gt;more&lt;/em&gt; tool calls with the plugin, Gemini 3.8 Flash made 26% &lt;em&gt;fewer&lt;/em&gt;. The plugin changes how agents work, not just what they score.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Agents wrote about 40% fewer test lines&lt;/strong&gt; and ran tests in 42% of evals versus 53% without it. Stripe is careful here, and I will be too: this does not mean fewer tests caused better scores. It means authoritative guidance reduced the need for excessive checking. Do not read it as permission to test less - your own tests are the runtime verification this benchmark deliberately does not do.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What Forge cannot tell you
&lt;/h2&gt;

&lt;p&gt;A benchmark you cannot criticise is a benchmark you should not trust, and to Stripe's credit, the sharpest criticisms are in its own docs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;No runtime verification.&lt;/strong&gt; Forge never executes the agent's code after the session. Its deterministic checks cover file creation, existence, regex content, and tool-call names and arguments; the rest is a judge model reading the transcript and the workspace. Stripe's methodology page states the consequence plainly: "A well-shaped but non-functional integration can score full marks. The benchmark asserts correct specification, not correct execution." A Forge pass means the agent made the right decisions on paper. Whether the webhook handler actually verifies signatures at runtime, on your stack, with your framework's body parsing - that is still your test suite's job.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The judge is a model.&lt;/strong&gt; Criteria are graded by an LLM, per criterion, with an explanation. Stripe mitigated this (per-criterion judging to prevent cross-contamination, a bias probe with a second judge), and Forge ships &lt;code&gt;forge regrade&lt;/code&gt; so you can re-judge saved evidence with a different model. But judge variance is real, reruns are not identical, and the strict metric is, in Stripe's own words, "sensitive to any data or judgement errors." Treat a single run as a reading, not a verdict: the harness supports repetitions for a reason.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;One domain, early days.&lt;/strong&gt; The bundled dataset is 69 cases, all Stripe. Forge's README calls itself "in early development," the runtime is Docker-only and local, and results say little about your agent's Prisma migrations or your WebSocket layer. The transferable part is the method - roleplayer, rubric, strict grading - which you can point at your own cases once you trust it on Stripe's.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Which posture fits you?&lt;/strong&gt; Real money on agent-written code with no strict gate today: run Forge before it ships. Mostly worried about outdated API patterns: plugin on, plus a strict self-check of your own diff. Prototype or a review habit that already catches dangerous diffs: agent drafts, you verify every money path - and graduate to Forge when the agent touches a real checkout. The full picker is interactive on zainvibes.com.&lt;/p&gt;

&lt;h2&gt;
  
  
  My verdict
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Default: install the plugin today, and run Forge before any agent-built payment code ships.&lt;/strong&gt; The plugin is free, takes minutes, and buys measured points on every model - there is no engineering argument against it. The eval run is the gate that turns "the agent seemed to do fine" into a strict pass rate you can say out loud in a review. For a single case it costs less than the coffee next to your keyboard.&lt;/p&gt;

&lt;p&gt;The conditions that would flip pieces of that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Prototype or internal demo, no real money:&lt;/strong&gt; plugin only. Skip the eval run until the code is headed for a real checkout - then run it before, not after.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Marketplace or platform money (Connect):&lt;/strong&gt; full posture, no exceptions. Connect scenarios involve the most scenario-specific decisions, your failure modes include other people's payouts, and "the agent wrote it" is not an answer you give a seller whose split was wrong.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;You already read every money-path diff line by line:&lt;/strong&gt; keep doing that, add the plugin, and run Forge when the surface grows - a second integration, a new model, a harness switch. All three move the score, and your eyes do not scale.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The agent is not touching payments at all:&lt;/strong&gt; Forge's dataset will not measure your risk. Steal the method instead - a roleplayer with private needs, a MUST/SHOULD rubric, strict grading - and write cases for the integration that can actually hurt you.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;What I would not do, after reading all 69 cases and the full results table: let any current agent write a subscription flow or a fulfillment gate unsupervised. The measured strict pass rates - 29.4% pooled, 52.9% for the very best - are not an insult to the models. They are a description of the job. Payments punish the last mile, and the last mile is exactly where agents stop checking.&lt;/p&gt;

&lt;h2&gt;
  
  
  The stack in this post
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Piece&lt;/th&gt;
&lt;th&gt;What it is&lt;/th&gt;
&lt;th&gt;Cost&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Forge&lt;/td&gt;
&lt;td&gt;Stripe's open-source eval harness (Apache-2.0)&lt;/td&gt;
&lt;td&gt;Free&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;stripe-integration-public-evals&lt;/td&gt;
&lt;td&gt;The 69-case public dataset, v1.0.0&lt;/td&gt;
&lt;td&gt;Free&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Stripe agent plugin&lt;/td&gt;
&lt;td&gt;Skills + MCP tools for your coding agent&lt;/td&gt;
&lt;td&gt;Free&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Claude Code / Codex / OpenCode&lt;/td&gt;
&lt;td&gt;The harnesses Forge runs&lt;/td&gt;
&lt;td&gt;Your existing setup&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Anthropic + OpenAI API keys&lt;/td&gt;
&lt;td&gt;Agent model, plus Ava and the judge&lt;/td&gt;
&lt;td&gt;Billable per run ($0.05-$6.13 per case at Stripe's numbers)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Docker&lt;/td&gt;
&lt;td&gt;Runs the agent in an isolated linux/amd64 container&lt;/td&gt;
&lt;td&gt;Free&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;If you are about to let an agent build the billing flow for a product that charges real cards, this is the cheapest insurance in the stack: an afternoon, a few dollars of model calls, and a strict number you can defend. If you would rather have the integration built and verified by a human who reads rubrics for a living, &lt;a href="https://dev.to/#contact"&gt;that is the work I take on&lt;/a&gt; - bring the scenario list, and we will grade the result the same strict way.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Sources: Stripe, "Stripe evals: Measuring real-world coding agent performance" (stripe.dev, November 19, 2026) - all benchmark figures, tables, and methodology. Forge README and docs, github.com/stripe/forge - quickstart, plugin treatment, result format, and stated limits. Hands-on notes: I cloned and built Forge and validated the sample experiment on November 20, 2026; I did not run a scored eval, which requires billable API keys, and no figure in this post comes from a run of my own.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>stripe</category>
      <category>ai</category>
      <category>testing</category>
      <category>typescript</category>
    </item>
    <item>
      <title>Zod-Validated LLM Output: The Repair Loop That Makes It Safe to Ship</title>
      <dc:creator>MUHAMMAD ZAIN UL ABIDIN</dc:creator>
      <pubDate>Tue, 17 Nov 2026 06:04:43 +0000</pubDate>
      <link>https://dev.to/zforzain2000/zod-validated-llm-output-the-repair-loop-that-makes-it-safe-to-ship-3b00</link>
      <guid>https://dev.to/zforzain2000/zod-validated-llm-output-the-repair-loop-that-makes-it-safe-to-ship-3b00</guid>
      <description>&lt;p&gt;Every model in production returns JSON that looks right and is not. I learned this the expensive way: a ticket triage feature that fed model output straight into the database, because the prompt said "return JSON only" and the first fifty calls obeyed. Call fifty-one returned a fenced code block with a hallucinated category, and the row it wrote took a support agent an afternoon to untangle. The fix was not a better prompt. It was a contract: a Zod schema that validates every model response, a salvage step for the failures a regex can fix, a bounded retry that feeds the validation error back to the model, and a dead-letter queue for whatever survives. This post is that loop, measured, with the provider matrix that decides which parts of your schema each vendor actually enforces.&lt;/p&gt;

&lt;h2&gt;
  
  
  Structured outputs guarantee shape, not truth
&lt;/h2&gt;

&lt;p&gt;Start with the sentence every vendor writes in its own docs, in different words: structured output constrains what tokens the model can emit, so the response matches your schema. It says nothing about whether the values are right. A schema-valid response can carry a wrong total, an invented SKU, or a confident enum value nobody would have picked. OpenAI, Anthropic and Google all say this. The guarantee is shape. Truth is still your job.&lt;/p&gt;

&lt;p&gt;There are three layers of failure, and only the first two are what most tutorials cover:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax failures.&lt;/strong&gt; The output does not parse: a trailing comma, an unclosed brace, prose wrapped around the JSON, a markdown fence. Constrained decoding eliminates most of these, but only when you actually use a strict mode. Prompt-enforced JSON - "return JSON only" in the prompt - does not.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Schema failures.&lt;/strong&gt; The output parses but does not match the contract: a key named &lt;code&gt;invoiceNo&lt;/code&gt; instead of &lt;code&gt;invoice_number&lt;/code&gt;, a string where a number was required, an enum value paraphrased instead of exact, a nested object on the wrong discriminated-union branch. Strict modes prevent most of these at decode time. They cannot prevent the next layer.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Semantic failures.&lt;/strong&gt; Valid, well-typed, and wrong. The best clean tool-use rate in an October 2026 benchmark across 19 models on 1,000 single-turn tasks was about 86 percent. The other 14 percent were wrong, skipped, or unnecessary calls and fabricated results - none of them malformed JSON. An agent that never produces malformed calls can still be wrong 15 to 20 percent of the time. No decoder fixes that. Only validation of business rules the schema cannot express, and a human in the loop for the expensive ones, does.&lt;/p&gt;

&lt;p&gt;The numbers behind the first two layers are now measured, not guessed. An October 2026 study (Verma, arXiv 2610.33949) ran 3,997 generation attempts across nine OpenAI and Anthropic model configurations with the schema stated in the prompt and Pydantic validation after. Markdown-fenced JSON was the most common failure: of 681 fenced failures, only 48.0 percent passed validation after the fence was removed. The rest hid further violations, mostly field-length violations. Fence-stripping feels like a fix. Half the time it is a bandage over a second failure.&lt;/p&gt;

&lt;p&gt;And truncation is a failure mode of its own. A separate October 2026 study of harness trimmers found 3,827 of 11,189 tool results were truncated into invalid JSON by the harness itself - langchain-core keeps the first 500 characters, smolagents deletes the middle past 20,000. A truncated response is not a wrong response. It is an incomplete one, and retrying it without raising the token budget reproduces it exactly.&lt;/p&gt;

&lt;h2&gt;
  
  
  The failure battery I ran
&lt;/h2&gt;

&lt;p&gt;I wanted my own numbers before writing a word of advice, so I ran a battery of eight realistic model outputs against a real triage schema - the same shape I use in production for document extraction, where every AI-generated section is validated against its own Zod schema before it is assembled. The schema:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;z&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s2"&gt;"zod"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;triage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;z&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;object&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;summary&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;z&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;min&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;category&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;z&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;enum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"billing"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"bug"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"feature"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"account"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;priority&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;z&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;enum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"low"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"medium"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"high"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"urgent"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;confidence&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;z&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;number&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;min&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;needsHuman&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;z&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;boolean&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;tags&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;z&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;z&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The battery, run with Zod 4.4.3 from this site's own dependencies (the article pins &lt;code&gt;zod@^4.6.7&lt;/code&gt;, the current release as of November 17, 2026):&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Model output&lt;/th&gt;
&lt;th&gt;Raw safeParse&lt;/th&gt;
&lt;th&gt;After salvage&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Clean JSON object&lt;/td&gt;
&lt;td&gt;Pass&lt;/td&gt;
&lt;td&gt;Pass&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Prose + fenced code block&lt;/td&gt;
&lt;td&gt;Fail: not valid JSON&lt;/td&gt;
&lt;td&gt;Pass&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Hallucinated enum value (&lt;code&gt;category: "refund"&lt;/code&gt;)&lt;/td&gt;
&lt;td&gt;Fail: invalid option&lt;/td&gt;
&lt;td&gt;Fail&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Truncated mid-object (token cap cut)&lt;/td&gt;
&lt;td&gt;Fail: not valid JSON&lt;/td&gt;
&lt;td&gt;Fail: no object found&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Number as string (&lt;code&gt;confidence: "0.92"&lt;/code&gt;)&lt;/td&gt;
&lt;td&gt;Fail: expected number&lt;/td&gt;
&lt;td&gt;Fail&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Missing required field (no &lt;code&gt;priority&lt;/code&gt;)&lt;/td&gt;
&lt;td&gt;Fail: invalid option&lt;/td&gt;
&lt;td&gt;Fail&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Extra unknown key (&lt;code&gt;internalNote&lt;/code&gt;)&lt;/td&gt;
&lt;td&gt;Pass (key stripped)&lt;/td&gt;
&lt;td&gt;Pass&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Number out of range (&lt;code&gt;confidence: 1.4&lt;/code&gt;)&lt;/td&gt;
&lt;td&gt;Fail: too big&lt;/td&gt;
&lt;td&gt;Fail&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Two of eight pass as returned. Three of eight pass after a deterministic salvage step - strip the prose and fences, extract the first &lt;code&gt;{...}&lt;/code&gt;. The other five need a model retry with the validation error fed back, and one of those five (truncation) needs a bigger token budget as well, because the same cap will cut the same output again.&lt;/p&gt;

&lt;p&gt;Two details in that table deserve a second look. First, the extra-key case passes, and that is a choice, not an accident: a plain &lt;code&gt;z.object&lt;/code&gt; strips unknown keys silently. If a model-added field vanishing bothers you, &lt;code&gt;z.strictObject&lt;/code&gt; rejects it instead, and &lt;code&gt;z.looseObject&lt;/code&gt; passes it through. Pick one per feature and write down why. Second, the string-number case is fixable without a retry: &lt;code&gt;z.coerce.number()&lt;/code&gt; accepts &lt;code&gt;"0.92"&lt;/code&gt; and returns &lt;code&gt;0.92&lt;/code&gt;. I use coerce only where the source genuinely cannot be trusted to type its numbers - query strings, form fields, model output - and never as a blanket habit, because it also accepts things you would rather reject.&lt;/p&gt;

&lt;p&gt;The check itself is effectively free. I measured 20,000 &lt;code&gt;safeParse&lt;/code&gt; calls on this schema at 25.1 milliseconds total, about 1.3 microseconds per check. The model call costs seconds and cents. Skipping validation to save time is not a trade-off; it is a rounding error dressed as one.&lt;/p&gt;

&lt;p&gt;Run the battery yourself. Every scenario below is the real schema, running in your browser:&lt;/p&gt;



&lt;h2&gt;
  
  
  Zod 4 is the contract
&lt;/h2&gt;

&lt;p&gt;If you learned Zod on v3, half of what you remember is now the deprecated way, and the v4 migration guide is the document to read before you write a tutorial. Zod 4.0 shipped in July 2025; the current release is 4.6.7 (October 12, 2026). The parts that matter for LLM output:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The schema converts to JSON Schema natively.&lt;/strong&gt; &lt;code&gt;z.toJSONSchema(schema)&lt;/code&gt; is built into v4 and replaces the &lt;code&gt;zod-to-json-schema&lt;/code&gt; package. This is the bridge the whole post stands on: one schema produces the TypeScript type, the runtime validator, and the JSON Schema the provider's structured-output mode consumes. When I print the triage schema through it, the output is a draft 2020-12 JSON Schema with &lt;code&gt;additionalProperties: false&lt;/code&gt; on the object - which is exactly what OpenAI's strict mode demands. Print yours once and read it. The provider sees the JSON Schema, not your Zod code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Unknown keys are a three-way choice.&lt;/strong&gt; &lt;code&gt;z.object&lt;/code&gt; strips them. &lt;code&gt;z.strictObject&lt;/code&gt; rejects them with an error. &lt;code&gt;z.looseObject&lt;/code&gt; passes them through. The method forms &lt;code&gt;.strict()&lt;/code&gt; and &lt;code&gt;.passthrough()&lt;/code&gt; still work but are deprecated. For model output I default to plain &lt;code&gt;z.object&lt;/code&gt; (strip) on extraction features and &lt;code&gt;z.strictObject&lt;/code&gt; on anything where a surprise key means a client bug.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Salvage lives per field.&lt;/strong&gt; &lt;code&gt;.catch(value)&lt;/code&gt; substitutes a fallback when a field exists but fails validation - &lt;code&gt;z.enum(["light", "dark"]).catch("light")&lt;/code&gt;. &lt;code&gt;.default(value)&lt;/code&gt; fills a missing field. &lt;code&gt;.prefault(value)&lt;/code&gt; is new in v4 and parses the fallback through the schema, which is what &lt;code&gt;.default()&lt;/code&gt; used to do in v3. Every automatic repair is a data decision: log it, because a field you silently defaulted is a field you chose not to know about.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;There is no partial parse.&lt;/strong&gt; &lt;code&gt;.parse()&lt;/code&gt; throws, &lt;code&gt;.safeParse()&lt;/code&gt; returns success or error, all or nothing. &lt;code&gt;.partial()&lt;/code&gt; makes top-level fields optional; &lt;code&gt;.deepPartial()&lt;/code&gt; was removed in v4. Salvage is per-field with &lt;code&gt;.catch&lt;/code&gt; and &lt;code&gt;.default&lt;/code&gt;, or at the streaming layer where the SDK yields only fully validated elements. Do not write a partial-parse helper that guesses at the missing half of an object. That is how corrupt rows get written with a confident face.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The error API changed.&lt;/strong&gt; Issues live on &lt;code&gt;.issues&lt;/code&gt;, not &lt;code&gt;.errors&lt;/code&gt;. &lt;code&gt;ZodError.format()&lt;/code&gt; and &lt;code&gt;.flatten()&lt;/code&gt; are deprecated in favour of top-level &lt;code&gt;z.treeifyError()&lt;/code&gt; and &lt;code&gt;z.flattenError()&lt;/code&gt;. The error customisation is one unified &lt;code&gt;error&lt;/code&gt; parameter; &lt;code&gt;invalid_type_error&lt;/code&gt;, &lt;code&gt;required_error&lt;/code&gt; and &lt;code&gt;errorMap&lt;/code&gt; are gone, and a schema-level error now wins over the error map passed to &lt;code&gt;.parse()&lt;/code&gt; - the opposite precedence to v3. String formats are top-level functions now: &lt;code&gt;z.email()&lt;/code&gt;, &lt;code&gt;z.url()&lt;/code&gt;, &lt;code&gt;z.uuid()&lt;/code&gt;; the method forms still work but are deprecated. And &lt;code&gt;z.coerce&lt;/code&gt; fields now take &lt;code&gt;unknown&lt;/code&gt; input, with a missing key on a coerce field erroring unless you add &lt;code&gt;.default()&lt;/code&gt; explicitly.&lt;/p&gt;

&lt;p&gt;One more v4 note, because it decides what you pin: Zod 4 requires &lt;code&gt;strict: true&lt;/code&gt; in tsconfig and TypeScript 5.5 or later. If your project cannot meet that yet, stay on v3 deliberately and say so in your code, rather than fighting the compiler.&lt;/p&gt;

&lt;h2&gt;
  
  
  The provider matrix is not one schema
&lt;/h2&gt;

&lt;p&gt;Here is the part that costs teams the most time: the three providers do not accept the same JSON Schema subset, and the differences are not cosmetic. A schema that is valid for OpenAI can be rejected by Anthropic, and a constraint Gemini silently ignores is a constraint your Zod schema must still enforce locally.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;OpenAI&lt;/th&gt;
&lt;th&gt;Google Gemini&lt;/th&gt;
&lt;th&gt;Anthropic&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;How you ask&lt;/td&gt;
&lt;td&gt;&lt;code&gt;response_format: { type: "json_schema", json_schema: { name, strict: true, schema } }&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;responseFormat&lt;/code&gt; with &lt;code&gt;mimeType: "application/json"&lt;/code&gt; and the schema (Interactions API); legacy &lt;code&gt;responseSchema&lt;/code&gt; + &lt;code&gt;responseMimeType&lt;/code&gt; on &lt;code&gt;generateContent&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;output_config.format: { type: "json_schema", schema }&lt;/code&gt; (GA January 29, 2026)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;What it guarantees&lt;/td&gt;
&lt;td&gt;100% schema adherence under &lt;code&gt;strict: true&lt;/code&gt; - constrained decoding means a non-conforming token cannot be emitted&lt;/td&gt;
&lt;td&gt;Shape when both the mime type and the schema are set; the mime type alone is only a strong hint&lt;/td&gt;
&lt;td&gt;Grammar-constrained sampling - the model cannot emit a character that breaks the schema&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Hard limits&lt;/td&gt;
&lt;td&gt;5,000 object properties, 10 nesting levels, 1,000 enum values; every property must be in &lt;code&gt;required&lt;/code&gt;; &lt;code&gt;additionalProperties: false&lt;/code&gt; on every object&lt;/td&gt;
&lt;td&gt;No published property or nesting ceiling; very large or deeply nested schemas may be rejected with a 400&lt;/td&gt;
&lt;td&gt;About 20 strict tools, 24 optional parameters, 16 union-typed parameters per request; no recursive schemas; no external &lt;code&gt;$ref&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;What it strips or rejects&lt;/td&gt;
&lt;td&gt;Rejects &lt;code&gt;allOf&lt;/code&gt;, &lt;code&gt;oneOf&lt;/code&gt;, &lt;code&gt;not&lt;/code&gt;, &lt;code&gt;dependentRequired&lt;/code&gt;, &lt;code&gt;if&lt;/code&gt;/&lt;code&gt;then&lt;/code&gt;/&lt;code&gt;else&lt;/code&gt; with a 400&lt;/td&gt;
&lt;td&gt;Ignores &lt;code&gt;minLength&lt;/code&gt;, &lt;code&gt;maxLength&lt;/code&gt;, &lt;code&gt;minProperties&lt;/code&gt;, &lt;code&gt;maxProperties&lt;/code&gt;, &lt;code&gt;uniqueItems&lt;/code&gt; - your Zod schema must enforce them&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;minimum&lt;/code&gt;, &lt;code&gt;maximum&lt;/code&gt;, &lt;code&gt;multipleOf&lt;/code&gt;, &lt;code&gt;minLength&lt;/code&gt;, &lt;code&gt;maxLength&lt;/code&gt; not supported; &lt;code&gt;minItems&lt;/code&gt; only 0 or 1; enum casing not guaranteed to match exactly&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;When it still fails you&lt;/td&gt;
&lt;td&gt;Safety refusal (detectable &lt;code&gt;refusal&lt;/code&gt; field); truncation at the token cap (&lt;code&gt;finish_reason: "length"&lt;/code&gt;)&lt;/td&gt;
&lt;td&gt;Refusals and truncation return HTTP 200 with schema-violating output; Google's own docs say to validate again on your side&lt;/td&gt;
&lt;td&gt;Refusal (&lt;code&gt;stop_reason: "refusal"&lt;/code&gt;); token-cap cut-off (&lt;code&gt;stop_reason: "max_tokens"&lt;/code&gt;); forced tool choice errors with manual extended thinking&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Three warnings from that table, because each one has bitten someone I know. First, Gemini's legacy pair is on the way out: Google's API reference marks &lt;code&gt;responseSchema&lt;/code&gt; deprecated and points to &lt;code&gt;responseFormat&lt;/code&gt; and the Interactions API, while the Vertex docs still present the legacy pair without a deprecation flag. Teach the new path, show the legacy one as widely deployed, and do not build examples on &lt;code&gt;gemini-2.5-*&lt;/code&gt; model strings - 2.5 Pro and 2.5 Flash are deprecated, and from December 1, 2026 API requests auto-route them to 3.8 Flash and 3.5 Flash-Lite. Second, Anthropic's SDK helpers strip the unsupported keywords into field descriptions and validate client-side - which is exactly why your local Zod check is not redundant even under strict mode. Third, OpenAI's launch-era limits (100 properties, 5 nesting levels, 500 enums) still circulate in old posts; the current figures are 5,000, 10 and 1,000.&lt;/p&gt;

&lt;p&gt;The practical consequence is simple: one schema, three wire formats, and your Zod schema is the only layer that sees all of them. Generate the JSON Schema from Zod, send the provider its subset, and validate the response against the original Zod schema when it comes back. The provider's guarantee and your validation are two different checks, and you need both.&lt;/p&gt;

&lt;h2&gt;
  
  
  The current AI SDK path
&lt;/h2&gt;

&lt;p&gt;Most tutorials you will find still teach &lt;code&gt;generateObject&lt;/code&gt;. That function was deprecated in AI SDK 6, and the current docs teach &lt;code&gt;generateText&lt;/code&gt; with the &lt;code&gt;output&lt;/code&gt; option instead. The SDK's own guide page lagged the migration guide for months, which is why the stale path is everywhere. If you are writing new code in November 2026, the shape is AI SDK 7 with Zod 4:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;generateText&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Output&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;NoObjectGeneratedError&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s2"&gt;"ai"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;z&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s2"&gt;"zod"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;output&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;generateText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;output&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Output&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;object&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;schema&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;triage&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;prompt&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Triage this ticket. Return JSON only."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Output.object&lt;/code&gt; is the common case. &lt;code&gt;Output.array&lt;/code&gt; takes an element schema plus &lt;code&gt;minItems&lt;/code&gt; and &lt;code&gt;maxItems&lt;/code&gt;, and with &lt;code&gt;streamText&lt;/code&gt; its &lt;code&gt;elementStream&lt;/code&gt; yields each element only once it is complete and validated. &lt;code&gt;Output.choice&lt;/code&gt; classifies from a fixed set of strings - the successor to the old &lt;code&gt;output: "enum"&lt;/code&gt;. &lt;code&gt;Output.json&lt;/code&gt; checks only that the response is valid JSON, with no schema validation at all. Structured output counts as a step in the SDK's multi-step model, so account for it in &lt;code&gt;stopWhen&lt;/code&gt; when you combine it with tool calls.&lt;/p&gt;

&lt;p&gt;When the response cannot be parsed or validated, &lt;code&gt;generateText&lt;/code&gt; rejects with &lt;code&gt;NoObjectGeneratedError&lt;/code&gt;. It preserves the raw &lt;code&gt;text&lt;/code&gt;, the &lt;code&gt;cause&lt;/code&gt; (for example a JSON parse error), the response metadata, and the token usage - everything the repair loop needs to decide what to do next. There is no partial object on failure. It throws; it does not hand back a half-broken object for you to guess at.&lt;/p&gt;

&lt;p&gt;One thing the SDK does not do for you: &lt;code&gt;maxRetries&lt;/code&gt; is a transport-level retry for failed requests, not a validation retry. A validation failure throws immediately, and your code decides whether to retry. That is the correct design - the retry needs the validation error as context, and only your code has it.&lt;/p&gt;

&lt;h2&gt;
  
  
  The repair loop
&lt;/h2&gt;

&lt;p&gt;Here is the loop, in the shape I run. It has four stages, and the order matters: validate first, salvage second, retry with the error third, dead-letter last.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;generateText&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Output&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;NoObjectGeneratedError&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nc"&gt;LanguageModel&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s2"&gt;"ai"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;z&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s2"&gt;"zod"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="no"&gt;MAX_ATTEMPTS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="cm"&gt;/** Deterministic salvage: strip prose and fences, extract the first {...}. */&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;salvage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;raw&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;raw&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;trim&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;fence&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;match&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="sb"&gt;```

(?:json)?\s*([\s\S]*?)

```&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fence&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fence&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;trim&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;indexOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"{"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;end&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;lastIndexOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"}"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;start&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;end&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;end&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;end&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;safeJsonParse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;raw&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;unknown&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;raw&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;raw&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="cm"&gt;/** Providers surface truncation differently; read whichever reason is present. */&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;isTruncated&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;NoObjectGeneratedError&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;cause&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cause&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;finishReason&lt;/span&gt;&lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;stopReason&lt;/span&gt;&lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;reason&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cause&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;finishReason&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="n"&gt;cause&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;stopReason&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;reason&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s2"&gt;"length"&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;reason&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s2"&gt;"max_tokens"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;isRefusal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;NoObjectGeneratedError&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;cause&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cause&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;stopReason&lt;/span&gt;&lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;refusal&lt;/span&gt;&lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="kt"&gt;unknown&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;cause&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;stopReason&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s2"&gt;"refusal"&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;cause&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;refusal&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="cm"&gt;/** Write the raw text, the schema version, and the model id to a review queue. */&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;deadLetter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;entry&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;raw&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;reason&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;ticketText&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Your queue of choice: a database table, a BullMQ dead-letter queue,&lt;/span&gt;
  &lt;span class="c1"&gt;// a Slack channel. The point is that it exists and a human reads it.&lt;/span&gt;
  &lt;span class="nb"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"dead-lettered extraction"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;entry&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;extractTriage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ticketText&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;LanguageModel&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;lastIssues&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;lastRaw&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;attempt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;attempt&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="no"&gt;MAX_ATTEMPTS&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;attempt&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;prompt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
      &lt;span class="n"&gt;attempt&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
        &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="sb"&gt;`Triage this ticket. Return JSON only.\n\n${ticketText}`&lt;/span&gt;
        &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sb"&gt;`The previous output failed validation:\n${lastIssues}\nReturn corrected JSON only.\n\n${ticketText}`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;output&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;generateText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;output&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Output&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;object&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;schema&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;triage&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;prompt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;output&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// typed, validated, done&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nc"&gt;NoObjectGeneratedError&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isInstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="n"&gt;lastRaw&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

      &lt;span class="c1"&gt;// Truncation is not a retryable shape error. Raise the budget.&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;isTruncated&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"output truncated: raise max_output_tokens"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;cause&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="c1"&gt;// A refusal is not a validation failure. Route it to review.&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;isRefusal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;deadLetter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;raw&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;lastRaw&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;reason&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"refusal"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ticketText&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"model refused: routed to review"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;cause&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;

      &lt;span class="c1"&gt;// Stage 2: deterministic salvage before spending another model call.&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;salvaged&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;salvage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lastRaw&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;salvaged&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;check&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;triage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;safeParse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;salvaged&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;check&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;success&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;check&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;

      &lt;span class="c1"&gt;// Stage 3: the next attempt carries the validation issues, not a&lt;/span&gt;
      &lt;span class="c1"&gt;// generic "try again". Field path plus message, nothing more.&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;failed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;triage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;safeParse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;safeJsonParse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lastRaw&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;issueLines&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;failed&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;issue&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="n"&gt;failed&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;issues&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="n"&gt;issueLines&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sb"&gt;`${issue.path.join(".")}: ${issue.message}`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="n"&gt;lastIssues&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;issueLines&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"; "&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="c1"&gt;// Stage 4: whatever survives three attempts never touches the database.&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;deadLetter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;raw&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;lastRaw&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;reason&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"validation failed after 3 attempts"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ticketText&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"triage failed validation after 3 attempts"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The rules that keep this loop honest:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Retry only when it helps.&lt;/strong&gt; A wrong enum, a missing field, a string where a number belongs - the model can fix all of these when told exactly what was wrong. A refusal cannot be fixed by asking again. A truncation cannot be fixed without a bigger token budget. The loop checks the failure kind before it spends the next call, because a retry that cannot succeed is not persistence, it is a billing event.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Cap it at three.&lt;/strong&gt; The first attempt is the model doing its best. The second is the model with the error in front of it. The third is the last chance for a flaky output. Past three, you are no longer repairing; you are hoping, and hoping at model-call prices. Whatever survives goes to the dead-letter queue with the raw text, the schema version, and the model id, so a human can see exactly what failed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Feed the error back, not a summary of it.&lt;/strong&gt; The retry prompt carries the Zod issues verbatim - field path plus message. "Try again" teaches the model nothing. &lt;code&gt;category: Invalid option: expected one of "billing"|"bug"|"feature"|"account"&lt;/code&gt; teaches it the exact constraint it violated. Cap the feedback at a handful of issues; a wall of twenty errors is noise the model will skim.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Log every automatic repair.&lt;/strong&gt; A &lt;code&gt;.catch()&lt;/code&gt; fallback, a salvage rescue, a second-attempt success - each one is a data decision your code made on the reader's behalf. Log the attempt count and the failure kind next to the record. When a user asks why a ticket was triaged "billing" instead of "account", the log is the answer.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Cost it before you ship it.&lt;/strong&gt; Validation costs about 1.3 microseconds per check - I measured it. The retry is the real cost: three attempts worst-case triples the extraction cost of that record. At low volume that is invisible; at a million extractions a month it is a line item. Budget the worst case, not the happy path, and alert on the retry rate the same way you would alert on an error rate. A climbing retry rate is a schema problem or a model problem, and either way it is cheaper to find in your dashboard than in your invoice.&lt;/p&gt;

&lt;p&gt;I ran this loop's logic against stub model outputs before publishing: a fenced first attempt rescued by salvage, a wrong-enum first attempt fixed on the second attempt with the error fed back, and a permanently invalid output dead-lettered after three attempts. The SDK call shape above is verified against the AI SDK docs I read while writing this post; the loop logic is verified by execution.&lt;/p&gt;

&lt;p&gt;&lt;br&gt;
  The same shape - validate at the boundary, retry with the error, dead-letter what survives - is the core of the 200-line harness I built for agents in &lt;a href="https://dev.to/blog/ai-agent-typescript-harness-200-lines"&gt;Your AI Agent Needs No Framework&lt;/a&gt;. The harness is the loop; this post is the contract it enforces. If you are choosing between a framework and a harness, the decision is in &lt;a href="https://dev.to/blog/agent-framework-vs-agent-harness"&gt;Agent Framework vs Agent Harness&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  The production checklist I run before shipping
&lt;/h2&gt;

&lt;p&gt;Six checks, in the order I run them, on the real feature - not on the demo. The first three are about the schema, the last three about the loop.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;The schema is pinned and the wire schema is printed.&lt;/strong&gt; &lt;code&gt;zod@^4.6.7&lt;/code&gt; in package.json, and &lt;code&gt;z.toJSONSchema()&lt;/code&gt; logged once and read by a human. The provider sees the JSON Schema, not your Zod code. If the conversion dropped a constraint, the wire is where you find out.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The unknown-key policy is deliberate.&lt;/strong&gt; Strip, pass through, or reject - chosen per feature, written down. The default (strip) is fine for extraction and wrong for anything where a surprise key means a client bug.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The salvage step is tested on all eight failure shapes.&lt;/strong&gt; Fence-stripping rescues the most common failure and only about half of fenced outputs are clean underneath. Run the battery from this post against your schema before you trust the salvage path.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Retries are bounded at three, carry the validation error, and never retry a refusal.&lt;/strong&gt; The retry prompt includes the Zod issues, not a generic "try again". Refusals go to human review. Truncations get a bigger token budget, not another identical attempt.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Truncation is detected from the finish reason, not from a parse error.&lt;/strong&gt; &lt;code&gt;finish_reason: "length"&lt;/code&gt;, &lt;code&gt;stop_reason: "max_tokens"&lt;/code&gt;, or an &lt;code&gt;incomplete&lt;/code&gt; status means the output was cut, not wrong. A parse error on a truncated response is a symptom. The fix is &lt;code&gt;max_output_tokens&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The dead-letter queue exists, and you have replayed one failure from it.&lt;/strong&gt; Whatever survives three attempts lands in a review queue with the raw text, the schema version, and the model id. A dead-letter queue you have never replayed from is a dead-letter queue you have not tested.&lt;/li&gt;
&lt;/ol&gt;



&lt;h2&gt;
  
  
  My verdict
&lt;/h2&gt;

&lt;p&gt;Use &lt;code&gt;generateText&lt;/code&gt; with &lt;code&gt;Output.object&lt;/code&gt;, a Zod 4 schema as the single contract, and the repair loop above - validate, salvage, retry with the error, dead-letter at three attempts. That is the setup I would defend for any feature whose output lands somewhere it cannot be undone: a database, another system, a user acting on it.&lt;/p&gt;

&lt;p&gt;The conditions that would flip it: if a human reviews every output before anything depends on it, drop the loop and keep the schema - the schema is the part that is hard to retrofit, the loop is easy to add later. If you run one provider and one model at high volume, put that provider's strict mode first and Zod second, and spend the saved effort on the business-rule checks no schema can express. And if the feature is a prototype or a read-only view, do not build any of this yet - log the raw outputs, and build the loop when the first bad JSON costs you a user or an hour.&lt;/p&gt;

&lt;p&gt;What I would not do, in any of those cases, is the thing I did on call fifty-one: trust the prompt. "Return JSON only" is a request, not a contract. A schema is a contract. The difference between them is the afternoon your support agent spends untangling a row that should never have been written.&lt;/p&gt;

&lt;p&gt;Not sure which posture fits your feature? Answer four questions and I will place you:&lt;/p&gt;



&lt;p&gt;If your agent features are growing past one extraction call and you want the loop, the harness, and the cost controls built as one system instead of bolted on one feature at a time, that is the layer I build for teams. &lt;a href="https://dev.to/#contact"&gt;Tell me about your project&lt;/a&gt; and I will tell you honestly whether you need the loop, the harness, or nothing at all.&lt;/p&gt;

&lt;p&gt;Related reading: &lt;a href="https://dev.to/blog/zod-as-a-contract"&gt;Zod as a Contract, Not a Comment&lt;/a&gt;, the primer this post builds on - one schema as type, validator, and JSON Schema. &lt;a href="https://dev.to/blog/tanstack-ai-vs-vercel-ai-sdk"&gt;TanStack AI vs Vercel AI SDK&lt;/a&gt; if you are still choosing the SDK the loop runs on.&lt;/p&gt;

&lt;h2&gt;
  
  
  FAQ
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Does structured output mean I can skip Zod validation?
&lt;/h3&gt;

&lt;p&gt;No. Structured output guarantees shape, not truth: a schema-valid response can still carry a wrong total, an invented enum value, or a factually wrong field. Providers also strip JSON Schema keywords they do not support, and refusals and token-cap truncation bypass the guarantee entirely. Keep Zod validation after every provider call.&lt;/p&gt;

&lt;h3&gt;
  
  
  Is generateObject still OK to use?
&lt;/h3&gt;

&lt;p&gt;It was deprecated in AI SDK 6 and the current docs teach &lt;code&gt;generateText&lt;/code&gt; with &lt;code&gt;Output.object&lt;/code&gt; instead. Old tutorials still show &lt;code&gt;generateObject&lt;/code&gt;, and the SDK's own guide page lagged the migration guide for months. New code should use &lt;code&gt;generateText&lt;/code&gt; with &lt;code&gt;Output.object&lt;/code&gt;, &lt;code&gt;Output.array&lt;/code&gt;, &lt;code&gt;Output.choice&lt;/code&gt; or &lt;code&gt;Output.json&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  What does Zod 4 change for LLM output?
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;z.toJSONSchema&lt;/code&gt; is built in, replacing the &lt;code&gt;zod-to-json-schema&lt;/code&gt; package. &lt;code&gt;z.strictObject&lt;/code&gt; and &lt;code&gt;z.looseObject&lt;/code&gt; replace the deprecated &lt;code&gt;.strict()&lt;/code&gt; and &lt;code&gt;.passthrough()&lt;/code&gt;. &lt;code&gt;.catch&lt;/code&gt; and &lt;code&gt;.prefault&lt;/code&gt; give you per-field salvage, and the error API is now &lt;code&gt;.issues&lt;/code&gt; with &lt;code&gt;z.treeifyError&lt;/code&gt;. There is still no partial-parse API: &lt;code&gt;safeParse&lt;/code&gt; is all or nothing.&lt;/p&gt;

&lt;h3&gt;
  
  
  How many retries should the repair loop allow?
&lt;/h3&gt;

&lt;p&gt;Three attempts, then dead-letter. Each retry carries the Zod issues (field path plus message) back to the model. Never retry a refusal blindly - route it to human review - and never retry a truncation without raising the token budget first, because the same cap will cut the same output again.&lt;/p&gt;

&lt;h3&gt;
  
  
  Which provider's structured output should I pick?
&lt;/h3&gt;

&lt;p&gt;Pick the provider by the model you need, not by the structured-output feature: all three guarantee shape under their strict modes and all three strip or reject part of the JSON Schema subset. OpenAI is the most complete, Gemini ignores string-length bounds, Anthropic ignores numeric bounds and caps &lt;code&gt;minItems&lt;/code&gt; at 0 or 1. Validate locally no matter which one you use.&lt;/p&gt;

&lt;h3&gt;
  
  
  How much does validation cost?
&lt;/h3&gt;

&lt;p&gt;Almost nothing. I measured 20,000 &lt;code&gt;safeParse&lt;/code&gt; calls on a real triage schema at 25.1 ms total, about 1.3 microseconds per check. The model call costs seconds and cents; the check is free by comparison. The real cost of the loop is retries: three attempts worst-case triples the extraction cost, so budget it.&lt;/p&gt;

&lt;h2&gt;
  
  
  The tools I would actually use
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Tool&lt;/th&gt;
&lt;th&gt;What it is&lt;/th&gt;
&lt;th&gt;What I use it for&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Zod 4.6.7&lt;/td&gt;
&lt;td&gt;TypeScript schema validation, free and open source&lt;/td&gt;
&lt;td&gt;The contract: type, validator, and JSON Schema from one object&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Vercel AI SDK 7&lt;/td&gt;
&lt;td&gt;Provider-agnostic AI SDK, free and open source&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;generateText&lt;/code&gt; with &lt;code&gt;Output.object&lt;/code&gt;, structured output across providers&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;OpenAI structured outputs&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;response_format&lt;/code&gt; with &lt;code&gt;json_schema&lt;/code&gt;, &lt;code&gt;strict: true&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;The most complete strict mode when the model choice is OpenAI&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Google Gemini structured output&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;responseFormat&lt;/code&gt; / Interactions API&lt;/td&gt;
&lt;td&gt;Structured output when the model choice is Gemini&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Anthropic structured outputs&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;output_config.format&lt;/code&gt; with &lt;code&gt;json_schema&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Structured output when the model choice is Claude&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;{/* MONETIZATION NOTE: Zod and the Vercel AI SDK are free open-source tools with no affiliate program to swap in. The three provider rows are usage-billed APIs; no affiliate links exist for them today. If a provider launches a referral program, swap its URL here. The money path for this post is freelance AI-integration work: teams pay to have this loop built into their extraction features, which is why the CTA points at /#contact. */}&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>zod</category>
      <category>ai</category>
      <category>llm</category>
    </item>
    <item>
      <title>Next.js Agent Memory: mem0 vs pgvector - Which I Would Ship</title>
      <dc:creator>MUHAMMAD ZAIN UL ABIDIN</dc:creator>
      <pubDate>Sun, 15 Nov 2026 13:15:08 +0000</pubDate>
      <link>https://dev.to/zforzain2000/nextjs-agent-memory-mem0-vs-pgvector-which-i-would-ship-3iop</link>
      <guid>https://dev.to/zforzain2000/nextjs-agent-memory-mem0-vs-pgvector-which-i-would-ship-3iop</guid>
      <description>&lt;p&gt;Every agent demo has the same amnesia: a user explains their stack, their preferences and their constraints on Monday, and on Tuesday the agent cheerfully asks again. Long-term memory is the fix, and in a Next.js app it comes down to two honest architectures: rent the whole pipeline from mem0, or build the storage half on pgvector inside the Postgres you already run. I built both behind one API route in a scratch Next.js app, ran the pgvector side live against a real Postgres with pgvector 0.8.7, and read mem0's current pricing page with my own eyes - which is where this comparison gets interesting, because most of the internet is quoting mem0 prices it no longer sells. Here is the code, the mechanics and the pick I would ship.&lt;/p&gt;

&lt;p&gt;The disclosure, up front, in the open: the pgvector build in this post ran for real - a local Postgres 16.2 with pgvector 0.8.7, seeded, searched, user-isolation-tested and index-tested at 20,000 rows, using small hand-built test vectors so the geometry is checkable by eye. The mem0 build is written against the real &lt;code&gt;mem0ai&lt;/code&gt; SDK (3.3.1) and typechecked against its types, but I have no mem0 API key in this environment, so it did not run end to end. Where a claim rests on mem0's docs rather than my run, I say so. Nothing here invents a latency number.&lt;/p&gt;

&lt;h2&gt;
  
  
  The short version
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Memory is two jobs, not one:&lt;/strong&gt; extracting facts from conversations, and storing plus retrieving them. mem0 sells you both. pgvector is only the second job - extraction becomes a prompt you own.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;If you already run Postgres, pgvector is my default.&lt;/strong&gt; One extension, one table, one index; no new vendor, no quotas, and your users' facts live in your database under your backup and deletion policies.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;mem0 wins on time and on not owning extraction.&lt;/strong&gt; One &lt;code&gt;add()&lt;/code&gt; call replaces a pipeline. Its free tier is genuinely usable (50,000 adds a month once your own LLM key is connected), but graph memory is now a paid platform feature, and the $19/$249 tiers in most comparison posts are legacy pricing the buy page no longer offers.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The deciding question is not features.&lt;/strong&gt; It is whether you want memory as infrastructure you operate or as a service you rent - and how much the data's home matters to you.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What "memory" means here, and what it does not
&lt;/h2&gt;

&lt;p&gt;Three different things get called memory, and comparisons fall apart when they blur:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Session memory&lt;/strong&gt; is the current conversation - the messages in the context window. Every chat framework has it; it dies with the session.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Document RAG&lt;/strong&gt; retrieves from content you published: docs, tickets, a knowledge base. The write path is an indexing job, done once.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Long-term agent memory&lt;/strong&gt; - this post's subject - is facts about a specific user, learned from that user's own conversations, written continuously and retrieved per user. The write path is the hard part: raw transcripts are noisy, contradictory and far too long to stuff into a context window. Somebody has to decide that "I deploy to production on Fridays" is a fact worth keeping, phrase it cleanly, notice when it changes, and forget the small talk around it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That deciding is called extraction, and it is the real product in this comparison. Storage and similarity search are solved problems. Keep that framing and every other difference between mem0 and pgvector falls out of it.&lt;/p&gt;

&lt;h2&gt;
  
  
  The same conversation, two pipelines
&lt;/h2&gt;

&lt;p&gt;Before the code, walk the claim. This is the exact three-message conversation from my scratch app, stepped through both stacks - extraction, storage, retrieval a week later, and the final tally of who did the work. The pgvector similarity scores shown in step 4 are from the live run described below.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;On zainvibes.com this section is interactive: a step-through simulation - walk the same three-message conversation through both pipelines and watch extraction, storage and retrieval happen on each side, ending with the tally of who did the work. &lt;a href="https://zainvibes.com/blog/nextjs-agent-memory-mem0-vs-pgvector" rel="noopener noreferrer"&gt;Try it on the original post.&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  mem0: you rent the extraction
&lt;/h2&gt;

&lt;p&gt;The mem0 pitch in one line: send conversations, get facts. The platform's pipeline runs an LLM over the transcript, extracts the salient facts, resolves them against what it already knows about that user, stores them, and later ranks them for a query using semantic, keyword and entity signals. The entire integration surface in TypeScript is two calls:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;MemoryClient&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s2"&gt;"mem0ai"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;MemoryClient&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;mem0&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;MemoryClient&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;apiKey&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;MEM0_API_KEY&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;apiKey&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"MEM0_API_KEY is not set"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;MemoryClient&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;apiKey&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Write path: hand over the transcript. Extraction is Mem0's job.&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;addConversation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;messages&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Message&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;mem0&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;messages&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;userId&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Read path: ranked memories for this user only.&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;searchMemories&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;results&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;mem0&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;search&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;filters&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;user_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;userId&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;topK&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;results&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;memory&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;memory&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;score&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;score&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That is genuinely all of it - this is the write and read path of the mem0 side of my scratch app, and it typechecks against the SDK's shipped types. What you are renting is everything behind &lt;code&gt;add()&lt;/code&gt;: the extraction prompt engineering, the update-versus-append decisions when a new fact contradicts an old one, the embedding pipeline, and the retrieval ranking. If you have ever maintained an extraction prompt against real user conversations, you know that is not nothing - real transcripts contain corrections, sarcasm and three facts tangled in one sentence.&lt;/p&gt;

&lt;p&gt;Two structural facts about mem0 matter more than the API. First, the open-source build is real: an Apache-2.0 library and a self-hosted server where you bring your own LLM, embedder and vector store - and one of the twenty-plus supported vector stores is pgvector itself, which tells you where the boundary of the product actually sits. Second, the boundary moved recently: graph memory (entity linking, relationship-aware retrieval) was removed from the open-source SDK in v2.0.0 and is now a platform-only, Custom-plan feature. If a comparison post tells you mem0's open-source version includes graph memory, it is describing 2025.&lt;/p&gt;

&lt;h3&gt;
  
  
  What mem0 actually costs in November 2026
&lt;/h3&gt;

&lt;p&gt;This is the part of the comparison the internet currently gets wrong, so I read the official pricing page directly instead of another blog's summary. The buy page today offers exactly two options:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;mem0 Free&lt;/th&gt;
&lt;th&gt;mem0 Custom&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Price&lt;/td&gt;
&lt;td&gt;$0&lt;/td&gt;
&lt;td&gt;Talk to sales&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Memory adds&lt;/td&gt;
&lt;td&gt;10,000/month for 30 days, then 50,000/month with your own LLM key&lt;/td&gt;
&lt;td&gt;Custom&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Retrievals&lt;/td&gt;
&lt;td&gt;1,000/month for 30 days, then 10,000/month with your own LLM key&lt;/td&gt;
&lt;td&gt;Custom&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Extraction LLM&lt;/td&gt;
&lt;td&gt;Mem0's models for 30 days, then your key, your bill&lt;/td&gt;
&lt;td&gt;-&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Graph memory&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Consolidation (Dream)&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Support&lt;/td&gt;
&lt;td&gt;Community&lt;/td&gt;
&lt;td&gt;Private Slack, SLA&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The $19 Starter and $249 Pro tiers that dominate comparison content are legacy: mem0's own FAQ still references them in places, and third-party directories still list them, but a price-history tracker records the ladder being restructured in late October 2026, with Starter and Pro closed to new accounts and existing customers grandfathered. If a buying decision matters here, price it from the pricing page, not from a roundup - including, if it ages, this one.&lt;/p&gt;

&lt;p&gt;The honest cost model for the free tier: your extraction and embedding run on your own LLM key, so your marginal cost is model spend, which scales with conversation volume. The platform counts adds and retrievals. That is a fair trade for most early products, and it is also a meter you do not control the shape of - a chatty user costs retrievals every single turn you choose to search memory.&lt;/p&gt;

&lt;h2&gt;
  
  
  pgvector: you own the pipeline
&lt;/h2&gt;

&lt;p&gt;The pgvector build is the same two functions, with the middle exposed. One table holds the facts:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="no"&gt;EXTENSION&lt;/span&gt; &lt;span class="k"&gt;IF&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;EXISTS&lt;/span&gt; &lt;span class="kt"&gt;vector&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="k"&gt;IF&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;EXISTS&lt;/span&gt; &lt;span class="n"&gt;agent_memories&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="kt"&gt;BIGSERIAL&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;user_id&lt;/span&gt; &lt;span class="kt"&gt;TEXT&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;memory&lt;/span&gt; &lt;span class="kt"&gt;TEXT&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;embedding&lt;/span&gt; &lt;span class="kt"&gt;vector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1536&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;created_at&lt;/span&gt; &lt;span class="kt"&gt;TIMESTAMPTZ&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="nb"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="k"&gt;IF&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;EXISTS&lt;/span&gt; &lt;span class="n"&gt;agent_memories_embedding_hnsw&lt;/span&gt;
  &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;agent_memories&lt;/span&gt; &lt;span class="k"&gt;USING&lt;/span&gt; &lt;span class="n"&gt;hnsw&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;embedding&lt;/span&gt; &lt;span class="n"&gt;vector_cosine_ops&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And the write and read paths are the ones from my scratch app:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;Pool&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s2"&gt;"pg"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;pool&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Pool&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;connectionString&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;DATABASE_URL&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Write path: you extracted the fact and embedded it. This just stores it.&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;addMemory&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;memory&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;embedding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;number&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;pool&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s2"&gt;"INSERT INTO agent_memories (user_id, memory, embedding) VALUES ($1, $2, $3::vector)"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;memory&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;vectorLiteral&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;embedding&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Read path: nearest facts for this user, by cosine distance.&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;searchMemories&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;queryEmbedding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;number&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;topK&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;rows&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;pool&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="sb"&gt;`SELECT memory, 1 - (embedding &amp;lt;=&amp;gt; $1::vector) AS similarity
     FROM agent_memories
     WHERE user_id = $2
     ORDER BY embedding &amp;lt;=&amp;gt; $1::vector
     LIMIT $3`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;vectorLiteral&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;queryEmbedding&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;topK&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;rows&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Everything mem0 hides lives just offstage from this listing: the extraction prompt that turned the transcript into those three clean facts runs before &lt;code&gt;addMemory&lt;/code&gt;, on your LLM bill, with quality that is entirely your responsibility; and the embedding call happens once per fact on write and once per question on read. Neither is hard. Both are yours forever.&lt;/p&gt;

&lt;p&gt;What pgvector itself gives you is mature and boring in the best way. The current release is 0.8.7 (October 2026). Search is exact by default - the extension's own README leads with "great at exact searches, with perfect recall" - and HNSW or IVFFlat indexes trade a little recall for speed when tables grow. The operator set covers cosine (&lt;code&gt;&amp;lt;=&amp;gt;&lt;/code&gt;), inner product, L2, L1, Hamming and Jaccard distances, and storage types run from full float vectors down to binary vectors for very large collections. It is preinstalled or one extension away on the hosted Postgres providers a Next.js app typically already uses, Supabase and Neon included.&lt;/p&gt;

&lt;h3&gt;
  
  
  What my live run actually proved
&lt;/h3&gt;

&lt;p&gt;Claims about my own build, so here is the evidence standard: I compiled PostgreSQL 16.2 from source in this sandbox (no system package was available), built pgvector 0.8.7 against it, and ran a test script that does five things:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Creates the table with a &lt;code&gt;vector(8)&lt;/code&gt; column and the HNSW index.&lt;/li&gt;
&lt;li&gt;Inserts four memories with hand-set 8-dimension embeddings - three for one user, one (a Tuesday deploy day) for a second user, so leakage would be visible.&lt;/li&gt;
&lt;li&gt;Searches with a "when do we deploy" query vector: the Friday memory ranks first at 0.9932 cosine similarity, ahead of the Postgres fact (0.2914) and the green-tea fact (0.0714). The geometry was set by hand, so this proves the ranking and scoping machinery, not embedding quality - real embeddings are the model's job.&lt;/li&gt;
&lt;li&gt;Asserts the second user's Tuesday memory never appears in the first user's results. It did not.&lt;/li&gt;
&lt;li&gt;Loads 20,000 filler memories and checks the HNSW index's top 5 against an exact scan's top 5: identical, with the standard caveat that HNSW recall is approximate in general and depends on build and search parameters (&lt;code&gt;hnsw.ef_search&lt;/code&gt; was raised to 200 for the check).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;What the run does not prove: anything about latency at production scale, or about how good real extraction is on either side. Those are your workload's questions. What it settles is that the pgvector layer is small, inspectable and behaves exactly as its SQL reads - there is no magic in it to debug later.&lt;/p&gt;

&lt;h2&gt;
  
  
  The honest comparison
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Dimension&lt;/th&gt;
&lt;th&gt;mem0 (platform)&lt;/th&gt;
&lt;th&gt;pgvector (your Postgres)&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Time to first memory&lt;/td&gt;
&lt;td&gt;Under an hour - two API calls&lt;/td&gt;
&lt;td&gt;A day - schema, extraction prompt, embedding wiring&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Extraction quality&lt;/td&gt;
&lt;td&gt;Their pipeline, tuned across many customers&lt;/td&gt;
&lt;td&gt;Your prompt, your eval problem&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Contradictions and updates&lt;/td&gt;
&lt;td&gt;Handled in their pipeline&lt;/td&gt;
&lt;td&gt;Your design decision (append, overwrite, supersede)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Where facts live&lt;/td&gt;
&lt;td&gt;Mem0's platform (or your server, OSS build)&lt;/td&gt;
&lt;td&gt;Your database, beside your users table&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Cost shape&lt;/td&gt;
&lt;td&gt;Free tier with your LLM key; usage past limits; Custom for graph/consolidation&lt;/td&gt;
&lt;td&gt;Your LLM + embedding calls; database you already pay for&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Quotas&lt;/td&gt;
&lt;td&gt;Adds and retrievals counted monthly&lt;/td&gt;
&lt;td&gt;None - your infrastructure limits&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Graph memory&lt;/td&gt;
&lt;td&gt;Platform, Custom plan&lt;/td&gt;
&lt;td&gt;Build it yourself (entity tables are a project)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Deleting one user&lt;/td&gt;
&lt;td&gt;API operation&lt;/td&gt;
&lt;td&gt;One DELETE with a user_id predicate&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Lock-in&lt;/td&gt;
&lt;td&gt;Facts and extraction logic live with the vendor; export discipline is on you&lt;/td&gt;
&lt;td&gt;None beyond Postgres&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Self-host escape hatch&lt;/td&gt;
&lt;td&gt;OSS library + server, BYO models and vector store&lt;/td&gt;
&lt;td&gt;Not applicable - it is already yours&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  What each side costs at real volumes
&lt;/h2&gt;

&lt;p&gt;No invented benchmarks - the cost structures, from the verified mechanics:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;On mem0 free&lt;/strong&gt;, a month with 40,000 conversations to extract and 8,000 memory searches fits inside the post-intro limits (50,000 adds, 10,000 retrievals) at $0 platform cost - but every one of those extractions ran an LLM call on your key, and every search is a retrieval whether or not it finds anything. Your real bill is your model's extraction spend, and it scales with conversation volume, not with users.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;On pgvector&lt;/strong&gt;, the same month costs the same extraction calls (your prompt now, possibly a smaller model since you control it), plus one embedding call per extracted fact and one per search, plus rows in a database already in your stack. Embedding calls are the cheapest calls in the AI stack by an order of magnitude; the storage is noise at this scale. There is no quota event in your future - the meter that runs out is your own database's capacity, on your own monitoring.&lt;/p&gt;

&lt;p&gt;The crossover is not really about money at small scale. It is about which failure mode you prefer: a vendor quota and a pricing page that can be restructured (as mem0's just was) versus a pipeline whose extraction quality is your problem on your worst week.&lt;/p&gt;

&lt;h2&gt;
  
  
  Tools mentioned in this post
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Tool&lt;/th&gt;
&lt;th&gt;What it is for&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;mem0&lt;/td&gt;
&lt;td&gt;Managed memory platform, plus the open-source library and server&lt;/td&gt;
&lt;td&gt;&lt;a href="https://mem0.ai" rel="noopener noreferrer"&gt;https://mem0.ai&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;pgvector&lt;/td&gt;
&lt;td&gt;The Postgres extension for vector storage and similarity search&lt;/td&gt;
&lt;td&gt;&lt;a href="https://github.com/pgvector/pgvector" rel="noopener noreferrer"&gt;https://github.com/pgvector/pgvector&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Supabase&lt;/td&gt;
&lt;td&gt;Hosted Postgres with pgvector preinstalled, common in Next.js stacks&lt;/td&gt;
&lt;td&gt;&lt;a href="https://supabase.com" rel="noopener noreferrer"&gt;https://supabase.com&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Neon&lt;/td&gt;
&lt;td&gt;Serverless Postgres with pgvector support&lt;/td&gt;
&lt;td&gt;&lt;a href="https://neon.tech" rel="noopener noreferrer"&gt;https://neon.tech&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;em&gt;On zainvibes.com this section is interactive: a decision picker - answer a few questions about your stack and your timeline and take the pick. &lt;a href="https://zainvibes.com/blog/nextjs-agent-memory-mem0-vs-pgvector" rel="noopener noreferrer"&gt;Try it on the original post.&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Before you ship either one
&lt;/h2&gt;

&lt;p&gt;The checklist I would pin above the memory layer, whichever side you picked - every item is a production incident class, not a nicety:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;On zainvibes.com this section is interactive: a pre-ship checklist - work through every item and track your progress before the memory layer goes live. &lt;a href="https://zainvibes.com/blog/nextjs-agent-memory-mem0-vs-pgvector" rel="noopener noreferrer"&gt;Try it on the original post.&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  My verdict: pgvector by default, mem0 when the calendar is the constraint
&lt;/h2&gt;

&lt;p&gt;For a Next.js app that already runs Postgres - which is most of the apps I build and review - &lt;strong&gt;I would ship pgvector&lt;/strong&gt;. The reasoning is not that mem0 is weak; its extraction pipeline is the better-engineered half of this comparison. It is that memory is core user data with a long memory of its own: it should live in the database you back up, under the deletion policies you already honor, with no quota and no pricing page between you and your users' facts. The extraction prompt is a day of work and a permanent small responsibility, and in exchange the whole layer is inspectable SQL that will still be running, unchanged, in five years.&lt;/p&gt;

&lt;p&gt;The answer flips in three named conditions. If you do not run Postgres and adding it just for memory makes no sense, mem0 is the rational buy. If memory is blocking a launch this week, ship mem0 now and keep your raw conversation logs in your own database, so a later move to pgvector is a data migration instead of an excavation. And if your product genuinely needs relationship-aware memory - entities, how facts about them change over time - that is graph memory, a Custom-plan feature on mem0 and a real project on pgvector; buy it.&lt;/p&gt;

&lt;p&gt;The answer flips back, too: if you start on mem0 and the retrieval meter, the data-residency conversation, or a pricing restructure starts to itch, the exit is well-defined precisely because the concepts are the same on both sides - facts, scoped to users, embedded and ranked. That portability is the quiet argument for learning the pgvector shape even if you rent first.&lt;/p&gt;

&lt;p&gt;If you are adding memory to an agent product and want the architecture reviewed before the data piles up, that is the kind of system I build for clients - &lt;a href="https://zainvibes.com/#contact" rel="noopener noreferrer"&gt;tell me about it here&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Frequently asked questions
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What is long-term memory for an AI agent?
&lt;/h3&gt;

&lt;p&gt;Long-term memory is the set of facts an agent keeps about a user across sessions - preferences, decisions, context like deploy days or database versions - extracted from past conversations and retrieved by relevance when a new conversation needs them. It is different from session memory (the current conversation's context window) and from RAG over documents: the source is the user's own history, and the write path matters as much as the read path.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is mem0 and how does it store agent memory?
&lt;/h3&gt;

&lt;p&gt;mem0 is a memory layer for AI agents. You send it conversation messages with a user id; its pipeline extracts the individual facts with an LLM, deduplicates and stores them per user, agent, app or run, and its search returns the relevant facts ranked for a new query. It is sold as a managed platform (free tier plus custom plans) and as an open-source library and self-hosted server where you bring your own LLM, embedder and vector store.&lt;/p&gt;

&lt;h3&gt;
  
  
  Is mem0 free?
&lt;/h3&gt;

&lt;p&gt;There is a real free tier. For the first 30 days it includes 10,000 memory adds and 1,000 retrievals a month with extraction running on Mem0's models; after that, connecting your own LLM key raises the free limits to 50,000 adds and 10,000 retrievals a month, with usage-based billing past the limits under a cap you set. Graph memory, consolidation and analytics are Custom-plan features. Older $19 Starter and $249 Pro tiers still appear in third-party comparisons but are no longer sold to new accounts.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is pgvector used for in agent memory?
&lt;/h3&gt;

&lt;p&gt;pgvector is a Postgres extension that stores embedding vectors in ordinary tables and searches them by distance (cosine, inner product, L2 and more), with exact search by default and HNSW or IVFFlat indexes for approximate search at scale. In an agent memory layer it is the storage and retrieval half: your code extracts facts from conversations, embeds them, inserts them as rows scoped to a user, and retrieves the nearest rows to a question's embedding. It gives you storage and ranking - extraction, deduplication and updates remain your code's job.&lt;/p&gt;

&lt;h3&gt;
  
  
  mem0 vs pgvector: which is better for a Next.js app?
&lt;/h3&gt;

&lt;p&gt;If you already run Postgres, pgvector is the better default for most Next.js apps: no new vendor, no per-call quotas, your users' facts stay in your own database, and the whole layer is one table, one index and one extraction prompt you control. mem0 is better when you do not run Postgres, need memory working this week, or do not want to own extraction quality and deduplication - and its graph memory is a platform feature you would otherwise have to build. The post works through both builds so you can judge the actual code difference.&lt;/p&gt;

&lt;h3&gt;
  
  
  Can I use pgvector with Supabase or Neon for agent memory?
&lt;/h3&gt;

&lt;p&gt;Yes. pgvector ships preinstalled or one CREATE EXTENSION away on most hosted Postgres providers, including Supabase and Neon, and the code is identical: the same table, the same cosine operator, the same HNSW index. If your Next.js app already uses one of them as its database, your memory store is already provisioned - you only add the extension, the table and the extraction step.&lt;/p&gt;

</description>
      <category>nextjs</category>
      <category>ai</category>
      <category>mem0</category>
      <category>pgvector</category>
    </item>
    <item>
      <title>Gemini 4 Hydrogen for Coding Agents: Cost, Setup, Verdict</title>
      <dc:creator>MUHAMMAD ZAIN UL ABIDIN</dc:creator>
      <pubDate>Sun, 15 Nov 2026 13:15:01 +0000</pubDate>
      <link>https://dev.to/zforzain2000/gemini-4-hydrogen-for-coding-agents-cost-setup-verdict-cj4</link>
      <guid>https://dev.to/zforzain2000/gemini-4-hydrogen-for-coding-agents-cost-setup-verdict-cj4</guid>
      <description>&lt;p&gt;Google retired the Flash name and gave its fast tier an element instead: Gemini 4 Hydrogen, launched November 12, 2026, aimed squarely at coding agents. The headline numbers are simple - $1.50 in, $7.50 out, a 1M-token window, four thinking levels - but the number that decides whether your agent makes money is none of those. It is the cost of one finished run, thinking tokens included. I went through the official pricing page, the model card and the migration guide, and I wrote the agent code against the real SDK. Here is what Hydrogen costs in practice, how you wire it up, where the launch claims need salt, and the traps waiting in a Flash migration.&lt;/p&gt;

&lt;p&gt;One disclosure before anything else, because this blog does not fake hands-on: I do not have a Gemini API key in my environment right now, so I could not run Hydrogen end to end. The code in this post is written against &lt;code&gt;@google/genai&lt;/code&gt; 3.3.0 and typechecked clean against the SDK's own types, and every price and capability below comes from Google's documentation, checked today. What I will not do is invent benchmark results and attribute them to a test I never ran. Google's and Artificial Analysis's numbers are labeled as theirs wherever they appear.&lt;/p&gt;

&lt;h2&gt;
  
  
  The short version: the fast tier grew up
&lt;/h2&gt;

&lt;p&gt;Hydrogen is the model Google wants doing the daily work of agents: writing and editing code, calling tools, running tests, answering questions about a codebase, and fanning out as parallel subagents. It replaces Gemini 3.8 Flash in that role, and 3.8 Flash stays available as the older, temporarily cheaper option.&lt;/p&gt;

&lt;p&gt;Three facts shape everything else in this post:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The price is flat.&lt;/strong&gt; $1.50 per 1M input and $7.50 per 1M output tokens across the entire context window - no long-context surcharge tier like older Pro models had.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Thinking is a priced dial.&lt;/strong&gt; Four levels (low, medium, high by default, and a new xhigh), and thinking tokens bill at the output rate. The level you choose is a budget decision, not just a quality one.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The window is unified.&lt;/strong&gt; Input, thinking and output share one 1,048,576-token budget. A giant context literally leaves less room for the answer.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If your agents are step-shaped - many small edits, tool calls, test runs and retries - this is the tier built for them, and the rest of this post is the working detail. If your workload is one long chain of hard reasoning, skip to the verdict: you are probably an orchestration customer, not a Hydrogen-only one.&lt;/p&gt;

&lt;h2&gt;
  
  
  Gemini 4 Hydrogen specs: what you are actually buying
&lt;/h2&gt;

&lt;p&gt;The full picture from the model page, so the rest of the post has a reference point:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;Gemini 4 Hydrogen&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Model ID&lt;/td&gt;
&lt;td&gt;&lt;code&gt;gemini-4-hydrogen&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Launched&lt;/td&gt;
&lt;td&gt;November 12, 2026&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Role&lt;/td&gt;
&lt;td&gt;Fast tier: everyday coding, tool use, agentic execution&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Context window&lt;/td&gt;
&lt;td&gt;1,048,576 tokens, unified (input + thinking + output)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Max output&lt;/td&gt;
&lt;td&gt;65,536 tokens per synchronous request; up to 1M with long decode (background execution)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Thinking levels&lt;/td&gt;
&lt;td&gt;low, medium, high (default), xhigh&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Input price (Standard)&lt;/td&gt;
&lt;td&gt;$1.50 per 1M tokens, free tier available&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Output price (Standard)&lt;/td&gt;
&lt;td&gt;$7.50 per 1M tokens, including thinking tokens&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Cached input&lt;/td&gt;
&lt;td&gt;$0.075 per 1M tokens (implicit caching only)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Batch / Flex&lt;/td&gt;
&lt;td&gt;$0.75 in, $3.75 out&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Priority&lt;/td&gt;
&lt;td&gt;$2.70 in, $13.50 out&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Inputs&lt;/td&gt;
&lt;td&gt;Text, image, video, audio, PDF&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Tools&lt;/td&gt;
&lt;td&gt;Function calling, structured outputs, code execution, computer use, grounding&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Two rows deserve a second look. The 65,536 output cap per synchronous request is the one your agent will actually hit: a long refactor or a big generated file needs background execution to go further, which is an architecture decision, not a parameter tweak. And "implicit caching only" is a genuine removal - Gemini 4 dropped explicit context caching entirely, which becomes migration trap number one below.&lt;/p&gt;

&lt;p&gt;The element naming is worth one sentence because it confuses everyone for a week: Hydrogen is the fast execution model, Argon is the efficient frontier reasoner, Carbon is the strongest frontier model. Google's own guidance is to pick by reasoning depth, latency and cost target - and, pointedly, to pair them.&lt;/p&gt;

&lt;h2&gt;
  
  
  The price that matters is cost per run, not cost per token
&lt;/h2&gt;

&lt;p&gt;A token price tells you almost nothing about an agent bill. An agent run is a loop: the context goes in again on every turn, tool results pile up, and the model thinks before it acts. What you pay for is input tokens (context, history, tool results) plus output tokens (thinking plus the visible answer) multiplied by the number of runs you actually serve.&lt;/p&gt;

&lt;p&gt;Here is the arithmetic, live. Set the shape of one of your runs and read the bill:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;On zainvibes.com this section is interactive: a cost calculator - set the shape of one of your runs (turns, context, cached share, thinking level) and read the real bill per run and per month. &lt;a href="https://zainvibes.com/blog/gemini-4-hydrogen-coding-agents" rel="noopener noreferrer"&gt;Try it on the original post.&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Three things the calculator cannot show you, which matter as much as the sliders:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Thinking level is the hidden multiplier.&lt;/strong&gt; Google prices thinking at the output rate, and in Artificial Analysis's testing, Hydrogen at xhigh used close to double the output tokens of the default high level, for 3 extra Intelligence Index points (54 vs 51). On a run that outputs 8,000 tokens at high, xhigh is not a quality upgrade - it is roughly a second output bill. Google's own advice is the right discipline: evaluate all four levels and pick the lowest one that clears your quality bar.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The cost-per-task comparison cuts in Hydrogen's favor - on Google's cited numbers.&lt;/strong&gt; Artificial Analysis measured a weighted average cost of $3.16 per Intelligence Index task for Hydrogen against $7.77 for 3.8 Flash, because a model that finishes in fewer turns spends fewer total tokens even at a higher rate. That is an independent lab's measurement, not mine, and your tasks are not their tasks - but the direction is the one that matters: per-token price comparisons systematically underrate models that end loops early.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Caching is where long-context agents win or bleed.&lt;/strong&gt; A coding agent resends its system prompt, tool definitions and repo context on every turn. Implicit caching discounts repeat input by 95% (to $0.075 per 1M) on prompts of at least 4,096 tokens, automatically. In the calculator, drag the cached share up and watch the monthly number: at agent scale, cache hit rate is a bigger lever than the tier you pick.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting up Gemini 4 Hydrogen: one call, then a real agent loop
&lt;/h2&gt;

&lt;p&gt;Start with the smallest honest thing - one call, with the thinking level set on purpose:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;GoogleGenAI&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s2"&gt;"@google/genai"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;GoogleGenAI&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;apiKey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;GEMINI_API_KEY&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;interaction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;interactions&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"gemini-4-hydrogen"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Audit this payment retry queue for race conditions and rewrite the lock acquisition logic."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;generation_config&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;thinking_level&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"xhigh"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nb"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;interaction&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;output_text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That is Google's documented quickstart shape on the Interactions API. The agent version is the same call inside a tool loop. This is the module I wrote and typechecked for this post - two tools, a turn limit, and &lt;code&gt;previous_interaction_id&lt;/code&gt; carrying the conversation (and the thought signatures you must not rebuild by hand) between turns:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;execFile&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s2"&gt;"node:child_process"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;readFile&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s2"&gt;"node:fs/promises"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;promisify&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s2"&gt;"node:util"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;GoogleGenAI&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s2"&gt;"@google/genai"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;execFileAsync&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;promisify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;execFile&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;GoogleGenAI&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;apiKey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;GEMINI_API_KEY&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nc"&gt;ThinkingLevel&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"low"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="s2"&gt;"medium"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="s2"&gt;"high"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="s2"&gt;"xhigh"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;tools&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"function"&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"read_file"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;description&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Read a UTF-8 text file from the project and return its contents."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;parameters&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"object"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;properties&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"string"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;description&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Project-relative file path"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;required&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"path"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"function"&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"run_tests"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;description&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Run the project test suite and return combined stdout/stderr."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;parameters&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"object"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;properties&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;runTool&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Record&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;unknown&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s2"&gt;"read_file"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;readFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"utf8"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s2"&gt;"run_tests"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;stdout&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;stderr&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;execFileAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"npm"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"test"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;timeout&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;120_000&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;stdout&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;stderr&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="c1"&gt;// A failing test run is information for the agent, not a crash.&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;stdout&lt;/span&gt;&lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;stderr&lt;/span&gt;&lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;stdout&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;stderr&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sb"&gt;`Unknown tool: ${name}`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;runCodingAgent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;task&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;thinkingLevel&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ThinkingLevel&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"high"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;maxTurns&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;interaction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;interactions&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"gemini-4-hydrogen"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;task&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;tools&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;generation_config&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;thinking_level&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;thinkingLevel&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;turn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;turn&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;maxTurns&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;turn&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;calls&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;interaction&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;steps&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;step&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;step&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s2"&gt;"function_call"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;calls&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;interaction&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;output_text&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;results&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;call&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="n"&gt;calls&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;call&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="s2"&gt;"function_call"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;continue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;isError&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;runTool&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;call&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;call&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;arguments&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="k"&gt;instanceof&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;isError&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="n"&gt;results&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"function_result"&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;call_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;call&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;call&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;is_error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;isError&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;interaction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;interactions&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"gemini-4-hydrogen"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;previous_interaction_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;interaction&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;results&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;tools&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;generation_config&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;thinking_level&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;thinkingLevel&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;interaction&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;output_text&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Three details in that code are load-bearing, and all three come from Google's migration notes rather than my taste. Tools are declared natively - prompt-invented JSON tool calls can come back as &lt;code&gt;MALFORMED_FUNCTION_CALL&lt;/code&gt;. A failed tool call returns its output as a result with &lt;code&gt;is_error&lt;/code&gt;, because Hydrogen is specifically tuned to adapt when tools fail instead of stalling. And the loop caps turns: an agent without a turn limit on a metered model is a billing incident with extra steps. If you use the Vercel AI SDK or AI Gateway instead, the model slug is &lt;code&gt;google/gemini-4-hydrogen&lt;/code&gt;; the same thinking-level discipline applies wherever you set it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Four thinking levels: a budget dial, not a quality dial
&lt;/h2&gt;

&lt;p&gt;Google's guidance table, condensed:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Level&lt;/th&gt;
&lt;th&gt;Token use&lt;/th&gt;
&lt;th&gt;Google's suggested use&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;low&lt;/td&gt;
&lt;td&gt;Fewest, lowest latency&lt;/td&gt;
&lt;td&gt;Cost- and latency-sensitive steps; still capable on agentic tasks&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;medium&lt;/td&gt;
&lt;td&gt;Lower&lt;/td&gt;
&lt;td&gt;Interactive coding, live prototyping, latency-sensitive tool calls&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;high (default)&lt;/td&gt;
&lt;td&gt;Balanced&lt;/td&gt;
&lt;td&gt;The recommended default: complex coding, agentic execution, tool calling&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;xhigh&lt;/td&gt;
&lt;td&gt;Most, deepest reasoning&lt;/td&gt;
&lt;td&gt;Hard multi-step problems where high falls short&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;My working rule for agent fleets: set the level per step type, not per app. Routing, classification and mechanical edits run at low or medium. The default workhorse steps run at high. xhigh is a targeted escalation for the step that failed at high - and Google explicitly warns it can overthink simple prompts, spending tokens to arrive slower at the same answer. The migration consequence is real too: 3.8 Flash defaulted to medium, Hydrogen defaults to high, so an unexamined model-string swap quietly raises both quality and cost on every call.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where the launch claims need salt
&lt;/h2&gt;

&lt;p&gt;Google's launch post makes three performance claims worth separating from the verified facts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;"2.5x faster than 3.8 Flash"&lt;/strong&gt; - Google's claim, at the default high setting. Plausible for the tier's purpose, unproven on your workload. Speed claims in this industry are measured on the vendor's happiest benchmark.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Intelligence Index 51 at high, 54 at xhigh, versus Argon's 50&lt;/strong&gt; - this one is Artificial Analysis's index, cited by Google. It is the most interesting number in the launch: the fast tier edges the efficient frontier model on a composite index. It is also a composite - your coding tasks may not track it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The Orchestrator numbers&lt;/strong&gt; - Carbon planning with Hydrogen executing reached near-Carbon success on Terminal-Bench with 24% less quota and 19% faster completion. Google-reported, exact gap unspecified ("near" is doing real work in that sentence). Treat it as a hypothesis worth testing on your own eval set, because if it holds even halfway, it reprices frontier-model agents.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;What is not a claim: Google says Hydrogen cuts fabricated claims by more than 5x versus 3.8 Flash by flagging missing context instead of guessing, and batches read and search tool calls in parallel. Both are behaviors you can verify cheaply on your own tasks within an afternoon, and for agent work they matter more than index points - an agent that says "this file is not in the context" instead of inventing its contents is the difference between a tool and a liability.&lt;/p&gt;

&lt;h2&gt;
  
  
  The migration traps nobody puts in the launch post
&lt;/h2&gt;

&lt;p&gt;Switching from 3.8 Flash is one model string - plus seven changes in Google's own migration checklist that break silently if you skip them. The checklist below is the interactive version; the three that bite hardest:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Explicit caching is gone.&lt;/strong&gt; If your cost model leans on &lt;code&gt;cachedContents&lt;/code&gt;, those requests fail on Gemini 4. Implicit caching is what remains, and it changes the math on long static contexts - recheck your per-run cost before migrating, not after the invoice.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Your output cap is now a thinking cap too.&lt;/strong&gt; &lt;code&gt;max_output_tokens&lt;/code&gt; counts thinking and output together. An old 8,192 cap that fit Flash answers will truncate Hydrogen answers mid-thought. Google's advice: raise caps to 65,536 and control spend with the thinking level instead.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Sampling parameters became errors.&lt;/strong&gt; In-range &lt;code&gt;temperature&lt;/code&gt; values are ignored, but out-of-range values return a 400. Somewhere in your config there is a &lt;code&gt;top_k&lt;/code&gt; from 2024. Find it before the deploy does.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;On zainvibes.com this section is interactive: a migration checklist - work through all seven changes from Google's migration guide and track your progress. &lt;a href="https://zainvibes.com/blog/gemini-4-hydrogen-coding-agents" rel="noopener noreferrer"&gt;Try it on the original post.&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Gemini 4 Hydrogen vs Argon vs 3.8 Flash: which runs your agents?
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;Hydrogen&lt;/th&gt;
&lt;th&gt;Argon&lt;/th&gt;
&lt;th&gt;3.8 Flash&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Role&lt;/td&gt;
&lt;td&gt;Fast execution tier&lt;/td&gt;
&lt;td&gt;Efficient frontier reasoning&lt;/td&gt;
&lt;td&gt;Previous fast tier&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Price (Standard)&lt;/td&gt;
&lt;td&gt;$1.50 / $7.50&lt;/td&gt;
&lt;td&gt;$2 / $10 until Nov 30, then $4 / $20&lt;/td&gt;
&lt;td&gt;$0.75 / $3.75 until Dec 31, then $1.50 / $7.50&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Context&lt;/td&gt;
&lt;td&gt;1M unified&lt;/td&gt;
&lt;td&gt;1M unified&lt;/td&gt;
&lt;td&gt;1M input, separate 64K output budget&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Thinking levels&lt;/td&gt;
&lt;td&gt;low, medium, high, xhigh&lt;/td&gt;
&lt;td&gt;Up to high&lt;/td&gt;
&lt;td&gt;Thinking budget era&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Free tier&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Pick it when&lt;/td&gt;
&lt;td&gt;Steps are many and small; latency and cost per task rule&lt;/td&gt;
&lt;td&gt;The task is long-horizon reasoning with fewer, harder turns&lt;/td&gt;
&lt;td&gt;You are mid-migration, or riding the promo price to Dec 31&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Still undecided? Answer four questions and take the verdict the post argues for:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;On zainvibes.com this section is interactive: a decision picker - answer four questions about your workload and take the pick the post argues for. &lt;a href="https://zainvibes.com/blog/gemini-4-hydrogen-coding-agents" rel="noopener noreferrer"&gt;Try it on the original post.&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Tools mentioned in this post
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Tool&lt;/th&gt;
&lt;th&gt;What it is for&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;Gemini API&lt;/td&gt;
&lt;td&gt;The paid and free API tiers Hydrogen runs on&lt;/td&gt;
&lt;td&gt;&lt;a href="https://ai.google.dev" rel="noopener noreferrer"&gt;https://ai.google.dev&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Google AI Studio&lt;/td&gt;
&lt;td&gt;Try Hydrogen in the browser, grab an API key&lt;/td&gt;
&lt;td&gt;&lt;a href="https://aistudio.google.com" rel="noopener noreferrer"&gt;https://aistudio.google.com&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Google Antigravity&lt;/td&gt;
&lt;td&gt;Google's agent platform; its Managed Agents default to Hydrogen&lt;/td&gt;
&lt;td&gt;&lt;a href="https://antigravity.google" rel="noopener noreferrer"&gt;https://antigravity.google&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Vercel AI Gateway&lt;/td&gt;
&lt;td&gt;Route &lt;code&gt;google/gemini-4-hydrogen&lt;/code&gt; through the AI SDK stack&lt;/td&gt;
&lt;td&gt;&lt;a href="https://vercel.com/ai-gateway" rel="noopener noreferrer"&gt;https://vercel.com/ai-gateway&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  My verdict: make Hydrogen the worker, keep a frontier model as the planner
&lt;/h2&gt;

&lt;p&gt;If you build coding agents, my default is: &lt;strong&gt;Hydrogen runs the steps, a frontier model plans the work, and the thinking level is set per step type.&lt;/strong&gt; Hydrogen alone is the right call for step-shaped workloads - issue fixes, test repair, codebase Q&amp;amp;A, subagent fan-out - where runs are many, latency matters, and no single step needs frontier reasoning. The flat price across the full window and the free tier for evaluation make it the cheapest serious agent model to adopt carefully.&lt;/p&gt;

&lt;p&gt;The answer flips in three named conditions. If your workload is one long reasoning chain rather than steps, that is Argon or Carbon work - Hydrogen is the executor, not the brain. If your cost model depends on explicit context caching, you do not migrate until that is reworked, full stop. And if you are happily on 3.8 Flash with no agent ambitions, the rational move is unglamorous: ride the half-price promo to December 31, budget the January doubling, and migrate when the price gap closes to zero - because from January 1, staying on Flash buys you the older model at the same price.&lt;/p&gt;

&lt;p&gt;If you are wiring a coding agent into a product and want the cost model reviewed before it scales, that is exactly the kind of system I build for clients - &lt;a href="https://zainvibes.com/#contact" rel="noopener noreferrer"&gt;tell me about it here&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Frequently asked questions
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What is Gemini 4 Hydrogen?
&lt;/h3&gt;

&lt;p&gt;Gemini 4 Hydrogen is Google's fast model tier for everyday coding and agentic workflows, launched November 12, 2026. It takes over the role Gemini 3.8 Flash held, under the Gemini 4 family's element naming (Hydrogen, Argon, Carbon). It has a unified 1,048,576-token context window, four thinking levels, and costs $1.50 per 1M input tokens and $7.50 per 1M output tokens on the paid tier, with a free tier available.&lt;/p&gt;

&lt;h3&gt;
  
  
  How much does Gemini 4 Hydrogen cost per million tokens?
&lt;/h3&gt;

&lt;p&gt;On the Standard tier, $1.50 per 1M input tokens and $7.50 per 1M output tokens, flat across the full 1M-token context window, with cached input at $0.075 per 1M. Batch and Flex processing halve that to $0.75 / $3.75. Priority processing costs $2.70 / $13.50. Output prices include thinking tokens, so the thinking level you pick directly sets your bill.&lt;/p&gt;

&lt;h3&gt;
  
  
  Is Gemini 4 Hydrogen free to use?
&lt;/h3&gt;

&lt;p&gt;Yes. Unlike Gemini 4 Carbon and Argon, which are paid-tier only, Hydrogen is available on the free tier of the Gemini API and in Google AI Studio, with rate limits. Free-tier content may be used to improve Google's products; paid-tier content is not.&lt;/p&gt;

&lt;h3&gt;
  
  
  What are the four thinking levels in Gemini 4 Hydrogen?
&lt;/h3&gt;

&lt;p&gt;Low, medium, high and xhigh. High is the default. Low spends the fewest tokens and suits high-throughput steps; medium fits interactive coding; high is the recommended default for complex coding and tool calling; xhigh spends the most tokens and is meant for hard multi-step problems where high falls short. Thinking tokens are billed at the output rate, so xhigh can roughly double a run's output cost.&lt;/p&gt;

&lt;h3&gt;
  
  
  Should I migrate from Gemini 3.8 Flash to Gemini 4 Hydrogen?
&lt;/h3&gt;

&lt;p&gt;For coding agents and tool-using workloads, yes - it is Google's recommended upgrade and the code change starts with the model string. Three traps matter: Gemini 4 removed explicit context caching (implicit caching only), the default thinking level rises from medium to high, and old low max_output_tokens caps can truncate answers because the cap now counts thinking and output together. Also note the price crossover: 3.8 Flash is half Hydrogen's price until December 31, 2026, then doubles to exactly Hydrogen's rate.&lt;/p&gt;

&lt;h3&gt;
  
  
  Gemini 4 Hydrogen vs Gemini 4 Argon: which should I use?
&lt;/h3&gt;

&lt;p&gt;Hydrogen for execution: fast, cheaper per token, built for many-step agent work. Argon is the efficient frontier reasoner - use it when a task is one long chain of hard reasoning rather than many steps, or pair them: Argon or Carbon plans, Hydrogen executes the steps. Carbon remains the strongest model for the hardest problems and costs the most thinking tokens per turn.&lt;/p&gt;

</description>
      <category>gemini</category>
      <category>ai</category>
      <category>aiagents</category>
      <category>llm</category>
    </item>
    <item>
      <title>A2UI 1.0 in Next.js: The Deep Dive and Your First Agent Screen</title>
      <dc:creator>MUHAMMAD ZAIN UL ABIDIN</dc:creator>
      <pubDate>Fri, 13 Nov 2026 04:54:02 +0000</pubDate>
      <link>https://dev.to/zforzain2000/a2ui-10-in-nextjs-the-deep-dive-and-your-first-agent-screen-43kk</link>
      <guid>https://dev.to/zforzain2000/a2ui-10-in-nextjs-the-deep-dive-and-your-first-agent-screen-43kk</guid>
      <description>&lt;p&gt;Your agent's worst answers are not wrong. They are shaped wrong. Ask it to compare three flights, approve a deploy, or collect six fields, and it hands you a paragraph - then three rounds of "which one?" while the decision dissolves into chat history. Google's answer to that shape problem is A2UI, an open protocol where the agent describes a screen as JSON and your app renders it with its own components. Version 1.0 went stable on November 11, 2026. I did not stop at the announcement: I installed Google's React renderer, fed it a real 1.0 payload in a Next.js app, clicked the buttons, and read the spec end to end. This is what I found - the protocol's anatomy, its security model, the places it is still held together with tape, and whether you should build on it.&lt;/p&gt;

&lt;h2&gt;
  
  
  The short version: the agent describes, your app draws
&lt;/h2&gt;

&lt;p&gt;A2UI splits one job into two. The agent decides what the user needs to see and sends a stream of JSON messages naming components from a catalog - a card, a text line, a choice picker, a button - plus the data those components bind to. Your application owns everything visual: it renders each named component with its own native implementation, in its own design system, at native speed. The agent never sends HTML, never sends code, and cannot invent a component you did not register.&lt;/p&gt;

&lt;p&gt;Three consequences follow from that split, and they are the whole pitch. First, one agent answer renders everywhere the catalog exists - web, Android, iOS, or inside an assistant like Claude or Gemini that hosts agent UI. Second, the trust boundary is structural: the worst a bad payload can do is arrange your own components badly. Third, styling is entirely yours, because the protocol carries no theme at all - a 1.0 message that tries to set one is invalid.&lt;/p&gt;

&lt;p&gt;You do not have to take the shape on faith. The playground below is the protocol in miniature: compose a surface, watch the payload rebuild itself, and press the rendered buttons to see the action messages an agent receives back.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The playground (static edition).&lt;/strong&gt; On &lt;a href="https://zainvibes.com/blog/a2ui-nextjs-deep-dive" rel="noopener noreferrer"&gt;zainvibes.com&lt;/a&gt; this section is a working playground: you pick a scenario (deploy approval, flight picker, expense form), toggle components, and the A2UI payload rebuilds itself live next to the rendered screen, with the action messages shown as you press its buttons. The deploy-approval surface, fully composed, is 3,131 bytes of JSON that renders on web, Android, iOS and in MCP Apps hosts. The same screen handwritten as React is roughly 118 lines of JSX for one platform, before the state wiring, validation and action plumbing around it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why agents answering in text breaks down
&lt;/h2&gt;

&lt;p&gt;Text is a fine medium for answers that are prose. It is a poor medium for answers that are decisions. Comparing three products in a paragraph forces the reader to hold the comparison table in their head. Approving a plan in chat means the plan's details scroll away while the approval stays ambiguous. Collecting structured input through conversation turns one form into six round trips, each one a chance to abandon the task.&lt;/p&gt;

&lt;p&gt;Google's framing is that agent interaction today sits where computing sat in the terminal era: capable, and answering almost everything in text. The interesting part is not the analogy, it is which tasks break first. In my own work building agents, the failures cluster in exactly three shapes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Approvals.&lt;/strong&gt; A deploy, a payment, a change to a record. The user must see precisely what will happen and confirm it. Text buries the specifics and makes "yes" ambiguous - yes to what, exactly, at which values?&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Comparisons and pickers.&lt;/strong&gt; Flights, plans, products, time slots. The information is inherently tabular and the action is a selection. A list in prose makes the user do the layout work the interface should do.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Forms.&lt;/strong&gt; Any time the agent needs five fields, chat becomes a slow, error-prone form with no validation until the end.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Notice what these three have in common: the set of screens is small and known in advance, even though the data changes every time. That is the exact workload a component catalog fits. A2UI is not trying to let agents design arbitrary interfaces. It is giving them a safe way to fill in the screens your product already knows it needs, with data only the agent has.&lt;/p&gt;

&lt;h2&gt;
  
  
  The protocol: four messages, one data model
&lt;/h2&gt;

&lt;p&gt;An A2UI conversation is a stream of JSON objects from agent to renderer. Four message types do the structural work, and every one of them is small enough to hold in your head.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;createSurface&lt;/code&gt; opens a named surface - a region of UI the agent owns - and in 1.0 it can carry the full component list and the initial data model inline, so a complete screen arrives in a single message.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;updateComponents&lt;/code&gt; adds or replaces component definitions on a surface.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;updateDataModel&lt;/code&gt; replaces the data at a JSON Pointer path, so the agent can change what the screen shows without resending the screen.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;deleteSurface&lt;/code&gt; tears a surface down.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Components arrive as a flat list, not a nested tree. Each component has an &lt;code&gt;id&lt;/code&gt;, a &lt;code&gt;component&lt;/code&gt; name from the catalog, and properties; containers reference their children by id, in what the spec calls an adjacency list. One component must carry the id &lt;code&gt;root&lt;/code&gt; - it mounts under the surface's implicit container. Here is a complete, valid 1.0 payload, the same shape as the deploy approval I rendered in my test build:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;"version"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"1.0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"createSurface"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;"surfaceId"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"deploy-confirm"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nl"&gt;"catalogId"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"https://a2ui.org/specification/v1_0/catalogs/basic/catalog.json"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nl"&gt;"sendDataModel"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nl"&gt;"components"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"root"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"component"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Card"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"child"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"col"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"col"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"component"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Column"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"children"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"title"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"picker"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"go"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"title"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"component"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Text"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"text"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Deploy to production?"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nl"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"picker"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nl"&gt;"component"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"ChoicePicker"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nl"&gt;"variant"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"mutuallyExclusive"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nl"&gt;"options"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
          &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;"label"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Production"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"value"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"production"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;"label"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Preview"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"value"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"preview"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nl"&gt;"value"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;"@path"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"/environment"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nl"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"go"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nl"&gt;"component"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Button"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nl"&gt;"variant"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"primary"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nl"&gt;"child"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"goLabel"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nl"&gt;"action"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="nl"&gt;"event"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"confirm_deploy"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="nl"&gt;"context"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;"environment"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;"@path"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"/environment"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
          &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"goLabel"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"component"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Text"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"text"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Confirm deploy"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nl"&gt;"dataModel"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;"environment"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"production"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The catalog named in &lt;code&gt;catalogId&lt;/code&gt; is the vocabulary contract. The basic catalog that ships with the spec has 18 components: layout (Row, Column, List, Card, Tabs, Divider, Modal), display (Text, Image, Icon, Video, AudioPlayer), and input (Button, CheckBox, TextField, DateTimeInput, ChoicePicker, Slider). You are not stuck with it. A catalog is just a schema document, and defining your own - your design system's components, with your prop shapes - is the intended production move. The agent generates against whichever catalog you put in its prompt.&lt;/p&gt;

&lt;p&gt;Two design choices in this anatomy matter more than they look. The flat adjacency list means an agent can stream components in any order and patch one component without resending the tree - partial updates are cheap. And the hard split between component structure and data model means the data can move on its own channel of updates: a live price, a typing user, and an agent patch all write to the same model the components read from.&lt;/p&gt;

&lt;p&gt;The return trip has its own message family. When a user presses a button, the renderer sends an &lt;code&gt;action&lt;/code&gt; message: the action's name plus a context object whose values are resolved from the data model at press time. Version 1.0 adds explicit function-call messages in both directions - &lt;code&gt;callAgentFunction&lt;/code&gt; when the UI needs the agent to check something mid-interaction (the spec's example is a flight picker verifying availability on the spot), and &lt;code&gt;callRendererFunction&lt;/code&gt; when the agent needs something only the client knows. Those two turn a mostly one-way description protocol into a conversation, and they are the headline feature of 1.0.&lt;/p&gt;

&lt;p&gt;The protocol is deliberately transport-agnostic. The spec defines bindings for AG-UI (the standard one for agent frontends), an A2A extension for agent-to-agent traffic, MCP tool calls and outputs, and plain SSE or WebSockets. The transport contract is three things: messages arrive in order, messages are framed as discrete JSON objects, and metadata can ride along - the renderer uses it to send the current data model back with actions when a surface sets &lt;code&gt;sendDataModel&lt;/code&gt;, and to exchange capability information about which catalogs each side supports.&lt;/p&gt;

&lt;h2&gt;
  
  
  Data binding: &lt;a class="mentioned-user" href="https://dev.to/path"&gt;@path&lt;/a&gt;, &lt;a class="mentioned-user" href="https://dev.to/call"&gt;@call&lt;/a&gt;, and a two-way street
&lt;/h2&gt;

&lt;p&gt;A component property in A2UI is rarely a plain value. The spec calls the bindable types Dynamic types, and each accepts three forms: a literal, a data binding written &lt;code&gt;{"@path": "/user/name"}&lt;/code&gt; using JSON Pointer syntax, or a function call written &lt;code&gt;{"@call": "formatCurrency", "args": {...}}&lt;/code&gt;. The &lt;code&gt;@&lt;/code&gt; namespace is reserved for the protocol - &lt;code&gt;@path&lt;/code&gt;, &lt;code&gt;@call&lt;/code&gt;, and a system function &lt;code&gt;@index&lt;/code&gt; that returns the position of the current item when a list template renders.&lt;/p&gt;

&lt;p&gt;The function set in the basic catalog tells you what the protocol expects to happen on the client. Formatting functions (&lt;code&gt;formatString&lt;/code&gt;, &lt;code&gt;formatNumber&lt;/code&gt;, &lt;code&gt;formatCurrency&lt;/code&gt;, &lt;code&gt;formatDate&lt;/code&gt;, &lt;code&gt;pluralize&lt;/code&gt;) turn raw model values into display text locally, so the agent ships numbers, not pre-formatted strings. &lt;code&gt;formatString&lt;/code&gt; supports interpolation - &lt;code&gt;"Build ${/build/id} from commit ${/build/commit}"&lt;/code&gt; is a binding, and in my test build it resolved against the data model exactly as written. Validation functions (&lt;code&gt;required&lt;/code&gt;, &lt;code&gt;email&lt;/code&gt;, &lt;code&gt;regex&lt;/code&gt;, &lt;code&gt;length&lt;/code&gt;, &lt;code&gt;numeric&lt;/code&gt;) power &lt;code&gt;@checks&lt;/code&gt;: declarative validation rules attached to inputs and buttons, evaluated by the renderer, with error messages the component displays. The &lt;code&gt;regex&lt;/code&gt; function even runs a static safety check on patterns first, rejecting the shapes that cause catastrophic backtracking. Someone on this spec has been hurt by regex denial-of-service before, and it shows in a good way.&lt;/p&gt;

&lt;p&gt;Binding runs both ways. The input components - TextField, CheckBox, Slider, ChoicePicker, DateTimeInput - write user input back into the data model at their bound path. Bind a Text label to the same path and it updates live as the user types. That two-way binding is what makes an A2UI surface feel like an app instead of a screenshot: the data model is shared state, the agent reads the final state through the action context or the &lt;code&gt;sendDataModel&lt;/code&gt; metadata, and nobody re-parses prose to find out what the user typed.&lt;/p&gt;

&lt;p&gt;Lists get a template mechanism instead of repetition. A container's children can be a template: one component id plus a path to an array in the data model. The renderer stamps the template once per item, bindings inside resolve relative to that item, and &lt;code&gt;${@index()}&lt;/code&gt; gives you numbering. An agent returning forty search results describes one row, not forty - which matters, because the agent is paying for every token of payload it writes.&lt;/p&gt;

&lt;h2&gt;
  
  
  The security model: the catalog is the permission slip
&lt;/h2&gt;

&lt;p&gt;The standard the project sets for itself is "safe like data, expressive like code", and the mechanism is genuinely simple. The agent's output is data - JSON describing components. It is never code, never markup, and the renderer only knows how to draw components from catalogs it registered. A prompt-injected agent, or a compromised one, cannot smuggle in a script tag, an iframe, or a component you did not build. Its entire vocabulary of harm is rearranging your own components.&lt;/p&gt;

&lt;p&gt;The function rules tighten this further. Every catalog function declares who may call it: the renderer only, the agent only, or both. Functions that require a real user gesture - &lt;code&gt;openUrl&lt;/code&gt; is the spec's example - must be declared renderer-only, and if an agent tries to invoke one through &lt;code&gt;callRendererFunction&lt;/code&gt;, the renderer rejects it with a structured error. Sensitive data gets a matching protection on the server side: Google points out that binding can happen server-side, so the values a component displays never have to pass through the model at all. The agent can say "put the account balance here" without ever seeing the balance.&lt;/p&gt;

&lt;p&gt;Now the part the launch posts skip. This model constrains components, not intent. An agent that wants to deceive your user can compose a perfectly legal surface from your catalog: a card that looks like a confirmation but confirms something else, a primary button labeled for the safe action wired to the destructive one. A2UI gives you no protection there, and it cannot - the same is true of any UI your own frontend team ships. The correct mental model is that A2UI makes agent output exactly as trustworthy as your own UI code, no more and no less. Two practices follow. Treat action handlers as API endpoints: authorize them server-side, every time, because the action name and context are client-supplied data. And validate payloads before rendering, against the catalog schema, so malformed or off-catalog output dies at the boundary - the spec even defines the error format for feeding validation failures back to the model so it can correct itself.&lt;/p&gt;

&lt;p&gt;One more boundary worth naming: the agent learns whatever the user types into bound fields when the action fires. If a surface collects something sensitive, that is a data-flow decision you made in the catalog and the surface design, not a protocol leak. Design surfaces like you design forms, because that is what they are.&lt;/p&gt;

&lt;h2&gt;
  
  
  Streaming: the screen assembles in order
&lt;/h2&gt;

&lt;p&gt;A2UI was designed for a model that is still thinking. Messages arrive as a stream, the renderer applies them in order, and the interface builds progressively: the spec tells renderers to draw placeholders for components whose children or data have not arrived yet, rather than waiting for a complete payload. A slow agent does not mean a blank panel - it means a screen that assembles itself, section by section, and fills in as bindings resolve.&lt;/p&gt;

&lt;p&gt;Three properties make this work in practice. The stream is stateful and ordered, which is why the transport contract demands reliable in-order delivery: a &lt;code&gt;updateDataModel&lt;/code&gt; that lands before its &lt;code&gt;createSurface&lt;/code&gt; is a corrupt stream, not a race you can shrug off. Updates are granular - one component definition or one data path per message - so a live-updating screen costs a trickle of small JSON objects, not full re-renders of the payload. And the end of the agent's turn is the transport's business, not a protocol message, which keeps the protocol usable over everything from server-sent events to A2A.&lt;/p&gt;

&lt;p&gt;The practical consequence for your frontend: perceived latency is a property of how early the first useful message arrives, and that is under the agent's control, not the protocol's. An agent that sends the surface skeleton first and streams data patches after will feel fast. One that composes the entire payload before sending anything will feel exactly as slow as the text it replaced. If you adopt A2UI, prompt and structure your agent for skeleton-first streaming from day one; retrofitting it is miserable.&lt;/p&gt;

&lt;h2&gt;
  
  
  A2UI vs AG-UI vs MCP Apps vs hand-rolled JSON
&lt;/h2&gt;

&lt;p&gt;These four get compared as if they were rivals. Two of them are not even the same kind of thing, and untangling that is worth more than any feature matrix.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;AG-UI&lt;/strong&gt; is the event protocol between an agent backend and a frontend: how messages, tool calls, state patches and UI payloads travel, with SDKs across the agent frameworks. It is a transport and interaction layer. A2UI payloads are one of the things it carries. Choosing AG-UI does not answer "what does the agent's UI look like" - A2UI is one possible answer to that question, riding on AG-UI's pipe.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;MCP Apps&lt;/strong&gt; is the opposite rendering philosophy. An MCP server registers an HTML resource under a &lt;code&gt;ui://&lt;/code&gt; URI, a tool points at it, and the host renders that HTML in a sandboxed iframe. The server ships real interface code; the host's job is containment. That buys total layout freedom - it is just a web page - at the cost of your design system, native feel, and any rendering outside a web view. A2UI ships a description; the host's own components do the drawing. Notably, they compose: Google documents running A2UI screens inside MCP Apps hosts, which is how Alation shows its feeds inside Claude.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Hand-rolled JSON&lt;/strong&gt; is what most of us actually have: the agent emits structured output, our frontend maps it to components we wrote. It works, it is fully ours, and it quietly becomes a private protocol - undocumented schema, no validation, no second renderer, and a rewrite waiting for the day the same agent needs a mobile app. I say this as someone who has &lt;a href="https://zainvibes.com/blog/ai-agent-typescript-harness-200-lines" rel="noopener noreferrer"&gt;built agents on a hand-rolled harness&lt;/a&gt; and defends the approach: the harness is not the problem, but an unversioned UI dialect of your own invention becomes one.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;A2UI&lt;/th&gt;
&lt;th&gt;AG-UI&lt;/th&gt;
&lt;th&gt;MCP Apps&lt;/th&gt;
&lt;th&gt;Hand-rolled JSON&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;What it is&lt;/td&gt;
&lt;td&gt;A UI description format&lt;/td&gt;
&lt;td&gt;An agent-frontend event protocol&lt;/td&gt;
&lt;td&gt;A way to ship HTML UI from an MCP server&lt;/td&gt;
&lt;td&gt;Your own structured output&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;What travels&lt;/td&gt;
&lt;td&gt;JSON component descriptions + data&lt;/td&gt;
&lt;td&gt;Events: messages, state, tool calls, UI payloads&lt;/td&gt;
&lt;td&gt;An HTML resource rendered by the host&lt;/td&gt;
&lt;td&gt;Whatever JSON you invented&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Who renders&lt;/td&gt;
&lt;td&gt;The host app, with its own components&lt;/td&gt;
&lt;td&gt;N/A - it is the pipe, not the picture&lt;/td&gt;
&lt;td&gt;The server's HTML, in a sandboxed iframe&lt;/td&gt;
&lt;td&gt;Your frontend, by hand&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Design system&lt;/td&gt;
&lt;td&gt;Yours, exactly&lt;/td&gt;
&lt;td&gt;N/A&lt;/td&gt;
&lt;td&gt;The server's, not the host's&lt;/td&gt;
&lt;td&gt;Yours, exactly&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Cross-platform&lt;/td&gt;
&lt;td&gt;Same payload on web, Android, iOS&lt;/td&gt;
&lt;td&gt;Wherever its SDKs run&lt;/td&gt;
&lt;td&gt;Web-view hosts only&lt;/td&gt;
&lt;td&gt;One frontend at a time&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Main risk&lt;/td&gt;
&lt;td&gt;Young spec, renderer maturity&lt;/td&gt;
&lt;td&gt;None for A2UI - they compose&lt;/td&gt;
&lt;td&gt;Iframe UX and host support variance&lt;/td&gt;
&lt;td&gt;Becoming an unversioned private protocol&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The decision this table supports: if your agent lives inside your own product, A2UI and hand-rolled JSON are the real contenders, and the question is whether you want a spec, a validator and a second renderer someday, or full control and full maintenance. If your agent must appear inside other people's assistants, you are in MCP territory - and A2UI can ride inside it. AG-UI enters the picture when you want a standard pipe between your agent and any of the above.&lt;/p&gt;

&lt;h2&gt;
  
  
  The ecosystem: who ships what
&lt;/h2&gt;

&lt;p&gt;Everything below is open source under Apache-2.0. There is nothing to buy, which is worth stating plainly in a landscape where agent tooling usually has a pricing page.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Piece&lt;/th&gt;
&lt;th&gt;What exists today&lt;/th&gt;
&lt;th&gt;Source&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Spec&lt;/td&gt;
&lt;td&gt;A2UI 1.0, current since the Nov 11, 2026 release; 0.9.1 previous, 0.8 deprecated&lt;/td&gt;
&lt;td&gt;&lt;a href="https://a2ui.org/specification/v1.0-a2ui/" rel="noopener noreferrer"&gt;a2ui.org&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Web renderers&lt;/td&gt;
&lt;td&gt;React, Angular, Lit (&lt;code&gt;@a2ui/react&lt;/code&gt;, &lt;code&gt;@a2ui/web_core&lt;/code&gt; at 0.15.0)&lt;/td&gt;
&lt;td&gt;&lt;a href="https://www.npmjs.com/package/@a2ui/react" rel="noopener noreferrer"&gt;npm&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Mobile renderers&lt;/td&gt;
&lt;td&gt;Flutter, SwiftUI at 1.0; Jetpack Compose at 0.9.1, 1.0 coming&lt;/td&gt;
&lt;td&gt;&lt;a href="https://a2ui.org" rel="noopener noreferrer"&gt;a2ui.org&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Agent SDKs&lt;/td&gt;
&lt;td&gt;Python, TypeScript, Dart - generation help, message translation, error correction&lt;/td&gt;
&lt;td&gt;&lt;a href="https://developers.googleblog.com/a2ui-10-build-delightful-fast-reliable-agentic-experiences/" rel="noopener noreferrer"&gt;Google announcement&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Transports&lt;/td&gt;
&lt;td&gt;AG-UI, A2A extension, MCP binding, SSE, WebSockets&lt;/td&gt;
&lt;td&gt;&lt;a href="https://a2ui.org/specification/v1.0-a2ui/" rel="noopener noreferrer"&gt;Spec&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Generation aids&lt;/td&gt;
&lt;td&gt;A2UI Express format, Python macros, decision models (ByteDance's Jev) instead of LLM-written JSON&lt;/td&gt;
&lt;td&gt;&lt;a href="https://developers.googleblog.com/a2ui-10-build-delightful-fast-reliable-agentic-experiences/" rel="noopener noreferrer"&gt;Google announcement&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Hosts and adopters&lt;/td&gt;
&lt;td&gt;Atlassian (Jira), Alation, Bloomberg, ByteDance (Lynx renderer); Gemini renders 0.9.1 today&lt;/td&gt;
&lt;td&gt;&lt;a href="https://developers.googleblog.com/a2ui-10-build-delightful-fast-reliable-agentic-experiences/" rel="noopener noreferrer"&gt;Google announcement&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Your first A2UI screen in Next.js, from a build that actually runs
&lt;/h2&gt;

&lt;p&gt;Enough spec. I built the smallest honest version of this: a Next.js 16 app where an agent's answer is a deploy-confirmation card, rendered by Google's official React renderer from one 1.0 message, with the button press coming back as an action. Everything in this section is from that build - the code ran, the typecheck passed, and I clicked the result in a browser before writing a word of it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Install the renderer and the core
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm install @a2ui/react @a2ui/web_core
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two packages: the React renderer and the framework-agnostic core that parses messages and holds surface state. As of this writing both are at 0.15.0 - the spec is 1.0, the packages are not, and that gap will matter again in the limits section. Pin exact versions.&lt;/p&gt;

&lt;h3&gt;
  
  
  Load the surface client-only
&lt;/h3&gt;

&lt;p&gt;The renderer registers custom elements the moment it loads, so importing it in a server component kills your build at prerender with &lt;code&gt;ReferenceError: HTMLElement is not defined&lt;/code&gt;. I hit this on the first build. The fix is the standard Next.js shape: wrap the surface component in &lt;code&gt;next/dynamic&lt;/code&gt; with &lt;code&gt;ssr: false&lt;/code&gt;, inside a small client component.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="s2"&gt;"use client"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;dynamic&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s2"&gt;"next/dynamic"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// The A2UI React renderer registers custom elements when it loads, so it&lt;/span&gt;
&lt;span class="c1"&gt;// cannot be imported on the server at all. Load the surface client-only.&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nc"&gt;AgentScreen&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;dynamic&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"./AgentScreen"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;AgentScreen&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;ssr&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;loading&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nc"&gt;Waiting&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;agent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Create the processor and feed it the payload
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;MessageProcessor&lt;/code&gt; from &lt;code&gt;@a2ui/web_core/v1_0&lt;/code&gt; owns the protocol state: give it your catalogs and an action handler, feed it messages, render the surfaces it builds. One gotcha lives here - name the generic. Without the explicit type argument on the processor, TypeScript infers a surface type that &lt;code&gt;A2uiSurface&lt;/code&gt; refuses, and the error gives you no hint that a type argument was the fix.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="s2"&gt;"use client"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;useState&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s2"&gt;"react"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;A2uiSurface&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nc"&gt;ReactCatalogComponent&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s2"&gt;"@a2ui/react"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;MessageProcessor&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;basicCatalog&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s2"&gt;"@a2ui/web_core/v1_0"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;agentMessages&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s2"&gt;"./agentMessages"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nc"&gt;AgentScreen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;lastAction&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;setLastAction&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;useState&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;unknown&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;processor&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;MessageProcessor&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;ReactCatalogComponent&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;basicCatalog&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;action&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// The renderer-to-agent return channel. In production this goes&lt;/span&gt;
        &lt;span class="c1"&gt;// back over your transport; here we keep it where we can see it.&lt;/span&gt;
        &lt;span class="nf"&gt;setLastAction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;action&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;action&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;processMessages&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;agentMessages&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;surfaces&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;setSurfaces&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
    &lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;processor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;surfacesMap&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;values&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nf"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;sync&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
      &lt;span class="nf"&gt;setSurfaces&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;processor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;surfacesMap&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;values&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;created&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;processor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;onSurfaceCreated&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sync&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;deleted&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;processor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;onSurfaceDeleted&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sync&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;created&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;unsubscribe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="n"&gt;deleted&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;unsubscribe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;processor&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;surfaces&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;surface&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;A2uiSurface&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;surface&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="n"&gt;surface&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;surface&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The payload itself - &lt;code&gt;agentMessages&lt;/code&gt; - is the deploy card from the protocol section, extended with a data-bound summary line, an environment picker, and a release-note field. In a real app those messages arrive from your agent over SSE or AG-UI; where they come from is the one part of this architecture the protocol refuses to care about. If your agent runs on the Vercel AI SDK or a hand-written loop - the choice I weighed in &lt;a href="https://zainvibes.com/blog/agent-framework-vs-agent-harness" rel="noopener noreferrer"&gt;framework vs harness&lt;/a&gt; - it emits the same JSON either way. The TypeScript agent SDK exists to help with generation and validation, but the wire format is the contract, not the SDK.&lt;/p&gt;

&lt;h3&gt;
  
  
  Press the button and read what comes back
&lt;/h3&gt;

&lt;p&gt;Rendered, the card shows the interpolated summary ("Build dpl_8f3ka from commit 5037730. 214 tests passing, 0 failing."), the environment radios, and the note field - none of it written as JSX by me. I selected Preview, typed a note, and clicked Confirm deploy. The action handler received exactly this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"confirm_deploy"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"context"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;"environment"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"preview"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nl"&gt;"note"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Ship the A2UI deep dive"&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two observations from that output. The round trip works precisely as the spec describes - context values resolved from the data model at press time, no prose to parse. And a real quirk: the ChoicePicker reports its selection as an array even in &lt;code&gt;mutuallyExclusive&lt;/code&gt; mode. Harmless, but it is the kind of detail you want to learn from a test build rather than from a production bug report, and your agent's action handler has to expect it.&lt;/p&gt;

&lt;p&gt;That is the entire integration: one client-only component, one processor, one payload, one action handler. The setup steps, in the checklist form I now keep for this:&lt;/p&gt;

&lt;p&gt;The six steps, as a checklist:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;[ ] &lt;strong&gt;Install @a2ui/react and @a2ui/web_core, then pin the exact versions&lt;/strong&gt; - the spec is 1.0 but the packages are 0.15.x, and 0.13, 0.14 and 0.15 each changed how v1.0 payloads are read.&lt;/li&gt;
&lt;li&gt;[ ] &lt;strong&gt;Load the surface client-only with next/dynamic and ssr: false&lt;/strong&gt; - the renderer registers custom elements when it loads, so importing it on the server dies at prerender.&lt;/li&gt;
&lt;li&gt;[ ] &lt;strong&gt;Create the MessageProcessor with basicCatalog and a named generic&lt;/strong&gt; - skip the generic and TypeScript infers a surface type that A2uiSurface refuses.&lt;/li&gt;
&lt;li&gt;[ ] &lt;strong&gt;Send one createSurface with components and dataModel inline&lt;/strong&gt; - the 1.0 single-message form replaces the three-message 0.9 sequence.&lt;/li&gt;
&lt;li&gt;[ ] &lt;strong&gt;Handle the action callback and log every action payload in development&lt;/strong&gt; - actions are the contract between your UI and your agent.&lt;/li&gt;
&lt;li&gt;[ ] &lt;strong&gt;Validate agent payloads against the schema before they reach the processor&lt;/strong&gt; - model-written JSON is untrusted input.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What 1.0 does not give you yet
&lt;/h2&gt;

&lt;p&gt;A deep dive that skips this section is a launch post with extra steps. Here is what I would want to know before betting a roadmap on A2UI, stated as plainly as the evidence allows.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The spec is 1.0; the software is 0.15.&lt;/strong&gt; The renderer and core packages sit at 0.15.0, and the three releases before that each changed how 1.0 payloads are read. The React package's own entry points still carry the previous version's naming. None of this is damning - it is what a healthy young ecosystem looks like - but it means upgrades are events you schedule and test, not bumps you merge on a Friday.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Platform coverage is uneven.&lt;/strong&gt; Jetpack Compose renders protocol 0.9.1 today, with 1.0 announced as coming soon and no date. Gemini - the highest-profile host - renders 0.9.1, and Gemini Enterprise's 1.0 upgrade is in progress, undated. An agent built on 1.0-only features, like the bidirectional function calls, does not yet render everywhere the marketing implies. Design for the 0.9.1 floor if reach matters, or accept a fragmented rollout.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The default look is no look.&lt;/strong&gt; Version 1.0 deleted the theme property on purpose: styling belongs to the host. The corollary nobody puts in the announcement is that the basic catalog with default styling looks like an unstyled form from 2009 - my test card did. The real work of an A2UI adoption is not the protocol, it is implementing the catalog against your design system, component by component. Budget for that, or your agent's screens will look like a wireframe escaped into production.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The vocabulary is small, deliberately.&lt;/strong&gt; Eighteen components, and in 1.0 the humble Text has exactly two variants: body and caption. There is no heading style, no table, no chart in the basic catalog. Real products will define custom catalogs - Alation's charts and ByteDance's Lynx component set are exactly that - but every custom component is one the agent must be taught and the renderer must implement, per platform. The catalog is your design system's API surface, and it will grow at the speed of your slowest platform team.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Generation reliability is a cost center, not a detail.&lt;/strong&gt; LLMs writing raw protocol JSON are slow and occasionally wrong, which is why the ecosystem already routes around it: a compact generation format (A2UI Express), server-side macros in Python that expand components programmatically, and decision models that pick components instead of writing text. Google's own examples lean on all three. If your plan is "the big model writes the JSON and we render it", your plan includes a validation-and-retry loop and the latency that comes with it. The prompt-generate-validate loop is not optional garnish; it is the production architecture.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;There are no public numbers.&lt;/strong&gt; No latency benchmarks, no token-cost comparisons, no production usage statistics from Google or the named adopters as of this writing. Atlassian, Alation, Bloomberg and ByteDance building with it is a real signal about direction. It is not evidence about your workload, and anyone who quotes you figures is making them up.&lt;/p&gt;

&lt;p&gt;None of these are reasons to ignore A2UI. All of them are reasons to adopt it with pinned versions, a small first surface, and your eyes open.&lt;/p&gt;

&lt;h2&gt;
  
  
  Should you adopt it? Run your situation
&lt;/h2&gt;

&lt;p&gt;The verdict section below gives you my answer. This picker gives you yours - the same reasoning, weighted by your answers instead of mine.&lt;/p&gt;

&lt;p&gt;The picker on &lt;a href="https://zainvibes.com/blog/a2ui-nextjs-deep-dive" rel="noopener noreferrer"&gt;zainvibes.com&lt;/a&gt; asks five questions - where your agent's answers need to render, what they look like today, how many agent-generated screens you will ship this year, whether your team can absorb a young spec's churn, and how exact the design-system match must be - and lands on one of three verdicts: adopt now starting with approvals and forms, pilot one surface behind your current UI, or wait and steal the pattern instead. My own answers are in the verdict below.&lt;/p&gt;

&lt;h2&gt;
  
  
  My verdict: adopt the pattern now, the protocol where it earns it
&lt;/h2&gt;

&lt;p&gt;Here is my call, with the conditions that would change it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;If your agent's answers are approvals, forms, pickers or comparisons, and those answers live in your own product, adopt A2UI now - narrowly.&lt;/strong&gt; One surface, the worst text answer you currently ship, rendered with the official renderer and the basic catalog, behind a flag. That pilot costs days, teaches you the integration shape, the generation reliability and the styling bill, and it is reversible at every step. This is the same discipline I argue for in &lt;a href="https://zainvibes.com/blog/agent-framework-vs-agent-harness" rel="noopener noreferrer"&gt;choosing between an agent framework and a harness&lt;/a&gt;: buy the smallest piece that answers the expensive question.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;If the same agent must appear on web and native mobile, or inside assistants you do not control, A2UI is the only option on the table that was designed for it.&lt;/strong&gt; Hand-rolled JSON means a second and third implementation of the same screens. MCP Apps means iframes and someone else's design language. The catalog model - describe once, render natively everywhere - is the protocol's real moat, and 1.0 is stable enough to build a catalog against if you pin your renderer versions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;If your agent answers in text and text is working, do nothing.&lt;/strong&gt; A protocol adoption is a cost you pay to fix a problem. No painful forms, no second platform, no problem - and "an open standard exists" is not a problem. Revisit when one of those changes, or when the Jetpack and Gemini hosts finish their 1.0 upgrades and the reach argument gets stronger.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Whatever you decide about the protocol, steal the pattern today.&lt;/strong&gt; Agent output as structured data, rendered against a component vocabulary you control, validated before it reaches the screen, with user input returning as structured actions. That architecture is right with or without Google's spec, it is most of what an A2UI migration later would need, and it ends the era of parsing your own agent's paragraphs. The flip conditions on my whole position: if renderer packages stall before a stable 1.x, or if a year from now the named adopters have quietly stopped shipping surfaces, the pattern survives and the protocol was a draft worth learning from. I do not expect that outcome. The problem is real, the split is right, and the team behind it is shipping.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Building an agent product?&lt;/strong&gt; If you are shipping an agent that has outgrown text answers - approvals, dashboards, forms your users actually complete - I build exactly this kind of system for clients: agent backends, the UI layer they render into, and the validation plumbing that keeps both honest. &lt;a href="https://zainvibes.com/#contact" rel="noopener noreferrer"&gt;Tell me what you are building&lt;/a&gt; and I will give you a straight read on whether A2UI, or something simpler, fits it.&lt;/p&gt;

&lt;h2&gt;
  
  
  FAQ
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What is A2UI?
&lt;/h3&gt;

&lt;p&gt;A2UI (Agent-to-User Interface) is Google's open protocol for generative UI. Instead of replying with text, an agent sends JSON messages describing a screen - components from a catalog your app registered, plus a data model to bind them to. Your app renders that description with its own native components. Version 1.0, released November 11, 2026, is the first stable version.&lt;/p&gt;

&lt;h3&gt;
  
  
  Is A2UI a competitor to MCP Apps or AG-UI?
&lt;/h3&gt;

&lt;p&gt;Mostly no. AG-UI is a transport and event protocol between agents and frontends; A2UI payloads ride on it. MCP Apps lets a server ship an HTML interface that a host renders in a sandboxed iframe; A2UI describes an interface instead, and A2UI screens can run inside MCP Apps hosts. The real competitor to A2UI is hand-rolled JSON rendering - the agent-UI format you invent yourself.&lt;/p&gt;

&lt;h3&gt;
  
  
  Which frameworks can render A2UI today?
&lt;/h3&gt;

&lt;p&gt;Google ships open-source renderers for React, Angular and Lit on the web, Flutter, and SwiftUI on iOS. The Jetpack Compose renderer for Android is at protocol 0.9.1 with 1.0 support announced as coming soon. Agent SDKs exist for Python, TypeScript and Dart.&lt;/p&gt;

&lt;h3&gt;
  
  
  Is it safe to render UI an agent generated?
&lt;/h3&gt;

&lt;p&gt;Safer than rendering agent-generated code, by design. The agent can only place components from the catalog you registered, it never sends executable code or markup, and functions that need a user gesture (like opening a URL) cannot be invoked by the agent at all. What the model does not protect you from is intent: an agent can compose a misleading screen from allowed components, so actions still need server-side authorization like any other API call.&lt;/p&gt;

&lt;h3&gt;
  
  
  Can I use A2UI with my existing Next.js agent?
&lt;/h3&gt;

&lt;p&gt;Yes. A2UI is transport-agnostic and framework-agnostic on the agent side: your route handler or agent loop produces the JSON messages and streams them over SSE, a WebSocket, AG-UI or A2A. The TypeScript agent SDK helps generate and validate messages, but nothing stops a hand-written harness from emitting the same payloads - the wire format is the contract.&lt;/p&gt;

&lt;h3&gt;
  
  
  Should I adopt A2UI now or wait?
&lt;/h3&gt;

&lt;p&gt;Adopt it now if your agent's answers are forms, approvals or comparisons, and especially if those answers need to land in more than one app. Wait if text answers are not your bottleneck, or if your team cannot absorb spec churn - the protocol is 1.0 but the renderer packages are still 0.15.x. Either way, steal the pattern: structured JSON against a component vocabulary you control.&lt;/p&gt;

</description>
      <category>a2ui</category>
      <category>nextjs</category>
      <category>ai</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Agent Framework vs Agent Harness: What I Would Build On in 2026</title>
      <dc:creator>MUHAMMAD ZAIN UL ABIDIN</dc:creator>
      <pubDate>Fri, 13 Nov 2026 04:30:48 +0000</pubDate>
      <link>https://dev.to/zforzain2000/agent-framework-vs-agent-harness-what-i-would-build-on-in-2026-4gk2</link>
      <guid>https://dev.to/zforzain2000/agent-framework-vs-agent-harness-what-i-would-build-on-in-2026-4gk2</guid>
      <description>&lt;p&gt;Every agent you ship sits on one of two routes. On the framework route, you build inside someone else's runtime: LangGraph, Mastra, an SDK agent class. On the harness route, the runtime is yours: a loop you wrote, tools you registered, limits you set. I have shipped both, including a 200-line TypeScript harness I still run. This post puts the two routes head-to-head on the costs that actually decide the matter - setup, debugging, churn, durability - and ends with the route I would build on in 2026, and the conditions that flip it.&lt;/p&gt;

&lt;h2&gt;
  
  
  The short answer
&lt;/h2&gt;

&lt;p&gt;Build on your own harness unless a run must survive restarts, wait days on a human, or route work between several agents. Those three requirements are the framework route's home ground, and hand-building them is the most expensive version of this decision. Everything else - one agent, minutes-long runs, mistakes a retry can absorb - is loop-shaped work, and a framework charges you a learning tax and a monthly churn tax for features that work never touches. There is also a middle lane that did not exist in this form two years ago: SDK primitives (the Vercel AI SDK's ToolLoopAgent, the OpenAI Agents SDK) that hand you the plumbing and leave the loop yours. My default in 2026 is that middle lane's plumbing under my own loop. The rest of this post is the evidence, route by route.&lt;/p&gt;

&lt;p&gt;Two earlier posts are the deep dives behind this one. If you want the loop itself, read &lt;a href="https://dev.to/blog/ai-agent-typescript-harness-200-lines"&gt;the 200-line TypeScript harness post&lt;/a&gt;. If you want the buying discipline - name the feature, name the week - read &lt;a href="https://dev.to/blog/do-you-need-an-ai-agent-framework"&gt;my framework decision guide&lt;/a&gt;. This post is the comparison those two circle around: the whole current field, on one table, against the harness.&lt;/p&gt;

&lt;h2&gt;
  
  
  Two routes, defined by who owns the loop
&lt;/h2&gt;

&lt;p&gt;A harness is everything around the model that makes an agent safe to run: the loop that calls the model, the tool registry, the context you assemble, the limits, the approval gates, the verification. On the harness route you write that machinery yourself, once, in code your team can read top to bottom. On the framework route you adopt that machinery as a library, with its own concepts - graphs, nodes, checkpointers, workflows, sessions - and your agent becomes configuration inside it.&lt;/p&gt;

&lt;p&gt;The distinction that matters is not features. It is ownership. When a framework route agent misbehaves, the cause can live in your code, the model's output, or the layer between them that assembled the prompt for you. When a harness route agent misbehaves, the first two are the only candidates. Anthropic's own engineering guidance makes this point from the vendor side: in "Building effective agents", the company reports that the most successful implementations it saw used simple, composable patterns rather than complex frameworks, and warns that frameworks "often create extra layers of abstraction that can obscure the underlying prompts and responses, making them harder to debug". That is a model vendor telling you the abstraction has a price. It does not make frameworks wrong. It makes them a purchase, and purchases get compared. So let us compare.&lt;/p&gt;

&lt;h2&gt;
  
  
  The framework field in November 2026
&lt;/h2&gt;

&lt;p&gt;The framework route is not one thing anymore. Here is the field as it stands this month, from each project's own documentation:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Option&lt;/th&gt;
&lt;th&gt;Route&lt;/th&gt;
&lt;th&gt;Shape&lt;/th&gt;
&lt;th&gt;Durability story&lt;/th&gt;
&lt;th&gt;What you pay&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;LangGraph&lt;/td&gt;
&lt;td&gt;Framework&lt;/td&gt;
&lt;td&gt;Graph runtime: nodes, edges, typed state&lt;/td&gt;
&lt;td&gt;Checkpoints at every super-step to a store you run (Postgres, SQLite); durability modes from exit-only to sync&lt;/td&gt;
&lt;td&gt;Library free (MIT); LangSmith platform from $39/seat/month&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Mastra&lt;/td&gt;
&lt;td&gt;Framework&lt;/td&gt;
&lt;td&gt;TypeScript-native: agents plus a workflow engine&lt;/td&gt;
&lt;td&gt;Workflow snapshots that suspend and resume, in storage you configure&lt;/td&gt;
&lt;td&gt;Library free; your database and hosting&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Vercel AI SDK 7&lt;/td&gt;
&lt;td&gt;Middle lane&lt;/td&gt;
&lt;td&gt;SDK with an agent class (ToolLoopAgent), approvals, tooling&lt;/td&gt;
&lt;td&gt;ToolLoopAgent runs in-process; durable runs via its WorkflowAgent and the Vercel platform&lt;/td&gt;
&lt;td&gt;Library free; platform and gateway usage if you adopt them&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;OpenAI Agents SDK&lt;/td&gt;
&lt;td&gt;Middle lane&lt;/td&gt;
&lt;td&gt;Lightweight primitives: agents, handoffs, guardrails, sessions, tracing&lt;/td&gt;
&lt;td&gt;Sessions persist conversation turns, not mid-run tool progress&lt;/td&gt;
&lt;td&gt;Library free; tracing lands in OpenAI's dashboard&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Claude Agent SDK&lt;/td&gt;
&lt;td&gt;Adopted harness&lt;/td&gt;
&lt;td&gt;Claude Code's loop, tools, and context management as a library&lt;/td&gt;
&lt;td&gt;Sessions you can resume or fork; permissions and hooks built in&lt;/td&gt;
&lt;td&gt;Library free to install; Anthropic commercial terms, Anthropic models&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Your own harness&lt;/td&gt;
&lt;td&gt;Harness&lt;/td&gt;
&lt;td&gt;Your loop, registry, limits: about 200 lines for a first agent&lt;/td&gt;
&lt;td&gt;Whatever you build: nothing on day one, which is the honest entry&lt;/td&gt;
&lt;td&gt;Your build time, then your upkeep&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;{/* MONETIZATION NOTE: LangGraph/LangSmith row - swap the official link for the LangChain partner/affiliate link if a program is joined. No affiliate relationship exists at publish time. &lt;em&gt;/}&lt;br&gt;
{/&lt;/em&gt; MONETIZATION NOTE: Mastra and Vercel rows are affiliate-swap candidates if programs open; links below are official documentation URLs, not affiliate links. */}&lt;/p&gt;

&lt;p&gt;Read the durability column carefully, because it is where the marketing and the documentation part company. LangGraph's checkpointer is the real thing: its docs describe a snapshot of graph state saved at each super-step, and three durability modes - "exit", "async", and "sync" - that let you choose how much state a crash is allowed to lose. Mastra's workflows genuinely suspend and resume from snapshots. The OpenAI Agents SDK's sessions, by contrast, persist the conversation, not the run: if the process dies halfway through a five-tool sequence, you resume from the last persisted turn, not the last tool call. And the Claude Agent SDK is the interesting hybrid: not a framework you configure, but a finished harness you adopt - the same tools, agent loop, and context management that power Claude Code, programmable in TypeScript, sessions included. It is the harness route with someone else's harness.&lt;/p&gt;

&lt;h2&gt;
  
  
  What each route costs to own
&lt;/h2&gt;

&lt;p&gt;Feature lists will not settle this, because both routes can eventually do everything on the list. Bills settle it. So here is the honest way I compare them: total engineer-days to build and to own, month by month, as the agent's shape changes. I built a small model of it. The constants are my estimates from shipping both routes, printed inside the widget so you can argue with them. The shape is what I am asking you to trust.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;On zainvibes.com this section is an interactive simulator: drag the run length, agent count and time horizon and watch the cumulative ownership cost of the framework route and the harness route cross. &lt;a href="https://zainvibes.com/blog/agent-framework-vs-agent-harness" rel="noopener noreferrer"&gt;Try it on the original post.&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Two readings matter. First, at the default settings - one agent, runs that finish in minutes - the framework never catches up: you pay roughly six days to learn and wire it properly, then a day a month in upgrades and abstraction-debugging, for a machine that does what two days of loop code does. Second, drag the run length to days, or the agent count up, and the harness line bends hard: the day you need restart survival, you are not adding a feature, you are building a second system - checkpointing, resume semantics, idempotent replay - and the framework's tax suddenly reads as a discount. Neither route is cheaper. Each is cheaper somewhere, and the crossover is a property of your agent's shape, not of the tools' quality.&lt;/p&gt;

&lt;h2&gt;
  
  
  Head-to-head: the eight rounds that decide it
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Round&lt;/th&gt;
&lt;th&gt;Framework route&lt;/th&gt;
&lt;th&gt;Harness route&lt;/th&gt;
&lt;th&gt;Edge&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Time to first working agent&lt;/td&gt;
&lt;td&gt;Hours, if your shape fits its shape&lt;/td&gt;
&lt;td&gt;A day or two, any shape&lt;/td&gt;
&lt;td&gt;Framework&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Debugging a bad run&lt;/td&gt;
&lt;td&gt;Trace UIs help; the assembled prompt must be dug out&lt;/td&gt;
&lt;td&gt;The messages array is the whole story&lt;/td&gt;
&lt;td&gt;Harness&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Surviving a restart mid-run&lt;/td&gt;
&lt;td&gt;Checkpoints and resume, built in (LangGraph, Mastra)&lt;/td&gt;
&lt;td&gt;Build it yourself, or rerun the run&lt;/td&gt;
&lt;td&gt;Framework&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Multi-agent routing&lt;/td&gt;
&lt;td&gt;Graphs and handoffs are first-class&lt;/td&gt;
&lt;td&gt;Nested conditionals, until you build routing&lt;/td&gt;
&lt;td&gt;Framework&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Upgrade churn&lt;/td&gt;
&lt;td&gt;Several minor releases a month across the field&lt;/td&gt;
&lt;td&gt;Your code changes when you change it&lt;/td&gt;
&lt;td&gt;Harness&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Observability&lt;/td&gt;
&lt;td&gt;Tracing integrations and dashboards included&lt;/td&gt;
&lt;td&gt;Structured logs until you wire a tracer&lt;/td&gt;
&lt;td&gt;Framework&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Switching providers&lt;/td&gt;
&lt;td&gt;SDK lanes abstract this well; graph frameworks vary&lt;/td&gt;
&lt;td&gt;One adapter file, if you kept the interface&lt;/td&gt;
&lt;td&gt;Tie, leaning SDK&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Leaving later&lt;/td&gt;
&lt;td&gt;Your state shapes and tools wear the framework's form&lt;/td&gt;
&lt;td&gt;The loop is yours; the tools move as data&lt;/td&gt;
&lt;td&gt;Harness&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Four of these rounds deserve prose, because the table flattens them.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Debugging.&lt;/strong&gt; This is the round I weight most heavily, because agents fail in production in ways demos never show. In my harness, when a run goes wrong I open one file: the messages the model received are the messages in the array, and the tool results are what the handlers returned. In a framework, my first hour on a bad run has repeatedly gone to a different question: which component assembled this prompt, and what did it leave out? Framework tracing genuinely helps once you have thousands of runs. Below that scale, it is a viewer for a problem you could have read directly.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Durability.&lt;/strong&gt; This is the round the framework route wins outright, and it is not close. Restart survival done correctly means checkpointed state, resume that does not re-run completed steps, and side-effect tools that tolerate replay. LangGraph's documentation treats this as the core of the product, down to pending writes from nodes that finished before a failure. I have built the homemade version of this. It worked, and it was the least tested, most feared code in the system. If your runs cross human waits measured in days, buy this round. Do not build it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Churn.&lt;/strong&gt; Version trackers recorded the JavaScript LangGraph package at 1.4.21 in early October and at 1.7.3 by November 10: several minor releases in about a month, each a small chance that a default moved under your agent. The OpenAI Agents SDK ships at a similar pace, roughly a minor a month. None of this is malpractice; the field is young and moving. But it is a standing monthly cost that lands on the framework side of the ledger, and it compounds with team size, because every upgrade is reviewed by whoever owns the agent that quarter.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The platform bill.&lt;/strong&gt; The libraries are free, and I want to be precise about that, because "frameworks cost money" is its own lazy claim. What costs money is the platform layer teams adopt with them. LangSmith, LangChain's tracing and deployment platform, is free for one seat with 5,000 traces a month; Plus is $39 per seat per month with 10,000 traces, and compute and storage meter on top ($1.50 per compute unit, $1.00 per storage unit, per the published pricing). A five-person team is $195 a month before usage. That can be worth every cent. It should still appear in the comparison, because the harness route's equivalent line is zero: your logs live in your stack, and &lt;a href="https://dev.to/blog/control-the-cost-of-an-ai-agent"&gt;the cost controls I put in every agent&lt;/a&gt; are plain numbers in your code either way.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where the framework route genuinely wins
&lt;/h2&gt;

&lt;p&gt;Three situations, stated as strongly as I can state them, because a comparison that cannot name the other side's best case is an advert.&lt;/p&gt;

&lt;p&gt;First, runs that must not restart. An approval flow that waits two days, a research run that costs real money per attempt, a migration agent halfway through a customer's data: rerunning from the top is either impossible or irresponsible. Checkpoints plus idempotent tools are the correct engineering, and LangGraph or Mastra ship it today.&lt;/p&gt;

&lt;p&gt;Second, genuinely multi-agent systems. Not one agent with many tools wearing a trench coat, but several agents with shared state and result-dependent routing. Graphs make those routes explicit and testable. In a loop, that routing becomes the worst code in the repo within a quarter.&lt;/p&gt;

&lt;p&gt;Third, teams that need to inspect every run. Thousands of runs a day, support asking why this one customer got this answer: a trace tree you can click beats a log stream you grep. Frameworks wire this up; on the harness route you will wire it yourself, later, under pressure.&lt;/p&gt;

&lt;p&gt;If your agent lives in one of those three situations today - not in a roadmap, today - the rest of this post is a formality. Take the framework route, and take it on purpose.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where the harness route genuinely wins
&lt;/h2&gt;

&lt;p&gt;The harness wins on the properties that matter every ordinary day. You can read the whole system. You can test it with a stub model and no API key, the way I tested the 200-line loop: turn limit fires, cost budget fires, the repeat-call guard fires, an unapproved write stays blocked. Your tool arguments are validated by schemas you wrote - the pattern in &lt;a href="https://dev.to/blog/zod-as-a-contract"&gt;my Zod contract post&lt;/a&gt; - before a handler ever runs. Your dependency surface is a model adapter and your own code, so a bad week in someone else's release cycle is not your bad week.&lt;/p&gt;

&lt;p&gt;It also wins the argument nobody puts in launch posts: most agents are small. One agent, a handful of tools, runs measured in minutes, inside a product whose team has other work. For that agent, the framework is the largest, least understood dependency in the repository, adopted to solve problems the agent does not have. Anthropic's guidance lands here again: add complexity only when it demonstrably improves outcomes. A loop you understand demonstrably works. That is the bar a framework has to clear, per agent, not per industry.&lt;/p&gt;

&lt;p&gt;Five questions settle which side of this your agent is on. They are not the questions from my earlier decision post; those were about buying a feature. These are about shape and cost:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;On zainvibes.com this is an interactive route picker: five questions about your agent, and it lands on the route the verdict below recommends. &lt;a href="https://zainvibes.com/blog/agent-framework-vs-agent-harness" rel="noopener noreferrer"&gt;Try it on the original post.&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The stack I actually build on
&lt;/h2&gt;

&lt;p&gt;Enough comparison. Here is my own default, the thing I reach for when a client or my own product needs an agent in TypeScript today.&lt;/p&gt;

&lt;p&gt;I start with SDK plumbing and my own loop. Concretely: the Vercel AI SDK for provider handling, streaming, and tool-call parsing - I compared it head-to-head with TanStack AI in &lt;a href="https://dev.to/blog/tanstack-ai-vs-vercel-ai-sdk"&gt;that post&lt;/a&gt; and it earns its place as plumbing - with ToolLoopAgent available when a sub-problem is genuinely loop-shaped and self-contained. But the agent's spine is mine: my messages, my registry, my limits, my approval gates, my verifier. That split is the whole strategy. The SDK absorbs the churn that is pure plumbing (provider quirks, parsing edge cases), and the parts that encode my product's decisions stay in code I control.&lt;/p&gt;

&lt;p&gt;Two exceptions. When the agent's job is working in a codebase - reading files, editing, running commands, verifying with tests - I adopt the Claude Agent SDK instead of building the harness, because that harness already exists, is maintained by the team that knows the failure modes best, and its sessions and permissions are the parts I would otherwise spend weeks rebuilding. And when a client's requirement sheet says runs must resume after deploys and wait on humans for days, I start on LangGraph and do not apologize for it: that is the checkpointing round, and I have already told you who wins it.&lt;/p&gt;

&lt;p&gt;Notice what this default is not. It is not "never use frameworks". It is a default with named exceptions, which is what a route decision should be. The failure I see in the wild is not the wrong route. It is the unchosen one: the framework adopted because a tutorial opened with it, or the loop still running a workload it outgrew a year ago.&lt;/p&gt;

&lt;h2&gt;
  
  
  Before you commit: six checks
&lt;/h2&gt;

&lt;p&gt;Whichever route the picker and the simulator pointed you at, run these six checks on the real agent before you call the decision made. They are route-agnostic, and in my experience they predict regret better than any feature matrix:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Name the feature and the week.&lt;/strong&gt; If you chose a framework, you can name the feature you are buying and the week you would miss it. If you chose a harness, you can name what you are deliberately not building yet.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Kill the process mid-run.&lt;/strong&gt; Do it, do not theorize about it. Watch whether the run reruns, resumes, or corrupts, and confirm that is the behavior your users were promised.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Put an idempotency key on every side-effect tool.&lt;/strong&gt; Any route that can resume can replay. A checkpoint never stopped a double charge; a key derived from the run and the step does.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Log the exact request sent to the model.&lt;/strong&gt; Every run, no exceptions. When a route assembles prompts for you, log the assembled result, not the template you believe produced it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Limits as numbers in code.&lt;/strong&gt; Maximum turns, maximum spend per run, a repeated-call guard. Checked where you can point at them, not inherited from defaults you never read.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Write down the exit signal.&lt;/strong&gt; The condition that would make you switch routes: a run that starts waiting days, a second agent, churn eating a day a month. Decided now, it is a tripwire. Decided later, it is an argument.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The same six, in a checklist that keeps your progress in this browser:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;On zainvibes.com this checklist is interactive and remembers your progress. &lt;a href="https://zainvibes.com/blog/agent-framework-vs-agent-harness" rel="noopener noreferrer"&gt;Try it on the original post.&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  My verdict
&lt;/h2&gt;

&lt;p&gt;For most teams building agents in 2026, I would build on the harness route - specifically, your own loop over SDK plumbing - and I would treat a graph framework as a targeted purchase for the day durability or multi-agent routing becomes a requirement you can point at, not a fear you can imagine. That is the same destination my earlier posts reached from other directions, and this head-to-head is why I still land there after comparing the whole field: the framework route's real advantages are narrow, deep, and decisive inside their territory, and its costs are broad, shallow, and charged every single month outside it.&lt;/p&gt;

&lt;p&gt;The verdict flips under three conditions, and I will state them as plainly as the pick. Flip to a framework when runs must survive restarts and human waits measured in days. Flip when several agents share state and route work by results. Flip when your team must inspect any of thousands of runs on demand. Inside those conditions, LangGraph is my framework pick for its checkpointing depth, Mastra when the team wants the workflow engine and memory in one TypeScript-native box, and the Claude Agent SDK whenever the job is Claude Code's job, whatever route you call that.&lt;/p&gt;

&lt;p&gt;If you are wiring an agent into a Next.js and NestJS product and want the route chosen on your workload rather than on a launch post, &lt;a href="https://dev.to/#contact"&gt;tell me about the project&lt;/a&gt;. The route decision is usually the cheapest week of the build and the most expensive one to get wrong.&lt;/p&gt;

&lt;h2&gt;
  
  
  FAQ
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What is the difference between an agent framework and an agent harness?
&lt;/h3&gt;

&lt;p&gt;The framework gives you someone else's loop, state model, and runtime: you configure agents, tools, and graphs inside it. A harness is the same machinery written by you around your own loop: your messages array, your tool registry, your limits. The difference is who owns the runtime, and who pays when it needs to change.&lt;/p&gt;

&lt;h3&gt;
  
  
  Which agent framework should a TypeScript team pick in 2026?
&lt;/h3&gt;

&lt;p&gt;If durability is the requirement, LangGraph has the most complete checkpointing story in the field, with durability modes you can tune per run. If you want a TypeScript-native framework with workflows and memory in one box, Mastra is the closer fit. If you only need agent primitives, the Vercel AI SDK's ToolLoopAgent or the OpenAI Agents SDK will feel lighter than either.&lt;/p&gt;

&lt;h3&gt;
  
  
  Is LangGraph worth it for a single agent?
&lt;/h3&gt;

&lt;p&gt;Usually not. A single agent that runs for minutes and can be rerun on failure gets nothing from a graph runtime except its learning curve. LangGraph starts paying when a run must survive restarts, wait on humans, or route work between several agents, and cannot simply be rerun from the top when it fails.&lt;/p&gt;

&lt;h3&gt;
  
  
  What does the framework route actually cost?
&lt;/h3&gt;

&lt;p&gt;The libraries are free. The bills are your time (learning the model, upgrading through fast release cycles), any platform you adopt (LangSmith Plus is $39 per seat per month, with usage billed on top), and the model tokens both routes pay. A harness replaces the platform and learning bills with your own build and upkeep time.&lt;/p&gt;

&lt;h3&gt;
  
  
  Can I start with a harness and move to a framework later?
&lt;/h3&gt;

&lt;p&gt;Yes, if you shape the harness for it: tools as plain objects with schemas and handlers, the model behind one interface, limits and approvals as separate functions, and idempotent writes. Migration then means replacing the loop, which is days of work. I detailed the full path in my framework decision post.&lt;/p&gt;

&lt;h3&gt;
  
  
  When is the Claude Agent SDK the right choice?
&lt;/h3&gt;

&lt;p&gt;When the agent's job looks like Claude Code's job: reading and editing files, running commands, working in a repo or a filesystem. You are adopting a finished harness rather than building one, sessions and permissions included, in exchange for building on Anthropic's models and its commercial terms.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>typescript</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>Anthropic Replaced Git With Scion: What It Means for Your Monorepo</title>
      <dc:creator>MUHAMMAD ZAIN UL ABIDIN</dc:creator>
      <pubDate>Tue, 10 Nov 2026 05:37:32 +0000</pubDate>
      <link>https://dev.to/zforzain2000/anthropic-replaced-git-with-scion-what-it-means-for-your-monorepo-177o</link>
      <guid>https://dev.to/zforzain2000/anthropic-replaced-git-with-scion-what-it-means-for-your-monorepo-177o</guid>
      <description>&lt;p&gt;Your merge queue is serial by design: one group of changes tests against main, lands, and only then does the next group start. That is safe, and it is fine, right up until the authors stop being human. Anthropic's monorepo went from about 2,500 merged changes a day in May 2026 to over 30,000 a day by September, most of that growth written by Claude agents, and Git, mirrors and all, stopped keeping up. So a team of seven built a new version control system called Scion and migrated the entire monorepo off Git in five months. This post is not a eulogy for Git. It is the math that shows where the serial wall actually stands, how Scion deleted it, and how far your team is from needing to care.&lt;/p&gt;

&lt;h2&gt;
  
  
  The numbers that broke Git
&lt;/h2&gt;

&lt;p&gt;Start with the workload, because Scion makes no sense without it. Every figure in this section is Anthropic-reported, published in its October 13, 2026 engineering post and covered independently by InfoQ on October 27. Nobody outside Anthropic has audited them, so read them as a credible self-report, not a benchmark.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Merged changes: about 2,500 a day in May 2026, over 30,000 a day by September. A tenfold-plus jump in four months.&lt;/li&gt;
&lt;li&gt;The main branch history alone is almost 100 GB. A compressed working copy is over 25 GB.&lt;/li&gt;
&lt;li&gt;CI performs on the order of 25 million code checkouts per day, before counting developer environments, agent sandboxes and research jobs. Anthropic's own illustration: fetched naively, that is over 600 PB of Git traffic a day.&lt;/li&gt;
&lt;li&gt;At peak, Anthropic operated several hundred internal Git mirrors to spread the load.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Anthropic did not skip the standard playbook. It used shallow clones, sparse checkouts, promisor remotes and repository snapshots kept in blob storage. The mirrors bought time, then became the problem: seeds fell further behind the tip of main between refreshes, mirrors strained against disk and memory limits, and history operations like diff and blame got fragile on partial clones.&lt;/p&gt;

&lt;p&gt;The sentence in Anthropic's post that explains everything else is this observation: like most companies, it was using Git as a centralized system with one authoritative copy, but nearly all of its clients were agents and jobs that are online by definition. Git's great strengths, full local copies and offline work, had become pure tax. Every checkout dragged around a history nobody on that machine would ever read.&lt;/p&gt;

&lt;h2&gt;
  
  
  How Scion works: Git's assumptions, inverted
&lt;/h2&gt;

&lt;p&gt;Scion is a client-server version control system built by Anthropic for its own monorepo. If Git assumes a smart client and a dumb store, Scion assumes the opposite.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The server does the work.&lt;/strong&gt; In Scion, the server answers history and blame questions, creates commits, and performs merges and rebases. The client keeps a bounded local object cache, lays out the working copy files on disk and watches the filesystem for changes. It computes very little on its own, and in exchange it needs very little data locally.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The unit of work is a changeset, not a tree.&lt;/strong&gt; A Git commit points at a full tree of the repository. A Scion changeset is an entry in a write-ahead log: it stores the list of files that changed relative to its first parent, plus content-addressed pointers to the compressed file contents in blob storage. Metadata lives in Spanner. There are no packfiles to build, no locking and no cross-server coordination in the write path, so changesets can be accepted at very high throughput. Branches (refs) barely feature: moving one is a compare-and-swap on a single Spanner row, and the workflow discourages refs except for main and release branches, because global state every server must agree on does not scale.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Derived data replaces stored history views.&lt;/strong&gt; A changeset only knows what changed, so Scion computes everything else, blame structures, path history, skip lists over the commit graph, from the changesets through one generic recursive mechanism. Each derived type is computed from its parents' values, can be computed eagerly or lazily, and can be added without changing the client. When a new index would make an operation faster, Anthropic adds a derived type instead of shipping a new client.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fetch means "what I want", never "what I have".&lt;/strong&gt; Git fetch negotiation revolves around what the client already holds, so the server can build a packfile of the difference. A Scion client starts from a root tree and recursively requests the trees and blobs it wants. Every object is immutable and content-addressed, so any cache can answer any request. In production the path is a three-tier cache: a daemon on each node, a regional cache streaming objects from memory, and the authoritative servers reading Spanner or cloud storage only on a miss. Anthropic reports a cache hit rate over 99% at each tier.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The working copy is optional.&lt;/strong&gt; Checkouts are sparse by design, containing only the directories the client asked for, and can be virtualized so files materialize only when read. Many jobs skip the checkout entirely: every query the client can make is also an API, so Anthropic's automated bisect and revert pipelines ask the server for diffs and merge bases directly, with no local repository at all.&lt;/p&gt;

&lt;p&gt;The reported results, again Anthropic's own numbers measured against its own old setup: an infrastructure footprint 200 times smaller than its previous Git mirroring infrastructure in CPU and memory, checkouts two orders of magnitude faster for CI and research jobs (seconds instead of minutes), and a 25% CPU capacity win in CI alone. Directionally, that is what you would expect when 25 million daily checkouts stop dragging full histories around. Treat the exact multipliers as one company's internal before-and-after, because that is all they are.&lt;/p&gt;

&lt;p&gt;One more piece of honesty, because it matters for how much of this is new: Anthropic credits Google's Piper and Meta's Sapling as prior art in the post itself, and after publication it expanded the credit to Meta's Mononoke, the server behind Sapling, whose data model Scion closely follows. Scion is an execution of ideas the industry has been developing for a decade, done fast, under pressure, with agents writing much of the code.&lt;/p&gt;

&lt;h2&gt;
  
  
  The real break: a serial merge queue cannot survive agent scale
&lt;/h2&gt;

&lt;p&gt;The checkout storm is only half the story. The deeper break is in how changes land.&lt;/p&gt;

&lt;p&gt;The industry-standard answer to a busy main branch is a merge queue: changes line up, each group is tested combined with everything ahead of it, and a global lock on main guarantees that what lands has passed. GitHub's merge queue, merge trains elsewhere, bors before them, all share the same shape. Safe, orderly, and fundamentally serial.&lt;/p&gt;

&lt;p&gt;Now do the arithmetic, because the arithmetic is the whole argument:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;30,000 merges a day is one merge landing every 2.9 seconds, on average, around the clock.&lt;/li&gt;
&lt;li&gt;A strictly serial queue running a 10-minute check per group lands at most 144 groups a day (1,440 minutes divided by 10). Batch five pull requests per group and you reach roughly 720 merges a day. Anthropic needed 30,000.&lt;/li&gt;
&lt;li&gt;For a serial queue to land 30,000 changes a day, the complete check for each merge would have to finish in under 3 seconds. Forever. Including your slowest flaky test.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Batching and speculative parallel checks stretch the model a long way, which is why real queues work fine at dozens or a few hundred merges a day. But the ceiling is structural: whenever merges per day multiplied by CI minutes exceeds the 1,440 minutes in a day, a purely serial design is over capacity before you argue about hardware. At 100 merges a day with 15-minute CI, that product is already 1,500.&lt;/p&gt;

&lt;p&gt;Anthropic's answer was to delete the lock. In its own words, a proper merge queue "doesn't make much sense at agent scale", and it could not effectively replicate the industry standard of a global repository lock. So changes land first, and the safety net moved: testing happens after the merge and before deployment. Concurrent changes do sometimes conflict, and Anthropic says Claude typically fixes those conflicts faster than a human can identify them. That trade only works when the fixer is an always-on agent and the cost of a briefly inconsistent main is lower than the cost of a queue that never drains.&lt;/p&gt;

&lt;p&gt;Do not take my word for where your team sits on this curve. Run it:&lt;/p&gt;



&lt;p&gt;Set the authors to engineers plus agents, and watch two things: the serial queue's backlog at the end of the day, and the exact author count where arrival rate passes service rate. That crossing point is the wall. Then use the step mode and walk the queue's failure modes one by one: a stale base, a rebase tax, and a red build at the front that stops every merge behind it. Switch to the parallel model and watch the same day play out without the lock, the same change landing in about twelve minutes instead of five hours and forty. The simulator is a simplified model: it assumes a constant 10-minute CI pass, no flaky tests, and a parallel model with six validators and no global lock. It is there to give you intuition for the shape of the problem, not to benchmark your pipeline.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Scion is not
&lt;/h2&gt;

&lt;p&gt;Three fences around the hype, all from Anthropic itself.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;You cannot have it.&lt;/strong&gt; Scion is internal. It is not open source, it is not sold, and Anthropic has announced no public release, waitlist or timeline. Anthropic says it would be difficult to offer in a broadly useful form: Scion diverges significantly from Git, so adopting it means migrating your whole development lifecycle, and it depends on companion systems (its code review tool Antique, code search, CI) that are highly bespoke.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;It is not an indictment of your repository.&lt;/strong&gt; Anthropic's verdict on its own system: "Scion is by no means a suitable replacement for git for general use." The concepts, it says, only make sense for truly enormous monorepos, with either tens of thousands of engineers or swarms of agents prolifically writing code. Anthropic is also unusually able to burn tokens on internal tooling, and it says so.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;It is not the direction Anthropic recommends for you.&lt;/strong&gt; Its stated hope is the opposite of replacement: that Git platforms absorb the successful parts, comprehensive server-side APIs like Scion's, better fetch negotiation, sparse and shallow features that compose well, and that it can partner with GitHub, GitLab and the open source community to get there. The company that left Git is telling you Git should evolve, not die.&lt;/p&gt;

&lt;h2&gt;
  
  
  What your team can steal today
&lt;/h2&gt;

&lt;p&gt;You cannot adopt Scion, but every pressure it answers has a shipping answer at normal scale.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;If your main branch is busy, turn on the merge queue you already have.&lt;/strong&gt; GitHub's merge queue is built into the platform for organization repositories. It merges first-in-first-out, builds temporary merge groups containing the base branch plus every pull request ahead of yours, and lands the group only when required checks pass on the combined result. A failing pull request is removed and the groups behind it are rebuilt automatically. Two settings matter most: merge limits, which let the queue batch several pull requests per group (that batching is exactly the multiplier the math above says you need), and build concurrency, which caps how many merge group checks run at once so the queue does not melt your CI budget. The one setup gotcha: your CI must trigger on the merge_group event, not just pull_request, or the queue will wait forever for checks that never report.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;If clones and checkouts are the pain, take Git's slice tools seriously.&lt;/strong&gt; Partial clone with sparse checkout is the same mitigation Anthropic itself ran before Scion, and it ships in the Git you already have. For a growing monorepo, a blobless partial clone plus sparse checkout of the directories a job actually touches removes most of the download and disk cost without changing anyone's workflow.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;If review throughput is the drag, stack your changes.&lt;/strong&gt; Graphite's whole product is stacked pull requests on top of GitHub, with automated merging that lands a stack one pull request at a time and restacks everything above it. Small, stacked changes review faster than one mega pull request, and the queue at the end gets smaller units to batch.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;If rebases and restacking eat your senior engineers' afternoons, try jj as a front end.&lt;/strong&gt; Jujutsu runs on top of your existing Git repository, so it is an afternoon experiment, not a migration. Its working copy is itself a commit that amends automatically as you edit, every change carries a stable change ID that survives rewrites, and conflicts are recorded inside commits instead of blocking every other operation. For developers who live in stacked, constantly rewritten history, which is exactly the shape agent-assisted work takes, it removes a category of pain Git makes you pay daily.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;And keep Sapling in mind as the proof of pattern.&lt;/strong&gt; Meta's Sapling is the open source relative in this family: a client you can install today that works on Git repositories and with GitHub, with stacks and restacking built in. Its own documentation is upfront that the server and virtual filesystem that let it scale to tens of millions of files inside Meta are not yet public. That split is the honest map of 2026: the client side of the post-Git future is testable now, the server side is what the forges will ship next.&lt;/p&gt;

&lt;p&gt;I have seen this progression from the other end, on ordinary team repositories: first the queue is a convenience, then it is load-bearing, then someone checks the wait time graph and realizes landing code, not writing it, is the team's real sprint bottleneck. Anthropic just hit that wall at a scale where the only way through was to rebuild the floor.&lt;/p&gt;

&lt;h2&gt;
  
  
  My verdict
&lt;/h2&gt;

&lt;p&gt;Keep Git. Anthropic did not leave Git because Git is badly designed; it left because its author count effectively became thousands of always-online agents landing a change every three seconds, and no serial system survives that. Your repository is not there, and the honest evidence is that almost nobody's is: Anthropic itself expects this class of problem to become commonplace over the next six to twelve months, which is a forecast, not a description of your Tuesday.&lt;/p&gt;

&lt;p&gt;Here is the pick I would defend for a normal team, in order:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Measure two numbers this week: merges per day on your busiest branch, and full CI minutes. Multiply them. Well under 1,440, your queue has headroom and Scion is a fascinating article, nothing more. Anywhere near it, batching stops being optional.&lt;/li&gt;
&lt;li&gt;Turn on your forge's merge queue before you feel you need it, with batching enabled. Queues are cheap to adopt early and miserable to adopt under pressure.&lt;/li&gt;
&lt;li&gt;Watch the author mix, not the tool announcements. The day agents open most of your pull requests, your bottleneck moves from writing code to landing it. That shift, not Scion itself, is the thing to prepare for.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Not sure which side of the line your team is on? Answer four questions and I will place you:&lt;/p&gt;



&lt;p&gt;If your numbers came back closer to the wall than you expected, and you are wiring agents into a Next.js and NestJS codebase that still has to land code safely every day, that is precisely the layer of the stack I build for teams. &lt;a href="https://dev.to/#contact"&gt;Tell me about your project&lt;/a&gt; and I will tell you honestly whether you need a merge queue, a pipeline rebuild, or nothing at all.&lt;/p&gt;

&lt;p&gt;Related reading: &lt;a href="https://dev.to/blog/cloudflare-deno-acquisition-workers-deployments"&gt;Cloudflare, Deno and the Workers deployment reshuffle&lt;/a&gt;, another case of platform infrastructure being rebuilt around a workload it was not designed for.&lt;/p&gt;

</description>
      <category>git</category>
      <category>monorepo</category>
      <category>devtools</category>
      <category>aiagents</category>
    </item>
    <item>
      <title>Gemini Omni 1.2 Flash in Next.js: The Setup and the Real Cost Per Clip</title>
      <dc:creator>MUHAMMAD ZAIN UL ABIDIN</dc:creator>
      <pubDate>Sat, 07 Nov 2026 04:50:23 +0000</pubDate>
      <link>https://dev.to/zforzain2000/gemini-omni-12-flash-in-nextjs-the-setup-and-the-real-cost-per-clip-kj5</link>
      <guid>https://dev.to/zforzain2000/gemini-omni-12-flash-in-nextjs-the-setup-and-the-real-cost-per-clip-kj5</guid>
      <description>&lt;p&gt;Google shut down the Veo preview models on the Gemini API in October and pointed everyone at Omni instead. On November 5, Omni 1.2 Flash went generally available: clips up to 15 seconds with native audio, edits you make in conversation instead of re-rendering from scratch, and a 30 percent price cut. This post wires it into a Next.js app with code I typechecked against the real SDK, and works out what a finished clip actually costs you.&lt;/p&gt;

&lt;p&gt;How this post was verified: the specs and prices come from Google's Omni developer guide and the official Gemini API pricing page, both read on November 7, 2026. Every code snippet below was installed next to Next.js 16.2.6 and typechecked against @google/genai 1.52.0, and one snippet from Google's own docs failed that check, which is section three. I did not run a live generation for this post, because no paid API key was available in my environment, so you will find no quality verdict here that I cannot back. Google published no benchmarks either. Judge the picture on your own prompts; judge the integration and the bill here.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Gemini Omni 1.2 Flash actually is
&lt;/h2&gt;

&lt;p&gt;It is Google's video generation and editing model for the Gemini API, model ID &lt;code&gt;gemini-omni-1.2-flash&lt;/code&gt;, generally available since November 5, 2026 on the paid tier. You feed it text, images, or video, and it returns video with synchronized native audio: 3 to 15 seconds per generation, up to 6 reference images, and follow-up edits that build on the clip you already made.&lt;/p&gt;

&lt;p&gt;The architectural point most launch coverage skipped: Omni does not run on &lt;code&gt;generateContent&lt;/code&gt; like the Gemini text and image models you already know. It runs on the Interactions API, a stateful endpoint where every generation returns an interaction ID and later calls can reference it. That one decision shapes your whole integration. Generation, conversational editing, and clip extension are the same call with different inputs, and the thing you persist in your database is not a file, it is the interaction ID.&lt;/p&gt;

&lt;p&gt;Against Omni 1.1 Flash, three things changed. Clips grew from a 10-second cap to 15 seconds, in one-second steps. Video output pricing dropped from $17.50 to $12 per million tokens, which is the 30 percent cut in the headlines. And if your code still calls &lt;code&gt;gemini-omni-1.1-flash&lt;/code&gt;, nothing breaks on December 3, 2026: Google routes those requests to 1.2 automatically and bills them at the lower 1.2 rates.&lt;/p&gt;

&lt;p&gt;Two honest caveats before the code. Google says character consistency, lip-sync, and camera control improved, and it published no benchmarks to support any of that, so treat those as claims to test, not results. And every output carries an invisible SynthID watermark, which matters if your client needs unwatermarked masters for broadcast work.&lt;/p&gt;

&lt;h2&gt;
  
  
  The setup: from paid API key to a playing clip
&lt;/h2&gt;

&lt;p&gt;You need four pieces: a paid-tier API key (there is no free tier for this model), the @google/genai SDK, one route handler that creates the interaction, and one small helper that pulls the video out of the response. A second route handler adds conversational editing. Everything below is one minimal App Router project, and it typechecks as a set.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 1: Get a key that can actually call the model
&lt;/h3&gt;

&lt;p&gt;Go to &lt;a href="https://aistudio.google.com" rel="noopener noreferrer"&gt;Google AI Studio&lt;/a&gt; and create an API key on a project with billing attached. I am putting this first because it is where most people will burn their first hour: the pricing page lists the free tier as "Not available" for both input and output on &lt;code&gt;gemini-omni-1.2-flash&lt;/code&gt;, so a free-tier key fails on this model no matter how correct your code is. Put the key in &lt;code&gt;.env.local&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;GEMINI_API_KEY&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;your_paid_tier_key
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 2: Install the SDK
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; @google/genai
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That is the only new dependency. The Omni calls live in &lt;code&gt;ai.interactions&lt;/code&gt;, next to the &lt;code&gt;ai.models&lt;/code&gt; surface you may already use for text and images.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 3: The generate route
&lt;/h3&gt;

&lt;p&gt;This is the whole server side of text-to-video: validate the prompt, clamp the duration to the supported 3 to 15 second window, create the interaction, and hand the video back as base64 your page can play immediately.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;GoogleGenAI&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@google/genai&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;NextRequest&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;NextResponse&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;next/server&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;extractVideo&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@/lib/extract-video&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// The @google/genai SDK needs the Node.js runtime, not Edge.&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;runtime&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;nodejs&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// A generation call runs until the clip is rendered, which is far longer&lt;/span&gt;
&lt;span class="c1"&gt;// than a default serverless timeout. On Vercel, raise the limit here&lt;/span&gt;
&lt;span class="c1"&gt;// (Pro plans and up), or move the call into a background job.&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;maxDuration&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;300&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ai&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;GoogleGenAI&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;apiKey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;GEMINI_API_KEY&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;GenerateVideoBody&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;prompt&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;durationSeconds&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// whole seconds, 3 to 15, default 10&lt;/span&gt;
  &lt;span class="nl"&gt;aspectRatio&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;16:9&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;9:16&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;resolution&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;360p&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;720p&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;1080p&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;4K&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;POST&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;request&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;NextRequest&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;body&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="kd"&gt;as &lt;/span&gt;&lt;span class="nx"&gt;GenerateVideoBody&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;prompt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prompt&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nf"&gt;trim&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;prompt&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;NextResponse&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;prompt is required&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;400&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;seconds&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;min&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;round&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;durationSeconds&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)));&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;interaction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;ai&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;interactions&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;model&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;gemini-omni-1.2-flash&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;input&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;prompt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;response_format&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;video&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;duration&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;seconds&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;s`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;aspect_ratio&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;aspectRatio&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;16:9&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;resolution&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;resolution&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;720p&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;video&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;extractVideo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;interaction&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;video&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;NextResponse&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;model returned no video&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;interactionId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;interaction&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="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;502&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;NextResponse&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;interactionId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;interaction&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="na"&gt;mimeType&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;video&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;mime_type&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;video/mp4&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;videoBase64&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;video&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Three &lt;code&gt;response_format&lt;/code&gt; fields do the real work. &lt;code&gt;duration&lt;/code&gt; takes a string from &lt;code&gt;"3s"&lt;/code&gt; to &lt;code&gt;"15s"&lt;/code&gt; and defaults to &lt;code&gt;"10s"&lt;/code&gt; when you leave it out; a value in &lt;code&gt;response_format&lt;/code&gt; overrides any length you mention in the prompt text, so set it here and keep your prompts about the scene. &lt;code&gt;aspect_ratio&lt;/code&gt; is &lt;code&gt;"16:9"&lt;/code&gt; by default or &lt;code&gt;"9:16"&lt;/code&gt; for portrait. &lt;code&gt;resolution&lt;/code&gt; is where the money hides: 360p and 720p are the native renders, while 1080p and 4K are upsampled from them and billed at higher token rates, which the cost section prices out.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;extractVideo&lt;/code&gt; helper is not decoration. Your first attempt at this route will follow Google's JavaScript examples, and it will not typecheck. The next section shows why, and the fix is fifteen lines.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 4: Play the clip in the browser
&lt;/h3&gt;

&lt;p&gt;The route returns base64, so the client is a form, a fetch, and a video element fed by a data URL. This is the complete page component from the same project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;use client&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;VideoResult&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;interactionId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;mimeType&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;videoBase64&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;VideoForm&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;prompt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setPrompt&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;A single continuous shot of a paper plane gliding over a library table, warm window light, gentle camera drift. No dialogue.&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;seconds&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setSeconds&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;instruction&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setInstruction&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Change the lighting to dusk.&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setResult&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;VideoResult&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;busy&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setBusy&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setError&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;string&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Record&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;unknown&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;setBusy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nf"&gt;setError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;method&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;POST&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Content-Type&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;application/json&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="na"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
      &lt;span class="p"&gt;});&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
      &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ok&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="s2"&gt;`request failed: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;status&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="nf"&gt;setResult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="kd"&gt;as &lt;/span&gt;&lt;span class="nx"&gt;VideoResult&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nf"&gt;setError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt; &lt;span class="k"&gt;instanceof&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;something went wrong&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;finally&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nf"&gt;setBusy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;main&lt;/span&gt; &lt;span class="na"&gt;style&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;maxWidth&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;760&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;margin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;2rem auto&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;fontFamily&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;system-ui&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Omni video studio&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;textarea&lt;/span&gt;
        &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;prompt&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
        &lt;span class="na"&gt;onChange&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;setPrompt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
        &lt;span class="na"&gt;rows&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
        &lt;span class="na"&gt;style&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;100%&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;label&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        Seconds (3-15):&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt; &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;input&lt;/span&gt;
          &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"number"&lt;/span&gt;
          &lt;span class="na"&gt;min&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
          &lt;span class="na"&gt;max&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
          &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;seconds&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
          &lt;span class="na"&gt;onChange&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;setSeconds&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;label&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;
        &lt;span class="na"&gt;disabled&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;busy&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
        &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/api/generate-video&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;prompt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;durationSeconds&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;seconds&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;busy&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Generating...&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Generate clip&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;

      &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt; &lt;span class="na"&gt;role&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"alert"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;

      &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&amp;gt;&lt;/span&gt;
          &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;video&lt;/span&gt;
            &lt;span class="na"&gt;controls&lt;/span&gt;
            &lt;span class="na"&gt;style&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;100%&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;marginTop&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;1rem&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
            &lt;span class="na"&gt;src&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;`data:&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;mimeType&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;;base64,&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;videoBase64&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
          &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
          &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Interaction: &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;interactionId&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
          &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;input&lt;/span&gt;
            &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;instruction&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
            &lt;span class="na"&gt;onChange&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;setInstruction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
            &lt;span class="na"&gt;style&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;70%&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
          &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
          &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;
            &lt;span class="na"&gt;disabled&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;busy&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
            &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
              &lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/api/edit-video&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="na"&gt;interactionId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;interactionId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="nx"&gt;instruction&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
              &lt;span class="p"&gt;})&lt;/span&gt;
            &lt;span class="si"&gt;}&lt;/span&gt;
          &lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
            Edit this clip
          &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;/&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;main&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Base64 in JSON is the right call for a first integration and the wrong call at scale: it inflates the payload by about a third, and the docs recommend switching &lt;code&gt;response_format&lt;/code&gt; to &lt;code&gt;delivery: 'uri'&lt;/code&gt; for videos over 4 MB, which means anything above 720p. When you make that switch, the same content part carries a &lt;code&gt;uri&lt;/code&gt; instead of &lt;code&gt;data&lt;/code&gt;, and your job becomes downloading and storing the file rather than passing it through.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 5: Edit the clip in conversation
&lt;/h3&gt;

&lt;p&gt;Editing is the feature that justifies this API's shape. The follow-up call passes the earlier interaction ID and a one-line instruction; you never re-upload the clip, and you get back a new interaction whose ID you keep for the next turn.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;GoogleGenAI&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@google/genai&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;NextRequest&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;NextResponse&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;next/server&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;extractVideo&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@/lib/extract-video&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// The @google/genai SDK needs the Node.js runtime, not Edge.&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;runtime&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;nodejs&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;maxDuration&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;300&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ai&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;GoogleGenAI&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;apiKey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;GEMINI_API_KEY&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;EditVideoBody&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;interactionId&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;instruction&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;POST&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;request&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;NextRequest&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;body&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="kd"&gt;as &lt;/span&gt;&lt;span class="nx"&gt;EditVideoBody&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;instruction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;instruction&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nf"&gt;trim&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;interactionId&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;instruction&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;NextResponse&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;interactionId and instruction are required&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;400&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="c1"&gt;// Conversational editing: the follow-up call references the earlier&lt;/span&gt;
  &lt;span class="c1"&gt;// interaction instead of re-uploading the clip, and returns a new&lt;/span&gt;
  &lt;span class="c1"&gt;// interaction whose id you keep for the next turn.&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;interaction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;ai&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;interactions&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;model&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;gemini-omni-1.2-flash&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;previous_interaction_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;interactionId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;input&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;instruction&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; Keep everything else the same.`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;video&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;extractVideo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;interaction&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;video&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;NextResponse&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;model returned no video&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;interactionId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;interaction&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="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;502&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;NextResponse&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;interactionId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;interaction&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="na"&gt;mimeType&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;video&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;mime_type&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;video/mp4&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;videoBase64&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;video&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Google's prompt guide is blunt about how to write these instructions: one change per turn, kept simple, with "Keep everything else the same" appended, which is why the route adds it for you. A paragraph-long edit brief produces edits you did not ask for.&lt;/p&gt;

&lt;h3&gt;
  
  
  The production warning I would tattoo on this route
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;create&lt;/code&gt; call waits for the render. That is fine in a demo and dangerous as a public endpoint: a user who double-clicks Generate has just bought two renders, and a platform timeout kills the request while the bill stands. Past the prototype stage, I would put this call behind a queue, store the interaction ID against the job, and let the user poll your own status endpoint. That is the same job pattern I built for &lt;a href="https://dev.to/blog/bullmq-nestjs-retries-idempotency-dead-letter-queue"&gt;BullMQ retries and dead-letter queues in NestJS&lt;/a&gt;, and the idempotency lesson transfers directly: one user action, one paid generation, no duplicates. One related switch to know about: the docs suggest &lt;code&gt;store: false&lt;/code&gt; for faster synchronous calls, but a clip stored nowhere cannot be edited later by &lt;code&gt;previous_interaction_id&lt;/code&gt;. Speed now or editability later; pick per feature, not by accident.&lt;/p&gt;

&lt;h2&gt;
  
  
  The trap in Google's own JavaScript examples
&lt;/h2&gt;

&lt;p&gt;Google's Omni guide shows the finished video being read from &lt;code&gt;interaction.output_video.data&lt;/code&gt; in the JavaScript tab. When I typechecked that pattern against @google/genai 1.52.0, it failed: the JavaScript SDK's &lt;code&gt;Interaction&lt;/code&gt; type has no &lt;code&gt;output_video&lt;/code&gt; property at all. That convenience field exists in the Python SDK. In JavaScript, the response carries an &lt;code&gt;outputs&lt;/code&gt; array of content parts, and the video is the part whose &lt;code&gt;type&lt;/code&gt; is &lt;code&gt;'video'&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So the extraction is a helper you write once and reuse in every route:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Interactions&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@google/genai&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Interaction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Interactions&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Interaction&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;VideoContent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Interactions&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;VideoContent&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// In the JavaScript SDK the finished video is not a top-level field on&lt;/span&gt;
&lt;span class="c1"&gt;// the interaction. It is the content part with type 'video' inside&lt;/span&gt;
&lt;span class="c1"&gt;// interaction.outputs - base64 in `data`, or a downloadable `uri` when&lt;/span&gt;
&lt;span class="c1"&gt;// you asked for URI delivery.&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;extractVideo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;interaction&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Interaction&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;VideoContent&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;video&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;interaction&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;outputs&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nf"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;part&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;part&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nx"&gt;VideoContent&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;part&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;video&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;video&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you are calling the REST endpoint directly instead of the SDK, expect a third shape: the video sits in the &lt;code&gt;steps&lt;/code&gt; array, in the &lt;code&gt;model_output&lt;/code&gt; step, as a base64 &lt;code&gt;video/mp4&lt;/code&gt; content entry. Three surfaces, three shapes, one model. The working rule I take from this: vendor documentation is written per language and drifts between them, so I typecheck every vendor snippet against the installed package before it enters a codebase. The typecheck is five seconds; debugging a silent &lt;code&gt;undefined&lt;/code&gt; in production is not.&lt;/p&gt;

&lt;h2&gt;
  
  
  What one clip really costs
&lt;/h2&gt;

&lt;p&gt;Video output is billed per token, and the token count depends on resolution: 5,792 tokens per second at 720p, charged at $12 per million. That is $0.0695 per second, which Google rounds to about $0.07. A 15-second 720p clip is 86,880 output tokens, or about $1.04, before the small input charge for your prompt and references at $1.50 per million tokens.&lt;/p&gt;

&lt;p&gt;Here is the full ladder from the pricing page, with the per-clip math done:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Resolution&lt;/th&gt;
&lt;th&gt;Tokens per second&lt;/th&gt;
&lt;th&gt;Per second&lt;/th&gt;
&lt;th&gt;8-second clip&lt;/th&gt;
&lt;th&gt;15-second clip&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;360p&lt;/td&gt;
&lt;td&gt;1,931&lt;/td&gt;
&lt;td&gt;$0.023&lt;/td&gt;
&lt;td&gt;$0.19&lt;/td&gt;
&lt;td&gt;$0.35&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;720p&lt;/td&gt;
&lt;td&gt;5,792&lt;/td&gt;
&lt;td&gt;$0.07&lt;/td&gt;
&lt;td&gt;$0.56&lt;/td&gt;
&lt;td&gt;$1.04&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1080p (upsampled)&lt;/td&gt;
&lt;td&gt;8,688&lt;/td&gt;
&lt;td&gt;$0.104&lt;/td&gt;
&lt;td&gt;$0.83&lt;/td&gt;
&lt;td&gt;$1.56&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4K (upsampled)&lt;/td&gt;
&lt;td&gt;17,376&lt;/td&gt;
&lt;td&gt;$0.209&lt;/td&gt;
&lt;td&gt;$1.67&lt;/td&gt;
&lt;td&gt;$3.13&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;If you want this math inside your own app, for budgets or usage-based billing, it is one small function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Rates from the official Gemini API pricing page, checked 2026-11-07.&lt;/span&gt;
&lt;span class="c1"&gt;// Video output is billed per token; the token count per second depends&lt;/span&gt;
&lt;span class="c1"&gt;// on resolution. 1080p and 4K are upsampled from the native render.&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;VIDEO_TOKENS_PER_SECOND&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;360p&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1931&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;720p&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;5792&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;1080p&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;8688&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;4K&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;17376&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="kd"&gt;as const&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;VideoResolution&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kr"&gt;keyof&lt;/span&gt; &lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;VIDEO_TOKENS_PER_SECOND&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;VIDEO_OUTPUT_USD_PER_MILLION_TOKENS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;clipCostUsd&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;resolution&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;VideoResolution&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;seconds&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;tokens&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;VIDEO_TOKENS_PER_SECOND&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;resolution&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;seconds&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;tokens&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;VIDEO_OUTPUT_USD_PER_MILLION_TOKENS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="nx"&gt;_000_000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the number that actually decides whether your feature makes money: the cost per accepted clip, not per generation. Generation is probabilistic. If one take in three is usable, your 15-second keeper at 720p cost you about $3.13, not $1.04. Two habits keep that survivable. Draft at 360p, where a 15-second take costs $0.35, and only render the winning prompt at 720p. And remember that a conversational edit is a fresh render at full price: editing saves you the re-briefing and keeps the scene consistent, but a 15-second edit at 720p is another $1.04, not a discount. There is also no Batch rate listed for this model on the pricing page as of November 7, so the usual 50 percent batch trick does not apply here.&lt;/p&gt;

&lt;h2&gt;
  
  
  Omni 1.2 Flash vs Veo 3.1 vs Runway Gen-4.5
&lt;/h2&gt;

&lt;p&gt;If your app talks to the Gemini API, this comparison starts with a funeral: the Veo 3.1 preview models on that API shut down on October 22, 2026, and Omni is the replacement Google names. Veo still exists on Google Cloud's Gemini Enterprise Agent Platform, and Runway Gen-4.5 is the strongest option outside Google. So the real question is not which logo wins; it is which platform your app should live on.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;Gemini Omni 1.2 Flash&lt;/th&gt;
&lt;th&gt;Veo 3.1 (Google Cloud)&lt;/th&gt;
&lt;th&gt;Veo 3.1 Fast (Google Cloud)&lt;/th&gt;
&lt;th&gt;Runway Gen-4.5&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;API route&lt;/td&gt;
&lt;td&gt;Gemini API, one API key&lt;/td&gt;
&lt;td&gt;Gemini Enterprise Agent Platform&lt;/td&gt;
&lt;td&gt;Gemini Enterprise Agent Platform&lt;/td&gt;
&lt;td&gt;Runway API&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Longest single clip&lt;/td&gt;
&lt;td&gt;15 seconds&lt;/td&gt;
&lt;td&gt;8 seconds&lt;/td&gt;
&lt;td&gt;8 seconds&lt;/td&gt;
&lt;td&gt;10 seconds&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Native audio&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;720p cost per second&lt;/td&gt;
&lt;td&gt;About $0.07&lt;/td&gt;
&lt;td&gt;$0.40&lt;/td&gt;
&lt;td&gt;$0.10&lt;/td&gt;
&lt;td&gt;$0.12&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Conversational editing&lt;/td&gt;
&lt;td&gt;Built in, via interaction IDs&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Status&lt;/td&gt;
&lt;td&gt;GA, November 5, 2026&lt;/td&gt;
&lt;td&gt;GA on Cloud; Gemini API previews shut down October 22, 2026&lt;/td&gt;
&lt;td&gt;GA on Cloud&lt;/td&gt;
&lt;td&gt;GA&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Prices checked November 6-7, 2026 against each vendor's published rates. Runway bills 12 credits per second on its API at $0.01 per credit, which is where its $0.12 comes from.&lt;/p&gt;

&lt;p&gt;Read the table by constraint, not by brand. Omni wins on clip length, editing loop, and price per second, and it is the only one of the four your existing Gemini API key can call today. Veo 3.1 standard tier is the quality lane for teams already running on Google Cloud, at nearly six times Omni's per-second price. Runway earns its place when your team already lives in its editor and the API is a delivery mechanism, not the product.&lt;/p&gt;

&lt;p&gt;The tools around the model, for reference:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Tool&lt;/th&gt;
&lt;th&gt;What it does in this setup&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;Google AI Studio&lt;/td&gt;
&lt;td&gt;Get the paid API key, prototype prompts before writing code&lt;/td&gt;
&lt;td&gt;&lt;a href="https://aistudio.google.com" rel="noopener noreferrer"&gt;https://aistudio.google.com&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Gemini API pricing page&lt;/td&gt;
&lt;td&gt;The source for every rate quoted in this post&lt;/td&gt;
&lt;td&gt;&lt;a href="https://ai.google.dev/gemini-api/docs/pricing" rel="noopener noreferrer"&gt;https://ai.google.dev/gemini-api/docs/pricing&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Gemini Omni developer guide&lt;/td&gt;
&lt;td&gt;Official patterns for generation, editing, and extension&lt;/td&gt;
&lt;td&gt;&lt;a href="https://ai.google.dev/gemini-api/docs/omni" rel="noopener noreferrer"&gt;https://ai.google.dev/gemini-api/docs/omni&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Google Cloud console&lt;/td&gt;
&lt;td&gt;The route to Veo 3.1 if you outgrow the API key&lt;/td&gt;
&lt;td&gt;&lt;a href="https://console.cloud.google.com" rel="noopener noreferrer"&gt;https://console.cloud.google.com&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Runway&lt;/td&gt;
&lt;td&gt;The non-Google alternative, Gen-4.5 on API credits&lt;/td&gt;
&lt;td&gt;&lt;a href="https://runway.com/pricing" rel="noopener noreferrer"&gt;https://runway.com/pricing&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;{/* MONETIZATION NOTE: no affiliate links are live in this table yet. If affiliate or partner programs for Google Cloud or Runway are joined later, swap the plain URLs above for tracked ones at these rows. */}&lt;/p&gt;

&lt;h2&gt;
  
  
  The limits that will bite you
&lt;/h2&gt;

&lt;p&gt;None of these are in the launch headlines, and every one of them is in the official guide's limitations section, which is worth more of your trust than the announcement:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;First and last frame interpolation only supports 3 to 10 seconds. Ask for a longer clip with two frames and you get an invalid argument error, not a longer clip.&lt;/li&gt;
&lt;li&gt;Extension adds 3 to 10 seconds per turn, up to a 30-second total, and it only appends to the end, using the last 10 seconds as context. There is no prepending and no mid-clip extension.&lt;/li&gt;
&lt;li&gt;Reference rules shift with duration: clips of 11 to 15 seconds accept one reference video (up to 3 seconds), clips of 3 to 10 seconds accept up to three, and the 6 reference images are allowed at any duration.&lt;/li&gt;
&lt;li&gt;Audio references are not supported in the API today, and neither is voice editing. You can generate dialogue; you cannot hand the model a voice to clone or fix a line of speech after the fact.&lt;/li&gt;
&lt;li&gt;There are no negative prompts, temperature, top_p, or system instructions on this model. Put exclusions in the prompt itself, in plain words.&lt;/li&gt;
&lt;li&gt;Editing or extending uploaded videos is unavailable to users in the EEA, Switzerland, and the UK; editing model-generated videos works in every supported region.&lt;/li&gt;
&lt;li&gt;Google's own stated weak spots are fast sports action, rapid script changes, and complex physics. If your product concept lives in one of those, budget for a much lower keeper rate than the math above assumes.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  My verdict: what I would ship
&lt;/h2&gt;

&lt;p&gt;If I were adding video generation to a Next.js product this week, I would build it on Gemini Omni 1.2 Flash, and it is not close. It is the only video model a Gemini API key can call now that the Veo previews are gone, it gives you the longest single clips of the four, the editing loop is a genuine workflow advantage rather than a demo trick, and at about $0.07 a second it is the cheapest 720p generation here. For a product demo generator, a UGC ad variant tool, or social clips inside a SaaS, this is the default pick.&lt;/p&gt;

&lt;p&gt;The answer flips in three situations. If your company already runs on Google Cloud and needs Veo's request-level controls and multiple outputs per call, stay on the platform and pay Veo's prices. If the video is the product and your team works in a creative editor all day, Runway's ecosystem matters more than a five-cent-per-second gap. And if your concept needs unbroken footage longer than 15 seconds as a single render, none of these four is your tool yet; you are stitching extensions or waiting.&lt;/p&gt;

&lt;p&gt;One last honesty note, because it decides how much of this post you should weight: the integration above is typechecked and the prices are from Google's own page, but I have not seen this model's output with my own eyes, and Google shipped no benchmarks for you to lean on either. Run your ten hardest prompts through it at 360p for about two dollars total before you design pricing around it. That experiment is worth more than any launch post, including this one.&lt;/p&gt;

&lt;p&gt;If you would rather hand this integration to someone who has already mapped the traps, that is the work I take on: production Next.js and NestJS builds where the API billing math is done before the first deploy, not after the first invoice. &lt;a href="https://dev.to/#contact"&gt;Tell me about your project&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  FAQ
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What is the model ID for Gemini Omni 1.2 Flash?
&lt;/h3&gt;

&lt;p&gt;gemini-omni-1.2-flash. It went generally available on November 5, 2026 on the paid tier of the Gemini API, and you call it through the Interactions API, not generateContent.&lt;/p&gt;

&lt;h3&gt;
  
  
  Is there a free tier for Gemini Omni 1.2 Flash?
&lt;/h3&gt;

&lt;p&gt;No. Google's pricing page lists the free tier as not available for both input and output on this model. You need a Gemini API key attached to a paid billing setup before the first call succeeds.&lt;/p&gt;

&lt;h3&gt;
  
  
  How much does a 15-second clip cost?
&lt;/h3&gt;

&lt;p&gt;About $1.04 of video output at 720p: 15 seconds at 5,792 tokens per second is 86,880 tokens, billed at $12 per million. The same clip costs about $0.35 at 360p and $1.56 upsampled to 1080p. Input tokens for your prompt and references come on top.&lt;/p&gt;

&lt;h3&gt;
  
  
  Does Omni use the generateContent API like other Gemini models?
&lt;/h3&gt;

&lt;p&gt;No. Omni runs on the Interactions API. In the @google/genai SDK that is ai.interactions.create, and the finished video comes back inside interaction.outputs as the content part with type video, not as a top-level field.&lt;/p&gt;

&lt;h3&gt;
  
  
  Can I edit a clip after generating it instead of starting over?
&lt;/h3&gt;

&lt;p&gt;Yes. Call ai.interactions.create again with previous_interaction_id set to the earlier interaction and a plain-language instruction like 'Change the lighting to dusk. Keep everything else the same.' Each edit is a fresh render, so it bills as a new generation.&lt;/p&gt;

&lt;h3&gt;
  
  
  What happens to code using gemini-omni-1.1-flash on December 3, 2026?
&lt;/h3&gt;

&lt;p&gt;Nothing breaks. Google automatically routes requests to gemini-omni-1.1-flash to gemini-omni-1.2-flash from December 3, 2026 and bills them at the 1.2 rates, with no code change required.&lt;/p&gt;

</description>
      <category>nextjs</category>
      <category>gemini</category>
      <category>ai</category>
      <category>typescript</category>
    </item>
    <item>
      <title>BullMQ in NestJS: Retries, Idempotency and Dead-Letter Queues</title>
      <dc:creator>MUHAMMAD ZAIN UL ABIDIN</dc:creator>
      <pubDate>Thu, 05 Nov 2026 06:33:53 +0000</pubDate>
      <link>https://dev.to/zforzain2000/bullmq-in-nestjs-retries-idempotency-and-dead-letter-queues-1h4p</link>
      <guid>https://dev.to/zforzain2000/bullmq-in-nestjs-retries-idempotency-and-dead-letter-queues-1h4p</guid>
      <description>&lt;p&gt;A queue that retries blindly is worse than no queue at all. It double-sends the welcome email, double-charges the customer, and then deletes the evidence by retrying the same broken job until the failure looks like noise. This post builds the setup I actually run in NestJS: retries that back off with jitter, jobs that cannot run twice, and a dead-letter queue that keeps every terminal failure inspectable and replayable.&lt;/p&gt;

&lt;p&gt;I learned this the way most backend lessons arrive: a welcome email went out three times. The endpoint that sent it called the mail provider directly, the provider timed out once, and my retry logic ran the whole send again instead of checking whether the first attempt had landed. The user got three identical emails, support got a ticket, and I got a rule I have kept since: a background job is only safe to retry if running it twice is harmless, and a job that fails for good must land somewhere a human can find it.&lt;/p&gt;

&lt;p&gt;That is what this post builds, in one NestJS module. The stack is BullMQ 6.7.0 with &lt;code&gt;@nestjs/bullmq&lt;/code&gt; 12.0.0 on NestJS 11, verified against the installed packages on November 5, 2026: every snippet below typechecks under strict TypeScript, and the backoff numbers are measured from BullMQ's own code, not copied from a tutorial. It is the same NestJS and Redis pair I ship in production, the one behind the real-time rooms in the &lt;a href="https://dev.to/work/healthcare-ireland"&gt;Healthcare Ireland build&lt;/a&gt; that I wrote up in &lt;a href="https://dev.to/blog/scaling-socketio-rooms-nestjs-redis-adapter"&gt;Scaling Socket.io Rooms in NestJS With the Redis Adapter&lt;/a&gt; and the hiring page at &lt;a href="https://dev.to/blog/hire-senior-nextjs-nestjs-developer-pakistan"&gt;Hiring a Senior Next.js + NestJS Developer from Pakistan&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  The three failures a naive queue hides
&lt;/h2&gt;

&lt;p&gt;A queue with default settings hides three failures at once. It retries instantly, so a struggling dependency gets hammered by every failed job at the same moment. It repeats side effects, so a job that crashed after sending the email sends it again on the retry. And it deletes its own evidence, because a failed job that is cleaned up too aggressively disappears before anyone reads the error. Each failure is cheap to prevent and expensive to debug after the fact, and all three come from treating the queue as a fire-and-forget box instead of a part of the system with its own failure modes.&lt;/p&gt;

&lt;p&gt;The fix is three mechanisms that belong together: backoff with jitter for retries, idempotency at two layers for repeated runs, and a dead-letter queue for terminal failures. None of them is optional if the job touches money, email, or any external system that charges per call. Skip one and the other two quietly stop protecting you: backoff without idempotency just spaces out the duplicate sends, and idempotency without a dead-letter queue means a permanently broken job retries five times and then vanishes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Set up the queue the documented way
&lt;/h2&gt;

&lt;p&gt;Install two packages and register one module. BullMQ is the queue library; &lt;code&gt;@nestjs/bullmq&lt;/code&gt; is the NestJS wrapper around it. The NestJS docs are direct about which one to pick in 2026: the older Bull package is in maintenance mode and only gets bug fixes, while BullMQ is actively developed. If you are starting now, BullMQ is the choice, and the wrapper is the documented way to use it inside NestJS.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;--save&lt;/span&gt; @nestjs/bullmq bullmq
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The module sets the defaults every job in the queue inherits. Three lines in it deserve attention before you copy the rest.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// notifications.module.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Module&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@nestjs/common&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;BullModule&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@nestjs/bullmq&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;DeadLetterListener&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./dead-letter.listener&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;DeadLetterService&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./dead-letter.service&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;MailService&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./mail.service&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;NotificationsProcessor&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./notifications.processor&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;NotificationsService&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./notifications.service&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Module&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;imports&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="nx"&gt;BullModule&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forRoot&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
      &lt;span class="na"&gt;connection&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;host&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;localhost&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;port&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;6379&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="c1"&gt;// Workers block on Redis; without this, ioredis aborts the&lt;/span&gt;
        &lt;span class="c1"&gt;// blocked command after a few retries during a Redis blip.&lt;/span&gt;
        &lt;span class="na"&gt;maxRetriesPerRequest&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;}),&lt;/span&gt;
    &lt;span class="nx"&gt;BullModule&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;registerQueue&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
      &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;notifications&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;defaultJobOptions&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;attempts&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="c1"&gt;// Custom type: BullMQ calls our backoffStrategy for this name.&lt;/span&gt;
        &lt;span class="c1"&gt;// delay: -1 is the documented placeholder for custom strategies.&lt;/span&gt;
        &lt;span class="na"&gt;backoff&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;capped-jitter&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;delay&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="na"&gt;removeOnComplete&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="c1"&gt;// Keep failures 30 days for inspection, cap the set at 5000.&lt;/span&gt;
        &lt;span class="na"&gt;removeOnFail&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="nx"&gt;_592_000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;count&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;5000&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
      &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;}),&lt;/span&gt;
    &lt;span class="nx"&gt;BullModule&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;registerQueue&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;dead-letter&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;}),&lt;/span&gt;
  &lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="na"&gt;providers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="nx"&gt;MailService&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;NotificationsService&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;NotificationsProcessor&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;DeadLetterListener&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;DeadLetterService&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;],&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;NotificationsModule&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;maxRetriesPerRequest: null&lt;/code&gt; is the line most tutorials skip and the one that bites first in production. BullMQ workers wait for jobs with blocking Redis commands, and the ioredis default aborts a command after a few retries. During a short Redis blip, that abort kills the blocked wait instead of letting the worker ride it out. Setting it to null removes the cap. It belongs in the connection options of every queue you run.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;removeOnFail&lt;/code&gt; shape matters as much as the values. Never set it to &lt;code&gt;true&lt;/code&gt; on a queue whose failures you need to read: &lt;code&gt;true&lt;/code&gt; deletes a failed job the moment it lands in the failed set, which is exactly when you want to look at it. The object form keeps up to 5,000 failed jobs for up to 30 days. One honest caveat from BullMQ's own type definitions: eviction is best-effort, evaluated when another job finishes, so treat the age as a rough bound, not a timer.&lt;/p&gt;

&lt;p&gt;Read the Redis URL from the environment in your real app instead of hardcoding &lt;code&gt;localhost&lt;/code&gt;. The snippet above uses localhost so you can run it against a local Redis; the shape is what matters, not the host.&lt;/p&gt;

&lt;h2&gt;
  
  
  Retries: attempts counts total runs, not retries
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;attempts: 5&lt;/code&gt; means the job runs at most five times: the first run plus up to four retries. The default is 1, which means no retry at all. This is the single most misread option in BullMQ, and it is written plainly in the type definitions, so there is no excuse for getting it wrong once you have read them. If you want three retries after the first failure, set &lt;code&gt;attempts: 4&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The second most misread behavior is what happens without a backoff: BullMQ retries a failed job with no delay, as soon as it fails. Five instant retries from every failed job, hitting a dependency that is already struggling, is how a small outage becomes a bigger one. Backoff is what turns retries from a hammer into a waiting room.&lt;/p&gt;

&lt;p&gt;The built-in strategies are enough for most queues. Fixed waits the same time before every retry. Exponential doubles the gap each time. I measured the exponential formula against BullMQ 6.7.0's own code: the delay is &lt;code&gt;2^(attemptsMade - 1) * delay&lt;/code&gt;, so with &lt;code&gt;delay: 1000&lt;/code&gt; the gaps are 1s, 2s, 4s, then 8s. Add &lt;code&gt;jitter: 0.3&lt;/code&gt; and each step is randomized within 70 to 100 percent of the computed delay, which stops a thousand jobs that failed in the same second from retrying in the same second.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Backoff&lt;/th&gt;
&lt;th&gt;Shape&lt;/th&gt;
&lt;th&gt;Use it when&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;None&lt;/td&gt;
&lt;td&gt;Immediate retry&lt;/td&gt;
&lt;td&gt;Never in production; use it only in tests where you want fast failure&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;{ type: 'fixed', delay: 1000 }&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Same 1s gap every time&lt;/td&gt;
&lt;td&gt;The dependency recovers in a known, short window&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;{ type: 'exponential', delay: 1000 }&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;1s, 2s, 4s, 8s&lt;/td&gt;
&lt;td&gt;The default for anything that calls an external service&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;{ type: 'exponential', delay: 1000, jitter: 0.3 }&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Same curve, each step randomized&lt;/td&gt;
&lt;td&gt;The default once more than a few jobs can fail together&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Custom strategy&lt;/td&gt;
&lt;td&gt;Whatever your function returns&lt;/td&gt;
&lt;td&gt;You need a cap, full jitter, or a provider's Retry-After header&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;When the built-in curve is not enough, write your own. A custom strategy is a function BullMQ calls with the number of attempts already made, and it returns the delay in milliseconds. Returning -1 fails the job immediately instead of retrying. Mine uses full jitter, a random delay anywhere from zero up to an exponentially growing ceiling, capped at 60 seconds, because full jitter spreads retries more evenly than the built-in partial jitter when a whole queue fails at once.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// backoff.strategy.ts&lt;/span&gt;
&lt;span class="cm"&gt;/**
 * Full-jitter exponential backoff with a 60s ceiling.
 * BullMQ calls this with the number of attempts already made.
 * Returning -1 would fail the job immediately; we always return a delay.
 */&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;cappedJitterBackoff&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;attemptsMade&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ceiling&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;min&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1000&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt; &lt;span class="nx"&gt;attemptsMade&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="nx"&gt;_000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;random&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;ceiling&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The strategy is registered on the worker, not the queue, through the second argument of &lt;code&gt;@Processor&lt;/code&gt;. One detail that trips people up: BullMQ only calls your function when the job's backoff type is a name it does not recognize as built-in. That is why the module above sets &lt;code&gt;backoff: { type: 'capped-jitter', delay: -1 }&lt;/code&gt; as the default: &lt;code&gt;capped-jitter&lt;/code&gt; is not a built-in type, so BullMQ hands the decision to the function, and &lt;code&gt;delay: -1&lt;/code&gt; is the documented placeholder for custom strategies.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// notifications.processor.ts (worker options only; the full file is below)&lt;/span&gt;
&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Processor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;notifications&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;concurrency&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;limiter&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;max&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;duration&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="na"&gt;settings&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;backoffStrategy&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;cappedJitterBackoff&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;NotificationsProcessor&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;WorkerHost&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="cm"&gt;/* ... */&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you do not need a custom strategy, do not write one. &lt;code&gt;{ type: 'exponential', delay: 1000, jitter: 0.3 }&lt;/code&gt; in the queue defaults gets you nearly the same behavior with zero code, and less code in the retry path is a feature.&lt;/p&gt;

&lt;h2&gt;
  
  
  Classify the error or every retry is wasted
&lt;/h2&gt;

&lt;p&gt;Not every failure deserves a retry. A timeout or a 500 from the mail provider might clear in seconds; an invalid email address will fail the same way on attempt five as it did on attempt one. Retrying the second kind burns five attempts, five backoff waits, and five log lines to learn nothing. The fix is to classify the error inside the processor and fail fast on the permanent kind.&lt;/p&gt;

&lt;p&gt;BullMQ gives you &lt;code&gt;UnrecoverableError&lt;/code&gt; for exactly this. Throw it and the job moves straight to the failed set, skipping whatever attempts remain. Throw a normal error and the job retries with backoff. Throw &lt;code&gt;RateLimitError&lt;/code&gt; when a provider rate-limits you and the job goes back to waiting without burning an attempt at all, because rate limiting is not counted as a real failure. One rule from the BullMQ docs applies to all three: thrown exceptions must be real &lt;code&gt;Error&lt;/code&gt; objects, not strings or plain objects, or BullMQ cannot handle them correctly.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// notifications.processor.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Processor&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;WorkerHost&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@nestjs/bullmq&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Job&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;UnrecoverableError&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;bullmq&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;cappedJitterBackoff&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./backoff.strategy&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;MailService&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;MailValidationError&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./mail.service&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;NotificationData&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./notifications.service&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;NotificationJobName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;send-welcome-email&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;send-invoice-email&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Processor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;notifications&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;concurrency&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;limiter&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;max&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;duration&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="na"&gt;settings&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;backoffStrategy&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;cappedJitterBackoff&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;NotificationsProcessor&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;WorkerHost&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;mail&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;MailService&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nf"&gt;process&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;job&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Job&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;NotificationData&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;NotificationJobName&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;void&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;switch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;job&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;send-welcome-email&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sendWithClassification&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;job&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;welcome&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;send-invoice-email&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sendWithClassification&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;job&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;invoice&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;default&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;UnrecoverableError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Unknown job name: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;job&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nf"&gt;sendWithClassification&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nx"&gt;job&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Job&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;NotificationData&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;NotificationJobName&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;template&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;void&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;mail&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
        &lt;span class="na"&gt;to&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;job&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;email&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nx"&gt;template&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="c1"&gt;// Same key on every retry: the provider dedupes the send.&lt;/span&gt;
        &lt;span class="na"&gt;idempotencyKey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;job&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;-&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;job&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="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt; &lt;span class="k"&gt;instanceof&lt;/span&gt; &lt;span class="nx"&gt;MailValidationError&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Permanent failure: skip the remaining attempts, go straight to failed.&lt;/span&gt;
        &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;UnrecoverableError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Invalid address for job &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;job&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="s2"&gt;: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two NestJS details in that file are worth naming. First, BullMQ has no &lt;code&gt;@Process('job-name')&lt;/code&gt; decorator: the older Bull package had one, BullMQ deliberately dropped it, and the NestJS docs tell you to switch on &lt;code&gt;job.name&lt;/code&gt; instead. Second, the processor is a class that extends &lt;code&gt;WorkerHost&lt;/code&gt; and implements &lt;code&gt;process()&lt;/code&gt;, registered as a provider; there is no separate worker bootstrap to write.&lt;/p&gt;

&lt;p&gt;The classification rule I use in practice: if the error message names something about the data (validation, not found, unauthorized, bad request), it is permanent. If it names something about the moment (timeout, connection reset, 429, 502, 503), it is transient. When I genuinely cannot tell, I retry: a wasted retry is cheaper than a dropped email, and the dead-letter queue below catches the job if I was wrong.&lt;/p&gt;

&lt;h2&gt;
  
  
  Idempotency: two layers, and only one of them is optional
&lt;/h2&gt;

&lt;p&gt;Retries assume it is safe to run the job again. That assumption breaks the moment a job has a side effect. A worker can crash after the mail provider accepted the send but before the worker recorded success, and the retry will send the same email a second time. The protection has two layers, and they solve different halves of the problem.&lt;/p&gt;

&lt;p&gt;Layer one is a deterministic job id at enqueue time. When you add a job with a &lt;code&gt;jobId&lt;/code&gt; that already exists, BullMQ ignores the duplicate add. A double-clicked submit button, a retried HTTP request, or a webhook provider delivering the same event twice all collapse into one job.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// notifications.service.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Injectable&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@nestjs/common&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;InjectQueue&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@nestjs/bullmq&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Queue&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;bullmq&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;NotificationData&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;invoiceId&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Injectable&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;NotificationsService&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(@&lt;/span&gt;&lt;span class="nd"&gt;InjectQueue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;notifications&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Queue&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

  &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nf"&gt;sendWelcomeEmail&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;job&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;queue&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;send-welcome-email&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;email&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="kd"&gt;satisfies &lt;/span&gt;&lt;span class="nx"&gt;NotificationData&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="c1"&gt;// Deterministic id: a retried HTTP request or a double-click&lt;/span&gt;
      &lt;span class="c1"&gt;// enqueues exactly one job. No ':' allowed in custom ids.&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;jobId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`welcome-email-&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;job&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="kd"&gt;as &lt;/span&gt;&lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nf"&gt;sendInvoiceEmail&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;invoiceId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;job&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;queue&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;send-invoice-email&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;email&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;invoiceId&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="kd"&gt;satisfies &lt;/span&gt;&lt;span class="nx"&gt;NotificationData&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;jobId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`invoice-email-&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;invoiceId&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;job&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="kd"&gt;as &lt;/span&gt;&lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Custom ids have two rules, both enforced by BullMQ's own code: they cannot contain a colon, and they cannot be plain integers. Use a readable prefix plus your business id, joined with a hyphen, and you stay clear of both. Note the limit of this layer, because it matters: once a job is removed from Redis, the same id can be added again. &lt;code&gt;removeOnComplete: 1000&lt;/code&gt; in the module above means a completed job's id becomes reusable once it ages out of the completed set. Layer one stops duplicates while the job exists; it says nothing about a job that already finished.&lt;/p&gt;

&lt;p&gt;Layer two is the one that actually makes retries safe, and it lives at the provider, not in the queue. Pass the same idempotency key to the mail provider on every attempt of the same job, and the provider dedupes the send at the source. The processor above builds the key from the job name and the job id, so attempt one and attempt four carry the identical key. If your provider does not support idempotency keys, the honest fallback is a unique constraint in your own database: an upsert on the same key, or a sent-log table with a unique index, checked and written in the same transaction as the effect. The naive version, a check-then-set flag in Redis or memory, has a race: the worker can crash after sending and before setting the flag, and the retry sends again. Put the key where the effect happens, not next to it.&lt;/p&gt;

&lt;p&gt;One more rule for job data: carry ids, not records. Job data must be serializable, and dead-letter entries keep a copy of the payload, so a job carrying a full user record quietly duplicates personal data into a second store. &lt;code&gt;{ userId, email }&lt;/code&gt; is enough; the worker fetches the rest.&lt;/p&gt;

&lt;h2&gt;
  
  
  Dead-letter queue: BullMQ ships the parts, not the queue
&lt;/h2&gt;

&lt;p&gt;BullMQ has no built-in dead-letter queue. It keeps terminally failed jobs in a failed set, and that set is a legitimate dead-letter store on its own. I build an explicit queue anyway, because it gives me separate retention, separate alerting, and a replay path that does not touch the main queue's cleanup settings. The pattern is a listener on the &lt;code&gt;failed&lt;/code&gt; event that copies the job into a second queue no worker consumes.&lt;/p&gt;

&lt;p&gt;The event semantics are the part most write-ups get wrong, so I verified them against BullMQ 6.7.0's source instead of repeating them. There are two &lt;code&gt;failed&lt;/code&gt; events and they fire at different times. The worker-level event fires on every failed attempt, including ones that will be retried. The QueueEvents &lt;code&gt;failed&lt;/code&gt; event fires only when the job actually lands in the failed set, which happens when retries are exhausted or &lt;code&gt;UnrecoverableError&lt;/code&gt; was thrown. The Lua script that moves a job to finished status emits the &lt;code&gt;failed&lt;/code&gt; event and a &lt;code&gt;retries-exhausted&lt;/code&gt; event together, only on that terminal path. So a QueueEvents listener sees exactly the jobs that are done failing, and nothing else. I still check &lt;code&gt;getState() === 'failed'&lt;/code&gt; inside the listener as a guard; it costs one Redis read and it covers every terminal path, including jobs that stalled too many times.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// dead-letter.listener.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;InjectQueue&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;OnQueueEvent&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;QueueEventsHost&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;QueueEventsListener&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@nestjs/bullmq&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Queue&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;bullmq&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;NotificationData&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./notifications.service&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;DeadLetterData&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;originalQueue&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;originalJobId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;NotificationData&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;failedReason&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;attemptsMade&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;failedAt&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;QueueEventsListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;notifications&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;DeadLetterListener&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;QueueEventsHost&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;InjectQueue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;notifications&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;notificationsQueue&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Queue&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;InjectQueue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;dead-letter&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;dlqQueue&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Queue&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="c1"&gt;// QueueEvents 'failed' fires only when a job lands in the failed set,&lt;/span&gt;
  &lt;span class="c1"&gt;// i.e. retries are exhausted or UnrecoverableError was thrown.&lt;/span&gt;
  &lt;span class="c1"&gt;// (The worker-level 'failed' fires on every attempt instead.)&lt;/span&gt;
  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;OnQueueEvent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;failed&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nf"&gt;onFailed&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;jobId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;failedReason&lt;/span&gt; &lt;span class="p"&gt;}:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;jobId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nl"&gt;failedReason&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt; &lt;span class="p"&gt;}):&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;void&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;job&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;notificationsQueue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getJob&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;jobId&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;job&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;job&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;job&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getState&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;failed&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;dlqQueue&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;dead-letter&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;originalQueue&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;notifications&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;originalJobId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;job&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="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;job&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;job&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="kd"&gt;as &lt;/span&gt;&lt;span class="nx"&gt;NotificationData&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nx"&gt;failedReason&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;attemptsMade&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;job&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;attemptsMade&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;failedAt&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;toISOString&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="kd"&gt;satisfies &lt;/span&gt;&lt;span class="nx"&gt;DeadLetterData&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="c1"&gt;// Deterministic DLQ id: a duplicate event cannot double-park the job.&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;jobId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`dlq-&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;job&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="s2"&gt;`&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The listener is registered as a provider, like the processor, and it uses the same queue names the module registered. Keep the full context in the dead-letter entry: original payload, error, attempt count, and timestamps. A dead-letter entry you cannot diagnose is just a bigger problem than the failed job was.&lt;/p&gt;

&lt;p&gt;Replay is a method, not a manual Redis edit. It removes the old failed job first, because re-adding the same job id while the old job still exists is silently ignored by BullMQ, then re-adds the job with its original id and removes the dead-letter entry.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// dead-letter.service.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Injectable&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@nestjs/common&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;InjectQueue&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@nestjs/bullmq&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Queue&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;bullmq&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;DeadLetterData&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./dead-letter.listener&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Injectable&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;DeadLetterService&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;InjectQueue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;notifications&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;notificationsQueue&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Queue&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;InjectQueue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;dead-letter&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;dlqQueue&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Queue&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

  &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nf"&gt;replay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;dlqJobId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;replayed&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;not-found&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;dlqJob&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;dlqQueue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getJob&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;dlqJobId&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;dlqJob&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;not-found&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;originalJobId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;dlqJob&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="kd"&gt;as &lt;/span&gt;&lt;span class="nx"&gt;DeadLetterData&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;failedJob&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;notificationsQueue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getJob&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;originalJobId&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="c1"&gt;// Remove the failed original first: re-adding the same jobId while&lt;/span&gt;
    &lt;span class="c1"&gt;// the old job still exists is silently ignored by BullMQ.&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;failedJob&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;failedJob&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;remove&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;notificationsQueue&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;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;jobId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;originalJobId&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;dlqJob&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;remove&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;replayed&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nf"&gt;failedCount&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;counts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;notificationsQueue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getJobCounts&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;failed&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;counts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;failed&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Wire &lt;code&gt;replay()&lt;/code&gt; to an admin endpoint or a small script, and alert on &lt;code&gt;failedCount()&lt;/code&gt; the same way you alert on a 5xx rate. A dead-letter queue nobody watches is a graveyard with better filing. In a bigger setup the listener would run as its own service so a crash in the notification workers cannot take the failure handling down with it; inside one NestJS app, the listener class above is the right size.&lt;/p&gt;

&lt;h2&gt;
  
  
  Protect the downstream: concurrency and the limiter
&lt;/h2&gt;

&lt;p&gt;Retries are bounded by attempts and backoff. The processing rate needs its own bound, or a recovered dependency gets hit by the full backlog at once. The &lt;code&gt;@Processor&lt;/code&gt; decorator takes worker options as its second argument: &lt;code&gt;concurrency: 5&lt;/code&gt; runs five jobs at a time per worker, and &lt;code&gt;limiter: { max: 50, duration: 1000 }&lt;/code&gt; caps the queue at 50 jobs per second. The limiter is global across workers for the queue, per the BullMQ docs, so two workers do not each get their own 50 per second. Size the limiter to what the downstream service actually tolerates, not to what your workers can produce: the point is protecting the mail provider, not throttling yourself for fun.&lt;/p&gt;

&lt;h2&gt;
  
  
  The production checklist I run through before shipping a queue
&lt;/h2&gt;

&lt;p&gt;&lt;br&gt;
&lt;br&gt;
Without it, a short Redis blip aborts the worker's blocked wait. This is the first thing I check when a queue stops processing after a deploy or a network flap.&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Defaults in &lt;code&gt;registerQueue&lt;/code&gt; keep every producer consistent. A producer that forgets &lt;code&gt;attempts&lt;/code&gt; silently gets one attempt and no retry, and you find out from a missing email, not from an error.&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Pick the most common permanent failure for your job, usually a validation error or a 400, and throw &lt;code&gt;UnrecoverableError&lt;/code&gt; for it. One classified error proves the path works; the rest can follow.&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
The job id stops duplicate enqueues. The provider key stops duplicate sends. One without the other leaves a hole, and the hole only shows up under exactly the conditions retries exist for.&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Add a job that always throws &lt;code&gt;UnrecoverableError&lt;/code&gt;, watch it land in the dead-letter queue with its full context, and replay it once. A DLQ you have never seen fill is a DLQ you have not tested.&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;code&gt;getJobCounts('failed')&lt;/code&gt; is the number to watch. A single failed job is normal; a rising count means a dependency is down or a deploy broke the processor.&lt;br&gt;
&lt;br&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  The tools I would actually use
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Tool&lt;/th&gt;
&lt;th&gt;What it is for&lt;/th&gt;
&lt;th&gt;Note&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;BullMQ&lt;/td&gt;
&lt;td&gt;The queue: retries, backoff, rate limiting, events&lt;/td&gt;
&lt;td&gt;Version 6.7.0 at the time of writing; the retry and backoff behavior in this post is verified against it&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;@nestjs/bullmq&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The NestJS wrapper: modules, processors, event listeners&lt;/td&gt;
&lt;td&gt;Version 12.0.0; works with NestJS 10, 11, and 12&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Redis&lt;/td&gt;
&lt;td&gt;The only storage BullMQ needs&lt;/td&gt;
&lt;td&gt;Run it managed in production; persistence on, because a queue that loses jobs on restart is a queue that silently drops work&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;ioredis&lt;/td&gt;
&lt;td&gt;The Redis client underneath BullMQ&lt;/td&gt;
&lt;td&gt;Comes with BullMQ; the only setting you touch is &lt;code&gt;maxRetriesPerRequest: null&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Your mail provider's idempotency key&lt;/td&gt;
&lt;td&gt;Dedupe at the send, not just at the enqueue&lt;/td&gt;
&lt;td&gt;Check that your provider supports one before you promise idempotency; if it does not, use a unique database constraint instead&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Bull Board&lt;/td&gt;
&lt;td&gt;A dashboard for queue counts, failed jobs, and retries&lt;/td&gt;
&lt;td&gt;Attach it to an admin route so you can see the failed set without opening a Redis CLI&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;{/* MONETIZATION NOTE: the tools table above is affiliate-ready. Swap the Redis or mail-provider rows for affiliate links only when a real program is joined; no affiliate claim is made here. */}&lt;/p&gt;

&lt;h2&gt;
  
  
  My verdict: what I would pick, and why
&lt;/h2&gt;

&lt;p&gt;If you are adding background jobs to a NestJS app today, I would pick BullMQ with the defaults in this post: five attempts, exponential backoff with jitter, error classification with &lt;code&gt;UnrecoverableError&lt;/code&gt;, a deterministic job id plus a provider idempotency key, and an explicit dead-letter queue with replay. That combination is the smallest setup that survives the three failures that actually happen in production: a dependency blip, a crash mid-send, and a permanently broken job. It is also the setup I can hand to another developer without a walkthrough, because every decision is visible in the module options rather than hidden in a wiki page.&lt;/p&gt;

&lt;p&gt;I would not pick the older Bull package for a new queue, because the NestJS docs say it is in maintenance mode and BullMQ is where the development happens. I would not write a custom backoff strategy until the built-in exponential curve with jitter proves too coarse, because the built-in one is measured, documented, and one line long. And I would not ship a queue without the dead-letter listener, because a queue without one turns every permanent failure into a silent disappearance, and silent disappearances are the failures your users report before your monitoring does.&lt;/p&gt;

&lt;p&gt;The answer flips in two cases. If a job has a side effect your provider cannot dedupe and no unique constraint can guard, set &lt;code&gt;attempts: 1&lt;/code&gt; and let the dead-letter queue catch it instead of retrying blindly; a job that must never run twice must never be retried. And if you have no Redis and no background work yet, do not add a queue at all: a database outbox table plus a cron job does the same work with one less service to operate, and you can move to BullMQ when the volume earns it. Outside those cases, the defaults above are the right starting point, and the boring documented shape is the right one to ship first.&lt;/p&gt;

&lt;p&gt;If you are building a production NestJS system and want a second pair of eyes on the queue design before it ships, that is the kind of work I do. &lt;a href="https://dev.to/#contact"&gt;Reach me through the contact section on the homepage&lt;/a&gt; with the job, the failure mode you are seeing, and the downstream service it calls, and we can walk through it in a first call.&lt;/p&gt;

&lt;h2&gt;
  
  
  FAQ
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What does attempts actually count in BullMQ?
&lt;/h3&gt;

&lt;p&gt;Total runs, not retries. &lt;code&gt;attempts: 5&lt;/code&gt; means the job runs at most five times: the first run plus up to four retries. The default is 1, which means no retry at all. If you want three retries after the first failure, set &lt;code&gt;attempts: 4&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why does my BullMQ job retry immediately instead of waiting?
&lt;/h3&gt;

&lt;p&gt;Because you set attempts without a backoff. BullMQ retries a failed job with no delay unless you configure one. Add &lt;code&gt;backoff: { type: 'exponential', delay: 1000 }&lt;/code&gt; and the gaps become 1s, 2s, 4s, then 8s. Add &lt;code&gt;jitter: 0.3&lt;/code&gt; so a thousand failed jobs do not all retry in the same second.&lt;/p&gt;

&lt;h3&gt;
  
  
  When should I throw UnrecoverableError?
&lt;/h3&gt;

&lt;p&gt;When the failure is permanent and another attempt will fail the same way: a validation error, an invalid email address, a missing record, a 400 from a provider. It moves the job straight to the failed set and skips the remaining attempts. Never use it for timeouts, 429s, 5xx errors, or anything a few seconds of waiting might fix.&lt;/p&gt;

&lt;h3&gt;
  
  
  Does a custom jobId stop duplicate jobs?
&lt;/h3&gt;

&lt;p&gt;At the enqueue step, yes: BullMQ ignores an add when a job with that id already exists. It is not a complete answer on its own, because once a job is removed from Redis the same id can be added again. Pair it with an idempotency key at the provider, so a retried job still cannot double-send.&lt;/p&gt;

&lt;h3&gt;
  
  
  Does BullMQ have a built-in dead-letter queue?
&lt;/h3&gt;

&lt;p&gt;No. BullMQ keeps terminally failed jobs in a failed set, and that set works as a basic dead-letter store. An explicit dead-letter queue is still worth building when you want separate retention, separate alerting, and a clean replay path. The pattern is a QueueEvents listener on the &lt;code&gt;failed&lt;/code&gt; event that copies the job into a second queue no worker consumes.&lt;/p&gt;

&lt;h3&gt;
  
  
  What does maxRetriesPerRequest: null do, and do I need it?
&lt;/h3&gt;

&lt;p&gt;Workers wait on Redis with blocking commands, and the ioredis default aborts a command after a few retries. During a short Redis blip that abort kills the blocked wait. Setting &lt;code&gt;maxRetriesPerRequest: null&lt;/code&gt; in the connection options removes the cap, so the worker keeps waiting instead of failing on a transient disconnect. Yes, set it on every queue connection.&lt;/p&gt;

</description>
      <category>nestjs</category>
      <category>bullmq</category>
      <category>redis</category>
      <category>typescript</category>
    </item>
    <item>
      <title>GitLab's Unauthenticated RCE: Check, Patch and Verify in 10 Minutes</title>
      <dc:creator>MUHAMMAD ZAIN UL ABIDIN</dc:creator>
      <pubDate>Wed, 04 Nov 2026 05:48:25 +0000</pubDate>
      <link>https://dev.to/zforzain2000/gitlabs-unauthenticated-rce-check-patch-and-verify-in-10-minutes-8ng</link>
      <guid>https://dev.to/zforzain2000/gitlabs-unauthenticated-rce-check-patch-and-verify-in-10-minutes-8ng</guid>
      <description>&lt;p&gt;If you run GitLab yourself, this is a drop-everything patch, and the trap is thinking you already did it. GitLab shipped a CVSS 10 unauthenticated RCE fix on October 21, 2026, and the patch many teams applied nine days earlier does not cover it. This post gives you the version table, the exact upgrade sequence, and the verification step most teams skip.&lt;/p&gt;

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

&lt;p&gt;On October 21, 2026, GitLab shipped a critical patch release fixing CVE-2026-110264, an integer overflow in its regular expression compiler that GitLab rates CVSS 10.0. GitLab says that under certain conditions, a specially crafted regular expression could allow an unauthenticated user to execute arbitrary code on a self-managed GitLab server.&lt;/p&gt;

&lt;p&gt;The flaw sits in Onigmo, the regular expression engine Ruby ships, which GitLab bundles inside its own packages.&lt;/p&gt;

&lt;p&gt;The scope is what makes this one unusual. The affected range starts at GitLab 7.0, from 2014, and covers every self-managed CE and EE release below the fixed version for its branch. GitLab.com is already patched and needs no action. The researcher behind the report used the handle joaxcar and reported through GitLab's HackerOne program. GitLab keeps full technical details private for 90 days after a fix, so the public record right now is the advisory, the version table, and GitLab's two detection rules, not a full exploit write-up.&lt;/p&gt;

&lt;h2&gt;
  
  
  Am I affected? The version table
&lt;/h2&gt;

&lt;p&gt;If you run a self-managed GitLab CE or EE install below the fixed version for your branch, you are affected, whatever your deployment type. GitLab's advisory covers Linux packages, Helm charts, Docker images and source installs alike. GitLab.com and qualifying GitLab Dedicated tenants are the only installs that need no action, and I list their exact position below the table.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Your branch&lt;/th&gt;
&lt;th&gt;Fixed release&lt;/th&gt;
&lt;th&gt;What to do&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;19.5&lt;/td&gt;
&lt;td&gt;19.5.1&lt;/td&gt;
&lt;td&gt;Upgrade to 19.5.1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;19.4&lt;/td&gt;
&lt;td&gt;19.4.3&lt;/td&gt;
&lt;td&gt;Upgrade to 19.4.3&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;19.3&lt;/td&gt;
&lt;td&gt;19.3.6&lt;/td&gt;
&lt;td&gt;Upgrade to 19.3.6&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;19.2&lt;/td&gt;
&lt;td&gt;19.2.9&lt;/td&gt;
&lt;td&gt;Upgrade to 19.2.9&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;19.1&lt;/td&gt;
&lt;td&gt;19.1.9&lt;/td&gt;
&lt;td&gt;Upgrade to 19.1.9&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;19.0&lt;/td&gt;
&lt;td&gt;19.0.10&lt;/td&gt;
&lt;td&gt;Upgrade to 19.0.10&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;18.11&lt;/td&gt;
&lt;td&gt;18.11.13&lt;/td&gt;
&lt;td&gt;Upgrade to 18.11.13&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Older than 18.11&lt;/td&gt;
&lt;td&gt;No backport in this release&lt;/td&gt;
&lt;td&gt;Upgrade to a supported line at or above 18.11.13&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;One caveat on the older backports, and it matters if you are on 18.11 through 19.2. Those backports cover this critical flaw, but they do not necessarily fix every other issue in the October 21 advisory. The other fixes in that advisory are listed for 19.3.6, 19.4.3 and 19.5.1. So a backport gets you out of the CVSS 10 hole, and a move to a current branch gets you the rest of the advisory too. Plan the second step even if the first step is what you ship today.&lt;/p&gt;

&lt;p&gt;GitLab Dedicated tenants on 19.3.5-internal0, 19.4.2-internal0 or later already include the fixes. If you are unsure which side of that line you sit on, your GitLab contact or your Dedicated version page answers it faster than guessing from a blog post, including this one.&lt;/p&gt;

&lt;h2&gt;
  
  
  The October 12 trap: why your last patch does not cover this
&lt;/h2&gt;

&lt;p&gt;The October 12 patch fixed a different flaw, so an install patched that day is still exposed to the October 21 flaw. CVE-2026-97229, fixed October 12 in 19.4.2, 19.3.5 and 19.2.8, is an integer overflow in the CI/CD regular expression path. CVE-2026-110264 is separate.&lt;/p&gt;

&lt;p&gt;Same engine family, different flaw, different fix. GitLab's detection notes place the October 21 flaw in Org-mode file rendering, a different path from the CI/CD path.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;CVE-2026-97229&lt;/th&gt;
&lt;th&gt;CVE-2026-110264&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Fixed&lt;/td&gt;
&lt;td&gt;October 12, 2026&lt;/td&gt;
&lt;td&gt;October 21, 2026&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Fixed versions&lt;/td&gt;
&lt;td&gt;19.4.2, 19.3.5, 19.2.8&lt;/td&gt;
&lt;td&gt;19.5.1, 19.4.3, 19.3.6, 19.2.9, 19.1.9, 19.0.10, 18.11.13&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;CVSS&lt;/td&gt;
&lt;td&gt;10.0&lt;/td&gt;
&lt;td&gt;10.0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;CISA KEV status at October 30&lt;/td&gt;
&lt;td&gt;Added October 29, 2026, federal due date November 1&lt;/td&gt;
&lt;td&gt;Not listed&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;If you hold this version&lt;/td&gt;
&lt;td&gt;19.4.2, 19.3.5 or 19.2.8&lt;/td&gt;
&lt;td&gt;You are patched for the first flaw only&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;That last row is the whole point of this section. I have seen teams close a ticket on October 12 and move on, and that ticket is not this ticket. Check your version against the October 21 table, not against your memory of patching recently. The exploitation picture also splits the same way: the sibling flaw reached CISA's Known Exploited Vulnerabilities catalog on October 29, with Alpine Linux reporting probing attempts against its instance on October 20 and 21, while CVE-2026-110264 itself had no reported exploitation and no KEV listing at the October 30 checks. A quiet status for one flaw is not cover for the other, and it is not a reason to wait on either.&lt;/p&gt;

&lt;h2&gt;
  
  
  Check your version in 30 seconds
&lt;/h2&gt;

&lt;p&gt;You need one number before you touch anything: the exact version your server is actually running, not the version you think you installed. The admin view and the command line agree with each other, so use whichever you can reach, and write the number down before you start the upgrade.&lt;/p&gt;

&lt;p&gt;&lt;br&gt;
&lt;br&gt;
Sign in as an admin and open Help, then the version page. The version string there, such as 19.4.2, is your branch and patch level in one read. If you cannot sign in, the command line step below gives you the same number.&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
With shell access, run &lt;code&gt;sudo gitlab-rake gitlab:env:info&lt;/code&gt; and read the GitLab version line. On a Linux package install, &lt;code&gt;apt-cache policy gitlab-ee&lt;/code&gt; (or &lt;code&gt;gitlab-ce&lt;/code&gt; for Community Edition) shows the installed version alongside the versions your package source offers.&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Match the first two numbers of your version to the table above. If your version is at or above the fixed release for that branch, you are patched for CVE-2026-110264. If it is below, you are affected, even if you patched on October 12.&lt;br&gt;
&lt;br&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Patch it: the 10-minute sequence
&lt;/h2&gt;

&lt;p&gt;Patching GitLab is an ordinary package upgrade on your own branch, not a major version jump, and that is good news for a same-day fix. The sequence below is the one I would run on a single-node Omnibus install, and the order matters: backup first, upgrade path second, install third, verify fourth.&lt;/p&gt;

&lt;p&gt;&lt;br&gt;
&lt;br&gt;
Run &lt;code&gt;sudo gitlab-backup create&lt;/code&gt; and confirm the backup file exists before you upgrade. A patch release can carry database migrations, and a backup is the only clean way back if the upgrade goes wrong. Do not skip this because the jump is small.&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
If you are moving within your branch to the fixed release, the jump is direct. If you are coming from an older line, check GitLab's upgrade paths documentation first, because GitLab defines required upgrade stops that must not be skipped. A skipped stop is how a routine patch turns into a broken install.&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
On Ubuntu or Debian with the Omnibus package, update your package lists and install the exact fixed version for your branch. For Enterprise Edition on 19.5, that looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;apt-get update
&lt;span class="nb"&gt;sudo &lt;/span&gt;apt-get &lt;span class="nb"&gt;install &lt;/span&gt;gitlab-ee&lt;span class="o"&gt;=&lt;/span&gt;19.5.1-ee.0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Swap the package name to &lt;code&gt;gitlab-ce&lt;/code&gt; for Community Edition and the version to your branch's fixed release, keeping the same &lt;code&gt;-ee.0&lt;/code&gt; or &lt;code&gt;-ce.0&lt;/code&gt; package suffix pattern for your edition. The package install runs &lt;code&gt;gitlab-ctl reconfigure&lt;/code&gt; at the end, which applies migrations and restarts services. Docker and Helm installs follow the same version target through their own image tag or chart upgrade, so the table above still decides the version even when the command differs.&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Run &lt;code&gt;sudo gitlab-ctl status&lt;/code&gt; and confirm the services show as running. Read the reconfigure output for migration errors instead of assuming a clean exit code means a clean database. If a service fails to start, stop and read the error before retrying; a second install over a half-migrated database makes diagnosis harder, not faster.&lt;br&gt;
&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Source and custom-image installs carry one extra step that Omnibus installs do not. The flaw lives in Ruby's Onigmo engine, so a GitLab you built yourself needs a Ruby that carries the underlying fix: Ruby 4.0.9, 3.4.12 or 3.3.13, per Ruby's October 23 notice, which maps four GitLab CVEs including this one to the engine fixes. Omnibus users get their Ruby through the GitLab package itself, so the package upgrade is the Ruby fix for them. If you run your own Ruby, patching GitLab without patching Ruby leaves the engine flaw in place.&lt;/p&gt;

&lt;h2&gt;
  
  
  Verify the patch actually took
&lt;/h2&gt;

&lt;p&gt;An upgrade that nobody verifies is a ticket closed on hope, and I have watched that exact mistake ship more than once. Verification takes under a minute, and it catches the two most common failures: the package installed but the old services never restarted, and the install landed on the wrong branch version.&lt;/p&gt;

&lt;p&gt;&lt;br&gt;
&lt;br&gt;
Repeat the version check from the earlier section after the upgrade, in the web interface or with &lt;code&gt;sudo gitlab-rake gitlab:env:info&lt;/code&gt;. The number must now read your branch's fixed release or higher. If it still reads the old version, the upgrade did not take, whatever the package manager said.&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Run &lt;code&gt;sudo gitlab-ctl status&lt;/code&gt; and confirm the services are running, then sign in and open a real project page. A login page that renders is not proof; a project page that loads after sign-in shows the application stack is actually up.&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Write the new version, the branch target, and today's date into your change log or ticket. The next time a GitLab advisory lands, that one line is what stops the October 12 trap from happening again, because you will compare against a written version instead of a memory.&lt;br&gt;
&lt;br&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Were you already probed? What to check in logs
&lt;/h2&gt;

&lt;p&gt;GitLab published two threat detections for self-managed customers looking for exploitation attempts against this flaw: "Onigmo native worker crash" and "Org mode render error burst". Those names are your search terms in your logs and monitoring, and they are worth checking even though no exploitation of this flaw had been reported at the October 30 checks.&lt;/p&gt;

&lt;p&gt;A crash or an error burst on its own does not prove a successful attack, and GitLab's own guidance is explicit on that point. A worker can crash for ordinary reasons, and a render error burst can come from a malformed file with no attacker behind it. What you are looking for is a pattern: repeated worker crashes or render error bursts clustered in time, especially against public projects, followed by anything unusual in the same window, such as unexpected processes, changed files, or odd outbound connections.&lt;/p&gt;

&lt;p&gt;If that pattern shows up, preserve the logs before you restart anything, finish the patch, and then treat secrets as exposed until you have reason not to. Rotate the credentials your GitLab server can read: deploy tokens, runner tokens, integration secrets, and any credentials stored in CI variables that a compromised server could reach. That rotation is the expensive part of the response, which is why the cheap part, patching before an incident, is worth doing today. If you maintain client GitLab installs, this whole check is billable, legitimate security work, and it is the same kind of emergency audit I take on through my &lt;a href="https://dev.to/#contact"&gt;contact section&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  If you cannot patch today
&lt;/h2&gt;

&lt;p&gt;GitLab published no workaround for CVE-2026-110264, so I am not going to invent one for you. Because the exact preconditions are undisclosed, no configuration change rules an instance out, and a web filter or a private-projects-only policy is not a substitute for the patch.&lt;/p&gt;

&lt;p&gt;That honesty is the value of this section: a stopgap that sounds reassuring but is not verified would leave you exposed while feeling safe, which is worse than knowing you are exposed.&lt;/p&gt;

&lt;p&gt;What you can do is shrink the window honestly. Put the patch in the first maintenance slot you can take, internet-facing instances first, and keep the detection names from the previous section in your monitoring until it lands. If the instance cannot go down at all, say so in your incident log, name the exposure, and do not let the ticket sit without a date. A patch with a date is a plan. A patch with no date is a hope, and hope is not a control.&lt;/p&gt;

&lt;p&gt;The same discipline applies to the sibling flaw. If you are still on a version that predates the October 12 fix, you are carrying a flaw that is already in CISA's exploited catalog, and that one jumps ahead of everything else in this post. Patch to the October 21 version for your branch and you cover both flaws in one upgrade, which is one more reason to use the October 21 table as your target instead of patching to October 12 and stopping.&lt;/p&gt;

&lt;h2&gt;
  
  
  My verdict: what I would do, in order
&lt;/h2&gt;

&lt;p&gt;If you run one self-managed GitLab instance, check the version now and patch it today, internet-facing instances first. Do not wait for a KEV listing, a public exploit, or a news cycle to tell you this one matters. A CVSS 10 rating from the vendor is already the full case for patching.&lt;/p&gt;

&lt;p&gt;The October 12 confusion is the only reason this post needed to exist, so check against the October 21 table, verify the running version after the upgrade, and write the new version down.&lt;/p&gt;

&lt;p&gt;If you run GitLab for clients, this week is a real engagement: a version audit across every install, the October 21 upgrade on each one, and a log check against GitLab's two detection names. It is the same emergency-response shape as my &lt;a href="https://dev.to/blog/php2shell-php-rce-fix"&gt;php2shell fix&lt;/a&gt;, where the same October 21 disclosure day produced a second critical patch, and the same rule applied: check, patch, verify, in that order, with no step skipped because the last patch felt recent.&lt;/p&gt;

&lt;h2&gt;
  
  
  FAQ
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What is CVE-2026-110264?
&lt;/h3&gt;

&lt;p&gt;CVE-2026-110264 is an integer overflow in GitLab's regular expression compiler, rated CVSS 10.0 by GitLab. Under certain conditions, GitLab says it could allow an unauthenticated user to execute arbitrary code on a self-managed GitLab server. It affects GitLab CE and EE from version 7.0 up to the fixed releases.&lt;/p&gt;

&lt;h3&gt;
  
  
  Which GitLab versions fix CVE-2026-110264?
&lt;/h3&gt;

&lt;p&gt;The fixed releases from October 21, 2026 are 19.5.1, 19.4.3, 19.3.6, 19.2.9, 19.1.9, 19.0.10 and 18.11.13. Your branch decides your target: a 19.4 install goes to 19.4.3, a 19.5 install goes to 19.5.1, and so on. Anything below your branch's fixed version is still affected.&lt;/p&gt;

&lt;h3&gt;
  
  
  I patched GitLab on October 12. Am I covered?
&lt;/h3&gt;

&lt;p&gt;No. The October 12 release fixed a different flaw, CVE-2026-97229, in the CI/CD regular expression path. The October 21 flaw, CVE-2026-110264, is separate. An install on 19.4.2, 19.3.5 or 19.2.8 from October 12 sits below the October 21 fixed version for its branch and needs one more upgrade.&lt;/p&gt;

&lt;h3&gt;
  
  
  Is GitLab.com affected by CVE-2026-110264?
&lt;/h3&gt;

&lt;p&gt;No action is needed on GitLab.com. GitLab says it already runs the patched version. GitLab Dedicated tenants on 19.3.5-internal0, 19.4.2-internal0 or later also include the fixes. This post is for self-managed CE and EE installs only.&lt;/p&gt;

&lt;h3&gt;
  
  
  Is there a workaround if I cannot patch today?
&lt;/h3&gt;

&lt;p&gt;GitLab published no workaround for CVE-2026-110264. Because the exact preconditions are undisclosed, no configuration change rules an instance out. Patching to your branch's fixed release is the only certain protection, so treat any delay as time spent exposed and patch at the first window you can take.&lt;/p&gt;

&lt;h3&gt;
  
  
  Is CVE-2026-110264 being exploited in the wild?
&lt;/h3&gt;

&lt;p&gt;As of the October 30 checks I cite below, GitLab had not reported exploitation of CVE-2026-110264 and it was not in CISA's Known Exploited Vulnerabilities catalog. Its sibling flaw CVE-2026-97229 is a different story: CISA added it to the catalog on October 29, 2026. Do not read the quiet status of one flaw as safety for the other.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;GitLab, "GitLab Critical Patch Release: 19.5.1, 19.4.3, 19.3.6, 19.2.9, 19.1.9, 19.0.10, 18.11.13", October 21, 2026: &lt;a href="https://docs.gitlab.com/releases/patches/patch-release-gitlab-19-5-1-released/" rel="noopener noreferrer"&gt;https://docs.gitlab.com/releases/patches/patch-release-gitlab-19-5-1-released/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;GitLab, Upgrade paths documentation: &lt;a href="https://docs.gitlab.com/update/upgrade_paths/" rel="noopener noreferrer"&gt;https://docs.gitlab.com/update/upgrade_paths/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Ostorlab, "GitLab Unauthenticated RCE Across 12 Years (CVE-2026-110264)": &lt;a href="https://blog.ostorlab.co/gitlab-unauthenticated-rce-cve-2026-110264.html" rel="noopener noreferrer"&gt;https://blog.ostorlab.co/gitlab-unauthenticated-rce-cve-2026-110264.html&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Rescana, "GitLab CVE-2026-110264: Critical Unauthenticated RCE in Self-Managed GitLab CE/EE": &lt;a href="https://www.rescana.com/post/gitlab-cve-2026-110264-unauthenticated-rce-october-2026" rel="noopener noreferrer"&gt;https://www.rescana.com/post/gitlab-cve-2026-110264-unauthenticated-rce-october-2026&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;ZeroHunt, "GitLab CVE-2026-110264: CVSS 10 unauthenticated RCE through a rendered .org file": &lt;a href="https://zerohunt.ai/blog/gitlab-cve-2026-110264-org-mode-regex-unauth-rce/" rel="noopener noreferrer"&gt;https://zerohunt.ai/blog/gitlab-cve-2026-110264-org-mode-regex-unauth-rce/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Brandefense, "GitLab Critical Vulnerability: Am I Affected?": &lt;a href="https://brandefense.io/security-news/gitlab-cve-2026-110264-critical-rce-what-to-do/" rel="noopener noreferrer"&gt;https://brandefense.io/security-news/gitlab-cve-2026-110264-critical-rce-what-to-do/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;ITDef, "GitLab CVE-2026-110264, CVE-2026-97229: How to Upgrade": &lt;a href="https://itdef.net/en/learn/gitlab-critical-patch-cve-2026-110264" rel="noopener noreferrer"&gt;https://itdef.net/en/learn/gitlab-critical-patch-cve-2026-110264&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>gitlab</category>
      <category>security</category>
      <category>devops</category>
    </item>
    <item>
      <title>Scaling Socket.io Rooms in NestJS With the Redis Adapter</title>
      <dc:creator>MUHAMMAD ZAIN UL ABIDIN</dc:creator>
      <pubDate>Tue, 03 Nov 2026 05:23:28 +0000</pubDate>
      <link>https://dev.to/zforzain2000/scaling-socketio-rooms-in-nestjs-with-the-redis-adapter-3pd4</link>
      <guid>https://dev.to/zforzain2000/scaling-socketio-rooms-in-nestjs-with-the-redis-adapter-3pd4</guid>
      <description>&lt;p&gt;I have watched the same failure play out in real-time features more than once. One server, one room, everything works. You add a second instance behind a load balancer to handle more connections, and half your users stop receiving messages. Nobody changed the room code. Nobody changed the gateway. The system just got bigger, and the thing that worked at one instance quietly stopped working at two.&lt;/p&gt;

&lt;p&gt;The cause is simple, and that is what makes it dangerous. Socket.io rooms live in memory by default. Instance A knows its own sockets. Instance B knows its own sockets. Neither knows the other's. When instance A broadcasts to a room, it only talks to the sockets it can see.&lt;/p&gt;

&lt;p&gt;This post is the fix I reach for in NestJS: the Redis adapter, the sticky-session rule that makes it actually work, and a room pattern that stops you from rebuilding the same in-memory trap in a new place. It is written for a NestJS app that already has a working gateway on one instance and now needs a second one.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why a second instance breaks your rooms
&lt;/h2&gt;

&lt;p&gt;A single Socket.io server holds every room, every socket, and every membership in its own process memory, so a broadcast to a room is a local loop over local sockets. Add a second instance and that loop still only covers local sockets, which means a message sent from instance A never reaches a room member connected to instance B. Nothing errors, no exception is thrown, and the sender's own test client often passes because it happens to be connected to the sending instance.&lt;/p&gt;

&lt;p&gt;That silent half-delivery is the signature of this bug. If your chat, live feed, or collaboration room works for some users and not others, and the split lines up with which instance they landed on, you are almost certainly looking at in-memory rooms behind a load balancer rather than a bug in your room logic.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Redis adapter, in one picture
&lt;/h2&gt;

&lt;p&gt;The Redis adapter replaces the in-memory room store with a shared channel that every instance listens to, so a broadcast becomes a publish step plus a local delivery step on each instance. When instance A emits to a room, it publishes that event to Redis; instance B receives it through its subscription and delivers it to its own local sockets in that room, and instance A does the same for its locals. Your gateway code does not change shape: &lt;code&gt;server.to(room).emit(...)&lt;/code&gt; keeps working, and the adapter changes where that call reaches.&lt;/p&gt;

&lt;p&gt;Two details in that picture matter for everything that follows. First, the adapter moves messages between servers, but it does not move your application state between servers. A map you built in instance A's memory is still invisible to instance B. Second, Redis becomes part of your real-time path, so a Redis outage is now a real-time outage unless you have planned for it. I cover both failure modes below, because the adapter solves the broadcast problem and creates two smaller problems you need to handle on purpose.&lt;/p&gt;

&lt;h2&gt;
  
  
  Wire it in NestJS
&lt;/h2&gt;

&lt;p&gt;Start from the documented NestJS shape and do not improvise the wiring. Install the three packages, extend &lt;code&gt;IoAdapter&lt;/code&gt;, connect two Redis clients, and register the adapter before the app starts listening. The two-client split is not optional decoration: a Redis connection in subscriber mode cannot issue regular commands, so the publishing client and the subscribing client must be separate connections, and &lt;code&gt;duplicate()&lt;/code&gt; gives the subscriber the same configuration on its own connection.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install &lt;/span&gt;redis socket.io @socket.io/redis-adapter
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// redis-io.adapter.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;IoAdapter&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@nestjs/platform-socket.io&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;ServerOptions&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;socket.io&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;createAdapter&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@socket.io/redis-adapter&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;createClient&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;redis&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;RedisIoAdapter&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;IoAdapter&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;adapterConstructor&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;ReturnType&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;createAdapter&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nf"&gt;connectToRedis&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;void&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;pubClient&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createClient&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;REDIS_URL&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;subClient&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;pubClient&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;duplicate&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;all&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="nx"&gt;pubClient&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="nx"&gt;subClient&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;()]);&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;adapterConstructor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createAdapter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pubClient&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;subClient&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;createIOServer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;port&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="nx"&gt;ServerOptions&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;server&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createIOServer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;port&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;options&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="nf"&gt;adapter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;adapterConstructor&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;server&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// main.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;NestFactory&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@nestjs/core&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;AppModule&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./app.module&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;RedisIoAdapter&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./redis-io.adapter&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;bootstrap&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;NestFactory&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;AppModule&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;redisIoAdapter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;RedisIoAdapter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;redisIoAdapter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connectToRedis&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;useWebSocketAdapter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;redisIoAdapter&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nf"&gt;bootstrap&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Three things in that code deserve a second look before you ship it. Connect both clients before the app listens, or early broadcasts can run before the adapter is ready. Read the Redis URL from the environment, never hardcode &lt;code&gt;localhost&lt;/code&gt;, or your staging and production setups will diverge in exactly the way that wastes a debugging afternoon. And keep the adapter construction in one place: every gateway in the app shares this adapter through &lt;code&gt;useWebSocketAdapter&lt;/code&gt;, so a second copy of the wiring in another module is a second chance to drift. This is the same production NestJS stack I describe on my &lt;a href="https://dev.to/blog/hire-senior-nextjs-nestjs-developer-pakistan"&gt;hiring page&lt;/a&gt;, where Socket.io and BullMQ on Redis are the real-time and background pair I ship with.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sticky sessions: the step everyone skips
&lt;/h2&gt;

&lt;p&gt;The Redis adapter moves broadcasts between instances, but it does not fix the handshake, and this is the step that sinks most first attempts at scaling Socket.io. A Socket.io client starts with HTTP long polling and then upgrades to a WebSocket connection, and those polling requests must reach the same instance that owns the session. If your load balancer round-robins a polling request to a different instance, that instance does not know the session, and the client gets errors and reconnect loops instead of a working socket.&lt;/p&gt;

&lt;p&gt;The NestJS docs state the rule plainly: with multiple load-balanced instances you must either disable polling by setting &lt;code&gt;transports: ['websocket']&lt;/code&gt; in the client configuration, or enable cookie-based routing (sticky sessions) in your load balancer, because Redis alone is not enough. Pick one deliberately:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Approach&lt;/th&gt;
&lt;th&gt;What it costs you&lt;/th&gt;
&lt;th&gt;When I pick it&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;WebSocket-only transport&lt;/td&gt;
&lt;td&gt;You lose the polling fallback, so clients behind strict proxies that block WebSocket upgrades cannot connect at all&lt;/td&gt;
&lt;td&gt;Internal tools, controlled clients, or a first scaled deployment where you can measure how many clients actually need polling&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Cookie-based sticky sessions&lt;/td&gt;
&lt;td&gt;Your load balancer must support it and you must test it, since a misconfigured cookie rule fails exactly like no rule at all&lt;/td&gt;
&lt;td&gt;Any public-facing app, because it keeps the polling fallback working while still pinning a session to one instance&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Do not substitute IP-based hashing and call it done without thinking. Users behind corporate NAT, mobile networks, and shared Wi-Fi can share an IP or change it mid-session, and IP hashing pins them by an address that is not really theirs. A cookie set by the load balancer tracks the session, not the network path, and that is the property you actually need. Whichever option you pick, test it with polling enabled and with two instances running, because a sticky rule that only works in a single-instance test is not a sticky rule.&lt;/p&gt;

&lt;h2&gt;
  
  
  Address users and tenants with rooms, not in-memory maps
&lt;/h2&gt;

&lt;p&gt;Once broadcasts cross instances, the next trap is addressing. The question stops being "how do I emit to a room" and becomes "how do I reach this one user, or this one tenant's room, when I have no idea which instance holds their socket". The answer is to stop asking which instance holds them. Address the room, and let the adapter find the instance.&lt;/p&gt;

&lt;p&gt;The pattern I use has two rules. First, every socket joins a room named for its user immediately after authentication, before it joins anything else. Second, every shared room is named for the thing it belongs to, not for a server or a process, so a tenant room looks like &lt;code&gt;tenant:9:project:41&lt;/code&gt; and a user room looks like &lt;code&gt;user:42&lt;/code&gt;. A service that needs to notify one user then emits to &lt;code&gt;user:42&lt;/code&gt; from any instance, and the adapter delivers it to whichever instance holds that socket.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// rooms.gateway.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;WebSocketGateway&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;WebSocketServer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;SubscribeMessage&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;OnGatewayConnection&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;MessageBody&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;ConnectedSocket&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@nestjs/websockets&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&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="nx"&gt;Socket&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;socket.io&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;WebSocketGateway&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;cors&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;origin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;CLIENT_ORIGIN&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;RoomsGateway&lt;/span&gt; &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nx"&gt;OnGatewayConnection&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;WebSocketServer&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="nx"&gt;Server&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nf"&gt;handleConnection&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Socket&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Authenticate first. Only join rooms for a verified user id.&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;userId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;userId&lt;/span&gt; &lt;span class="kd"&gt;as &lt;/span&gt;&lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`user:&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;SubscribeMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;join-project&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nf"&gt;onJoinProject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;MessageBody&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="nx"&gt;projectId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;ConnectedSocket&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Socket&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Check membership in your database here, then join.&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`project:&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;projectId&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;joined&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;projectId&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nf"&gt;notifyProject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;projectId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;unknown&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Callable from any instance. The adapter routes it.&lt;/span&gt;
    &lt;span class="k"&gt;this&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="nf"&gt;to&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`project:&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;projectId&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;emit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;project-updated&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That gateway sketch leaves two things deliberately out, and both are worth naming. Authentication is shown as a placeholder because token validation belongs in your own guard or handshake check, not in a copy-pasted snippet, and a socket that joins a room before authentication is a socket that can listen to a room it should never see. Membership checks are also a placeholder: joining &lt;code&gt;project:41&lt;/code&gt; must be gated on a database check that this user belongs to project 41, because a room name is not a permission. Validate the join payload before trusting it too; I use Zod at every boundary, as I wrote up in &lt;a href="https://dev.to/blog/zod-as-a-contract"&gt;Zod as a contract&lt;/a&gt;. The adapter will faithfully deliver messages to anyone in the room, so the join step is where your access control lives.&lt;/p&gt;

&lt;p&gt;One more habit pays off here. When you need to know who is actually connected, use the adapter's own tools, such as &lt;code&gt;fetchSockets&lt;/code&gt;, instead of a local map. A local map answers "who is connected to this instance". The adapter answers "who is connected to this system", and at two or more instances only the second question is useful.&lt;/p&gt;

&lt;h2&gt;
  
  
  Pub/sub vs Redis Streams: my pick
&lt;/h2&gt;

&lt;p&gt;Socket.io now ships two Redis-backed adapters, and the choice between them is a real decision rather than a version detail. The classic adapter forwards packets through Redis pub/sub. The newer Redis Streams adapter forwards them through a Redis stream, and its docs give it one clear advantage: it handles a temporary disconnection from Redis and resumes the stream without losing packets, where the classic pub/sub adapter does not offer that resume behavior. Both still need sticky sessions, and the Streams docs say so directly, so the new adapter does not remove the handshake work from the previous section.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Adapter&lt;/th&gt;
&lt;th&gt;How messages move&lt;/th&gt;
&lt;th&gt;Disconnect behavior&lt;/th&gt;
&lt;th&gt;NestJS wiring&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Classic &lt;code&gt;@socket.io/redis-adapter&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Redis pub/sub, two clients&lt;/td&gt;
&lt;td&gt;A gap during a Redis outage can lose packets&lt;/td&gt;
&lt;td&gt;Documented in the NestJS adapter guide, shown above&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;@socket.io/redis-streams-adapter&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;One Redis stream, one client&lt;/td&gt;
&lt;td&gt;Resumes the stream after a temporary disconnect without losing packets&lt;/td&gt;
&lt;td&gt;Custom wiring through the same &lt;code&gt;IoAdapter&lt;/code&gt; extension point, not the documented NestJS example&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;My pick is boring on purpose: start with the classic adapter from the NestJS docs. It is the documented path, it matches the examples other NestJS developers will recognize when they read your code, and its failure mode is one you can plan around with reconnect handling and client-side resync. Move to the Streams adapter when temporary Redis disconnects are a real problem in your deployment, not a theoretical one, and when you have time to verify the custom wiring against your own gateway tests. A newer adapter is not automatically the right adapter for a system that is already working.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prove it with two instances on your machine
&lt;/h2&gt;

&lt;p&gt;A scaling change that has never been observed across two instances is a guess, and this one is cheap to observe. Run Redis locally, start your NestJS app twice on two ports, connect one client to each instance, and then trigger a broadcast from a service running on only one of them. If both clients receive it, the adapter is working. If only the client on the sending instance receives it, check the adapter registration first, then the Redis connection, then the room name, in that order, because those three cover most wiring mistakes.&lt;/p&gt;

&lt;p&gt;Make the test adversarial in one specific way: connect with polling enabled, not websocket-only, and watch what happens when the load balancer is in the path. A setup that passes with a direct connection to one instance and fails behind the balancer has a sticky-session problem, not an adapter problem, and no amount of adapter debugging will fix it. The same test with websocket-only transport passing and polling failing points at the same place. Name the failure before you fix it, or you will fix the wrong layer twice.&lt;/p&gt;

&lt;p&gt;Also verify the negative case. Disconnect a client, then broadcast to its room, and confirm nothing crashes and no stale delivery appears when it reconnects. Room membership is tied to the socket, not the user, so a reconnected client must rejoin its rooms after authentication. If your client code assumes rooms survive a reconnect, your users will silently stop receiving messages after every network drop, and that failure looks exactly like the original scaling bug.&lt;/p&gt;

&lt;h2&gt;
  
  
  When Redis is down, and other failure modes
&lt;/h2&gt;

&lt;p&gt;Putting Redis in the middle of your real-time path changes what an outage means, so plan for it before it happens. The classic adapter has no stream to resume from, so messages published while Redis is unreachable do not wait politely for it to come back. Your clients need a resync path for exactly this case: on reconnect, the client asks for current state over HTTP or replays its room joins, and the server answers with the latest state rather than assuming the client saw every event. That resync endpoint is not extra polish. It is the thing that turns a Redis blip into a brief catch-up instead of a permanently stale screen.&lt;/p&gt;

&lt;p&gt;Three more failure modes deserve a line each. First, keep payloads small. Every broadcast is published to Redis and then fanned out to every instance, so a large payload is multiplied by your instance count before it reaches a single client. Send ids and small deltas, and let clients fetch full objects over HTTP when they need them. Second, treat Redis as trusted internal infrastructure. The Socket.io docs warn that adapter messages are not signed or encrypted by the adapter, so do not expose Redis to untrusted networks, use authentication and TLS where your setup supports them, and never put long-lived secrets in event payloads. Third, watch the fan-out itself: a room with thousands of members across many instances turns one emit into thousands of deliveries, and that is a product decision as much as an infrastructure one. If a room grows that large, split it by topic or shard it by tenant before you scale the instance count to compensate.&lt;/p&gt;

&lt;h2&gt;
  
  
  The tools I would actually use
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Tool&lt;/th&gt;
&lt;th&gt;What it is for&lt;/th&gt;
&lt;th&gt;Note&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Redis&lt;/td&gt;
&lt;td&gt;The shared channel between Socket.io instances&lt;/td&gt;
&lt;td&gt;Run it as managed infrastructure in production, not as a container you forget about on a single host&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;@socket.io/redis-adapter&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The documented NestJS scaling path&lt;/td&gt;
&lt;td&gt;The two-client setup above; keep its version aligned with your Socket.io version&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;@socket.io/redis-streams-adapter&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The disconnect-resilient alternative&lt;/td&gt;
&lt;td&gt;Evaluate it when Redis gaps become a measured problem, not before&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;A load balancer with cookie stickiness&lt;/td&gt;
&lt;td&gt;Pinning polling sessions to one instance&lt;/td&gt;
&lt;td&gt;Test it with polling enabled; IP hashing is a weaker substitute&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Docker Compose&lt;/td&gt;
&lt;td&gt;Running Redis plus two app instances locally&lt;/td&gt;
&lt;td&gt;The cheapest way to observe the two-instance behavior before deploying it&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;{/* MONETIZATION NOTE: the tools table above is affiliate-ready. Swap the Redis and load-balancer rows for affiliate links only when a real program is joined; no affiliate claim is made here. */}&lt;/p&gt;

&lt;h2&gt;
  
  
  My verdict: what I would pick, and why
&lt;/h2&gt;

&lt;p&gt;If your NestJS app has one instance today and needs two tomorrow, I would pick the classic Redis adapter from the NestJS docs, cookie-based sticky sessions at the load balancer, and room-per-user addressing from day one. That combination is documented, it is easy for the next developer to reason about, and it fixes the actual failure: broadcasts that only reach local sockets.&lt;/p&gt;

&lt;p&gt;I would not pick the Streams adapter first, even though its disconnect behavior is better, because the documented path matters more than the newer feature when you are changing a live real-time system. I would not pick websocket-only transport for a public app, because giving up the polling fallback trades one visible failure for a quieter one on restrictive networks. And I would not ship any of it without the two-instance test above, because a scaling setup you have not watched fail and recover is a setup you do not understand yet.&lt;/p&gt;

&lt;p&gt;The answer flips in two cases. If you already measure packet loss during Redis disconnects and users notice stale screens after every blip, the Streams adapter earns its custom wiring. If your clients are all internal and controlled, websocket-only transport is simpler than sticky sessions and the tradeoff disappears. Outside those cases, the boring documented combination is the right default, and boring is a compliment when the system in question is the one your users stare at all day.&lt;/p&gt;

&lt;p&gt;If you are building a multi-tenant real-time system and want a second pair of eyes on the room design before it ships, that is the kind of production NestJS work I do. &lt;a href="https://dev.to/#contact"&gt;Reach me through the contact section on the homepage&lt;/a&gt; with the room model, the instance count, and the failure you are seeing, and we can walk through it in a first call.&lt;/p&gt;

&lt;h2&gt;
  
  
  FAQ
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Why do Socket.io rooms break when I add a second NestJS instance?
&lt;/h3&gt;

&lt;p&gt;By default each Socket.io server keeps its rooms in its own memory. A broadcast from instance A only reaches sockets connected to A, so a user on instance B never sees it. The Redis adapter fixes this by forwarding broadcasts through Redis, so every instance delivers the message to its own local sockets in that room.&lt;/p&gt;

&lt;h3&gt;
  
  
  Do I still need sticky sessions if I use the Redis adapter?
&lt;/h3&gt;

&lt;p&gt;Yes. The NestJS docs warn about this directly: Redis alone is not enough. Socket.io starts with HTTP long polling and then upgrades to WebSocket, and the polling requests must reach the same instance that owns the session. Either force websocket-only transport or turn on cookie-based routing at your load balancer.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why does the Redis adapter need two Redis clients?
&lt;/h3&gt;

&lt;p&gt;A Redis connection in subscriber mode cannot issue regular commands, so one connection cannot both publish and subscribe. The standard setup creates a publishing client and then duplicates it for subscribing. That split is in the NestJS docs example and it is the shape to copy, not a detail to simplify away.&lt;/p&gt;

&lt;h3&gt;
  
  
  Should I use the classic Redis adapter or the Redis Streams adapter?
&lt;/h3&gt;

&lt;p&gt;Start with the classic Redis adapter if you are following the NestJS docs, because that is the documented NestJS path. Consider the Redis Streams adapter when temporary Redis disconnects matter to you, because it resumes the stream without losing packets where the classic pub/sub adapter does not. Either way you still need sticky sessions.&lt;/p&gt;

&lt;h3&gt;
  
  
  How do I send a message to one user when I do not know which instance they are on?
&lt;/h3&gt;

&lt;p&gt;Put every socket in a room named for its user, such as user:42, right after authentication. Then any instance can emit to that room and the adapter routes it to the instance holding that socket. Never keep a socketId to userId map in one instance's memory and expect other instances to read it.&lt;/p&gt;

&lt;h3&gt;
  
  
  What breaks first when a Redis-backed Socket.io setup grows?
&lt;/h3&gt;

&lt;p&gt;Usually one of three things: the load balancer sends polling requests to the wrong instance, one instance holds state the others cannot see, or a broadcast payload grows until Redis fan-out becomes the bottleneck. Fix them in that order: sticky sessions first, room-based addressing second, and smaller payloads plus fewer broadcast rooms third.&lt;/p&gt;

</description>
      <category>nestjs</category>
      <category>redis</category>
      <category>websockets</category>
      <category>typescript</category>
    </item>
  </channel>
</rss>
