获取 API 密钥
api.models

turn · 停止原因

turn.stopReason

模型为什么停止。 它是一个您循环分支的字段, 是一个从短短的圆环树枝中给出完整答案的字段。

tool_use · end_turnbranch on it, not on the text
Using Python?

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

你将看见的,

停止死线含义do do do Do Do
tool_use它决定调用工具。 工具呼叫被控制 。跑他们,附加结果,再打电话。
end_turn结束了 答案是内容停止,把内容交给问话的人
const turn = await api.models.turn({ model, messages, tools });

if (turn.stopReason === "tool_use") {
  // there is work to do before there is an answer
} else {
  return turn.message.content;
}
turn = api.models.turn(model=model, messages=messages, tools=tools)

if turn["stopReason"] == "tool_use":
    pass  # there is work to do before there is an answer
else:
    answer = turn["message"]["content"]
停止Reason 的分支, 永远不要在工具Calls 是否为空上

转弯可以同时携带文本和工具电话——模型说明它将要做什么,然后做。首先测试文本会提前一步循环停止,然后失去工作。

当它停在天顶上时

转弯也会结束,因为它已经失去空间,而不是因为它已经完成。 这不是一个错误,你得到一个有效的回答,但回答不完整,这正是一个危险的情况,因为没有东西扔。

// Treat anything that is neither of the two as « did not finish »
const DONE = new Set(["end_turn", "tool_use"]);

if (!DONE.has(turn.stopReason)) {
  // Typically the output ceiling. Raise maxOutputTokens, lower thinking,
  // or ask for something shorter — but do not use the text as if it were whole.
  throw new Error(`the turn did not finish: ${turn.stopReason}`);
}
# Treat anything that is neither of the two as « did not finish »
DONE = {"end_turn", "tool_use"}

if turn["stopReason"] not in DONE:
    # Typically the output ceiling. Raise max_output_tokens, lower thinking,
    # or ask for something shorter — but do not use the text as if it were whole.
    raise RuntimeError(f"the turn did not finish: {turn['stopReason']}")

通常原因就是 maxOutputTokens在一个思考模式上,推理是从同一最高比率上用到的——见 思考思考.

循环结束

tool_use 重复。 绕过步骤数, 并约束支出—— 循环与两者都无法运行, 直到有别的东西阻止它。

const MAX_STEPS = 12;
const MAX_CREDITS = 200;
let spent = 0;

for (let step = 0; step < MAX_STEPS; step += 1) {
  const turn = await api.models.turn({ model, messages, tools });
  spent += turn.usage.credits;

  messages.push(turn.message);

  if (turn.stopReason === "end_turn") return turn.message.content;
  if (turn.stopReason !== "tool_use") throw new Error(`unfinished: ${turn.stopReason}`);
  if (spent > MAX_CREDITS) throw new Error(`budget spent after ${step + 1} steps`);

  for (const call of turn.message.toolCalls) {
    messages.push({ role: "tool", toolCallId: call.id, name: call.name,
                    content: String(await run(call.name, call.args)) });
  }
}

throw new Error(`no answer after ${MAX_STEPS} steps`);
MAX_STEPS = 12
MAX_CREDITS = 200

def solve(messages):
    spent = 0
    for step in range(MAX_STEPS):
        turn = api.models.turn(model=model, messages=messages, tools=tools)
        spent += turn["usage"]["credits"]

        messages.append(turn["message"])

        if turn["stopReason"] == "end_turn":
            return turn["message"]["content"]
        if turn["stopReason"] != "tool_use":
            raise RuntimeError(f"unfinished: {turn['stopReason']}")
        if spent > MAX_CREDITS:
            raise RuntimeError(f"budget spent after {step + 1} steps")

        for call in turn["message"]["toolCalls"]:
            messages.append({"role": "tool", "toolCallId": call["id"], "name": call["name"],
                             "content": str(run(call["name"], call["args"]))})

    raise RuntimeError(f"no answer after {MAX_STEPS} steps")
两者的界限都赢得了自己的地位

步数在环绕中会遇到一个循环。 信用计数在环绕中会遇到一个代价高昂的进展。 它们以不同的方式失败, 并且两者都可能遭遇一次运行。

如果这就是你准备写的循环,代理引擎就是它—— 已经设置了权限、预算、重试和后备模型。 代理引擎.