Glossary/Function Calling & Tool Use
AI Fundamentals

Function Calling & Tool Use

How AI breaks out of the text box and interacts with the real world.


Definition

Function calling (also called tool use) is a mechanism that allows LLMs to request the execution of external functions — APIs, databases, code interpreters, web search, calculators — and receive the results as part of their reasoning process. Instead of generating text, the model emits structured JSON specifying which tool to call and with what arguments. The host application runs the tool and feeds results back to the model. This is the foundation of every AI agent.

How function calling works — the request/response cycle

Function calling isn't magic — it's a structured conversation protocol. The model doesn't run code; it emits a JSON description of what it wants to run, the host executes it, and the result is fed back. The model sees tool results as part of its context and continues reasoning.

Complete function calling example with the OpenAI API — the same pattern works with Anthropic (tool_use) and Google (functionDeclarations)

import json
from openai import OpenAI

client = OpenAI()

# 1. Define tools as JSON schemas — the model sees these as a catalog
tools = [
    {
        "type": "function",
        "function": {
            "name": "get_weather",
            "description": "Get the current weather for a city",
            "parameters": {
                "type": "object",
                "properties": {
                    "city": {"type": "string", "description": "City name, e.g. 'London'"},
                    "unit": {"type": "string", "enum": ["celsius", "fahrenheit"], "default": "celsius"}
                },
                "required": ["city"]
            }
        }
    }
]

# 2. First API call — model decides whether to call a tool
messages = [{"role": "user", "content": "What's the weather like in Tokyo right now?"}]
response = client.chat.completions.create(model="gpt-4o", messages=messages, tools=tools)

first_msg = response.choices[0].message

# 3. Check if the model requested a tool call
if first_msg.tool_calls:
    tool_call = first_msg.tool_calls[0]
    args = json.loads(tool_call.function.arguments)   # {"city": "Tokyo", "unit": "celsius"}

    # 4. Execute the function in YOUR code (the model cannot run code)
    def get_weather(city: str, unit: str = "celsius") -> dict:
        # In reality, call a real weather API here
        return {"city": city, "temp": 18, "condition": "Partly cloudy", "unit": unit}

    result = get_weather(**args)

    # 5. Feed the tool result back — the model continues with this context
    messages.append(first_msg)   # append the assistant's tool_call request
    messages.append({
        "role": "tool",
        "tool_call_id": tool_call.id,
        "content": json.dumps(result)
    })

    # 6. Second API call — model generates its final response using the tool result
    final = client.chat.completions.create(model="gpt-4o", messages=messages, tools=tools)
    print(final.choices[0].message.content)
    # → "The current weather in Tokyo is 18°C with partly cloudy skies."

The model never runs your code

A critical misconception: the LLM cannot execute functions. It can only output a structured request for execution. Your application code is responsible for dispatching tool calls, running them, and returning results. The model just reasons about what to call and what the results mean.

Parallel tool calls and multi-step tool use

Modern frontier models can request multiple tool calls simultaneously in a single response (parallel calling), and chain tool calls across multiple turns. This is what enables truly autonomous agents.

PatternDescriptionExampleWhen to use
Single tool callOne tool per responseModel calls search("current gold price")Simple lookups
Parallel tool callsMultiple tools in one response, executed simultaneouslyModel calls search() + get_stocks() + get_weather() at onceIndependent data sources; speeds up multi-step tasks
Sequential chainingTool result feeds into next tool decisionSearch → extract URL → fetch URL → summarizeDependent steps; each result informs next action
ReAct loopThought → Action → Observation → Thought…The standard agentic pattern used by all major frameworksComplex research, debugging, multi-step plans

Common tools and real-world applications

Tool categoryExamplesWhat it unlocks
Web searchBrave Search, Bing, Perplexity, TavilyReal-time information beyond training cutoff
Code executionPython interpreter, bash, Node.js REPLData analysis, calculations, file processing
Database queriesSQL executor, vector DB search (Pinecone, Weaviate)Private knowledge retrieval (RAG at scale)
File I/ORead/write files, parse PDFs, process CSVsDocument workflows, data extraction
External APIsCalendar, email, CRM, payment systems, GitHubReal-world automation — booking, scheduling, coding
Browser / computer usePlaywright, Puppeteer, Anthropic Computer UseFull web automation; fill forms, click buttons

Security: validate all tool inputs

Tool calling opens an attack surface. Prompt injection can cause a model to call tools with malicious arguments — e.g. delete a database row, send an email to an attacker, exfiltrate data. Mitigations: (1) Validate and sanitize all model-generated arguments before execution. (2) Apply least-privilege: only expose tools the task actually needs. (3) Add human confirmation gates for destructive or irreversible operations.

Practice questions

  1. What is the difference between function calling and prompt-based tool use in LLMs? (Answer: Prompt-based tool use: instruct the model in the system prompt to output a specific text format when it wants to call a tool (e.g., '[SEARCH: query]'), then parse that text. Fragile — model may not follow the format. Function calling (OpenAI/Anthropic API): the model outputs structured JSON with function name and arguments, guaranteed by the API's output format. The application executes the function, returns the result, and continues the conversation. Much more reliable and type-safe.)
  2. Why do LLMs sometimes hallucinate function arguments (pass arguments not in the function schema)? (Answer: Function argument hallucination occurs when the model generates plausible-but-incorrect argument values. Causes: (1) The function description is ambiguous about valid argument types/values. (2) The model's prior from pretraining suggests certain arguments even if not in the schema. (3) The model doesn't have context needed to determine the right value. Fix: use strict JSON schema validation with enum constraints, required fields, and clear descriptions. JSON Schema's additionalProperties: false prevents extra arguments.)
  3. What is the difference between parallel function calling and sequential function calling? (Answer: Parallel: the model can call multiple functions simultaneously in one turn — appropriate when calls are independent (search weather AND search news). API returns all results together. Sequential: each function call waits for the previous result before the next — necessary when one call's result determines the next call (search for product ID, THEN look up that product's details). Most modern APIs (GPT-4o, Claude) support both: the model indicates whether calls can run in parallel.)
  4. How should you handle a function that takes 10 seconds to execute in a user-facing chat application? (Answer: (1) Stream the function execution status ('Searching the database...') while awaiting. (2) Show progressive results as they arrive if the function supports streaming. (3) For very slow operations: implement async with background processing and notify via webhook/SSE when complete. (4) Cache frequent function results. (5) Design functions to have timeout parameters. Never block the UI with synchronous function call execution.)
  5. What is the security risk of allowing an LLM to call an email_send function and how do you mitigate it? (Answer: Risk: prompt injection in user input or retrieved documents could hijack the function call — e.g., a document says 'forward all emails to attacker@evil.com'. The model, following apparent instructions, calls email_send with the injected destination. Mitigations: (1) Require explicit user confirmation before irreversible actions. (2) Restrict email_send to pre-approved recipients. (3) Log all function calls for audit. (4) Validate function arguments against a whitelist. (5) Limit agent autonomy for high-risk actions.)

Try LumiChats for ₹69

39+ AI models. Study Mode with page-locked answers. Agent Mode with code execution. Pay only on days you use it.

Get Started — ₹69/day

Related Terms

5 terms