DevInterviewMasterStart free →
AI & AutomationFree to read

Tool Use

Give Your AI Superpowers Beyond Just Talking

Learn how LLMs use external tools and APIs through function calling. This is the foundation that makes AI agents actually useful in the real world.

What is Tool Use & Function Calling?

How LLMs Go Beyond Just Generating Text

Analogy - The Smart Assistant

Imagine a brilliant intern who knows everything from textbooks but has never used a computer, phone, or calculator. They can discuss quantum physics but cannot check today's weather. Function calling is like giving that intern a phone, a calculator, and access to the internet - now they can actually DO things, not just TALK about things.

The Core Idea:

By default, LLMs can only generate text. They cannot check live stock prices, send emails, query databases, or book tickets. Tool use (also called function calling) lets LLMs request the execution of external functions. The LLM does not run the function itself - it tells YOUR application which function to call and with what arguments. Your code executes the function and sends results back to the LLM.

The Flow:

  1. 1. You define tools: Tell the LLM what functions are available (name, description, parameters)
  2. 2. User asks something: "What is the weather in Mumbai?"
  3. 3. LLM decides to use a tool: Instead of guessing, it outputs a structured function call: get_weather(city="Mumbai")
  4. 4. Your app executes it: Calls the real weather API, gets result: 32 degrees C, Humid
  5. 5. LLM gets the result: Formulates a natural language response using real data

Note: Function calling is THE fundamental building block of AI agents. Without it, LLMs are just fancy text generators. With it, they become powerful assistants that can interact with the real world.

How Function Calling Works Under the Hood

The Technical Mechanism

Tool Definition - JSON Schema:

You describe your tools using JSON Schema. Each tool has a name, description (crucial for the LLM to decide when to use it), and parameter definitions with types and descriptions.

Tool: get_stock_price
Description: "Get the current stock price for an Indian company"
Parameters:
  - symbol (string, required): "NSE stock ticker, e.g. RELIANCE, TCS"
  - exchange (string, optional): "NSE or BSE, default NSE"

LLM Decision Making:

When a user message comes in, the LLM has three choices:

  • Respond directly: If it can answer from its training data ("What is HDFC Bank?")
  • Call one tool: If it needs one piece of external data ("HDFC Bank share price?")
  • Call multiple tools: If it needs several pieces of data ("Compare HDFC and ICICI prices") - parallel or sequential calls

Why Descriptions Matter:

The LLM picks the right tool based on the description you provide. A vague description like "gets data" will confuse the model. A good description like "Get the current real-time stock price for any company listed on NSE/BSE. Returns price in INR, daily change, and volume" helps the LLM make the right choice every time.

Key Insight:

The LLM does NOT execute the function. It only generates a structured JSON saying "I want to call function X with parameters Y." YOUR application code is responsible for actually running the function, handling errors, and sending results back. This is a safety feature - you control what actually gets executed.

Note: Write clear, detailed tool descriptions. The LLM uses these descriptions to decide which tool to call. Poor descriptions lead to wrong tool choices and bad user experiences.

Tool Use Across Different Providers

OpenAI, Anthropic, Google - Same Idea, Different APIs

OpenAI (GPT-4, GPT-4o):

Uses the term "function calling" or "tools". You pass a "tools" array in the API request. The model responds with "tool_calls" containing function name and arguments as JSON. Supports parallel tool calls (multiple functions at once). Introduced "strict mode" for guaranteed valid JSON output.

Anthropic (Claude):

Uses the term "tool use". You define tools with input_schema (JSON Schema). Claude responds with "tool_use" content blocks. Claude is known for being very good at deciding WHEN to use tools vs when to answer directly. Also supports a special "computer use" tool for GUI interaction.

Google (Gemini):

Uses "function calling" terminology. Similar pattern - define function declarations, Gemini responds with function call parts. Supports automatic function calling mode where the SDK can execute functions for you.

Key Differences:

FeatureOpenAIClaudeGemini
Parallel CallsYesYesYes
Strict JSONYesVia promptVia config
Force Tool Usetool_choicetool_choicetool_config
Computer UseNoYesNo

Note: The core pattern is identical across all providers: define tools, LLM decides to call one, your code executes it, send results back. Only the API format differs slightly.

Common Mistakes & Best Practices

Avoid These Tool Use Pitfalls

Mistake 1: Too Many Tools

Giving an LLM 50+ tools confuses it. It struggles to pick the right one. Keep your tool set focused. If you have many tools, use a two-stage approach: first LLM call picks the category, second call uses tools from that category only.

Mistake 2: Poor Tool Descriptions

Vague descriptions like "process data" or "helper function" cause wrong tool selection. Write descriptions as if explaining to a smart human who has never seen your codebase. Include what it does, when to use it, and what it returns.

Mistake 3: No Error Handling

Tools fail - APIs timeout, databases go down, input validation fails. Always send error messages back to the LLM in a structured way so it can either retry, try a different approach, or inform the user gracefully.

Best Practices:

  • Validate inputs: Check LLM-generated parameters before executing. The LLM might hallucinate invalid values.
  • Set timeouts: Tool execution should have reasonable timeouts. Do not let a slow API hang your agent.
  • Log everything: Log every tool call, its parameters, and results. Essential for debugging.
  • Limit scope: Do not give tools write access unless absolutely needed. Read-only tools are safer.
  • Human-in-the-loop: For dangerous operations (delete, transfer money), require human approval before executing.

Note: Never blindly execute LLM-generated function calls in production. Always validate inputs, check permissions, and add safety rails for destructive operations.

Real-World Tool Use Patterns

Practical Applications of Function Calling

Pattern 1: Information Retrieval

Most common pattern. LLM fetches real-time data that it does not have in training data.

  • Weather lookup, stock prices, cricket scores
  • Database queries for customer information
  • Document search in company knowledge base

Pattern 2: Action Execution

LLM takes actions in the real world on behalf of the user.

  • Send email, create calendar event, post to Slack
  • Book a flight, order food, schedule a cab
  • Create Jira ticket, deploy code, run tests

Pattern 3: Multi-Step Workflows

LLM chains multiple tool calls to accomplish complex tasks.

  • Step 1: Search flights Delhi to Goa
  • Step 2: Check user's calendar for conflicts
  • Step 3: Book the best available flight
  • Step 4: Send confirmation email to user

Pattern 4: Computation Offloading

LLMs are bad at precise math and data processing. Offload these to tools.

  • Complex calculations (EMI, tax, compound interest)
  • Data analysis on large CSV files
  • Code execution for precise results

Note: Start simple with information retrieval tools, then gradually add action tools with proper safety checks. Multi-step workflows are the gateway to full AI agents.

Interview Questions - Tool Use & Function Calling

Q: What is function calling in LLMs and why is it important?

Function calling lets LLMs request the execution of external functions/APIs instead of just generating text. The LLM outputs a structured JSON with the function name and arguments, your application executes it, and sends results back. It is important because it bridges the gap between LLM knowledge (static, training data) and real-world actions (live data, APIs, databases). Without function calling, LLMs cannot check weather, query databases, or take any real-world action.

Q: Does the LLM actually execute the function? Explain the safety implications.

No, the LLM does NOT execute functions. It only generates a structured request saying "call function X with parameters Y." Your application code is responsible for actual execution. This is a critical safety feature because: (1) You can validate parameters before execution. (2) You can check permissions and authorization. (3) You can add human-in-the-loop for dangerous operations. (4) You control which functions are actually available. Never blindly execute LLM-generated calls in production without validation.

Q: How do you handle errors when a tool call fails?

Send the error back to the LLM as a tool result with a clear error message. The LLM can then: (1) Retry the call with corrected parameters. (2) Try an alternative tool or approach. (3) Inform the user about the issue gracefully. Always include structured error information (error type, message, suggestion). Never silently swallow errors or crash - the LLM is designed to handle failures and adapt its approach.

Frequently Asked Questions

What is Tool Use?

Learn how LLMs use external tools and APIs through function calling. This is the foundation that makes AI agents actually useful in the real world.

How does Tool Use work?

How LLMs Go Beyond Just Generating Text Analogy - The Smart Assistant Imagine a brilliant intern who knows everything from textbooks but has never used a computer, phone, or calculator. They can discuss quantum physics but cannot check today's weather.

Browse all AI & Automation topics →

Practice this on DevInterviewMaster

Read the full Tool Use breakdown with interactive demos, quizzes, and Hinglish notes.

Open the interactive topic →

800+ system-design, LLD, coding, and design-pattern topics. Unlock everything with Pro (₹499, one-time) or Ultimate (₹999, one-time) — lifetime access, no subscription.