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
WHENtext of each condition in the order you list them. The first match wins. WHENmust be deterministic. A condition such asthe customer is doneis a compile error (PLAIN_ENGLISH_COMPLETE_WHEN). Useorder_id IS SETortask_done == true.- The condition must read state that something writes: a
GATHERfield, a tool result mapping, aSET, 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
CONSTRAINTSrule fails on the current state. The constraintON_FAILruns instead. The runtime withholds a completion three times, then releases it. - At a flow
THEN: COMPLETE, the runtime checksHANDOFFconditions 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
- Applies the
SETassignments of the matching condition. - Stores a snapshot of the session values when
STOREis set. - Builds the message: the
RESPONDtext, or the default message, or nothing for a returning child. - Marks the agent complete. A root agent ends the session.
- For a
RETURN: truechild, copies theON_RETURNMAPvalues to the supervisor and makes the supervisor active.
- A
COMPLETEcondition with noRESPONDshows the default messageThis conversation has been completed.Setconversation_completeinMESSAGESto change it. - A returning child with no
RESPONDreturns 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_CONTEXTkeeps 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
- The customer answers the prompt. The runtime stores
payment_amount. - After the turn, the runtime evaluates
payment_amount IS SET. The result is true. - The runtime checks
CONSTRAINTS. The agent has none. - The runtime marks the agent complete and shows the
RESPONDtext with the amount filled in. - This agent is the root agent. The session ends.
- 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 withTHEN: COMPLETE shows its RESPOND text and ends the agent in the same turn. The agent does not wait for the customer.
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 withRETURN: 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.
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
SetTERMINAL: 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.
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 blockingCONSTRAINTS rule fails on the current state. The rule ON_FAIL runs instead.
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.
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 showscompletion_checkwith the condition and a true result, then acompletedecision. - Check the source.
completion_checkcarriessource, such aspost_turn_eval,flow_transition, orterminal_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 showshandoff_return_handlerwithmappedFields. 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
WHENreads state that something writes. Name the writer for each variable. - Every
WHENis deterministic. The compiler rejects plain-English text. - The closing text lives in one place: the step
RESPONDor the completionRESPOND. - Every
RETURN: truechild has aCOMPLETEcondition or a flowTHEN: COMPLETE. Otherwise the supervisor waits. - Every
RETURN: falsespecialist has a planned end:COMPLETE, aHANDOFFback, or an escalation. - Every
TERMINAL: falsecompletion clears its own condition withSET. - The
MESSAGESconversation_completetext 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.