Concepts / Function Calling in Language Models

Function Calling in Language Models

A single model performs one kind of action, while external tools let an agent perceive its environment and act upon it.

  • Programming

Where a Single Model Stops

Picture a model working entirely on its own. A language model can produce text, and an image generator can produce pictures. Each one is good at a single kind of output. Neither model, by itself, can check the weather, look up a bank balance, or send a message. Its reach ends where its own inputs and outputs end.

What do you think happens?

Can a text-only language model check today's weather by itself, without any help from outside the model?

  • Yes, because it was trained on a large amount of text

  • No, because its reach ends where its own inputs and outputs end

Reveal answer
Answer:

No, because its reach ends where its own inputs and outputs end

Training gives the model patterns in text, but checking the current weather requires reading from something outside the model. That is the gap that tools fill.

An agent is different because it can be connected to things outside the model. A system does not need external tools to count as an agent, but without them its capabilities stay narrow. The central question for this article is this: once an agent can reach beyond the model, what kinds of tools does it need, and what does each kind change about the agent's behavior?

What Tools Add to an Agent

Tools do two jobs. Some let an agent take in information about its surroundings, and others let it change something in those surroundings. Both jobs matter. An agent that can only observe is stuck watching, and an agent that can only act has no picture of what it is acting on.

Definition

The tool inventory is the full set of tools an agent can call. The inventory determines what the agent is able to do, which makes it a design decision. Adding tools widens what the agent can accomplish, but a larger inventory is harder to understand and to use well. Choosing the right inventory usually takes experimentation rather than a single correct answer.

Reading Versus Writing: Perceive and Act

Read-only tools let a model draw information from its data sources. Write tools let the same kind of system change those sources. The difference is easiest to see with one tool that does both. A SQL executor can retrieve a data table, which is a read, but it can also change or delete that table, which is a write. An email API can read a message and can also send a reply. A banking API can report a current balance and can also initiate a transfer.

The two directions behave differently. A read flows from the data source back to the agent, so the agent gains information without changing anything. A write flows from the agent outward into the environment, so the agent's decision now has consequences that persist after the call finishes. Write actions are what make automation possible. An agent could research prospective customers, draft and send emails, read the replies, follow up, record orders, and update a database, all without a person handling each step.

Retrieve data tableSQL executor: readChange or deletetableSQL executor: writeRead messageEmail API: readSend replyEmail API: writeReport balanceBanking API: readInitiate transferBanking API: write
What is the difference between a tool that only reads state and one that changes state, and how does each one affect the environment the agent sees afterward?

A helpful comparison is the workplace. A new intern should not be given authority to delete the production database, and an unreliable AI should not be allowed to start bank transfers. Humans make mistakes too. Tools are what let both humans and models accomplish far more than they could alone.

Three Families of Tools

Once an agent's inventory is in view, its tools can be sorted into three families. The families are not mutually exclusive. A single tool can serve more than one purpose, and an agent's inventory will often mix all three. The map below shows how the families relate to the agent they extend.

Agent with toolsKnowledgeaugmentationenriches what the agentknows nowKnowledge retrievalexample: Chameleonretrieval toolCapability extensioncovers tasks models handlepoorlyCalculator and codeinterpreternarrow, reliable jobsEnvironmentinteractionwrite actions on theenvironmentEmail send and banktransferactions that change stateincludesincludesincludesexampleexampleexample
How do the three tool categories relate to one another and to the single agent they extend, and which example tools belong under each?

Tracing One Request Through Tools

A tool call is not a single event. It is an exchange among three parties: the model, a runtime that handles the call, and the external tool. The sequence below is a generic illustration of this round trip, showing one request from start to finish. Read it as a pattern rather than as a fixed protocol.

ModelFunction-call runtimeExternal tool1. emits structured call2. executes the call3. returns result4. passes result back5. writes final answer
In what order do the model, the function-call runtime, the external tool, and the model's final answer exchange messages for one request?
Worked example

A question that needs arithmetic

A user asks: 'What is 48 multiplied by 37?' The agent has a calculator tool in its inventory.

  1. 1
    Recognize the need

    The model identifies that exact multiplication is a task it handles poorly, so the calculator is the appropriate tool rather than a guessed answer.

  2. 2
    Emit the call

    The model produces a structured request naming the calculator and giving the operands 48 and 37.

  3. 3
    Execute the tool

    The runtime passes the request to the calculator, which performs the narrow job reliably and returns 1776.

  4. 4
    Compose the answer

    The result returns to the model, which uses it to write the reply to the user. The model still does the reasoning around the number.

Result

The user receives the answer 1776, and the arithmetic was done by the tool rather than by the model's weights.

How a Planner Routes Requests

Tools only help if the agent knows which ones to use and in what order. That job belongs to planning. The model is responsible for solving a task, and a task is defined by its goal and its constraints. The goal says what must be achieved, and the constraints limit how it may be achieved. Complex tasks cannot be handled in a single leap, so the agent produces a plan: a roadmap that lays out the steps needed to accomplish the task.

The planner is the component that decides which tools to call and in what order. In a multimodal setting, the planner reads the incoming request and decides whether text generation, image generation, or both are needed. Either branch can run alone, and when the request calls for both, their outputs are combined into one reply. The same routing pattern explains how ChatGPT produces text and images, since it relies on DALL-E as its image generator.

Incoming requestImage needed?Text-to-image toolCombine outputsReply to userText needed?Language model textplanyesnoyesimage outputtext outputsingle reply
When a request arrives, which branch does the planner choose at each step, and how do outputs from one tool feed into the next step or back into the planner?
Worked example

Routing a request that needs both text and a picture

A user writes: 'Describe a calm lake at sunrise, and draw it.' The planner must decide which branches to run.

  1. 1
    Read the goal

    The goal has two parts: a written description and a picture. The planner records both as required outputs.

  2. 2
    Choose the branches

    Because both outputs are needed, the planner runs the text branch and the image branch rather than only one of them.

  3. 3
    Route to the image tool

    The text-to-image tool receives a drawing request. The model itself never learns to draw; it hands the request to a tool that can.

  4. 4
    Combine the results

    The text and the image are merged into one reply, which is returned to the user.

Result

One reply contains both the description and the picture, produced by two tools coordinated by the planner.

The idea runs in the other direction too. A model that accepts only text can call an image captioning tool to interpret pictures, a transcription tool to handle audio, or an OCR tool to read text out of PDFs. Tool use can also outperform the alternatives: the Chameleon system pairs a GPT-4-powered agent with 13 tools and outperforms GPT-4 alone on several benchmarks. On ScienceQA, Chameleon improves on the best published few-shot result by 11.37%. On TabMWP, a benchmark of tabular math word problems, it improves accuracy by 17%.

Mistakes Beginners Make

  • Common mistake

    Assuming a model with tools can do anything a tool can do without limits

    Why it fails

    Each tool covers a narrow job. A calculator does arithmetic reliably, but it does not check the weather or send a message.

    Do this instead

    Ask which specific tool in the inventory covers the task before assuming the agent can do it.

  • Common mistake

    Treating every tool as read-only because it looks like a lookup

    What it looks like

    A SQL executor can retrieve a table, but the same tool can also change or delete that table.

    Why it fails

    A single tool can perform both reads and writes, and the write side persists after the call finishes.

    Do this instead

    Check what each operation of a tool changes before granting access.

  • Common mistake

    Believing a bigger tool inventory is always better

    Why it fails

    A larger inventory widens what the agent can accomplish, but it is harder to understand and to use well.

    Do this instead

    Choose the inventory through experimentation, adding tools that serve real needs.

  • Common mistake

    Confusing the planner with a tool; the planner decides which tools to call, and it is not itself one of the tools in the inventory

Practice: Classify and Route

MEDIUM

For each tool below, decide whether it is read-only or a write action, and name the tool family it belongs to: (a) a weather lookup, (b) a tool that sends a reply email, (c) a unit converter that changes pounds to kilograms, (d) a retrieval tool that adds documents to the model's context. Then describe the route a planner would take for the request 'Translate this paragraph and then email it to my team.'

Hints
  • A read flows from the data source back to the agent, and a write flows outward into the environment.
  • Knowledge augmentation supplies material into working context; capability extension covers tasks a model handles poorly; environment interaction changes state.
  • The request has two steps, so think about which output must feed into which next step.

Tools and the Single Agent

  1. 1
    A single model's reach ends at its own inputs and outputs, while an agent connected to external tools can perceive its environment and act upon it.
  2. 2
    Read-only tools let an agent gather information without changing anything, and write tools change data sources or the environment with greater risk.
  3. 3
    The three tool families are knowledge augmentation, capability extension, and environment interaction, and one tool may serve more than one purpose.
  4. 4
    A planner decides which tools to call and in what order, routing a request through text, image, or both branches before combining the results.

Key Takeaways

  • A single model performs one kind of action, while external tools let an agent perceive its environment and act upon it.
  • Read-only actions gather information, and write actions change the environment or data sources with greater risk.
  • Tools fall into three families: knowledge augmentation, capability extension, and environment interaction.
  • A planner decides which tools to call and in what order, forming a plan around the task's goal and constraints.

Related concepts

More concepts starting with F