liveBook · Manning

Tool use

As explained in Context Engineering

Tool calling is a model capability for requesting that an external tool or operation be invoked.

Tool use lets an LLM request external operations, such as API calls, database queries, or local code execution. The model emits a structured request; an agent harness validates and executes it, then returns the result to the model. This separates probabilistic reasoning from external execution. section 4.2 section 1.5.3

Tool definitions put names, descriptions, and argument schemas in the context window. The resulting calls and outputs usually join the working context; only relevant outputs are later persisted as memory. Tools therefore extend both what an agent can do and what the model knows when it generates a response. section 1.5.3 section 4.6.1

A function-calling cycle has four main steps:

  • The harness assembles the prompt, instructions, active state, and tool definitions for the model. section 4.2
  • The model returns a call identifier, function name, and typed arguments. section 4.2
  • The harness validates and executes the request, then adds the call and its output to the message history, matched by identifier. section 4.2
  • A second model call uses the result to generate a grounded response. Identifiers also allow multiple tool calls to be correlated with their outputs in a single turn. section 4.2
Figure 4.2 Function calling workflow: tool definitions and the user prompt enter the context window, a first model call returns a tool call, the tool executor runs it, and a second model call turns the tool output into the final answer
function calling v9

Follow the data flow between the context window, model, harness, and external tool. section 4.2

Function calling uses predefined, schema-described capabilities. A CLI lets an agent drive local executables through a shell, while MCP provides a protocol for servers to expose tools, resources, and prompts. These paradigms share the pattern of a model producing an invocation for the harness to execute. section 1.5.3 section 4.7

Weather lookup

A Python application exposes a mock weather function to an OpenAI model. It sends the function definition with the prompt, executes the requested function, and adds the returned weather value to the model’s context for the final answer. section 4.6.1

Listing 4.1 Python script using an OpenAI model and a mock weather tool
from openai import OpenAI
import json

client = OpenAI()

TOOLS = [{ #1
    "type": "function",
    "name": "get_weather", #2
    "description": "Get the current weather in a location",
    "parameters": {
        "type": "object",
        "properties": {
            "location": {
                "type": "string",
                "description": "City or place name"
            }
        },
        "required": ["location"]
    }
}]

def query_model(prompt, model="gpt-4o-mini"):
    response = client.responses.create( #3
        model=model,
        input=prompt,
        tools=TOOLS,
        tool_choice="required",
    )
    input_items = list(response.output)
    for item in response.output: #4
        if item.type != "function_call":
            continue
        function_name = item.name
        function_args = json.loads(item.arguments)
        result = route_function_call(function_name, function_args) #5
        input_items.append({
            "type": "function_call_output",
            "call_id": item.call_id,
            "output": json.dumps(result),
        })
    final_response = client.responses.create( #6
        model=model,
        input=input_items,
        tools=TOOLS,
    )
    return final_response.output_text

Inspect the Python tool definition and model request used in the weather example. section 4.6.1

Each tool cycle adds calls and outputs to the active context and requires at least two model calls, so long loops can consume context capacity quickly. Because tools can act on external systems, apply controls such as least-privilege permissions, argument validation, sandboxing, approval gates, and invocation tracing. section 4.2 section 4.7

How Context Engineering covers it

How deep it goes
Explains the request-execution-response cycle, including context assembly, call identifiers, parallel calls, and context costs.
Examples
Uses a mock weather lookup and a bug investigation involving an issue function, shell command, and MCP documentation.
What you can do after
Apply function calling via the OpenAI API to execute a local function and return its result to the model.
How it connects
Connects tool use with context windows, agent harnesses, function calling, command-line interfaces, and MCP.

Also explained in

Compare how 18 books explain tool use →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Context Engineering. The section links point to the parts of the book it draws on.