Zum Inhalt springen

Tool Use & Function Calling

Knowledge

An LLM on its own can only produce text. It's tool use that turns it into an agent: it can query APIs, search databases, read files, execute code, and send emails. Tools are the agent's hands and eyes.

What Is Function Calling?

Function calling is the mechanism through which an LLM invokes a tool in a structured way. Instead of responding with free-form text, the model returns a JSON block that precisely describes which tool to call with which parameters.

The Flow

  1. You provide the LLM with a list of available tools (name, description, parameters)
  2. The LLM decides whether and which tool to call
  3. It returns a structured tool call (JSON)
  4. Your system executes the call and returns the result
  5. The LLM processes the result and either responds or calls another tool

Tool Definition

Here's what a typical tool definition looks like:

{
  "name": "search_database",
  "description": "Searches the product database for items",
  "parameters": {
    "type": "object",
    "properties": {
      "query": {
        "type": "string",
        "description": "Search term"
      },
      "category": {
        "type": "string",
        "enum": ["electronics", "clothing", "books"],
        "description": "Product category (optional)"
      },
      "max_results": {
        "type": "integer",
        "description": "Maximum number of results",
        "default": 10
      }
    },
    "required": ["query"]
  }
}

Tool Call by the LLM

When the LLM decides to use the tool, it responds like this:

{
  "tool_calls": [{
    "name": "search_database",
    "arguments": {
      "query": "USB-C cable",
      "category": "electronics",
      "max_results": 5
    }
  }]
}

iImportant

The LLM doesn't execute the tool call itself! It only generates the intent. Your code (the "orchestrator") performs the actual API call and returns the result to the LLM.

Common Tool Categories

Information Retrieval:

  • Web Search -- Current information from the internet
  • Database Query -- Structured data from databases
  • File Read -- Reading documents and files
  • API Calls -- Querying external services (weather, stocks, CRM)

Executing Actions:

  • File Write -- Creating or modifying files
  • Email Send -- Sending messages
  • Code Execute -- Running Python, JavaScript, etc.
  • Database Write -- Storing or updating data

Analysis and Processing:

  • Calculator -- Mathematical computations
  • Image Analysis -- Describing or analyzing images
  • Data Transform -- Converting data (CSV to JSON, etc.)

What actually happens technically when an LLM 'calls' a tool?

Understand

Best Practices for Tool Design

1. Clear Descriptions

The LLM decides whether to use a tool based on its description. A vague description leads to incorrect or missed tool calls.

// Bad
{ "name": "do_stuff", "description": "Does things" }

// Good
{
  "name": "get_customer_orders",
  "description": "Retrieves all orders for a customer from the last 30 days. Returns order number, date, status, and total amount."
}

2. Few, Focused Tools

Don't give the agent 50 tools at once. The more tools available, the more often the LLM picks the wrong one. 5-10 well-described tools are better than 50 vague ones.

3. Sensible Defaults

Make parameters optional where it makes sense. The LLM doesn't need to explicitly set every parameter when a good default exists.

Multi-Tool Orchestration

In practice, agents often use multiple tools in combination. A support agent might, for example:

  1. get_customer_info(email) -- Load customer data
  2. search_orders(customer_id) -- Search orders
  3. check_return_policy(order_id) -- Check return policy
  4. create_return_ticket(order_id, reason) -- Initiate a return

The LLM decides at each step which tool to use next -- based on the results from previous tools.

*Parallel Tool Calls

Modern APIs like Claude and GPT support parallel tool calls. When the agent recognizes that two queries are independent, it can start both simultaneously. This saves time and money.

Why shouldn't you give an agent 50 tools at once?

Apply

Task

Find the current Bitcoin price

Press Start or Space to start the agent loop.

Complete the tool definition for a weather API:

{ "name": "get_weather", "description": "Returns the current weather for a city", "parameters": { "type": "object", "properties": { "city": { "type": "", "description": "Name of the city" } }, "required": ["city"] } }

Reflect

Tool use transforms LLMs from text generators into actors. Quality depends on precise tool descriptions and a limited selection -- 5 to 10 focused tools lead to better results than an overloaded toolbox. Less truly is more here.