Mecanismo de agentes

O ciclo

Un passo è un movement del modele plus gli utensili che ha richiesto. Il loop ruote fino a che il modele non cesse di chiedere — o fino a che un plafond che si pone dice contrario.

maxSteps 1004 stop reasonsone turn per step
Using Python?

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

Ce succede in un pas

  • Se si chiamava stop(), se si raggiungeva il limite d'escalade, o se si spargeva il budget.
  • Construi il prompt del sistema — spazio de lavoro, modo, utensili, storages, memoria.
  • Clear old tool results se il contesto è pesante, então compact se non era suficiente.
  • Pedir un virat di modello, con le declarazion di utensili. Emit turn.
  • Se il model scrise text, emit text.
  • Se non si chiesse un utensilio, il loop termina.
  • Sen dane ergue le chiamali — in paralel o uno a la volta — e anexi ogni risultato come un messaggio tool.

Nulla di più decide quando stop: il proprio silenzio del modele è il final normal.

I 4 finals

ResonSignificado
end_turnIl modele rispondit senza chiamare un utensilio.
stoppedstop() fu chiamato. L'outil actual termina, niente di nuovo inizia.
max_stepsIl tectus di step è alcantat — maxSteps, 100 per omissió.
budgetI crediti scaduts rèallès budget.
const result = await agent.run({ inputs: "…" });

switch (result.stopReason) {
  case "end_turn":  break;                       // it answered
  case "max_steps": console.warn("ceiling reached — the work is unfinished"); break;
  case "budget":    console.warn("budget spent — ", result.usage.credits); break;
  case "stopped":   break;                       // you asked it to stop
}
result = agent.run(inputs="…")

reason = result["stopReason"]
if reason == "max_steps":
    print("ceiling reached — the work is unfinished")
elif reason == "budget":
    print("budget spent — ", result["usage"]["credits"])
# "end_turn": it answered · "stopped": you asked it to stop
max_steps e budget non sono fallimenti, e non success

La run termina con quels texts il modele prodotto da ultimo, che può essere un pensòn semi-finit. Tratte ognuna ragion come «inacabat» e non come una risposta, e o elevare il tetto o restrinse la task.

Qua run() restitue

{
  text: "I read the four notes. Two are unfinished: …",
  todos: [{ content: "Check the figures", status: "completed" }],
  usage: { credits: 31, turns: 6, toolCalls: 9 },
  stopReason: "end_turn",
  model: "gemini-3.5-flash"
}
{
    "text": "I read the four notes. Two are unfinished: …",
    "todos": [{"content": "Check the figures", "status": "completed"}],
    "usage": {"credits": 31, "turns": 6, "toolCalls": 9},
    "stopReason": "end_turn",
    "model": "gemini-3.5-flash",
}

text è l'ultima cosa che il model scrise, che non è sempre l'ultimo passo: una run che termina a max_steps restituisce il testo più recente, anche se un utensilu lo seguisse.

Il tecturat de step

maxSteps predefinitos a 100, che è alto a propósito — é un guarda fuga, non un budget. La cosa que normalmente deve parare una run é budget, porque é denominata in quello de cui realmente te importa.

Un subagente è plafonat a 40passs quel che il plafond del padre, dal momento che è dato un lavoro autonomo.

Abortare un run

const running = agent.run({ inputs: "…" });

setTimeout(() => agent.stop(), 30_000);

const result = await running;    // stopReason: "stopped"
import threading

threading.Timer(30, agent.stop).start()

result = agent.run(inputs="…")    # stopReason: "stopped"

stop() aborta la richiesta del modelo in volo e imposta la bandera che il buco lee in cima di ogni passo. Le chiamate di ferramentas già in fila in fila nella stessa volta risposta « Interrompeu dal utente. » in lugar de rulare. I bambini sono interrompeti troppo.

Un errore termina la run

Qualsiase cosa un utrel è presa e restituit al modele come un resultado utrel failed — il model adapts. What is no capturat è un fallo del turn stesso dopo tentas e replies: un error evento è emit e run() rejets.