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)
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?โ
Web Search
Search APIs: Tavily, Serper, Brave, Bing.
Use case: live news, sports results, product availability.
Database Lookups
Fetch user records, inventory, product prices.
Example: โShow me my last 5 transactions.โ
Math & Specialized Tasks
Calculator, unit conversion, finance APIs, weather APIs.
Example: โConvert $100 to INR at todayโs rate.โ
Automation & Actions
LLMs can execute tasks, not just reply with text.
Example: Book tickets, send an email, update a calendar.
Data Enrichment
Mix reasoning + tool data.
Example: โList top 5 coffees under โน500 in Indiaโ โ LLM calls product API โ formats neatly.
๐น Popular Web Search Tools
| Tool/API | Best Use Case | Production Ready? | Notes |
| Serper | Hobby/small side projects | โ No | Wrapper around Google. Quick, but limited. |
| Brave API | Privacy-focused search | โ No | Good for experiments, slower than Google. |
| Tavily | Real-world apps | โ Yes | Most reliable, structured JSON responses, AI-optimized. |
| Bing Search | Microsoft ecosystem apps | โ Yes | Works well, but sometimes noisy results. |
๐ Best choice for prototyping/production = Tavily.
๐น Tool Calling Flow
User asks a question.
LLM decides: Do I have enough knowledge? If not โ request a tool.
LLM generates a tool_call object.
{ "tool": "webSearch", "arguments": { "query": "latest cricket world cup winner" } }Backend intercepts & executes the tool (API call, function).
Tool response fed back into the conversation.
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.