In this chapter
- Build Two tools and one round trip by hand: the model asks, our program looks the order up.
- Learn How to read a tool call and tell who runs the tool.
- Find out Why one round trip isn’t enough for Ada’s question.
300 orders for one grinder
In chapter 2 I sent the model all three of Ada’s orders to answer one question about her grinder. As an experiment, I added 300 orders from other customers. That took the input from about 400 tokens to about 17,000. Ada got the same shipping update either way.
Across the café counter, someone could say “let me look that up” and open the order Ada asked about. What would the model need to do the same?
The model can’t open an order. It can ask our program to, and the result travels back in a second request.
No order details any more.
A name, a description and a schema each.
“My order A-1042 has not arrived yet. …”
- name
- getOrder
- arguments
- id: A-1042
Not an answer. The model fills in a form, and what happens with it is up to our program.
Our program runs getOrder and adds the call and its result to the request.
{"id":"A-1042", … "status":"shipped",
- name
- trackShipment
- arguments
- trackingId: TRK-58213
A second form. This program stops after two requests, so nobody picks it up.
How does the model ask for an order?
Function tool
Our program gives the model a name, a description and a schema for the arguments. We keep the function that looks up the order.
The model can return a tool call alongside a message or on its own. For Ada, the call names getOrder and supplies {"id":"A-1042"} as its arguments. OpenAI calls this function calling. OpenAI’s function calling guide
- name
- getOrder
- description
- Returns status, items, total and (once shipped) the tracking id of one order.
- parameters
- id: stringAn order id from the index.
run: ({ id }) => findOrder(id) ?? { error: … }Our code looks for a matching order number. The sample data holds Ada’s three orders.
The description tells the model what getOrder can look up. The schema tells it to put Ada’s order number in the id field.
What’s still missing after getOrder? Ada’s order says the grinder has shipped and gives a tracking ID. The carrier has the last scan and the delivery estimate. Our second tool, trackShipment, uses that ID to look them up.
Build it
One file is new in typescript/03-tools. Here’s the entry for getOrder in tools.ts. Its run function looks up an order by ID.
export const tools: Tool[] = [
{
name: 'getOrder',
description: 'Returns status, items, total and (once shipped) the tracking id of one order.',
parameters: {
type: 'object',
properties: { id: { type: 'string', description: 'An order id from the index.' } },
required: ['id'],
additionalProperties: false,
},
run: ({ id }) => findOrder(id) ?? { error: `Unknown order: ${id}` },
},Our sample shop contains only Ada’s three orders. In a shared order table I’d look up the order using both its number and the customer ID from the signed-in session.
The Request tab shows what goes into the first call. The shop policies and Ada’s order numbers and dates are already there. In the Code tab, index.ts checks the response for function_call items. If there are none, it prints the answer as it did in chapter 1.
// What the model is told about each tool. The function itself stays with us.
const definitions = tools.map((tool): FunctionTool => ({
type: 'function',
name: tool.name,
description: tool.description,
parameters: tool.parameters,
strict: true,
}));
const input: ResponseInputItem[] = [{ role: 'user', content: question }];
// Request 1: the model answers, or it asks for a tool.
const first = await client.responses.create({ model, instructions, input, tools: definitions });
console.log(`request 1 (${first.usage?.input_tokens} input tokens)`);
const calls = first.output.filter((item) => item.type === 'function_call');
if (calls.length === 0) {
console.log(first.output_text);
process.exit();
}gpt-6.2-solYou are the assistant of a small online shop that sells coffee.
You answer questions from its customers.
Answer in two or three sentences, in the language of the question.
If you do not know something, say so and offer to pass the question on to the support team.
Shop policies:
Cancellation: an order can be cancelled free of charge while its status is "processing". Once it has shipped it cannot be cancelled.
Returns: a delivered order can be returned within 30 days of delivery for a full refund.
Shipping: delivery takes 3 to 5 working days after an order has shipped.
Orders of the customer, Ada:
- A-0981, placed on 2026-10-02
- A-1042, placed on 2026-11-14
- A-1077, placed on 2026-11-19[
{
"role": "user",
"content": "My order A-1042 has not arrived yet. Where is it, and when should I expect it?"
}
][
{
"type": "function",
"name": "getOrder",
"description": "Returns status, items, total and (once shipped) the tracking id of one order.",
"parameters": {
"type": "object",
"properties": {
"id": {
"type": "string",
"description": "An order id from the index."
}
},
"required": ["id"],
"additionalProperties": false
},
"strict": true
},
{
"type": "function",
"name": "trackShipment",
"description": "Returns the last scan and the expected delivery date of one shipment.",
"parameters": {
"type": "object",
"properties": {
"trackingId": {
"type": "string",
"description": "The tracking id of a shipped order."
}
},
"required": ["trackingId"],
"additionalProperties": false
},
"strict": true
}
]With strict: true, a call to getOrder must supply an id string. Even a made-up order number is a string. The lookup tells us whether that order exists in the records the function can read. Here’s how index.ts runs each function the model asked for and sends its result back.
// The model only asked. Running the tool is up to our code.
input.push(...(first.output as ResponseInputItem[]));
for (const call of calls) {
const tool = tools.find((t) => t.name === call.name);
const result = tool
? tool.run(JSON.parse(call.arguments))
: { error: `Unknown tool: ${call.name}` };
console.log(`→ ${call.name}(${call.arguments})`);
console.log(`← ${JSON.stringify(result)}`);
input.push({
type: 'function_call_output',
call_id: call.call_id,
output: JSON.stringify(result),
});
}
// Request 2: the same conversation, now with the result in it.
const second = await client.responses.create({ model, instructions, input, tools: definitions });Both the model’s call and our result go into input. The matching call_id links the result to the call that requested it.
Run it
If you set up the repository in chapter 1, this chapter is one command.
pnpm ch03This came back from gpt-6.2-sol.
request 1 (319 input tokens)
→ getOrder({"id":"A-1042"})
← {"id":"A-1042","placedOn":"2026-11-14","status":"shipped","shippedOn":"2026-11-17","trackingId":"TRK-58213","items":["1 x hand grinder","1 x espresso beans, 1 kg","1 x paper filters, pack of 100"],"total":"88.40 EUR"}
request 2 (428 input tokens)
The model asks for another tool: trackShipment({"trackingId":"TRK-58213"})Follow the two IDs. Ada supplied A-1042, so the model could ask for getOrder in its first response. Only the result of that lookup supplied TRK-58213. The model uses it in its second response to ask for trackShipment. Our program stops there. It has run one tool, and Ada is still waiting for a delivery estimate.
Key idea
The model asks. Our code runs.
The first request was a little smaller than the one with Ada’s three full orders in chapter 2. Across both requests I sent nearly twice as many input tokens, and in chapter 2 Ada already had her delivery estimate. What changed was which details I sent. The model saw the items in her grinder order only after it asked for them. The contents of her other two orders stayed in the shop. So would the 300 orders from other customers that pushed chapter 2’s request to about 17,000 tokens. The request no longer grows with the shop’s order table.
What travels with a tool call?
With tools, two kinds of text are new in the request: the result of a call and the definitions of the tools. Each brings a problem.
A result is text the model reads
Look at the order our program sent back. The model gets to read every item name. The shop wrote those. A result from trackShipment would also carry the carrier’s scan text, written outside the shop. Someone could put instructions in a text like that to steer the reply. That’s indirect prompt injection. The text reaches the model through the tool result. OWASP on prompt injection · Anthropic on untrusted tool results
[…]
← {"id":"A-1042","placedOn":"2026-11-14","status":"shipped","shippedOn":"2026-11-17","trackingId":"TRK-58213","items":["1 x hand grinder","1 x espresso beans, 1 kg","1 x paper filters, pack of 100"],"total":"88.40 EUR"}
[…]Unused tool definitions still take up space
Tool definitions are text in the request too, billed as input on every call. Ada’s question about returns needs no tool: the model answers from the policies, and both definitions went along anyway. Two are cheap. OpenAI suggests keeping the number small, below twenty at the start of a turn. OpenAI on tools and token usage
request 1 (303 input tokens)
You can return your order for a full refund within 30 days of delivery. The 30-day window starts when your order is delivered, not when you place it.Who runs the tool when the model asks?
In this chapter, our program ran the tool. That’s one of three places where a tool can run.
Our code reads the tool name and arguments from the response, calls getOrder and sends the order back.
For OpenAI’s web search we add { type: 'web_search' } to the tools in the request. OpenAI runs the search and the model can answer with what it finds in that same request.
The Model Context Protocol (MCP) is an open standard that lets an application use tools from a separate server. In chapter 10 we move the lookups to such a server, where the shop’s chat and support desk can both call getOrder. MCP documentation
The Node.js question with web search
In chapter 1 I asked for the latest Node.js LTS release and got an outdated answer. I tried the same question with OpenAI’s built-in web search. The last line of the output shows the query it ran. OpenAI’s web search guide · Code for this experiment
pnpm extra:cutoffFrom training alone:
**Node.js 24.x (“Krypton”)** is the latest LTS line. For the exact latest patch, check the [official Node.js download page](https://nodejs.org/en/download) or run `nvm version-remote --lts`.
With web search:
As of December 1, 2026, the latest Node.js LTS release is **v26.13.1** (“Lithium”), released on November 11, 2026. ([nodejs.org](https://nodejs.org/en/download/?utm_source=openai))
searched: site:nodejs.org Node.js latest LTS release December 1 2026 Node 26 LTS downloadWith web search the model found the right LTS line, Node.js 26. It still missed a release. Node.js had published 26.14.0 the day before. Node.js 26.14.0 release notes
In practice
I keep in the context what is short, stable and needed for almost every question, like the three policies here. What is large, changes often or is only needed now and then goes behind a tool.
For the grinder manual from chapter 2, I could give the model a search tool that returns the relevant passage when Ada asks how to clean the burrs. The grinder manual in chapter 2
OpenAI added function calling in June 2023. The shop uses it through the Responses API. The timeline in chapter 0
In short
In short
- A function tool gives the model a name, a description and an argument schema. Our code runs the function when the model asks.
- The first response asks for Ada’s order. We run
getOrderand send its result back. The next response asks for the shipment. - A tool result is text the model reads, whoever wrote it. Tool definitions are text too and count as input on every request.
- Built-in tools run at the provider. OpenAI ran the web search for us, and the answer still missed the latest Node.js release.
- What is short, stable and needed for almost every question stays in the context. What is large, changes often or is rarely needed goes behind a tool.
Next
The model asked for trackShipment. Our program printed the call and stopped. In chapter 4 I’ll put the lookup in a loop so the model can read the carrier’s update and tell Ada when to expect her grinder.
The code is in typescript/03-tools.
Phillipp Bertram