본문으로 건너뛰기

이 영상과 텍스트는 영어로 제공됩니다.

Finish reason: telling a finished AI answer from a cut-off one

AI Concepts #194

An AI answer cut off by a dropped connection can look exactly like a complete one. One small label at the end of every streamed reply tells them apart.

자세히

What a finish reason is

When an AI sends its answer in pieces as it writes (streaming), the very last message should carry a short label called the finish reason. It says why the answer ended. The two you meet first:

  • stop: the model decided it had said everything it wanted to say.
  • length: the answer hit the maximum length you allowed, counted in tokens (small pieces of words), so it was chopped off mid-sentence. That is your limit at work, not the model's fault.

Other reasons exist too. Later chapters of the course add one for when the model asks to use a tool and one for when a content filter steps in.

What the video shows

The video's hook is two AI answers that look identical, where one of them is broken. An answer cut off by a dropped connection can look exactly like one that ended properly. The lesson: every streamed reply should end with a reason, and if the reason is missing, you do not save the answer as finished.

The experiment behind it

The course ended the same answer in three different ways and recorded what the app received:

| How it ended | Text received | Pieces | Error? | Finish reason | |---|---|---|---|---| | Length limit of 5 tokens | "A tide gauge is a" | 5 | No | length | | Connection closed cleanly | "A tide gauge is a" | 5 | No | none | | Connection destroyed | "A tide gauge is" | 4 | Yes | none |

Look at the first two rows. The same text, the same number of pieces, and no error either time. As far as the app could tell, the data simply stopped coming, which is all a stream of data can ever do. The only difference is the label: one says "length", the other says nothing at all.

The third row is the easy case. A connection that is torn down abruptly does raise an error, and it also loses the piece that was on its way, which is why that text is one word shorter.

An everyday example

Imagine a long letter arriving by fax. If the last page ends with a signature, you know you have all of it. If it ends halfway down a page with no signature, maybe the writer stopped there, or maybe the machine ran out of paper. The words themselves cannot tell you which. The signature can.

How to use it

The rule fits in one line: no finish reason means the answer did not finish, it was interrupted. So:

  • Treat a missing finish reason as a failure, every time.
  • Never store that text as a complete answer, for example in a saved conversation.
  • Do not rely on catching errors alone: the dangerous case above raised none.

A "length" ending is different. The answer really is cut short, but at least you know why: it reached the limit you set.

Why it matters

It is easy to build a chat app that only watches for errors. That catches the loud failure and misses the quiet one, and the quiet one leaves half-finished answers sitting in a user's history looking complete. Checking one label at the end of every stream is all it takes to tell the two apart.

Learn it step by step in Chapter 14 of our free course AI From Scratch: Your First Production LLM Call: Streaming, Retries, Timeouts.

다른 플랫폼

이 챕터의 다른 영상

무료 강좌 챕터 14의 내용을 바탕으로 AI 도움을 받아 작성한 글입니다.