Working through this module?
Module 06 · Page 7 of 20 · 8 sections
Tool Use and Function Calling in Agentic AI
How an LLM decides to call a tool, the JSON contract it fills in, and the full round trip.
What is Tool Use?
- Tool use allows an agent to interact with external systems, APIs, databases, websites or custom tools to perform real-world tasks.
- The LLM decides when to use a tool based on the user request and available tool descriptions.
- It extends the capabilities of agents beyond text generation to take real actions in the real world.
What is Function Calling?
- Function calling is a mechanism where the LLM returns a structured output (JSON) to call a specific function or tool.
- Instead of generating plain text, the model generates a function name and arguments.
- The application executes the function, gets the result and sends it back to the LLM for the final response.
How Tool Use Works?
User Querye.g. “Get weather of Delhi”
LLMDecides to use tool
Function Call{"name": "get_weather", "arguments": {"city": "Delhi"}}
Execute ToolCall external API
Tool ResultWeather data
LLMGenerate final answer
Final Responsee.g. “Delhi weather is 28°C and sunny”
Example Tools Agents Can Use
- Web Search
- Search latest information from internet.
- Weather API
- Get real-time weather information.
- Database
- Query internal company data.
- Code Execution
- Run Python code for calculations.
- File System
- Read, write and process files.
- Custom APIs
- Call business-specific APIs (e.g. flight booking).
1
2
3
4
5
6
Function Calling Flow (Detailed)
User
AgentLLM
External ToolAPI / function
Function Schema (JSON Example)
json
{
"name": "get_weather",
"description": "Get current weather information for a city",
"parameters": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "Name of the city (e.g. Delhi)"
},
"unit": {
"type": "string",
"enum": ["celsius", "fahrenheit"],
"description": "Temperature unit"
}
},
"required": ["city"]
}
}Python Example (OpenAI Function Calling)
python
from openai import OpenAI client = OpenAI() functions = [ { "name": "get_weather", "description": "Get current weather information", "parameters": { "type": "object", "properties": { "city": {"type": "string", "description": "City name"} }, "required": ["city"] } } ] response = client.chat.completions.create( model="gpt-4o", messages=[{"role": "user", "content": "What's the weather in Delhi?"}], functions=functions, function_call="auto" ) print(response.choices[0].message.tool_calls)
Key Takeaways
- Tool use allows agents to interact with the real world.
- Function calling provides structured and reliable ways to call tools.
- Agents can use multiple tools sequentially.
- Proper tool descriptions help the LLM decide correctly.
- Tool use is essential for building useful and production-ready agents.