Skip to main content
Back to how-to guides Use this pattern when an agent finishes its work: it closes the session, or it finishes a bounded task and returns control to a supervisor.

Concept

COMPLETE means this agent is finished. What happens next depends on how the agent started. A specialist entered with RETURN: false cannot return with COMPLETE. To give control back to its supervisor, it needs a HANDOFF to that supervisor. See How to switch the active agent during a conversation.

Ways to complete

Use the COMPLETE: section when the end depends on state. Use THEN: COMPLETE when a flow step knows it is the last step. Use both when the flow ends the agent and a condition ends it earlier.

Completion conditions

  • The runtime evaluates the WHEN text of each condition in the order you list them. The first match wins.
  • WHEN must be deterministic. A condition such as the customer is done is a compile error (PLAIN_ENGLISH_COMPLETE_WHEN). Use order_id IS SET or task_done == true.
  • The condition must read state that something writes: a GATHER field, a tool result mapping, a SET, or a returned field.
  • In a flow, the runtime checks conditions before an automatic advance only when the next step already ran in this turn. Forward steps run first, so a trivially true condition does not end the flow early.
  • A matching condition does not complete the agent while a CONSTRAINTS rule fails on the current state. The constraint ON_FAIL runs instead. The runtime withholds a completion three times, then releases it.
  • At a flow THEN: COMPLETE, the runtime checks HANDOFF conditions first. A matching route takes over instead of completing. A child that returns to a supervisor skips this check.

What the runtime does on COMPLETE

  1. Applies the SET assignments of the matching condition.
  2. Stores a snapshot of the session values when STORE is set.
  3. Builds the message: the RESPOND text, or the default message, or nothing for a returning child.
  4. Marks the agent complete. A root agent ends the session.
  5. For a RETURN: true child, copies the ON_RETURN MAP values to the supervisor and makes the supervisor active.
Message rules:
  • A COMPLETE condition with no RESPOND shows the default message This conversation has been completed. Set conversation_complete in MESSAGES to change it.
  • A returning child with no RESPOND returns silently. The supervisor continues the conversation.
  • In a reasoning agent, a matching completion replaces the model text for that turn. Put the whole closing message in RESPOND.
  • MODEL_CONTEXT keeps different text for later model calls than the customer saw.

Minimal working example

The agent collects a payment amount and ends the session with a confirmation. payment_amount comes from the GATHER field.

How it works

  1. The customer answers the prompt. The runtime stores payment_amount.
  2. After the turn, the runtime evaluates payment_amount IS SET. The result is true.
  3. The runtime checks CONSTRAINTS. The agent has none.
  4. The runtime marks the agent complete and shows the RESPOND text with the amount filled in.
  5. This agent is the root agent. The session ends.
  6. A later message in the same session gets This conversation has been completed. The customer must start a new session.

Common variations

End from a flow step

A flow step with THEN: COMPLETE shows its RESPOND text and ends the agent in the same turn. The agent does not wait for the customer.
Do not end an entry step with RESPOND and THEN: COMPLETE when the customer must answer. The agent completes before the customer replies. Use GATHER so the step waits. If the agent also has a COMPLETE: section, a matching condition runs at THEN: COMPLETE. Keep the closing text in one place.

Return a child to its supervisor

A child entered with RETURN: true completes with a condition. The supervisor keeps the session and routes the next request. Customer_Lookup_Agent has no RESPOND, so it returns silently. customer_id comes from its GATHER field and returns through MAP. Account_Service_Agent is a stand-in for your specialist.
When Customer_Lookup_Agent completes, the session does not end. The supervisor becomes active and continues. When Account_Service_Agent completes, the session ends, because it was entered with RETURN: false. For the return mechanics, ON_RETURN options, and replay of the original request, see How to switch the active agent during a conversation and How to transfer control to another agent and return later.

Show a closing message without ending the session

Set TERMINAL: false to render a closing message or widget and keep the session open. Clear the condition with SET in the same completion. Otherwise it fires again on the next turn.
Write SET on one line: SET: summary_shown = true. A SET: followed by indented lines is ignored with warning W360. After this completion, the next message reopens the agent as a fresh intent. If the same condition fires again on the first turn after reopening, the runtime ends the session and logs NON_TERMINAL_COMPLETION_NO_PROGRESS.

Hold a completion until a rule passes

A completion does not fire while a blocking CONSTRAINTS rule fails on the current state. The rule ON_FAIL runs instead.
If the customer answers no, the completion is withheld and the rule message shows. The runtime releases a withheld completion after three withholds in a row. Give the customer a way to pass the rule.

Store a snapshot, keep model context, and set the default message

STORE saves a snapshot of the session values under a name. MODEL_CONTEXT keeps text for the model that differs from the customer text. A condition with no RESPOND shows the conversation_complete message.
An amount of 1500 matches the first condition. An amount of 200 matches the second. The second shows the MESSAGES text. Order the conditions from most specific to least specific. The first match wins.

Verification

  • Compile the project. Fix every error and warning on the COMPLETE: section.
  • Test the end. Send a valid answer to the minimal example, for example 120. The trace shows completion_check with the condition and a true result, then a complete decision.
  • Check the source. completion_check carries source, such as post_turn_eval, flow_transition, or terminal_step. It tells you which checkpoint ended the agent.
  • Check the session end. Send a second message. The reply is the conversation-complete message and no agent runs.
  • Test a child. Complete Customer_Lookup_Agent. The trace shows handoff_return_handler with mappedFields. The next reply comes from the supervisor.
  • Test a withheld completion. Fail the constraint. The trace shows a decision completion_withheld_by_constraint.
  • Test a non-terminal completion. The reply shows and the next message routes as a new request.
  • Read traces with How to debug routing decisions in traces.

Production readiness checklist

  • Every WHEN reads state that something writes. Name the writer for each variable.
  • Every WHEN is deterministic. The compiler rejects plain-English text.
  • The closing text lives in one place: the step RESPOND or the completion RESPOND.
  • Every RETURN: true child has a COMPLETE condition or a flow THEN: COMPLETE. Otherwise the supervisor waits.
  • Every RETURN: false specialist has a planned end: COMPLETE, a HANDOFF back, or an escalation.
  • Every TERMINAL: false completion clears its own condition with SET.
  • The MESSAGES conversation_complete text tells the customer how to start again.
  • Test a customer who sends a message after the session ends.
  • Test the first match when two conditions are both true.
  • Monitor sessions that never complete and children that never return.

Common mistakes

Troubleshooting