惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

The Last Watchdog
The Last Watchdog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
GbyAI
GbyAI
Y
Y Combinator Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
The GitHub Blog
The GitHub Blog
博客园_首页
小众软件
小众软件
I
InfoQ
J
Java Code Geeks
月光博客
月光博客
S
Secure Thoughts
Microsoft Security Blog
Microsoft Security Blog
V
Visual Studio Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Stack Overflow Blog
Stack Overflow Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
N
News and Events Feed by Topic
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
The Cloudflare Blog
T
Threat Research - Cisco Blogs
A
About on SuperTechFans
H
Help Net Security
MongoDB | Blog
MongoDB | Blog
博客园 - 聂微东
人人都是产品经理
人人都是产品经理
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Latest news
Latest news
G
GRAHAM CLULEY
IT之家
IT之家
C
Cisco Blogs
Last Week in AI
Last Week in AI
Engineering at Meta
Engineering at Meta
L
LangChain Blog
The Register - Security
The Register - Security
SecWiki News
SecWiki News
M
MIT News - Artificial intelligence
NISL@THU
NISL@THU
T
Tenable Blog
博客园 - Franky
美团技术团队
I
Intezer
U
Unit 42
雷峰网
雷峰网
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
S
SegmentFault 最新的问题
C
Cyber Attacks, Cyber Crime and Cyber Security

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant Common SOC 2 Failures (Real World) Stop Vibe-Checking Your AI App: A Practical Guide to Evals How to Use SonarQube and SonarScanner Locally to Level Up Your Code Quality Your Next To-Do App Is Dead — I Replaced Mine with an OpenClaw AI Sign a Nostr event in 60 lines of Python using coincurve — no nostr-sdk, no nbxplorer, no rust toolchain ITGC Audit Explained Like You’re in Big 4 Patch Tuesday abril 2026: Microsoft parcha 163 vulnerabilidades y un zero-day en SharePoint Stop scraping everything: a better way to track competitor price changes Listing on MCPize + the Official MCP Registry while routing payments OUTSIDE the marketplace — how I kept 100% of my x402 revenue Building an AI-Powered Risk Intelligence System Using Serverless Architecture Why We Ripped Function Overloading Out of Our AI Toolchain Testing AI-Generated Code: How to Actually Know If It Works SaaS Churn Is Killing Your Business. Here Is What to Do About It (Without a Support Team) The Speed of AI Is No Longer Linear - And Self-Improving Models Are Why How to Implement RBAC for MCP Tools: A Practical Guide for Engineering Teams From Standard Quote to Persuasive Proposal: AI Automation for Arborists I built a CLI that scaffolds complete multi-tenant SaaS apps Axios CVE-2025–62718: The Silent SSRF Bug That Could Be Hiding in Your Node.js App Right Now The dashboard that ended our friendship Data Pipelines Explained Simply (and How to Build Them with Python) The Hidden Cost of AI Systems Nobody Talks About. undefined vs undeclared, and how typeof behaves Switching from file-based jobs to NATS/Kafka in Rust without changing code io_uring Adventures: Rust Servers That Love Syscalls Why Agentic AI is Killing the Traditional Database The POUR principles of web accessibility for developers and designers Quantum Neural Network 3D — A Deep Dive into Interactive WebGL Visualization How To Install Caveman In Codex On macOS And Windows Automation Pipeline Reliability: Why Your Workflow Breaks When Nobody Is Watching I Built an 'Open World' AI Coding Agent — It Works From ANY Folder From Freelancing to Product: A Tech Service Company's SaaS Transformation China's AI Giants: Adding Tencent Hunyuan & ByteDance Doubao to AI University (74 Providers) On the Vibe Coders and Their Lies clerk: Auto-Summarize Your Claude Code Sessions AI Weekly — 2026/04/10–04/17 | The Model Lockdown Is Here, but the Toolchain Is the Real Battleground AI 週報 — 2026/04/10–2026/04/17 模型封鎖潮來了,但工具鏈才是真戰場 Maybe this is how Open-Source apps are born... 🚀 Fine-Tune LLMs with LoRA and QLoRA: 2026 Guide tRPC v11 + Next.js App Router: End-to-End Type Safety Without the Boilerplate ShadCN UI in 2026: Why I Stopped Installing Component Libraries and Started Owning My Components SaaS Billing in React Server Components: Stripe + Supabase Without a Single `useEffect` Join our DEV Weekend Challenge — $1,000 in Prizes Across TEN winners! Submissions Due April 20 at 6:59 AM UTC. Implementing FSRS Spaced Repetition in Flutter + Supabase — Adding Memory Science to an AI Learning App "I Texted My Localhost From the Train — Claude Code Fixed the Bug Before I Got Home" I Built a Sales Prep AI and It Went Deeper Than Expected Design to Code #2: One JSON, Eleven Outputs Solving the 100M-Row Problem: A Summary Table Pattern for High-Volume Push Notification Logs Flutter Web With Wasm: What Actually Changes For Developers I Built 50 Royalty-Free Soundtracks for My Side Project in a Weekend Using AI Music Generation The Vibe Coding Security Checklist: 7 Things to Check Before You Ship Stop Letting Googlebot Guess Fix Your React App's SEO Right Desconstruindo o Streaming do LinkedIn: Como Criar um Engine de Extração de Vídeo de Alta Performance com HLS e FFmpeg (EDA Part-1) EDA (Exploratory Data Analysis) Explained With Real Life — Why Looking at Your Data Is the Most Important Step in Machine Learning Brand Relationship Management at Scale: Our 4-Touch Outreach System for 200+ Brands Why String.fromEnvironment() Might Return an Empty String in Dart JGuardrails 1.0.0 — Hardening Java LLM Apps Against Jailbreaks, Toxicity, and Prompt Injection Plan and Schedule a Full Week of Threads Content From One Claude Conversation Coding Cat Oran Ep3, Five Tables Changed Everything Updated: BFF Pattern I'm done watching freelancers get buried by 200 proposals. So I'm building the alternative. This is my first post BFS Algorithm in Java Step by Step Tutorial with Examples Tracking LLM Pricing Monthly: An Open Dataset for 22 AI Models How We Measure Content ROI on a Comparison Site: Revenue Attribution Without Perfect Data Introducing Nova AI Ops: The AI-Native Operating System for SRE Teams I built a free desktop video downloader for Windows — Grabbit How Talkie OCR Helps Vision-Impaired & Dyslexic Users Read the World Around Them VRCFaceTracking安装和iPhone面捕配置教程,有bug Even CrowdStrike Can't See Your Agents The Automation Gold Rush: What n8n Workflows and Claude Are Opening Up for Developers Right Now
Your Agent Retried. The Email Sent Twice.
Siddhant Jai · 2026-04-25 · via DEV Community

A practical guide to idempotency, budget guardrails, and risk gates for TypeScript AI agents — with real code.


There's a class of production bug that doesn't throw an exception, doesn't show up in your error logs, and doesn't alert anyone. Your AI agent just quietly does the same thing twice.

The email sends twice. The Stripe charge fires twice. The database record duplicates. No stack trace. No crash. Just a confused user and a support ticket you don't want to explain.

This isn't a rare edge case. It's what happens when you take agent frameworks — tools designed for reliability through retries — and connect them to side effects that aren't designed for retries.

Let me show you exactly what goes wrong, and exactly what fixes it.


Why Every Agent Framework Retries

LangGraph, Vercel AI SDK, Mastra, and OpenAI Agents SDK all have the same design assumption baked in: LLM calls and tool calls can fail transiently, so the framework should retry them.

This is a reasonable assumption. Networks timeout. APIs rate-limit. A 30-second agent workflow failing at step 28 because of a 500ms blip would be maddening if you couldn't retry. LangGraph has a RetryPolicy object you pass directly to nodes. Vercel AI SDK defaults to maxRetries: 2 (3 total attempts). Mastra handles retries at the workflow runner level.

The problem is not that they retry. The problem is that they retry without knowing what your side effect already did.

The framework sees a timeout. It doesn't know whether your tool call completed before the timeout or during it. So it retries the whole thing. And if your tool calls resend.emails.send() or stripe.charges.create(), those run again — because nothing told them not to.

This is a well-documented issue. LangGraph's own GitHub discussions have open threads about tools being called repeatedly until hitting the recursion limit. An arXiv study surveying 12 major agent frameworks found that no framework enforces exactly-once execution at the tool-call level — they all delegate that responsibility to you.


The Real Cost of Getting This Wrong

In November 2025, a team's LangGraph research pipeline had four agents working together. Two of them — an Analyzer and a Verifier — got stuck ping-ponging requests to each other. No step limits. No cost ceiling. No alert. The loop ran for 11 days while the team assumed the growing API bill was organic growth. Final cost: $47,000.

That's an extreme case. But the everyday version of this is much more common: a user clicks "Submit" twice, the network is slow, your agent fires the tool twice, and a welcome email hits their inbox twice. Or a Stripe charge creates a duplicate. It's not catastrophic — it's just the kind of bug that erodes user trust and creates support burden.

The industry is starting to take this seriously. A LinkedIn post from an AI engineering lead put it clearly: "Most AI pipelines are one bad retry away from a silent infinite loop." The community thread on preventing duplicate tool execution has hundreds of engineers describing the exact same failure mode. OpenAI's own community forum flags it as a major issue for create operations.


What Doesn't Fix It

Before getting to the solution, it's worth naming the things that look like solutions but aren't:

Prompt engineering ("don't call the same tool twice") is not enforceable. Agents re-plan. They retry. They don't remember what they said in a previous prompt injection scenario.

Reducing maxRetries doesn't help either. Duplicate calls can happen on the first attempt if the response is slow or ambiguous. Fewer retries just means more failures, not fewer duplicates.

Client-side deduplication — tracking calls in memory on the agent side — breaks across crashes, timeouts, parallel subagents, and worker restarts.

The fix has to live at the tool boundary, outside the agent's reasoning loop, in something the agent cannot bypass.


The Fix: Application-Level Guardrails with @keelstack/guard

@keelstack/guard is a small TypeScript package — 180 lines, zero runtime dependencies — that wraps any async action with three production safety primitives. You don't configure anything to start. You just wrap the call.

Primitive 1: Idempotency

The core idea is simple: give every logical operation a stable key. The first time that key is seen, the action runs and the result is stored. If the same key comes in again — from a retry, a parallel agent, a workflow resume — the stored result is returned and the action is not run again.

import { guard } from '@keelstack/guard';

const result = await guard({
  key: `send-welcome:${userId}`,       // stable, unique per operation
  action: () => resend.emails.send({
    to: user.email,
    subject: 'Welcome to the app!',
  }),
});

console.log(result.status);     // 'executed' | 'replayed'
console.log(result.fromCache);  // false | true

Enter fullscreen mode Exit fullscreen mode

When the agent retries with the same key, result.status comes back as 'replayed'. The email service never receives a second call.

The key is doing the real work here. A good idempotency key is stable and scoped to the specific logical operation — stripe-charge:${invoiceId} is right, op-${Date.now()} is wrong because it changes on every retry. The README has a full guide on key construction .

By default, results are stored in-memory with a 24-hour TTL. For production deployments with multiple instances, you bring your own Redis-backed Ledger:

import type { Ledger, LedgerEntry } from '@keelstack/guard';
import { createClient } from 'redis';

const redis = createClient({ url: process.env.REDIS_URL });
await redis.connect();

const redisLedger: Ledger = {
  async get(key) {
    const raw = await redis.get(`guard:${key}`);
    return raw ? (JSON.parse(raw) as LedgerEntry) : undefined;
  },
  async set(key, entry) {
    const ttl = Math.max(0, Math.floor((entry.expiresAt - Date.now()) / 1000));
    await redis.set(`guard:${key}`, JSON.stringify(entry), { EX: ttl || undefined });
  },
  async delete(key) { await redis.del(`guard:${key}`); },
  async list() {
    const keys = await redis.keys('guard:*');
    const entries = await Promise.all(keys.map(k => redis.get(k)));
    return entries.flatMap(e => e ? [JSON.parse(e) as LedgerEntry] : []);
  },
  async prune() { return 0; },
};

const result = await guard({ key: 'my-op', action: myAction, ledger: redisLedger });

Enter fullscreen mode Exit fullscreen mode

A first-party @keelstack/guard-redis adapter is on the roadmap — but the interface is simple enough to wire yourself today .


Primitive 2: Budget Enforcement

This is the $47,000 problem solved at the tool level. You set a per-user spend limit. Before every action, the guard checks current spend against the limit. If the limit is hit, the action is blocked and returns status: 'blocked:budget' — the agent never makes the API call.

const result = await guard({
  key: `ai-call:${userId}:${requestId}`,
  action: () => openai.chat.completions.create({
    model: 'gpt-4o',
    messages: [{ role: 'user', content: prompt }],
  }),
  budget: {
    id: userId,
    limitUsd: 2.00,           // hard cap: $2 per user per day
    warnAt: [0.5, 0.8],       // warn callbacks at 50% and 80%
    onWarn: ({ percentUsed, id }) => {
      console.warn(`User ${id} at ${(percentUsed * 100).toFixed(0)}% of budget`);
    },
  },
  extractCost: (res) => {
    const tokens = res.usage?.total_tokens ?? 0;
    return (tokens / 1_000_000) * 15;   // gpt-4o pricing
  },
});

if (result.status === 'blocked:budget') {
  return Response.json({
    error: 'Daily AI budget exceeded',
    spent: result.budgetInfo?.spent,
    limit: result.budgetInfo?.limit,
  }, { status: 429 });
}

Enter fullscreen mode Exit fullscreen mode

The extractCost function is how you tell the guard what each call costs. It could be token-based, flat-fee, or any formula that makes sense for your API. The guard accumulates spend per id and enforces the hard stop before the next call fires .


Primitive 3: Risk Gate

Some actions shouldn't run without explicit intent — deleting a record, cancelling a subscription, sending a bulk communication. The risk gate classifies actions as safe, reversible, or irreversible, and applies a policy: allow, log, warn, or block.

const result = await guard({
  key: `delete-account:${userId}`,
  action: () => db.users.delete({ where: { id: userId } }),
  risk: {
    level: 'irreversible',
    policy: 'block',
    onRisk: (info) => {
      auditLog.write({
        key: info.key,
        level: info.level,
        blocked: info.blocked,
      });
    },
  },
});

if (result.status === 'blocked:risk') {
  return Response.json({ error: 'Action blocked by risk policy' }, { status: 403 });
}

Enter fullscreen mode Exit fullscreen mode

Default policies if you don't specify: safe → allow, reversible → log, irreversible → warn. The onRisk callback fires regardless of whether the action was blocked — useful for audit trails .


The Fourth Primitive: Failure Handling

FailureConfig controls what happens when an action throws. The default (policy: 'retry') rethrows the error and leaves the key uncached, so future attempts with the same key can try again. The compensate policy lets you run a cleanup callback before rethrowing — useful when you need to undo a partial side effect:

const result = await guard({
  key: `provision-resource:${tenantId}`,
  action: () => cloudProvider.createInstance(config),
  failure: {
    policy: 'compensate',
    onError: async ({ key, error }) => {
      await alertOpsTeam(`Failed to provision ${key}: ${error.message}`);
    },
  },
});

Enter fullscreen mode Exit fullscreen mode


Putting It All Together

These primitives compose. A single guard() call can enforce all three simultaneously:

const result = await guard({
  key: `stripe-charge:${invoiceId}`,
  action: () => stripe.charges.create({
    amount: amountCents,
    currency: 'usd',
    customer: customerId,
  }),
  budget: {
    id: tenantId,
    limitUsd: 500,
    warnAt: [0.7, 0.9],
  },
  extractCost: () => amountCents / 100,  // actual charge amount
  risk: {
    level: 'irreversible',
    policy: 'log',
    onRisk: (info) => auditLog.write(info),
  },
});

Enter fullscreen mode Exit fullscreen mode

One wrapper. The charge cannot fire twice (idempotency). It cannot exceed your tenant spend ceiling (budget). Every execution is logged to your audit trail (risk gate).


Works With Any Framework

Because guard() wraps any async () => T, there's no framework coupling. You use it inside whatever tool definition your framework expects:

Vercel AI SDK:

import { tool } from 'ai';
import { guard } from '@keelstack/guard';
import { z } from 'zod';

const sendEmailTool = tool({
  description: 'Send a confirmation email',
  parameters: z.object({ userId: z.string(), subject: z.string() }),
  execute: async ({ userId, subject }) => {
    return guard({
      key: `send-email:${userId}:${subject}`,
      action: () => resend.emails.send({ to: await getEmail(userId), subject }),
    });
  },
});

Enter fullscreen mode Exit fullscreen mode

LangGraph.js:

import { tool } from '@langchain/core/tools';
import { guard } from '@keelstack/guard';
import { z } from 'zod';

const chargeUserTool = tool(
  async ({ amountUsd, invoiceId }) => {
    const result = await guard({
      key: `stripe-charge:${invoiceId}`,
      action: () => stripe.charges.create({ amount: amountUsd * 100, currency: 'usd' }),
      risk: { level: 'irreversible', policy: 'log' },
    });
    return result.value;
  },
  {
    name: 'charge_user',
    schema: z.object({ amountUsd: z.number(), invoiceId: z.string() }),
  }
);

Enter fullscreen mode Exit fullscreen mode


Honest Limitations

The default in-memory ledger is process-local. If you run two Node.js instances, they don't share deduplication state — you need a shared Redis backend for that. Simultaneous same-key calls within a single process are lock-joined, but cross-process race safety depends on your ledger implementation's atomicity guarantees .

The package doesn't do loop detection (identifying a repeating sequence of different tool calls). It doesn't do semantic caching. It solves the specific, well-defined problem of duplicate side effects from retried tool calls — and it solves that problem cleanly.


Install It Today

npm install @keelstack/guard

Enter fullscreen mode Exit fullscreen mode

Node ≥ 20. TypeScript ≥ 5 (optional but recommended). Zero runtime dependencies. MIT licensed .

The full source is on GitHub — five files, no magic, no hidden deps.

Coming next: first-party @keelstack/guard-redis adapter, OpenTelemetry spans per guard call, and a hosted dashboard to see every replayed duplicate and budget block per user and per agent. If you want early access and want to shape what the dashboard looks like, join the beta waitlist.


If you've hit this problem in production — or if you have a horror story about an agent that wouldn't stop calling the same endpoint — I'd genuinely like to hear it. The comments are open.