How function calling works

Function calling is an interaction protocol between an LLM and external systems. The model receives
a description of the available tools and, while generating its answer, decides whether one of them
needs to be invoked:

1. The developer describes the functions:
   search_products(query: string, category?: string) -> Product[]
   check_inventory(product_id: string) -> {in_stock: boolean, qty: int}
   add_to_cart(product_id: string, qty: int) -> CartItem

2. User: "Find me red Nike trainers under $120"

3. The model returns a call:
   {"function": "search_products", "args": {"query": "red Nike trainers", "max_price": 120}}

4. The client calls the API, receives a product list, passes it back to the model

5. The model generates an answer for the user from real data

The loop can repeat several times: the model calls functions in sequence, refines what it knows and
finally produces the answer.

Function calling in e-commerce

In a trading context, function calling opens interaction patterns that were not previously possible.
A typical tool set for a store’s AI assistant:

Function What it does Type of action
search_products Semantic search across the catalogue Read
get_product_details Full product information Read
check_inventory Availability and quantity Read
get_recommendations Personalised recommendations Read
add_to_cart Adds an item to the basket Write
apply_promo Applies a promo code Write
initiate_checkout Starts order placement Write

Read functions are safe. Write functions — initiate_checkout above all — require explicit
confirmation from the user or a human-in-the-loop control.

Structured output and JSON Schema

Reliable function calling requires strict typing. A function description in JSON Schema:

{
  "name": "search_products",
  "description": "Search products by query and filters",
  "parameters": {
    "type": "object",
    "properties": {
      "query": {"type": "string", "description": "Search query"},
      "category": {"type": "string", "description": "Product category"},
      "max_price": {"type": "number", "description": "Maximum price"}
    },
    "required": ["query"]
  }
}

Important: models hallucinate function arguments, especially for parameters that do not exist.
Always validate the model’s output before making a real API call.

Parallel and sequential calls

Modern models support calling several functions in a single turn, which matters for compound
requests:

  • In parallel: stock, price and reviews for one product
  • In sequence: first find the product, then check its availability by product_id

Parallel calling lowers response latency but requires dependencies between calls to be managed.