Phase 0: pre-loop setup

Before the loop starts, the runtime resolves three things: the compiled system instruction, the tool list, and the initial session context. All I/O for these must have happened during onStart(). Once the loop begins, latency budget is committed.

Advertisement

Phase 1: model call

Every iteration begins with a model invocation. The runtime packages the current message history + tool descriptions + instruction into a single prompt, submits it, and awaits either a text response or a tool-call decision.

// Inside the runtime — simplified
ModelResponse resp = model.generate(
    session.history(),
    agent.getTools(),
    agent.getInstruction()
);
if (resp.isToolCall()) {
    dispatchTool(resp.getToolCall());
} else {
    // final text — exit loop
}
Advertisement

Phase 2: tool dispatch

If the model returned a tool call, the runtime looks up the tool by name, validates arguments against its schema, and invokes it. The result is fed back into the message history as a tool_result role message.