api.models

turn · tools

api.models.turn({ tools })

What the model may ask for. You declare them, it decides, you execute — nothing runs on our side.

optionalsent on every turntwo schema forms
Using Python?

Switch any code block to Python: the choice applies to every page. Python SDK guide and resource reference.

A declaration is a name, a description and a parameter schema. The model never runs anything: it answers with a decision, and running it is yours — which is also what makes it safe to expose things it would be dangerous to hand to anyone else.

One declaration

{
  name: "weather",
  description: "Current weather for a city.",
  parameters: {
    city: { type: "string", description: "City name", required: true },
    unit: { type: "string", enum: ["c", "f"] },
  },
}
{
    "name": "weather",
    "description": "Current weather for a city.",
    "parameters": {
        "city": {"type": "string", "description": "City name", "required": True},
        "unit": {"type": "string", "enum": ["c", "f"]},
    },
}

Two ways to write the parameters

The short form is a map of fields, each with required on it. The full form is an object JSON Schema. Both are accepted; the short one exists because writing a schema by hand for three parameters is out of proportion.

// Short
parameters: {
  city: { type: "string", required: true },
  unit: { type: "string", enum: ["c", "f"] },
}

// Full — reach for this when it gets complicated
parameters: {
  type: "object",
  properties: {
    city: { type: "string" },
    unit: { type: "string", enum: ["c", "f"] },
  },
  required: ["city"],
}
# Short
parameters = {
    "city": {"type": "string", "required": True},
    "unit": {"type": "string", "enum": ["c", "f"]},
}

# Full — reach for this when it gets complicated
parameters = {
    "type": "object",
    "properties": {
        "city": {"type": "string"},
        "unit": {"type": "string", "enum": ["c", "f"]},
    },
    "required": ["city"],
}
Accepted types
string · number · integer · booleanScalars.
array · objectNest with items and properties.

Every declaration is paid for on every turn

Tools are sent with each call, so they are part of the input you are billed for — a tool the model never chooses still costs tokens each time it is offered. On a loop of twenty turns, a tool is paid for twenty times.

Send what this turn may call, not everything you support

Narrowing the list by what the conversation is actually about is the cheapest optimisation available in a tool-calling loop, and it also makes the model choose better.

const ALL = { weather: weatherTool, invoices: invoiceTool, calendar: calendarTool };

// Pick per turn rather than declaring the catalogue
const tools = Object.entries(ALL)
  .filter(([name]) => relevant(name, conversation))
  .map(([, tool]) => tool);
ALL = {"weather": weather_tool, "invoices": invoice_tool, "calendar": calendar_tool}

# Pick per turn rather than declaring the catalogue
tools = [tool for name, tool in ALL.items() if relevant(name, conversation)]

The description is the instruction

It is the only thing telling the model when to reach for this rather than answer directly. Say what it does, what it needs, and when not to use it.

WeakBetter
« Gets data »« Current weather for a city. Use for today only — it has no forecast. »
« Search »« Searches our product catalogue by name. Not for customer records. »
« Sends an email »« Sends an email. Only after the user has confirmed the recipient and the text. »

Carrying the signature back

A tool call can come back carrying a signature. Send it back unchanged on the following turn, or the turn is refused. Keeping the whole assistant message and pushing it back as-is handles this without you thinking about it.

// Right: the message goes back whole
messages.push(turn.message);

// Wrong: rebuilt, and the signature is gone
messages.push({ role: "assistant", content: turn.message.content,
                toolCalls: turn.message.toolCalls.map(({ id, name, args }) => ({ id, name, args })) });
# Right: the message goes back whole
messages.append(turn["message"])

# Wrong: rebuilt, and the signature is gone
messages.append({"role": "assistant", "content": turn["message"]["content"],
                 "toolCalls": [{"id": c["id"], "name": c["name"], "args": c["args"]}
                               for c in turn["message"]["toolCalls"]]})

Several calls in one turn

A turn can ask for more than one tool at once. Run them however you like — concurrently when they are independent — and answer each with its own tool message.

const results = await Promise.all(
  turn.message.toolCalls.map((call) => run(call.name, call.args).catch((e) => `Error: ${e.message}`)),
);

turn.message.toolCalls.forEach((call, i) => {
  messages.push({ role: "tool", toolCallId: call.id, name: call.name, content: String(results[i]) });
});
from concurrent.futures import ThreadPoolExecutor

def safe_run(call):
    try:
        return run(call["name"], call["args"])
    except Exception as error:
        return f"Error: {error}"

calls = turn["message"]["toolCalls"]
with ThreadPoolExecutor() as pool:
    results = list(pool.map(safe_run, calls))

for call, result in zip(calls, results):
    messages.append({"role": "tool", "toolCallId": call["id"], "name": call["name"], "content": str(result)})
Decide for yourself what may run unattended

The model chooses; it does not authorise. Anything that sends, publishes, deletes or spends should pass a check of your own before it runs — the engine's permission categories are one way of thinking about which.

Errors

CodeStatusWhen
invalid_tools400tools was present but not an array.
turn_failed502The provider refused the declarations — usually a schema it cannot accept.

Keep parameter schemas plain. A schema with references or composition that a provider will not accept fails the whole turn, not just that one tool.