Skip to main content

Module run_loop

Module run_loop 

Source
Expand description

Agent run loop — the core iterative execution engine.

Orchestrates the turn-by-turn lifecycle of an agent invocation: policy checking, context building, model calling (streaming or complete), response processing, tool execution, and persisting. Extracted from super::agent::AgentRuntime to keep that file under 1000 lines.

§States

The loop is driven by TurnState transitions via TurnTransition:

CheckingPolicy → BuildingContext → CallingModel
                                              ↓
ProcessingResponse ←────────────────── ←── ←──┘
       ↓ (tool calls)
ExecutingTools → Persisting → CheckingPolicy (next iteration)

§Recovery

On provider context overflow (FinishReason::Length) the loop falls back to a streaming-only model call, and on output truncation it prompts the model to continue where it left off. Snapshot-based recovery allows resuming from any intermediate state.