Hasty Briefsbeta

Bilingual

How AI tool calling works (40 lines of vanilla JavaScript)

9 hours ago
  • Tool calling turns a chat model into an agent by letting it request function calls via structured text, not by running code itself.
  • The core loop has four steps: model generates tool_use, your program runs the function, returns tool_result, and repeats until the model gives a text answer.
  • No hidden memory or planning exists; the conversation array is the entire state, and the loop is the program.
  • Tool descriptions are prompts in JSON Schema; well-written descriptions prevent the model from misusing tools.
  • Tool results are appended with role 'user', and the reply must be appended whole, preserving all blocks for quality.
  • Costs grow quadratically as the conversation resends all previous messages; trimming tool results and using prefix caching (cache_control) save significant tokens and money.
  • Four rules: always answer tool calls (even errors), return all results in one message, append replies intact, and cap loop iterations to avoid runaway costs.
  • While SDKs offer built-in tool runners (e.g., toolRunner in Python/TS), writing your own loop once is essential for understanding and debugging.
  • The exercise: use a harness with 21 tools and a virtual filesystem to practice; break things intentionally to learn, with safety measures like /wipe and /undo.
  • Next steps: refine tool descriptions (Lesson 2 follow-on), explore loops and goals (Lesson 3), and learn about context management (Lesson 5).