agenticoutputs
Tool Calling & Functions

Tool Calling: Bridging AI and Execution

Language models are text prediction engines. They do not have internet access, database connections, or file execution rights. Tool calling is the standard handshake that lets the model ask the client application to run external code, returning the results back to the context.

0. INJECT TOOL SCHEMA (JSON-SCHEMA) 1. MODEL CALLS TOOL (JSON INTENT) 4. RESULT INJECTED TO CONTEXT REASONING CORE LLM CLIENT APP EXECUTOR 2. EXECUTE 3. RETURN EXTERNAL API / DATABASE

The execution loop: The model writes a request, the client intercepts and runs it against the target system, and passes the result back. The model never runs the code itself.

01 The Execution Cycle

The four stages of a secure tool invocation.

Step 01

Injection

The client app sends the system prompt along with schemas of available tools. Each schema declares parameters, argument types, and a narrative description.

Step 02

Intent

The model reasons and generates a structured block containing tool name and specific argument parameters. It pauses text generation and enters a wait state.

Step 03

Run

The client app intercepts the structured block, executes the function locally or remotely, and captures the string response or database table payload.

Step 04

Feedback

The client app appends the captured tool response to the prompt history as a tool message. The model resumes execution with the new reference context.

02 The JSON Contract

Schema declaration vs. the matching model intent request.

Input: Client Schema Declaration
{
  "name": "get_customer_balance",
  "description": "Fetch customer outstanding balance by ID",
  "parameters": {
    "type": "object",
    "properties": {
      "customer_id": {
        "type": "string",
        "description": "Unique CRM account identifier"
      }
    },
    "required": ["customer_id"]
  }
}
Output: Model Tool Call Intent
{
  "tool_calls": [
    {
      "id": "call_t98765",
      "type": "function",
      "function": {
        "name": "get_customer_balance",
        "arguments": "{\"customer_id\": \"crm_9831\"}"
      }
    }
  ]
}
03 Failure Modes

Four architectural bottlenecks when models invoke tools.

Parameter Hallucination

The model invents arguments not present in the tool's JSON schema (e.g. sending a email address field when the function only supports user IDs).

Mitigation: Strict schema enforcement & prompt overrides.

JSON Malformation

The model breaks brace formatting, forgets to escape quotes, or truncates the argument string before completion, throwing syntax parsing errors.

Mitigation: Structured output modes (JSON Mode / Structured Outputs).

Type Mismatches

The model sends integers as strings or booleans as integers, breaking runtime validators and typed target handlers (like TypeScript or Go APIs).

Mitigation: Cast/coerce arguments on intercept.

Stagnation loops

The tool throws an error message, and the model attempts to invoke the same tool with identical arguments repeatedly, burning API tokens.

Mitigation: Out-of-band loop caps and error budget handlers.

"Text generation describes the world. Tool calling changes it."

01

The client is the compiler and the compiler rules. The model never runs the code directly; the orchestrator intercepts, validates, and executes.

02

Clear schema descriptions are code dependencies. A poorly described parameter guarantees that the model will guess its usage incorrectly.