Back to AI Recipes

Tool Calling

Tool UseFunction Calling

Capability

Enable LLMs to invoke external tools, APIs, and functions dynamically.

Summary

Tool Calling enables LLMs to interact with external systems by defining structured interfaces for function invocation. The model detects when to call tools, extracts the correct parameters, and processes responses. This pattern extends LLM capabilities beyond text generation to real-world action execution.

How it works

  1. Tool Definition: Define available tools with schemas for parameters and returns
  2. Tool Invocation: Model decides which tool to call based on user intent
  3. Parameter Extraction: Model generates structured arguments matching the schema
  4. Execution: External system processes the tool call and returns results
  5. Response Integration: Model incorporates tool output into final response

Tool types

  • Search: Web search, database query, vector retrieval
  • Compute: Calculator, code execution, data processing
  • Action: API calls, workflow triggers, device control
  • Information: Knowledge base lookup, entity resolution

Best practices

  • Use structured schemas for parameter validation
  • Implement retry logic for transient failures
  • Validate tool responses before model consumption
  • Set clear boundaries on allowed tool operations
  • Log tool usage for debugging and compliance
Code View

Tool Calling Implementation

// Tool Calling recipe using OpenAI
// Install: bun add openai

import OpenAI from "openai";

async function main() {
  const input = "Add your prompt here.";
  const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
  const system = "You are a senior AI engineer and technical writer. Apply the agent recipe to structure the reasoning and produce a useful result. Recipe: Tool Calling. Description: Enable LLMs to invoke external tools, APIs, and functions dynamically. Focus: Capability Provide actionable, implementation-ready guidance.";
  const user = `Request: ${input}`;

  const openaiResponse = await openai.chat.completions.create({
    model: "gpt-4o-mini",
    messages: [
      { role: "system", content: system },
      { role: "user", content: user },
    ],
  });

  const openaiText = openaiResponse.choices[0]?.message?.content?.trim() ?? "";

  console.log(openaiText);
}

main().catch((error) => {
  console.error(error);
  process.exitCode = 1;
});