Skip to main content

How to build an LLM app with function calling (tools)

Wire function calling into an LLM app: define tool schemas, detect tool-call requests, execute your functions, return results, and loop until the model produces a final answer.

Difficulty
Intermediate
Duration
50 minutes
Steps
6

What function calling is

Function calling, sometimes called tool use, lets an LLM ask your application to run a function. The model does not run code itself. Instead it emits a structured request naming a function and its arguments. Your code executes the function, returns the result, and the model continues. This turns a chat model into something that can check inventory, query a database, or send an email.

Prerequisites

  • An LLM that supports tool calling
  • A runtime such as Python 3.10+ or Node.js 18+
  • Functions you want to expose, each with clear inputs and outputs

Steps

1. Define your tools as schemas

Each tool needs a name, a description, and a JSON Schema for its parameters. The description teaches the model when to use the tool.

{
  "name": "get_weather",
  "description": "Get the current weather for a city",
  "input_schema": {
    "type": "object",
    "properties": {"city": {"type": "string"}},
    "required": ["city"]
  }
}

2. Send the user message with tool definitions

Include the tool list in your API call alongside the conversation. The model decides whether to answer directly or request a tool.

3. Detect a tool-call request

Inspect the response. If it contains a tool-call block, extract the tool name and parsed arguments.

if response.stop_reason == "tool_use":
    call = response.tool_calls[0]

4. Execute the function and return the result

Map the tool name to your real function, run it, and send the output back as a tool-result message tied to the call id.

result = registry[call.name](**call.arguments)
messages.append(tool_result(call.id, result))

5. Loop until a final answer

The model may chain several tool calls. Repeat send, detect, execute until the response is plain text with no further tool requests.

6. Handle errors and bad arguments

Validate arguments before executing. Return a clear error string as the tool result so the model can recover, retry, or ask the user.

Verification

Ask a question that requires a tool, such as live weather. Confirm the model requests the tool, your function runs, and the final answer reflects the returned data. Then ask a question that needs no tool and confirm the model answers directly.

Next Steps

Add more tools, enforce timeouts and rate limits, log every call for auditing, and add guardrails so destructive actions require confirmation.

Prerequisites

  • Familiarity with JSON
  • An LLM that supports tool calling
  • A programming runtime (Python or Node)

Steps

  • 1
    Define your tools as schemas
  • 2
    Send the user message with tool definitions
  • 3
    Detect a tool-call request
  • 4
    Execute the function and return the result
  • 5
    Loop until a final answer
  • 6
    Handle errors and bad arguments

Category

AI ML