Function Calling / Tool Use

In One Sentence (TL;DR)

Letting the LLM emit a typed JSON request to call your code/APIs instead of guessing live data.

What It Is

Function calling (tool use) allows you to pass JSON Schema descriptions of functions—such as `get_order_status(order_id)` or `search_tickets(status)`—alongside your prompt. Instead of making up an answer, the model emits a structured tool-call payload; your backend validates the arguments, executes the actual function securely, and feeds the result back so the model can write the final reply.

Software Engineering Analogy

An RPC dispatcher where the LLM acts as a smart router emitting a typed JSON request and your server runs the controller.

How It Works (Step-by-Step)

  1. 01Send the user prompt + JSON Schema definitions of allowed tools.
  2. 02Model either replies directly with text OR returns `{ name: 'get_order_status', args: { order_id: 'ORD-42' } }`.
  3. 03Your application validates permissions, executes the real function, and sends the return value back to the model.
  4. 04Model reads the tool output and generates the grounded user response.

Real-World Scenario

User asks 'Where is my package ORD-90412?' -> LLM calls `get_order_status('ORD-90412')` -> your backend queries the shipping API -> LLM replies 'Your order shipped yesterday and arrives Thursday.'

When to Use vs. When NOT to Use

When to Use

Use whenever the model needs live data, deterministic math, database lookups, or external side effects.

When NOT to Use

Never expose unvalidated shell/SQL execution or mutate production records without authorization checks.

Common Beginner Mistakes

  • Thinking the LLM executes code itself—YOUR backend executes the function and owns all security checks.
  • Exposing destructive tools like `run_raw_sql(query)` without strict validation or human confirmation.
  • Writing vague tool/parameter descriptions (the LLM relies on docstrings and schema descriptions to pick args correctly).

Code Example

Tool schema + model call + tool result round-trip · json
{
  "1_tool_schema_sent_to_model": {
    "name": "get_order_status",
    "description": "Look up live shipping status for a customer order ID.",
    "parameters": {
      "type": "object",
      "properties": {
        "order_id": { "type": "string", "description": "Order ID like ORD-90412" }
      },
      "required": ["order_id"]
    }
  },
  "2_model_emits_call": {
    "name": "get_order_status",
    "args": { "order_id": "ORD-90412" }
  },
  "3_your_server_returns": {
    "status": "IN_TRANSIT",
    "eta": "2026-12-08"
  }
}