# turn()

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

`api.models.turn(options) → Promise<Turn>`

One stateless model turn with native tool calling. This is what the agent engine calls on every step — the loop lives in the SDK, the server only serves the turn.

Status: POST /api/ai/turns · billed like generate · streams

## Parameters {#parameters}

| Name | Type | Requirement | Description |
| --- | --- | --- | --- |
| `model` | string | required | Model name, e.g. `gemini-3.5-flash`. Throws if missing. |
| `messages` | array | required | The whole conversation so far. Must not be empty. |
| `system` | string |  | System instruction, sent apart from the messages. |
| `tools` | array |  | Tool declarations. Each one costs tokens on every turn, so send only what this turn may call. |
| `maxOutputTokens` | integer |  | Imposed on the provider, not just declared. Thinking counts inside it. |
| `temperature` | number |  | `0` is applied, not treated as absent. |
| `thinking` | string |  | `minimal` · `low` · `medium` · `high`. |
| `sessionId` | string |  | Links this turn's cost to a session in the token ledger. Changes nothing else. |
| `onText` | function |  | Called with each text delta. Switches the call to SSE. |
| `signal` | AbortSignal |  | Cancels the request. |

## Returns {#returns}

```javascript
{
  message: {
    role: "assistant",
    content: "I will read the file first.",
    toolCalls: [
      { id: "call_0", name: "read", args: { path: "notes.md" }, signature: "..." }
    ]
  },
  stopReason: "tool_use",      // or "end_turn"
  usage: { credits: 3, inputTokens: 1840, outputTokens: 77, thinkingTokens: 54 },
  model: "gemini-3.5-flash",
  servedModel: "gemini-3.5-flash"
}
```

> **Send the signature back**
>
> A tool call can come back with a signature. Send it back unchanged on the following turn, or the turn is refused. Carry the field even if you do not use it.

## Streaming {#streaming}

Passing onText switches to server-sent events. Three frame types arrive: text with a delta, turn with the complete result, error. The promise resolves on the turn frame.

```javascript
const turn = await api.models.turn({
  model: "gemini-3.5-flash",
  messages: [{ role: "user", content: "Summarise this in three lines." }],
  onText: (delta) => process.stdout.write(delta),
});
```

## Timeouts {#timeouts}

The client's default is 60 seconds. turn() raises it to 300, because a turn may think for a long time, and generate() removes it entirely — a video takes minutes, and abandoning the request would not stop the server from billing it.

## Errors {#errors}

| Code | Status | When |
| --- | --- | --- |
| turn_incomplete | 502 | The stream ended without a turn frame. |
| turn_failed | 502 | The provider refused the turn. The message is the provider's, redacted. |
| NotEnoughCredits | 402 | The balance will not cover the pre-authorisation. |
| context_too_large | 400 | Refused before any debit. |

```python
{
    "message": {
        "role": "assistant",
        "content": "I will read the file first.",
        "toolCalls": [
            {"id": "call_0", "name": "read", "args": {"path": "notes.md"}, "signature": "..."},
        ],
    },
    "stopReason": "tool_use",      # or "end_turn"
    "usage": {"credits": 3, "inputTokens": 1840, "outputTokens": 77, "thinkingTokens": 54},
    "model": "gemini-3.5-flash",
    "servedModel": "gemini-3.5-flash",
}
```

```python
import sys

turn = api.models.turn(
    model="gemini-3.5-flash",
    messages=[{"role": "user", "content": "Summarise this in three lines."}],
    on_text=lambda delta: sys.stdout.write(delta),
)
```
