Working through this module?0 of 19 done
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.

6.1

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.
6.2

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.
6.3

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”
6.4

Example Tools Agents Can Use

1
Web Search
Search latest information from internet.
2
Weather API
Get real-time weather information.
3
Database
Query internal company data.
4
Code Execution
Run Python code for calculations.
5
File System
Read, write and process files.
6
Custom APIs
Call business-specific APIs (e.g. flight booking).
6.5

Function Calling Flow (Detailed)

User
AgentLLM
External ToolAPI / function
Seven steps; the LLM never runs the tool itself.
6.6

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"]
  }
}
6.7

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)
6.8

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.