Tool use and function calling
6.2.3· AI-ML › AI Agents & Tool Use
Overview
Tool use (jise function calling bhi kehte hain) woh capability hai jo language models ko external systems, APIs, databases, aur code execution environments ke saath interact karne deti hai — predefined functions ke structured calls generate karke. Sirf text generate karne ki jagah, model real world mein actions request kar sakta hai.
The architecture: how tool use works
Step 1: Tool schema definition
Tum model ko available tools ek function schema use karke describe karte ho — typically JSON Schema format mein:
{
"name": "get_weather",
"description": "Retrieves current weather for a location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "City name, e.g. 'San Francisco, CA'"
},
"units": {
"type": "string",
"enum": ["celsius", "fahrenheit"],
"description": "Temperature unit"
}
},
"required": ["location"]
}
}Yeh format kyun?
- Model ko jaanna chahiye: tool kya karta hai (
description), kaunse arguments leta hai (parameters), aur kya required hai. - JSON Schema machine-readable hai aur iska ek clear contract hai: types, enums, validation rules.
Step 2: Model generates function calls
Jab user poochhe "What's the weather in Paris?", model (tool schemas diye hue) output karta hai:
{
"function_call": {
"name": "get_weather",
"arguments": "{\"location\": \"Paris, France\", \"units\": \"celsius\"}"
}
}Yeh karna kaise seekhta hai? Training/fine-tuning ke dauran, models ko yeh examples dikhaye jaate hain:
- User intent → correct function call
- Multi-step reasoning jisme tool use zaroori ho
- Kab tool call karna hai vs. directly answer dena hai
Modern models (GPT-4, Claude 3+, Gemini 1.5+) mein yeh unke instruction-following mein baked in hai.
Step 3: Your system executes the function
Tum JSON parse karte ho, arguments validate karte ho, aur actual API call karte ho:
def execute_tool(func_name, arguments):
if func_name == "get_weather":
args = json.loads(arguments)
# Call real weather API
return requests.get(f"https://api.weather.com?loc={args['location']}").json()Critical safety point: Model code execute nahi karta. Tum control karte ho kya run hoga. Yeh arbitrary code execution ko rokta hai.
Step 4: Inject result back into context
Tum tool result model ko wapas return karte ho:
{
"role": "function",
"name": "get_weather",
"content": "{\"temp\": 18, \"condition\": \"cloudy\", \"units\": \"celsius\"}"
}Tab model final user-facing response generate karta hai:
"It's currently 18°C and cloudy in Paris."
Why this matters: from chatbot to agent
Without tool use
- Model sirf training data se jawab de sakta hai (stale, koi personal info nahi)
- Jo facts use pata nahi unke baare mein hallucinations
- Koi action nahi le sakta
With tool use
- Real-time data: current weather, stock prices, user ka calendar
- Personalization: user ka database query karo, preferences access karo
- Actions: meetings book karo, emails bhejo, code run karo
- Verifiability: calculations code se hote hain, model ki arithmetic se nahi
User: "I need to fly from NYC to Tokyo next Tuesday, what's the weather and my budget?"
Step 1: Model calls get_flight_info(origin="NYC", dest="Tokyo", date="2026-07-08")
→ Result: {"price": 850, "duration": "14h"}
Step 2: Model calls get_weather(location="Tokyo", date="2026-07-08")
→ Result: {"temp": 28, "condition": "sunny"}
Step 3: Model calls check_user_budget(category="travel")
→ Result: {"remaining": 1200}
Final response: "The flight costs 350 in your travel budget. Tokyo will be sunny and 28°C—pack light!"
Har step kyun?
- Flight info: Actual pricing chahiye, guess nahi kar sakte
- Weather: Real-time forecast chahiye, training data nahi
- Budget: Private user data hai, database query karna padega
Implementation patterns
Pattern 1: Single-turn function calling
Model ek hi pass mein decide karta hai tool call karna hai ya nahi:
response = llm.chat(
messages=[{"role": "user", "content": "What's 2^128?"}],
tools=[calculator_tool],
tool_choice="auto" # Model decides
)
if response.tool_calls:
result = execute(response.tool_calls[0])
final = llm.chat(
messages=[...previous, {"role": "function", "content": result}]
)Tradeoff: Fast hai, lekin multi-step reasoning nahi kar sakta.
Pattern 2: Agentic loop (ReAct pattern)
Model baar baar reason aur act karta hai jab tak task complete na ho jaye:
while not done:
response = llm.chat(messages, tools=all_tools)
if response.is_finalanswer:
return response.content
for tool_call in response.tool_calls:
result = execute(tool_call)
messages.append({"role": "function", "content": result})Yeh kyun kaam karta hai: Model tools ko chain kar sakta hai (search → docs padho → code likho), ReAct (Reason + Act) prompting ke jaisa.
Simple queries ke liye (weather): ~1 call. Complex (research + analysis): 5-10 calls.
Derivation: Agar probability hai ki model directly answer de sakta hai, aur average complexity hai jo tools require karti hai, toh task completion se pehle expected tool invocations approximately hain, jahan success rate per tool call hai. Practice mein, models ko complex decomposition ke liye multiple attempts chahiye hote hain.
Common mistakes and fixes
❌ Bad:
{"function": "execute_python", "args": {"code": "import os; os.system('rm -rf /')"}}Kyun sahi lagta hai: Model ne generate kiya, toh safe hoga.
The fix:
- Raw code execution ko kabhi tool ke roop mein expose mat karo
- Allowlisted operations use karo:
{"function": "calculate", "args": {"expression": "2+2"}} - Kisi bhi execution ko sandbox karo (containers, timeouts, resource limits)
Steel-man: "Model aisa nahi karega" wali intuition fail hoti hai kyunki:
- Prompt injection use se malicious calls output ho sakti hain
- Models security implications nahi samajhte
- Ek bura function schema = full compromise
❌ Bad: Model web_search call karta hai, API timeout ho jaati hai, poora agent hang ho jaata hai.
Kyun sahi lagta hai: Tools ko bas kaam karna chahiye.
The fix:
def execute_tool_safe(call, timeout=10, retries=2):
for attempt in range(retries):
try:
return func_map[call.name](**call.args, timeout=timeout)
except TimeoutError:
if attempt == retries - 1:
return {"error": "Tool timeout"}Phir error ko function result ke roop mein inject karo — model ko retry karne do ya gracefully give up karne do.
Model ko ek saath 50 tools dena.
Kyun fail hota hai: Models ki "tool selection accuracy" limited hoti hai. 50 options ke saath, woh galat tools pick karte hain, parameters hallucinate karte hain, ya give up kar dete hain.
The fix:
- Hierarchical tools:
search_tools.google,search_tools.wiki - Dynamic tool filtering: Har task category ke liye sirf relevant tools dikhao
- Tool descriptions tumhare sochne se zyada matter karti hain — model
descriptionfield par rely karta hai
Empirical data (OpenAI): 5 tools ke saath accuracy: ~95%. 20 tools ke saath: ~75%. 50 ke saath: ~50%.
Variants and extensions
Parallel function calling
Model multiple independent tools simultaneously call kar sakta hai:
{
"tool_calls": [
{"id": "call_1", "function": {"name": "get_weather", "arguments": "..."}},
{"id": "call_2", "function": {"name": "get_traffic", "arguments": "..."}}
]
}Parallel execute karo, dono results return karo → faster agents.
Forced function calling
tool_choice: "required" set karo taaki model ko hamesha at least ek tool call karne par majboor karo. Structured data extraction ke liye useful:
# Extract structured data
response = llm.chat(
"Extract user intent from: 'Book me a flight to Paris'",
tools=[extract_booking_tool],
tool_choice="required"
)
# Guaranteed JSON outputRecall
Socho agar isko ek 12-saal ke bachche ko explain karna ho:
"Theek hai, tum jaante ho ChatGPT stories likh sakta hai aur sawaalon ke jawaab de sakta hai? Lekin woh actually kuch kar nahi sakta jaise weather check karna ya email bhejna, hai na? Woh sirf words type karta hai.
Tool use ChatGPT ko apps wala phone dene jaisa hai. Tum use batate ho 'Hey, mere paas ek weather app, calculator app, aur ek email app hai.' Ab jab tum poochho 'Weather kaisa hai?', ChatGPT andaza lagane ki jagah kehta hai 'Theek hai, main weather app use karta hoon' aur likh deta hai:
CALL weather_app WITH location='London'
Phir tum actually app kholte ho (ChatGPT nahi kar sakta), result laate ho (jaise '15°C aur baarish'), aur ChatGPT ko batate ho. Ab woh keh sakta hai 'London mein 15 degree aur baarish ho rahi hai!'
Jaadu yeh hai ki ChatGPT jaanta hai kab app use karni hai aur use kya batana hai. Yahi hamne use train kiya hai. Lekin hum hamesha control karte hain kaunsi apps woh use kar sakta hai — woh khud TikTok download karke post karna shuru nahi kar sakta!"
Connections
- 6.2.01-What-are-AI-agents: Tool use ek agent ke "haath" hain
- 6.2.02-Agent-architectures-and-planning: ReAct aur doosre patterns reasoning loops mein tool calls use karte hain
- 4.3.02-Prompt-engineering-techniques: Tool schemas prompt engineering ka ek form hain
- 7.1.01-RAG-fundamentals: Tools aksar RAG systems wrap karte hain (search, retrieve, answer)
- 5.2.03-JSON-mode-and-structured-outputs: Function calling structured output hai execution semantics ke saath
#flashcards/ai-ml
LMs mein tool use (function calling) kya hai? :: Woh capability jo LMs ko structured JSON function calls generate karne deti hai taaki external APIs, databases, ya code execution ke saath interact kar sakein, unhe text generators se agents mein badal deti hai jo actions le sakte hain.