Skip to main content

Module loop_runner

Module loop_runner 

Source
Expand description

Claude-Code-shaped streaming agent loop.

run(...) returns an async stream of SdkMessage. The loop:

  1. Yields System{Init} carrying the session id.
  2. POSTs to the configured chat-completions endpoint.
  3. On finish_reason == "tool_calls": yields one Assistant message with text + tool_use blocks, runs each tool through the permission gate (read-only tools in parallel, mutating tools sequentially), yields one User message containing all tool_result blocks, and continues.
  4. On any other finish reason: yields the final Assistant text and a Result{Success} carrying usage, cost, and turn count.
  5. Enforces max_turns and max_budget_usd; transport errors yield Result{ErrorDuringExecution}.

Functionsยง

run
Run the agent loop and stream SdkMessages in turn order.