Lesson 1 of 5 Building Tool Ecosystems with MCP

Controlling Function Calling with Specified and Dynamic Tool Use

This lesson explains function calling and the difference between specified and dynamic tool use, which decide how much freedom the model has in choosing the tools through which agents query systems, perform actions and run code.

Learning outcomes

After completing this lesson, you will be able to:

  • Describe how function calling works.
  • Control tool selection with the tool_choice parameter.
  • Decide when dynamic tool use suits a task and how to limit the number of tools.

How function calling works

With function calling, the application sends tool definitions with the request: each has a name, a description and a JSON Schema for its parameters. The model may respond with one or more function calls containing arguments that match the schema. The application executes the function, sends the result back in a function_call_output item, and the model continues. Strict mode constrains the arguments to the schema exactly. The model never executes anything itself; the application stays in control of what actually runs.

tools = [{
    "type": "function", "name": "get_order", "strict": True,
    "description": "Look up one order by its ID. Use before answering any question about an order.",
    "parameters": {"type": "object", "additionalProperties": False, "required": ["order_id"],
                   "properties": {"order_id": {"type": "string", "pattern": "^ORD-[0-9]{6}$"}}},
}]
r = openai.responses.create(model=CHAT, input=question, tools=tools,
                            tool_choice={"type": "function", "name": "get_order"})   # specified tool use
call = next(item for item in r.output if item.type == "function_call")
result = get_order(**json.loads(call.arguments))
r = openai.responses.create(model=CHAT, previous_response_id=r.id, tools=tools,
                            input=[{"type": "function_call_output", "call_id": call.call_id, "output": json.dumps(result)}])

The following diagram shows the messages exchanged for one function call.

How function calling works. The application sends tool definitions with the request: a name, a description and a JSON Schema for the parameters, with strict mode constraining arguments to the schema. The model may respond with a function call whose arguments match the schema. The application, not the model, executes the function and sends the result back as a function_call_output item, and the model continues to its answer.
The model proposes; the application executes.
Put in order

One function call round trip

Exam skills 2.3.1

Arrange the steps of a function call with the Responses API.

Log in to try this activity Interactive activities are free with an account.

Specified tool use

Specified tool use controls whether and which tool the model calls through the tool_choice parameter: auto lets the model decide, required forces at least one tool call, none disables tools, and naming a function forces that function. Forcing a tool suits workflow steps where a call is mandatory, such as always retrieving the customer record before answering, or extracting structured data through a function schema. Restricting the available tools per step (allowed tools) achieves a similar effect while leaving some choice.

Match the pairs

tool_choice values

Exam skills 2.3.1

Match each <code>tool_choice</code> setting with its effect.

Log in to try this activity Interactive activities are free with an account.

Dynamic tool use

Dynamic tool use gives the model freedom to choose tools, decide their order and call several in parallel, which suits open-ended tasks. It also covers tools discovered at run time: an agent connected to an MCP server lists the server's current tools, and an agent with a very large catalogue can select a relevant subset per request by embedding similarity between the request and tool descriptions. Tool selection accuracy depends on clear names, distinct descriptions that say when to use and when not to use each tool, and a manageable number of tools per call.

The following table summarizes the options.

ModeSetting or mechanismUse when
Model choosestool_choice autoOpen-ended tasks with several useful tools
At least one calltool_choice requiredThe answer must come from a tool
Specific tooltool_choice naming the functionA workflow step needs exactly that call
Discovered at run timeMCP list_tools, tool retrievalTool catalogues change or are large
Scenario questions

Letting the model choose

Exam skills 2.3.1

Apply dynamic tool use to agent scenarios.

Log in to try this activity Interactive activities are free with an account.
Worked example

Too many tools

An internal IT agent has 120 function tools. It often chooses a wrong but similarly named tool, and each request uses 18,000 input tokens before the user's question is processed.

Selecting the most relevant 10-15 tools per request by similarity between the request and tool descriptions reduces both errors and tokens, and rewriting descriptions to distinguish similar tools helps further. Alternatively, split the tools across specialist agents behind a router. A larger model might choose slightly better but would increase cost on every request.

Check yourself

How can a call to a particular tool be made mandatory?

Show answer

With tool_choice, which can force a specific tool, or any tool, on that request.

Exam Tips

Remember for the exam

  • The application executes tools; the model only proposes calls.
  • tool_choice forces a specific tool or any tool when a call is mandatory.
  • Large catalogues: select a subset per request or split across agents.