api.models

turn()

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.

POST /api/ai/turnsbilled like generatestreams
Using Python?

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

Parameters

modelstringrequired

Model name, e.g. gemini-3.5-flash. Throws if missing.

messagesarrayrequired

The whole conversation so far. Must not be empty.

systemstring

System instruction, sent apart from the messages.

toolsarray

Tool declarations. Each one costs tokens on every turn, so send only what this turn may call.

maxOutputTokensinteger

Imposed on the provider, not just declared. Thinking counts inside it.

temperaturenumber

0 is applied, not treated as absent.

thinkingstring

minimal · low · medium · high.

sessionIdstring

Links this turn's cost to a session in the token ledger. Changes nothing else.

onTextfunction

Called with each text delta. Switches the call to SSE.

signalAbortSignal

Cancels the request.

Returns

{
  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"
}
{
    "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

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.

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),
});
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),
)

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

CodeStatusWhen
turn_incomplete502The stream ended without a turn frame.
turn_failed502The provider refused the turn. The message is the provider's, redacted.
NotEnoughCredits402The balance will not cover the pre-authorisation.
context_too_large400Refused before any debit.