Skip to main content

Command Palette

Search for a command to run...

Tool Calling with LLMs

Published
โ€ข4 min readโ€ขView as Markdown
Tool Calling with LLMs

๐Ÿ”น What is Tool Calling?

  • Definition: Tool calling is when LLMs (Large Language Models) are given access to external functions, APIs, or databases.

  • Instead of hallucinating answers, the model decides:

    ๐Ÿ‘‰ โ€œDo I know this? Or should I call a tool to get the real answer?โ€

  • Tools = just functions that LLMs can request during a conversation.

โœ… Example:

  • Without tools โ†’ โ€œWho won the 2025 IPL final?โ€ โ†’ Model fails (knowledge cutoff).

  • With tool calling โ†’ Model calls a web search API โ†’ gets fresh data โ†’ replies correctly.


๐Ÿ”น Why Use Tool Calling? (Use Cases)

  1. Knowledge Beyond Cutoff

    • LLMs are trained only up to a fixed date (knowledge cutoff).

    • Tools let them fetch real-time info.

    • Example: โ€œWhatโ€™s the weather in Kolkata today?โ€

  2. Web Search

    • Search APIs: Tavily, Serper, Brave, Bing.

    • Use case: live news, sports results, product availability.

  3. Database Lookups

    • Fetch user records, inventory, product prices.

    • Example: โ€œShow me my last 5 transactions.โ€

  4. Math & Specialized Tasks

    • Calculator, unit conversion, finance APIs, weather APIs.

    • Example: โ€œConvert $100 to INR at todayโ€™s rate.โ€

  5. Automation & Actions

    • LLMs can execute tasks, not just reply with text.

    • Example: Book tickets, send an email, update a calendar.

  6. Data Enrichment

    • Mix reasoning + tool data.

    • Example: โ€œList top 5 coffees under โ‚น500 in Indiaโ€ โ†’ LLM calls product API โ†’ formats neatly.


Tool/APIBest Use CaseProduction Ready?Notes
SerperHobby/small side projectsโŒ NoWrapper around Google. Quick, but limited.
Brave APIPrivacy-focused searchโŒ NoGood for experiments, slower than Google.
TavilyReal-world appsโœ… YesMost reliable, structured JSON responses, AI-optimized.
Bing SearchMicrosoft ecosystem appsโœ… YesWorks well, but sometimes noisy results.

๐Ÿ‘‰ Best choice for prototyping/production = Tavily.


๐Ÿ”น Tool Calling Flow

  1. User asks a question.

  2. LLM decides: Do I have enough knowledge? If not โ†’ request a tool.

  3. LLM generates a tool_call object.

     {
       "tool": "webSearch",
       "arguments": { "query": "latest cricket world cup winner" }
     }
    
  4. Backend intercepts & executes the tool (API call, function).

  5. Tool response fed back into the conversation.

  6. LLM uses result โ†’ generates final human-friendly answer.

๐Ÿ” Repeat if multiple tools needed until final response is ready.


๐Ÿ”น Example Code (Web Search Tool)

tools: [
  {
    type: "function",
    function: {
      name: "webSearch",
      description: "Search for latest information",
      parameters: {
        type: "object",
        properties: {
          query: { type: "string", description: "The search query" }
        },
        required: ["query"]
      }
    }
  }
],
tool_choice: "auto"
  • tool_choice: "auto" โ†’ LLM decides when to call the tool.

  • Backend captures tool_call โ†’ executes API โ†’ returns results.

  • LLM integrates tool output โ†’ gives final reply.


๐Ÿ”น Knowledge Cutoff

  • Every model has a knowledge cutoff date (last training data).

  • Example: GPT-4 cutoff โ†’ Oct 2023.

  • If asked about 2025 events โ†’ must use tool calling (e.g., web search).


๐Ÿ”น Streaming Responses

  • Normally: wait until the full answer is ready.

  • With streaming: receive tokens live as theyโ€™re generated.

  • Benefits:

    โœ… Feels instant & interactive.

    โœ… Great for chatbots, coding assistants, writing apps.

โš ๏ธ Limitation:

  • response_format: json_object โŒ does not support streaming.

    • Because streaming JSON risks broken/incomplete JSON mid-way.

๐Ÿ”น Best Practices

  • Use one reliable search API (Tavily recommended).

  • Always validate tool input/output (use schema validation).

  • Add timeouts, retries, and error handling in production.

  • Prevent infinite loops if LLM keeps calling the tool.


โœจ In short:

Tool calling makes LLMs more powerful and trustworthy by connecting them to real data, APIs, and actions. It bridges the gap between static knowledge and dynamic, real-world needs.