50  Tool Use & Function Calling

TipTL;DR

Tool use extends a language model beyond text generation: the model emits structured calls to external functions (search, code interpreter, APIs), receives the results, and continues reasoning. The backbone is any instruction-following model; the pattern is model-agnostic.

Depends on: Agent Architectures Overview

50.1 Why this matters

A pretrained language model’s weights encode knowledge and reasoning ability, but not access to anything that changes after training: today’s weather, a private database, or the exact result of a calculation. Tool use closes that gap without retraining: the model learns when and how to call an external function, and treats the function’s return value as new information to reason over, the same “generate, then condition on new input” loop as Alignment’s RAG section, applied to arbitrary functions instead of only a document retriever.

50.2 The mechanism

50.2.1 ReAct: interleaving reasoning and acting

ReAct (Yao et al., 2022) structures generation as an explicit interleaving of three step types, repeated until the task is done.

Thought Action Observation Tool / environment
  • Thought: a reasoning step in natural language, deciding what to do next.
  • Action: a call to a tool, formatted so it can be parsed and executed, for example search("current weather in Tokyo").
  • Observation: the tool’s return value, inserted back into the context for the next Thought.

This is chain-of-thought reasoning (Scaling Laws) with the ability to pause and consult the outside world mid-reasoning, rather than reasoning purely from what the model already knows.

50.2.2 Function calling as a structured contract

Modern instruction-tuned models support function calling directly: the model is given a schema describing available functions (name, arguments, types) and, instead of writing free-form text, emits a structured call matching that schema, which the calling application executes and returns the result of. This turns tool use from a prompting trick (ReAct’s parsed-from-text actions) into a first-class, reliably-parseable output format.

import json

def call_model_with_tools(user_query, available_tools):
    # A tool-calling model outputs a structured call instead of free text.
    return {"tool": "search", "arguments": {"query": user_query}}

tools = [{"name": "search", "parameters": {"query": "string"}}]
call = call_model_with_tools("current weather in Tokyo", tools)
print(json.dumps(call, indent=2))
{
  "tool": "search",
  "arguments": {
    "query": "current weather in Tokyo"
  }
}

50.2.3 Code execution and self-taught tool use

Two further points fill out how tool use is built and trained in practice.

  • Code interpreter: a code interpreter is a tool like any other, except its “return value” is the result of running arbitrary code the model wrote. This is particularly powerful for tasks with a verifiable, computable answer (arithmetic, data analysis, plotting): the model does not need to internally simulate execution, it writes code, runs it, and reads back the real result.
  • Toolformer (Schick et al., 2023): shows tool-calling ability can be taught with self-supervision rather than hand-written examples. The model generates candidate API calls at plausible points in a large text corpus, keeps only the calls that measurably improve its ability to predict the following text, and fine-tunes on the resulting self-filtered examples.

50.2.4 What to observe

  • ReAct is a prompting pattern (structuring free-text output); function calling is a model capability (a trained-in structured output format). Modern systems mostly use the latter, with ReAct-style reasoning interleaved around it.
  • A code interpreter is not a special case: it is a tool whose “observation” happens to be a program’s real output rather than a search result.
  • Toolformer’s filtering signal (does this call improve next-token prediction?) is the same self-supervised principle behind Word Embeddings and every masked or contrastive objective in this book: manufacture the training signal from the data itself.

50.3 Application & impact

Concept here What it became Where you see it today
ReAct’s thought-action-observation loop The template for agentic reasoning Nearly every LLM agent framework
Function calling A trained model capability, not a prompt trick Standard API feature of modern instruction-tuned LLMs
Code interpreter Verifiable computation as a tool Data analysis and coding agents
Toolformer’s self-supervised filtering Tool use learned without hand-labeled examples A recurring idea in agent training research
NoteKey takeaway

Tool use turns a static, frozen-knowledge model into a system that can consult the outside world mid-reasoning. ReAct established the pattern as a prompting technique; function calling made it a trained-in capability; Toolformer showed the capability itself can be learned self-supervised, the same manufactured-label principle threaded through this entire book.

← Back to: Agent Architectures Overview