AI Briefing
KO

AI Agent Tool Calling Principles and Cost Optimization Implemented in Vanilla JS

·2026.09.15 09:00

Key point

This article analyzes the Tool Calling loop structure and cost issues arising from the Stateless nature of AI Agents implemented in Vanilla JS, introducing cost optimization methods through Prefix caching and four essential implementation rules.

Details

AI Agents do not have the model execute code directly; instead, they go through a 4-step loop: 1) passing the conversation and tool list, 2) the model's tool_use call, 3) actual tool execution and returning tool_result, and 4) generating the model's final response. In this process, the model is Stateless, requiring the entire conversation array to be resent every turn, which incurs massive input token costs when tool output volume is high (Fat tool output). For example, if 8k tokens are returned in 2 out of 10 turns, they are resent 8 times, resulting in an additional cost of 64k tokens. To resolve this, tool return volume should be minimized, or Prefix caching (cache_control) should be applied to reduce input costs to approximately 0.1x upon reuse. Additionally, for stable operation, one must adhere to responding with tool_result for all calls, consolidating parallel call results into a single message, preserving the original reply.content, and limiting the turn count (e.g., 20 turns) to prevent infinite loops.

This summary was generated automatically by AI. Check the original for the author's claims and context. Copyright belongs to the original author.

Our guide explains how the AI works. Report summary errors, attribution issues, or removal requests via Contact.