When website research ends with output that does not parse, retryInvalidOutput runs the whole request again, up to two more times, as a fresh tool-using conversation with the same 6000-token output limit.
It never looks at the error's finishReason, so an answer cut off at the limit (reasoning tokens count toward it) is retried the same way and is likely to be cut off again.
- What to change
- In the catch, read error.finishReason. On 'length', raise maxOutputTokens for the next attempt (or lower the effort or the amount asked for) instead of resending the same request; keep the retry as it is for a normal stop with bad JSON.
- Why it matters
- Each attempt re-runs up to seven model steps and the paid searches and page reads they trigger, and bills the full output again. A reply that hit the limit repeats the same cost and the same failure.
- How often it happens
- The code shows the problem; how often it happens is assumed. To measure it: How often generateText throws NoObjectGeneratedError with finishReason 'length' in website research, from the Workers logs or the seo spend rows.
- Estimate
- up to about 6,000 output tokens plus a full tool loop per wasted attempt, at most two attempts per run (estimate)
What the estimate assumes
Only runs whose first attempt ends in a length cut-off are affected; how many that is is not known from the code. Prompt figure assumes about seven steps of a few thousand input tokens, mostly cached.