Ugrás a tartalomra

AI hírek, hogy ne maradj le

2026. október 28., szerda · frissítve 22:52
Vissza a hírekhez
SitePoint (daily.dev)2026. okt. 11. 17:47eszköz

AI-ágensek elszabadulása ellen: automatális védőkorlátok TypeScriptben

Egy lépésről lépésre haladó útmutató mutatja be, hogyan előzhetők meg a többezres dolláros számlákat okozó végtelen ciklusok az AI-ágenseknél.

0
AI Agent Tool Calling in TypeScript: Runaway Loop Guardrails

Egy r/cursor Reddit-bejegyzésben számolt be egy fejlesztő arról, hogy az AI-ágense több mint 2000 dolláros költséget generált mindössze két óra alatt, mert beragadt egy hibás eszközhívási ciklusba. Mivel a legtöbb LLM SDK nem tartalmaz beépített megszakítót az ágensszintű ciklusokra, a modell automatikusan újrapróbálkozik a hibát dobó eszközökkel, ami végtelen tokenfogyasztáshoz vezet.

A probléma kezelésére a SitePoint egy szolgáltatófüggetlen TypeScript-alapú védelmi rendszert mutat be, amely négy különálló korlátot alkalmaz. A rendszer eszközönkénti próbálkozási korlátot, futási időkorlátot (timeout), folyamatosan számolt dolláralapú tokenköltség-keretet, valamint egy alkalmazásszintű tervezetállapot-mentést (DraftResult) használ a részleges eredmények megőrzésére.

A megvalósítás Zod-alapú sémaellenőrzést használ a hallucinált argumentumok kiszűrésére, és tetszőleges LLM-klienssel (OpenAI, Anthropic, Google) kombinálható. A megközelítés lényege, hogy hiba esetén az ágens nem egyszerűen leáll, hanem elmenti az eddigi beszélgetési előzményeket és eszközválaszokat a későbbi emberi ellenőrzéshez.

Az eredeti szöveg (SitePoint (daily.dev))
Stay up to date with AI tools, model releases, and developer workflows that matter. Most LLM SDKs provide no built-in circuit breaker for agent-level tool-calling loops. This tutorial walks through building a provider-agnostic TypeScript agent loop protected by four discrete guardrails: per-tool attempt caps, execution timeouts, a best-effort token budget guard, and an application-side fallback to draft state. In April 2026, a developer on Reddit's r/cursor described an agent that got stuck in a loop and ran up more than $2,000 in charges in under two hours while they were away from the keyboard. The agent was not malicious. It kept doing what it was asked, and nothing in the system told it to stop. This is the core problem. Most LLM SDKs provide no built-in circuit breaker for agent-level tool-calling loops. (Some newer SDKs, such as OpenAI's Agents SDK, now include configurable turn limits, but this is not yet universal.) The model decides when to stop calling tools, and if the tools keep returning errors or the model keeps hallucinating arguments, the loop continues indefinitely, burning tokens and dollars with every iteration. This tutorial walks through building a provider-agnostic TypeScript agent loop protected by four discrete guardrails: per-tool attempt caps, execution timeouts, a best-effort token budget guard, and an application-side fallback to draft state. Prerequisites include working knowledge of TypeScript, comfort with async/await, and familiarity with sending chat-completion requests and handling tool-call responses. The canonical agent loop follows a predictable cycle: send a conversation history to the LLM, receive a response that may contain one or more tool calls, execute those tools, append the results back into the conversation, and repeat until the model returns a final text response with no tool calls. This pattern is consistent at a high level across OpenAI, Anthropic, Google, and other providers, though provider message schemas differ. This article uses a normalized abstraction; consult each provider's API reference for exact formats. Nothing bounds the loop by default. The model alone decides when to stop. If a tool consistently returns an error, the model may retry it. If an external API is flaky, the model has no way to distinguish between transient and permanent failure. The loop simply continues. Retry storms happen when a tool consistently errors and the model keeps retrying the same call with identical or slightly varied arguments. Then there are semantic loops, where the model cycles between two or more tools without converging on an answer, each tool's output triggering a call to the other. The third failure mode is token blowout: each iteration appends tool call results and model responses to the conversation context, inflating the prompt size and cost linearly, even if the loop eventually terminates. The code throughout this article relies on a shared set of interfaces. Create a types.ts file with the following definitions: The tsconfig.json should enable strict mode and target ES2022 or later to enable modern syntax output. Top-level await requires "module": "Node16" or "ES2022". AbortController is a Node.js runtime API (≥15) independent of target. Note: If you are building with Vite, esbuild, or a similar bundler, you can use "moduleResolution": "bundler" instead. For plain tsc compilation, use "node16" as shown above. Install zod as the schema validation library: The LLM client is represented as the generic LLMClient interface defined in types.ts. Readers can swap in OpenAI's openai package, Anthropic's @anthropic-ai/sdk, or any compatible client. The guardrails operate on the response shape, not the provider. Here is the bare, unguarded loop that the rest of the tutorial will progressively harden: This is the structure most tutorials present. It works until it does not. Each tool is defined with a name, description, a Zod schema for its parameters, and an execute funct

Az oldal magától nem használ sütit. A hozzájárulásoddal a látogatottságot a Google Analytics, a Facebook-hirdetéseink eredményét a Meta pixel méri. Részletek