Threads max turns error

merged…
osebo56d ago

Can you explain why I’m getting this error

IMG_8248.png
IMG_8248.png
8 repliesoldestnewest
Osebotfable-5-156d ago

Traced the error in the screenshot to lib/agent/runner.mjs — the per-tier maxTurns cap. Writing up the explanation (no code change unless you want one).

Osebotfable-5-156d ago

Why you got Reached maximum number of turns (15)

Short version: that run was triaged as a "quick" job, quick jobs are capped at 15 agent turns, and the task (moving the ☰ toggle in the header) needed more than 15 tool calls to finish. The Agent SDK aborts the run when it hits the cap, the runner catches that as a fatal error, and the UI renders it as "The run crashed before finishing."

The chain, step by step

  1. Triage picks a tier. When you post with "Ask Claude" ticked, lib/data.ts calls the fast decider (lib/decide.ts → triagePost). Its prompt says: tier quick for trivial edits/answers, full for anything touching multiple files or requiring tests. The header-toggle request read as a "trivial CSS edit", so it got tier: "quick".

  2. Tier sets the turn budget. In threadide/lib/agent/runner.mjs:185:

    maxTurns: ctx.tier === "quick" ? 15 : 80,
    

    A "turn" here is one assistant step — every Read, Grep, Bash, Edit, and every forum post_reply counts. For a UI change the agent typically does: plan post (1) → read CSS/nav files (3–5) → edits (2–3) → build a mockup + install Chromium + screenshot (3–5) → upload_screenshot (1) → commit/push (1–2) → diff/done post (1). That's easily 15–20, so the cap fires right after the plan post — which is exactly what you see: a PLAN reply, then FAILED two minutes later.

  3. SDK throws, runner reports failure. @anthropic-ai/claude-agent-sdk (the …/runner/node_modules/@anthropic-ai/c… path in the stack) surfaces the cap as an error. runner.mjs's .catch posts it to /api/internal/jobs/:id/finish with ok: false, and lib/agent/finish.ts:41 renders the generic "The run crashed before finishing." card with the raw error underneath. So nothing actually "crashed" — the run was cut off on purpose by its budget.

Was work lost?

Probably partly. Edits made before the cap are still on the branch's sandbox but were not committed/pushed unless a commit happened before turn 15. Re-running the same request in a new post will start over from the branch as pushed.

Options if you want this fixed (I have not changed anything yet)

  • A. Raise the quick cap — e.g. 15 → 40 in runner.mjs. Cheapest fix; quick runs on the cheap model stay cheap since turns are short. Most likely the right first move.
  • B. Make triage less eager to pick quick — tighten the prompt in lib/decide.ts so anything involving UI/screenshots or git commits is full.
  • C. Fail gracefully — in runner.mjs, detect the max-turns result and post a failed reply saying "ran out of turn budget (quick tier, 15); re-post to run as full" instead of the scary "crashed" message with a stack trace.
  • Workaround right now: re-post the same request; if the decider picks quick again, add a hint like "this needs a screenshot" so it's classified full (80 turns).

Say which of A/B/C you'd like and I'll do it in this thread.

oseboasked Osebot56d ago

I want to raise the max turns? Can you add your recommendation?

Osebotfable-5-156d ago

Going with option A: raise the quick tier cap in lib/agent/runner.mjs from 15 → 40 (leaving full at 80). Will also pull the numbers into named constants so the budget is easy to find/tune later. One-file change, then commit + push.

Osebotfable-5-156d agothreadide@12e1c89

Raised the quick-tier turn budget: 15 → 40

One file changed: threadide/lib/agent/runner.mjs. Pushed to thread/0ed20e93 — open a PR from that branch to deploy.

What changed

  • The per-tier caps now live in a single MAX_TURNS constant near the top of the file instead of an inline ternary buried in the query() options:
    const MAX_TURNS = {
      quick: Number(process.env.QUICK_MAX_TURNS) || 40,
      full:  Number(process.env.FULL_MAX_TURNS)  || 80,
    };
    
  • quick is 40 (was 15). full stays 80.
  • Both can be overridden without a code change via QUICK_MAX_TURNS / FULL_MAX_TURNS env vars on the worker/sandbox, if you want to tune it in prod.
  • The run log now prints the budget at start (model … (tier quick, maxTurns 40)) so it's obvious from the job log which cap applied when a run dies.
  • Comment above the constant explains what a "turn" is and why 15 was too tight, for whoever touches this next.

Why 40

A run's fixed overhead is roughly: plan post (1) + reads/greps (3–5) + edits (2–3) + commit/push (1–2) + done post (1) ≈ 10–12 turns, so 15 left almost no room for actual work. A small UI change with a mockup screenshot adds ~5–8 more. 40 comfortably covers that while still bounding the cheap Haiku tier — quick runs stay cheap because each turn is short. If you see quick runs still hitting the cap, bump QUICK_MAX_TURNS in the env rather than editing code.

Not changed (from the earlier options)

  • B (make triage less eager to pick quick) and C (friendlier "ran out of budget" message instead of "crashed") are untouched. C is still worth doing at some point — the current error UI makes a deliberate budget stop look like a crash. Say the word and I'll do it in a follow-up.

Verified with node --check; the runner has no tests and can't be exercised in this sandbox (needs the app + a job).

Diff · 1 files
--- a/threadide/lib/agent/runner.mjs
+++ b/threadide/lib/agent/runner.mjs
@@ -1,7 +1,8 @@
 // Runs INSIDE the sandbox. Node 22+, no build step. Dependencies are installed by the launcher:
 //   @anthropic-ai/claude-agent-sdk, zod
 //
-// Env: APP_URL, JOB_ID, RUNNER_TOKEN, ANTHROPIC_API_KEY, AGENT_MODEL, QUICK_MODEL, WORKDIR
+// Env: APP_URL, JOB_ID, RUNNER_TOKEN, ANTHROPIC_API_KEY, AGENT_MODEL, QUICK_MODEL, WORKDIR,
+//      QUICK_MAX_TURNS, FULL_MAX_TURNS (optional overrides of the per-tier turn budgets below)
 //
@@ -14,6 +15,17 @@
 const WORKDIR = process.env.WORKDIR ?? process.cwd();
 
+// Per-tier turn budgets. A "turn" is one assistant step, so every Read/Grep/Edit/Bash call and
+// every forum post_reply counts. Hitting the cap makes the SDK throw "Reached maximum number of
+// turns (N)", which surfaces in the thread as "The run crashed before finishing." The old quick
+// cap of 15 was too tight: plan post + a few reads + edits + commit/push + done post is already
+// ~12-15, and anything that screenshots blows past it. 40 leaves headroom for a small UI change
+// while still bounding the cheap tier; 80 stays the budget for full runs.
+const MAX_TURNS = {
+  quick: Number(process.env.QUICK_MAX_TURNS) || 40,
+  full: Number(process.env.FULL_MAX_TURNS) || 80,
+};
+
 const logLines = [];
@@ -165,7 +177,8 @@ async function main() {
   const model = ctx.model ?? (ctx.tier === "quick" ? process.env.QUICK_MODEL ?? "claude-haiku-4-5" : process.env.AGENT_MODEL ?? "claude-sonnet-4-5");
   state.model = model;
-  log(`model ${model} (tier ${ctx.tier ?? "full"})`);
+  const maxTurns = ctx.tier === "quick" ? MAX_TURNS.quick : MAX_TURNS.full;
+  log(`model ${model} (tier ${ctx.tier ?? "full"}, maxTurns ${maxTurns})`);
 
@@ -182,7 +195,7 @@ async function main() {
       permissionMode: "bypassPermissions", // the sandbox is the boundary
-      maxTurns: ctx.tier === "quick" ? 15 : 80,
+      maxTurns,
       settingSources: [],
oseboasked Osebot56d ago

Can there just be no max turns and however long it takes i however long

Osebotfable-5-156d ago

Yes — the SDK's maxTurns is optional; omitting it means "run until the agent says done". The real ceiling then becomes the sandbox timeout (45 min in lib/agent/launch.ts), which is a sane backstop. I'll remove the turn cap from runner.mjs (keeping an env-var opt-in for anyone who wants one back) and explain the tradeoff.

Osebotfable-5-156d agothreadide@063b41b

Removed the turn cap — runs now go until the agent finishes

Yes, this works. maxTurns is an optional Agent SDK setting; when it isn't passed, the SDK just keeps looping until the agent stops calling tools. One file changed, threadide/lib/agent/runner.mjs, pushed to thread/0ed20e93 (commit 063b41b, on top of yesterday's 12e1c89).

What changed

  • The per-tier MAX_TURNS = { quick: 40, full: 80 } block is gone. maxTurns is now only passed to query() if a MAX_TURNS=<n> env var is set on the sandbox; otherwise it's omitted → unlimited.
  • QUICK_MAX_TURNS / FULL_MAX_TURNS env overrides (added yesterday) are removed; the single MAX_TURNS replaces them. Nothing in the repo or deploy config referenced them.
  • Run log now prints maxTurns unlimited (or the number) at start so it's obvious which mode a job ran in.

"However long it takes" — what still bounds a run

There is one hard limit left, and I'd keep it: the sandbox wall-clock timeout, SANDBOX_TIMEOUT_MS in lib/agent/launch.ts, default 45 minutes. The Vercel Sandbox is created with that timeout and self-terminates when it expires. This is the safety net against a genuinely stuck run (agent looping on a failing command, etc.) — without it a runaway job would burn API tokens indefinitely. 45 min is far above what any run so far has needed (the failed one died at ~2 min), so in practice you'll never see it. If you ever do, bump SANDBOX_TIMEOUT_MS in the env.

Note that a timeout kill surfaces differently from the old turn-cap error: the sandbox just disappears, the runner never calls /finish, and finish.ts has a cleanup path for that ("already gone… the timeout will clean it up"). So you'd see a run stuck in running rather than a crash card — worth knowing if it ever happens.

Cost implication

The quick tier still uses the cheaper model (QUICK_MODEL, Haiku by default), so cheapness now comes from the model choice rather than a turn budget. A quick run that turns out to be big will just take more Haiku turns instead of dying. If you'd rather such tasks escalate to Sonnet, that's the triage-prompt tweak (option B from earlier) — not done here.

Verified with node --check; no automated tests cover the runner and it can't be exercised in this sandbox.