Back to blog

The Tool Use Pattern: How AI Agents Actually Work

Alset TeamApril 25, 20267 min

Agents Are Just Loops

Strip away the hype and an AI agent is a simple pattern: a language model that can call functions. The model doesn't execute code. It doesn't access databases. It outputs a structured request — "call this function with these arguments" — and your code does the rest.

Here's the core loop:

  • User sends a message
  • Model receives the message plus a list of available tools (JSON Schema definitions)
  • Model decides: respond with text, or call a tool
  • If tool call: your code executes the function, sends the result back
  • Model sees the result, decides next action
  • Repeat until the model responds with text (no more tool calls)
  • That's it. Every AI agent — from simple chatbots to complex autonomous systems — runs some variation of this loop.

    Why JSON Schema Matters

    The tool definitions you give the model are JSON Schema objects. They describe the function name, parameters, types, and constraints. The model uses these schemas to generate valid function calls.

    {
      "name": "search_customers",
      "description": "Search the CRM by name, email, or account ID",
      "parameters": {
        "type": "object",
        "properties": {
          "query": { "type": "string" },
          "field": {
            "type": "string",
            "enum": ["name", "email", "account_id"]
          },
          "limit": { "type": "integer", "default": 10 }
        },
        "required": ["query", "field"]
      }
    }

    The quality of your schema directly determines the quality of the agent. Vague descriptions produce wrong tool calls. Missing constraints produce invalid arguments. Your tool schema is the interface contract between the model and your system.

    Schema Design Principles

  • Be specific in descriptions. "Search customers" is worse than "Search the CRM by name, email, or account ID. Returns matching customer records with their subscription status."
  • Use enums for constrained choices. Don't let the model guess valid values.
  • Mark required fields explicitly. Models will omit optional parameters when unsure.
  • Keep parameter count low. More than 5-6 parameters per tool and accuracy drops. Split into multiple tools instead.
  • The Execution Layer

    The model outputs tool calls as structured JSON. Your code is responsible for:

  • Validation: Check that arguments match the schema before executing
  • Authorization: Does this user have permission to call this tool?
  • Execution: Run the actual function (database query, API call, calculation)
  • Error handling: Return structured errors the model can understand and retry
  • Result formatting: Send back results in a format the model can reason about
  • async function executeTool(call: ToolCall): Promise<ToolResult> {
      const handler = toolHandlers[call.name];
      if (!handler) return { error: "Unknown tool" };
    
      try {
        const result = await handler(call.arguments);
        return { success: true, data: result };
      } catch (err) {
        return { error: err.message, retryable: true };
      }
    }

    Multi-Step Reasoning

    The real power emerges when the model chains tool calls. Ask "find our biggest customer and check their support tickets" and the model will:

  • Call search_customers with sort by revenue
  • See the result, extract the customer ID
  • Call get_support_tickets with that customer ID
  • Synthesize both results into a coherent answer
  • Each step is deterministic code execution. The model only decides which tool to call and what arguments to pass. Your code controls everything else.

    The Enterprise Takeaway

    Building AI agents isn't about prompt engineering tricks. It's about designing clean tool interfaces, writing robust execution layers, and controlling the loop. The model is a reasoning engine. Your tools are the hands.

    Get the tools right and the agent works. Get them wrong and no amount of prompt tuning will save you.

    Ready to build?

    Explore our enterprise AI courses — build production systems with real enterprise data patterns.

    Explore enterprise courses