AI with AIlabs
← All guides
3 min read

My model's answers kept running long and I fixed it two different ways

One answer got cut off mid-sentence. Another was just a little too big for the schema. Same symptom, opposite fixes.


Build LogClaude CodeClaude APIZodNext.jsVercel

Learn · Oct 29, 2026

The first real run of my PRD generator worked. The email arrived. Two of the three sections passed validation on the first try.

The third one failed twice. The error said it needed Steps 1 to 8 in order, and the output clearly didn't have them.

The same day, a different route in the main app threw "Step generation failed" for a slightly bigger project. Different code, different feature, same feeling. The model had said too much.

It took me a while to see these were two separate problems. They needed opposite fixes.

Read the raw output before you touch the cap

My first instinct was to blame the prompt. I didn't. The failed run had stored the raw model answers, so I read them. Both attempts stopped mid-sentence. One ended right after a "### Step 4" heading. The other quit inside Step 5. That's not a bad answer. That's an answer that ran out of room. The cap I'd set was 6000 output tokens. The usage numbers were well above what the visible text would explain. That's the part I'd missed: the cap also covers whatever the model thinks through before it starts writing.

Give every section its own cap

One shared limit was wrong for all three sections. The agentic brief used about 1.5k tokens. The harness spec used about 6.3k. The build steps needed far more. So each section got its own ceiling: 6000 for the brief, 10000 for the harness spec, 16000 for the build steps. Short sections stay cheap. The long one gets room. Timing was the other worry. The three sections took roughly 10, 40 and 80 seconds on the old cap. The route has 300 seconds. So a new section never starts within 90 seconds of the end of the budget, and anything unfinished gets picked up by a retry.

Treat a cut-off answer as a failure, not as an answer

This is the one I care about most. When the model stops because it hit the limit, the SDK tells you. The finish reason is "length". Before, that text could slide through as if it were complete. Now the call throws a named error, OutputTruncated, and the error log says "output cut off" instead of the useless "model call failed." I also check the error by name, not only by class. If a bundler ever loads the module twice, the class check can miss. The name check still catches it.

Ask for shorter on the retry

A plain retry with the same prompt and the same cap can fail the same way. So when the first attempt was cut off, the retry adds one instruction: write the same document again, much shorter. I also trimmed the original prompt. Each Claude Code prompt in the output stays under 120 words. No more than five verification bullets per step. Short sentences for the goal and the failure note. The fix is partly a bigger box and partly a smaller thing going into it.

Give the schema some slack and trim in code

The other failure was the opposite kind. Nothing was cut off. The model just wrote more than my zod schema allowed. The build-plan route accepted at most 12 steps and at most 3 checks per step. The prompt asked for fewer. But a slightly larger app produced a slightly longer plan, the schema rejected the whole thing, and the user saw "Step generation failed." Now the schema accepts up to 16 steps and 6 checks. The code trims each checklist to 3 after validation. The prompt still asks for the smaller numbers. The real limit lives in the prompt and the code, not in a validator that throws away a good answer over a few extra lines.

What broke: Two things, and they looked alike. In the PRD generator, the build section was cut off mid-sentence because my output cap was too small, and the cap counted the model's reasoning too. In the build-plan route, a strict schema turned a slightly long answer into a hard failure. The first needed more room and an honest error. The second needed a looser gate and a trim.

The fix: For the cut-off answer: a separate cap per section, a named error when the finish reason is length, and a retry that asks for a shorter version. For the over-long answer: a schema with some slack, a trim in code, and the real limits kept in the prompt. The stuck lead resumed on its own after the deploy, with only the build section retried.

Audit an AI route for cut-off and over-long answers
Review the API route at [path/to/route.ts] that calls a language model and validates the result. Check for two failure modes and fix both.

1. Cut-off answers. Find every model call. Does it check the finish reason? If the reason is "length", the call must throw a named error (for example OutputTruncated) and never return the partial text. Confirm the output token limit is set per call, based on the longest output that call produces, and remember the limit also covers reasoning before the answer. On retry after a cut-off, append an instruction to answer much shorter.

2. Over-long answers. Find every schema that validates the model's reply. List each .max() on an array or string. For any limit that exists only to keep the output tidy, let the schema accept some extra, then trim in code after validation. Keep the real limit in the prompt.

Also make sure the error log records a short reason (the error class, the status, and 'output cut off' when it applies), never just 'model call failed'.

Show me the diff and add tests for: cut off then success, cut off twice, and a reply with more items than the prompt asked for.

Get new guides and builds in your inbox

No noise. New artifacts, skills, and lessons when they drop — and early access when agents launch.

next_steps.exe

Want a full structured path instead of one-off guides?

That's Learn AI with AI — a sequenced course that takes you from zero to building real things, with community and accountability built in.

Check out Learn AI with AI →