Parameters
modelstringrequiredModel name, e.g. gemini-3.5-flash. Throws if missing.
messagesarrayrequiredThe whole conversation so far. Must not be empty.
systemstringSystem instruction, sent apart from the messages.
toolsarrayTool declarations. Each one costs tokens on every turn, so send only what this turn may call.
maxOutputTokensintegerImposed on the provider, not just declared. Thinking counts inside it.
temperaturenumber0 is applied, not treated as absent.
thinkingstringminimal · low · medium · high.
sessionIdstringLinks this turn's cost to a session in the token ledger. Changes nothing else.
onTextfunctionCalled with each text delta. Switches the call to SSE.
signalAbortSignalCancels 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",
}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
| 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. |