api.models

Overview

Two ways to call a model, and they are not variants of each other. A session remembers and hands back finished outputs; a turn remembers nothing and hands back the model's next move.

session · turnone resourcecredits on both
Using Python?

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

Most of the time you want a session: you ask, you get text or a structured object or a file, and the conversation carries on by itself. The turn is the lower layer — it is what an agent loop calls, over and over, and what you call when the loop is yours to write.

The two shapes, side by side

SessionTurn
Created byapi.models.create()nothing — it is one call
StateKept server-side when memory is onNone. You send the whole history every time
Hands backFinished outputs: text, JSON, images, audio, videoThe model's next move: text, or tool calls
ToolsServer-side search and fetch, by settingYour own declarations, called back to you
DeadlineNone — a long generation runs to the endFive minutes
You use it forAssistants, extraction, generationWriting your own agent loop
If you are not sure, start with a session

The turn asks you to carry the conversation, re-send it each time, and decide what a tool call means. That is the right trade when you are building a loop, and an unnecessary one when you are not.

The methods

MethodDoes
create({ model, settings, schema, memory, storage })Opens a session. Returns an AssistantSession.
get(id)Reopens a saved session. Returns an AssistantSession.
list({ limit })The account's sessions, newest first. Returns plain objects.
turn({ model, messages, tools, … })One stateless model turn, with native tool calling.
get() and list() do not return the same kind of thing

get() hands back an AssistantSession, with generate() on it. list() hands back plain data, with no methods — pass an id from it to get() when you want to carry on.

A session, end to end

const assistant = await api.models.create({
  model: "gemini-3.5-flash",
  settings: { instructions: "Answer in French, in at most three sentences." },
});

const result = await assistant.generate({
  inputs: [{ type: "text", content: "Résume ce contrat." },
           // A document is always a url — unlike image, audio or video, "file" has no content form.
           { type: "file", url: "https://example.com/contrat.pdf" }],
});

result.outputs;        // [{ type: "text", content: "…" }]
result.usage.credits;  // what it actually cost

await assistant.generate({ inputs: [{ type: "text", content: "Et les risques ?" }] });
// the contract is still in the conversation — you do not re-send it
assistant = api.models.create(
    model="gemini-3.5-flash",
    settings={"instructions": "Answer in French, in at most three sentences."},
)

result = assistant.generate(inputs=[
    {"type": "text", "content": "Résume ce contrat."},
    {"type": "file", "url": "https://example.com/contrat.pdf"},
])

result["outputs"]            # [{"type": "text", "content": "…"}]
result["usage"]["credits"]   # what it actually cost

assistant.generate(inputs=[{"type": "text", "content": "Et les risques ?"}])
# the contract is still in the conversation — you do not re-send it

A turn, end to end

let messages = [{ role: "user", content: "What is the weather in Tunis?" }];

const turn = await api.models.turn({
  model: "gemini-3.5-flash",
  messages,
  tools: [{ name: "weather", description: "Current weather for a city.",
            parameters: { city: { type: "string", required: true } } }],
});

turn.stopReason;          // "tool_use"
turn.message.toolCalls;   // [{ id, name: "weather", args: { city: "Tunis" }, signature }]

// You run the tool, you append the result, you call again.
messages = [{"role": "user", "content": "What is the weather in Tunis?"}]

turn = api.models.turn(
    model="gemini-3.5-flash",
    messages=messages,
    tools=[{"name": "weather", "description": "Current weather for a city.",
            "parameters": {"city": {"type": "string", "required": True}}}],
)

turn["stopReason"]              # "tool_use"
turn["message"]["toolCalls"]    # [{"id", "name": "weather", "args": {"city": "Tunis"}, "signature"}]

# You run the tool, you append the result, you call again.

The loop around that is yours. If you would rather not write one, the agent engine is that loop already built — see Agent engine.

What each one costs

Both are billed on what reaches the provider, in the units the catalogue gives for that model. Two things are worth knowing before you design around either.

  • A session with memory pays for its history. Every call re-sends the conversation, so a long one costs more per answer than a short one — until the older part is summarised for you, which happens on its own.
  • A turn pays for its declarations. Tool definitions travel on every single turn. Ten tools you never call still cost tokens each time.