Skip to main content

Overview

The agent loop stops when AgentState::shouldStop() returns true. You can trigger stops from within tools using AgentStopException, or apply guard hooks directly (StepsLimitHook, TokenUsageLimitHook, ExecutionTimeLimitHook). When a tool stops the agent, the last step is a ToolExecution — not a FinalResponse. This means finalResponse() returns empty. Use currentResponse() to get the best available output regardless of how the agent stopped:
  • finalResponse() — strict: only returns text when the LLM completed naturally (no pending tool calls)
  • currentResponse() — pragmatic: returns finalResponse() if available, otherwise the last step’s output
Key concepts:
  • AgentStopException: Throw from a tool to stop the loop immediately
  • StopSignal / StopReason: Describes why the loop stopped
  • finalResponse() vs currentResponse(): Strict vs pragmatic response access
  • Guard hooks: step/token/time limits implemented via lifecycle interception

Example