Function calling

Definition
The mechanism that lets an AI model trigger real code, look something up, send something, update a record, rather than only produce text.

Why it matters

This is the step from a chatbot that talks to an assistant that acts. Without it, a model can describe how to check an order. With it, the model checks the order, using the real data rather than a guess, which also reduces made-up answers. It also moves part of the reliability problem onto the functions. The model chooses which one to call and what to pass in, so clear descriptions and checks on your side decide whether it behaves.

How to apply it

  • Describe each function narrowly: what it does, what it needs and what it returns.
  • Validate every input before running it. A value is not safe because a model produced it.
  • Keep functions small and single-purpose. A function that does too much gives the model more ways to misuse it.
  • Start with read-only functions, and add write access later.
  • Log every call and its inputs, so a wrong action can be traced.
  • Add human approval before anything hard to reverse, such as sending money or deleting a record.

What it is

A language model on its own only produces text. Function calling lets it ask for something to be done. The developer describes a set of functions, each with a name, a purpose and the inputs it needs. When the model decides one is useful, it replies with the function name and the values to pass in. The surrounding software runs the function and hands the result back, and the model carries on with that result in front of it.

The model never runs anything itself. Your code does, and your code decides whether to allow it. Different AI providers use different names for the same idea, mostly tool use or function calling.

Common mistakes

  • Trusting the arguments. The model may pass an order number that belongs to another customer. Check permissions in your code, not in the prompt.
  • Vague descriptions. If two functions sound alike, the model picks the wrong one. Say what each does, and when not to use it.
  • Giving write access too early. A model that can send, delete or pay can do real damage on its first wrong guess. Start with read-only functions.
  • One large function. A function that does five things gives five ways to go wrong and is hard to log or test.
  • No log. Without a record of each call and its inputs, a wrong action cannot be traced.
  • Leaving out errors. If a function fails and the model is not told clearly, it may carry on as if it succeeded.
Worked example

Suppose a support team of six runs an assistant with two functions. The first looks up an order by its number in the shop's system. The second drafts a refund but does not send it. A customer asks where their parcel is. The model calls the lookup with the order number from the message, receives the real delivery status and answers from that result rather than from a guess. A person reviews every drafted refund before anything is paid.

The team builds this on the Anthropic API, where its own code receives each function request, checks the inputs and runs the lookup. In the test run the assistant drafted refunds only for delivered orders, because the function description said so. The model never runs anything itself. Its code does, and the code decides what is allowed.

Tools in the example

Some links are affiliate links: we may earn a commission at no cost to you. It never decides a ranking. How we work with partners

  1. Article

    MCP server

    A standard way of offering functions and data to an AI agent.

  2. Article

    Guardrails

    The checks that stop a function call doing harm unattended.

  3. Article

    Autonomous agent

    An agent that chains several calls together to reach a goal.

  4. Article

    Hallucination

    What a real lookup helps to prevent.