Multi-Agent Token Loop Circuit Breaker
One rogue LangGraph agent burned $2,000 over a weekend. This kills it in 3 seconds.
The Problem
Autonomous AI agents built with LangGraph, CrewAI, or Mastra get caught in infinite recursive loops. They call LLM APIs 50–200 times before you notice. By Monday morning you have a $2,000 unexpected API bill and a cloud provider suspension notice.
The Solution
A TypeScript/Next.js Edge Middleware that tracks tracing IDs across recursive agent steps, calculates live token cost mid-execution, and triggers a hard circuit breaker — instantly killing the execution loop when a single session exceeds a configurable budget threshold.
Why You Have No Choice
You cannot uncap your API keys with confidence until this is in place. Every production AI agent deployment needs a financial circuit breaker. This is insurance for your cloud budget.
Code Preview
This is what you'll drop into your codebase. The full package includes all configuration options and integrations.
// circuit-breaker.middleware.ts
import { CircuitBreaker } from './circuit-breaker.config'
export const config = { matcher: '/api/agent/:path*' }
export default CircuitBreaker.middleware({
budgetUSD: 5.00, // kill after $5 per session
windowMinutes: 60, // rolling window
tracingHeader: 'x-agent-trace-id',
onTripped: async (ctx) => {
await ctx.log({ reason: 'budget_exceeded', cost: ctx.totalCost })
return ctx.kill() // returns 429 + kills execution
}
})What's in the Package
Single TypeScript file — drop into your Next.js middleware.ts or use as a standalone Edge Function. Works with OpenAI, Anthropic, Groq, and any OpenAI-compatible API.
Errors This Package Fixes
Instant download after payment. Secure checkout via Lemon Squeezy.
⚖️ Developer Tool — Important Notice
VektorIndex developer tools are technical software packages provided to assist developers in implementing compliance-relevant technical controls. They do not constitute legal advice, legal services, or a guarantee of regulatory compliance. Each tool solves a specific technical problem — whether a tool satisfies your specific legal obligations depends on your use case, jurisdiction, and how the tool is configured and deployed. You must engage qualified legal counsel to confirm your compliance posture. VektorIndex does not represent that use of any tool will make your product compliant with any specific law or regulation.
Prices in USD. Billed monthly. Annual plans available at 20% discount. Taxes may apply depending on your jurisdiction. VektorIndex is a digital goods merchant — you are purchasing a software license, not a physical product. Subscription management: Lemon Squeezy customer portal.