Skip to main content
Surface why a bot turn ended — so a chat user reads “I stopped at the step limit” instead of a silently truncated reply. Off by default — clean completions and existing deployments are byte-for-byte unchanged.

Quick Start

1

Turn the note on

Pass surface_completion_reason=True to the session manager. A turn that stops early appends a note to the reply.
A truncated turn now ends with ⏳ I stopped after reaching the step limit — reply "continue" to carry on.
2

Emit a custom TurnCompletion yourself

Return an AgentReply with your own TurnCompletion when the agent already knows why it stopped.
detail overrides the default note verbatim — it’s already user-safe, no template applied.

How It Works

The gateway reads the turn’s completion after each reply and appends a note only when the turn stopped early and the flag is on.

Configuration Options

surface_completion_reason is a parameter on BotSessionManager — there is no BotConfig field for it today, and build_session_manager does not forward it. Construct the session manager directly, or use the raw helpers below in a custom reply path.

Completion reason vocabulary

The reasons mirror Agent.last_stop_reason and the built-in _COMPLETION_NOTES map. .truncated is True for anything not in {"completed", ""}.

The TurnCompletion Contract

TurnCompletion wraps the coarse stop reason into a portable value the reply path can render.

Gateway Integration

Operators wiring a custom reply path use the two raw helpers directly.
extract_completion checks, in order:
  1. AgentReply.completion if the result is an AgentReply.
  2. dict["completion"] if the result is a serialised dict.
  3. result.completion attribute on any object.
  4. result.last_stop_reason string on any object (the plain-text-agent fallback).
  5. None.
append_completion_note returns the text untouched when enabled=False, when there is no completion, or when the turn completed cleanly — so turning it on only affects truncated turns.

Common Patterns

Turn on notes for early stops on public bots

One flag on the session manager, matching the Quick Start.

Return an AgentReply with a custom detail

For agent code that already knows why it’s stopping.

Serialise across a network hop

Round-trip via to_dict() / from_dict() on both TurnCompletion and AgentReply.

Best Practices

Clean completions never surface a note, so turning surface_completion_reason on only affects truncated turns. Leave it off for bots where a silent reply is fine.
detail is user-safe, agent-authored, and localisable per turn. Set it when the agent knows the specific reason instead of relying on the generic default.
Set max_steps=5 so ⏳ step limit notes surface fast while you tune step budgets.
The gateway’s default cancelled note is generic — set your own detail to replace it.

Messaging Bots

All supported platforms: Telegram, Discord, Slack, WhatsApp

Max Steps

Bound how many steps an agent turn may take

Error Handling

Retries, fallbacks, and graceful failure

Bot Streaming Replies

Live streaming responses for Telegram, Slack, and Discord