DEV Community

tanmay kuamr chaurasia
tanmay kuamr chaurasia

Posted on

Function Calling 101: How AI Agents Execute Real-World Actions

When building modern AI applications, a common misconception is that Large Language Models (LLMs) can directly connect to databases, call external REST APIs, or send emails on their own.

They can't.

An LLM is a deterministic-prompt, text-in, text-out engine. It doesn't have an IP address, database drivers, or API keys. If you ask a standard model to "cancel order #1042," it cannot reach out to your database and update a row.

So, how does an AI chatbot turn into an active AI agent? The answer lies in function calling (also known as tool use).

The Misconception vs. The Reality

Instead of executing actions directly, the model emits a structured request indicating intent. It tells your application code: "I need you to run function X with arguments Y."

The runtime execution loop works in five distinct steps:

  1. User Request: The user asks the agent to perform a specific task.
  2. Tool Definition: Your application sends the prompt to the model along with a schema of available tools.
  3. Structured Intent: The model analyzes the request and returns a JSON payload containing the function name and parsed parameters.
  4. Backend Execution: Your application code receives this instruction, validates the parameters, executes the actual code (SQL query, API call, email send), and receives the raw result.
  5. Final Response: Your backend sends the execution output back to the model, allowing it to compose a clear response or decide the next step.

Step-by-Step Implementation in Python

Let's look at how to implement a function schema and a safe execution dispatcher in Python.

First, define the core logic and register the JSON schema that tells the LLM how to invoke the tool:

import json

# 1. Backend execution logic
def fetch_order_status(order_id: str) -> dict:
    """Queries internal database for order status."""
    mock_db = {
        "ORD-992": {"status": "shipped", "carrier": "FedEx", "tracking": "1Z9999"}
    }
    return mock_db.get(order_id, {"error": "Order not found"})

# 2. Function schema provided to the LLM
tools = [
    {
        "type": "function",
        "function": {
            "name": "fetch_order_status",
            "description": "Get current fulfillment status and tracking info for an order ID.",
            "parameters": {
                "type": "object",
                "properties": {
                    "order_id": {
                        "type": "string",
                        "description": "The unique order identifier, e.g., ORD-992"
                    }
                },
                "required": ["order_id"]
            }
        }
    }
]
Enter fullscreen mode Exit fullscreen mode

Next, build the execution dispatcher that processes the model's structured intent safely:

def handle_tool_call(tool_name: str, raw_arguments: str) -> str:
    """Parses arguments and routes execution to the correct backend function."""
    tool_registry = {
        "fetch_order_status": fetch_order_status
    }

    if tool_name not in tool_registry:
        return json.dumps({"error": f"Tool '{tool_name}' is not registered."})

    try:
        arguments = json.loads(raw_arguments)
        result = tool_registry[tool_name](**arguments)
        return json.dumps(result)
    except json.JSONDecodeError:
        return json.dumps({"error": "Invalid JSON arguments generated by model."})
    except Exception as exc:
        return json.dumps({"error": f"Execution failed: {str(exc)}"})
Enter fullscreen mode Exit fullscreen mode

When a user asks "Where is my order ORD-992?", the model returns a structured tool call request for fetch_order_status with {"order_id": "ORD-992"}. Your Python backend runs handle_tool_call, retrieves the live record, and passes it back to the model to generate the final response.

Building Reliable Agents in Production

While model reasoning gets most of the attention, the system engineering around tool execution is what makes an agent production-ready.

1. Define Strict Schemas

Models rely entirely on your function descriptions. Vague descriptions lead to invalid parameter formatting or wrong tool choices. Use explicit JSON Schemas or Pydantic models to define exact types and constraints.

2. Prepare for Edge Cases

APIs time out, external services drop connections, and LLMs can occasionally generate malformed JSON. Your backend dispatcher must capture these failures cleanly and feed structured error messages back to the model, allowing it to recover or inform the user safely.

3. Enforce Permissions and Guardrails

Never grant an LLM direct execution access to production infrastructure without boundaries. Implement strict API scopes, read-only permissions where applicable, and require human confirmation steps before invoking high-impact functions like processing refunds or deleting user records.

Function calling converts an LLM from a simple conversational bot into a predictable interface for your backend services. Getting the surrounding orchestration code right is key to building systems that don't break quietly in production.

About me

Tanmay builds AI agents and automation for businesses and is open to freelance projects. Invite readers to connect on LinkedIn.

Top comments (0)