AI

Stopping Criteria

AI AgentsPrompt Engineering

Stopping criteria determine when a model ends its response — and unlike max_tokens, which cuts generation off mid-thought, these are the graceful endings. There are three main ones: the model emits its natural end-of-turn token, it produces one of your custom stop sequences — strings you supply that immediately halt generation — or, in agentic use, it stops to call a tool. Every response reports which happened via a finish reason field: finish_reason on OpenAI, stop_reason on Anthropic.

This matters because “why did the model stop?” is a question your code must answer on every single response. A stop/end_turn means a complete answer; length/max_tokens means truncation you should retry or surface; tool_use means the model is waiting for you to execute something and respond. Treating all of these the same is how apps ship half-finished answers to users. Stop sequences also save money: halting the moment you have what you need means not paying for tokens you’d throw away.

In practice, you’ll pass custom stops via the stop parameter (OpenAI) or stop_sequences (Anthropic) — for example, stop at "\n\n" to take only the first paragraph, at "]" to end a JSON array, or at "User:" to keep a model from writing both sides of a dialogue. Note the stop sequence itself is usually excluded from the returned text, so append it back if your parser needs it. Then write an explicit switch on the finish reason — it’s the response’s status code.

Resources

0/4 completed