# turn · tools

Canonical URL: https://developers.iagenify.com/sdk/ref/models/turn/tools/
Language: en

`api.models.turn({ tools })`

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

Status: optional · sent on every turn · two schema forms

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 {#shape}

```javascript
{
  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 {#schemas}

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.

```javascript
// 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 · boolean | Scalars. |
| array · object | Nest with items and properties. |

## Every declaration is paid for on every turn {#cost}

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.

```javascript
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);
```

## The description is the instruction {#descriptions}

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.

| Weak | Better |
| --- | --- |
| « 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 {#signature}

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.

```javascript
// 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 })) });
```

## Several calls in one turn {#parallel}

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.

```javascript
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]) });
});
```

> **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 {#errors}

| Code | Status | When |
| --- | --- | --- |
| invalid_tools | 400 | tools was present but not an array. |
| turn_failed | 502 | The 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.

- [turn · messages](/sdk/ref/models/turn/messages): Where a tool result goes, and the loop around it.
- [api.mcp](/sdk/ref/mcp): Tools from a connected server, declared the same way.

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

```python
# 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"],
}
```

```python
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)]
```

```python
# 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"]]})
```

```python
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)})
```
