C.W.K.
Stream
Lesson 10 of 10 · published

Closing the Loop — When to End the Conversation

~12 min · conversation, closing

Level 0Apprentice
0 XP0/100 lessons0/14 achievements
0/120 XP to next level120 XP to go0% complete

The conversation has to end

Most prompt engineering writes about how to keep a conversation going. The harder question is when to end it cleanly. End-states are common in real systems: task completed, user satisfied, error reached, transfer to human, idle timeout.

Explicit end-states

  • Task done — the agent confirms completion and stops looking for next tool calls.
  • Handoff — escalation to human, transfer to another agent. The transcript is the brief.
  • Refusal end — request refused, alternatives offered, conversation closes unless user redirects.
  • Error end — unrecoverable error; structured error returned to caller.

Why this matters

An agent that doesn't know how to end keeps tool-calling, keeps asking for clarification, keeps generating preambles. The end-state vocabulary is part of the contract: the prompt names what 'done' looks like and the loop wrapper checks for it.

Code

Loop with explicit end-states·python
while True:
    out = call_model(messages)
    if out.tool_calls:
        results = run_tools(out.tool_calls)
        messages += [out, results]
        continue
    if out.content.endswith("[done]"):
        return success(out.content)
    if out.content.startswith("[handoff]"):
        return handoff(out.content)
    return ask_user(out.content)

External links

Exercise

Add explicit end-states to one agent prompt. Add the loop wrapper logic. Run 20 conversations and verify each one ends in a named state.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.