Anatomy of an agent turn
Understanding what happens inside a single agent turn makes it straightforward to debug stalled loops, inject approvals, steer a running agent, or build live UIs. Both PiSharp.Agent (in-memory) and PiSharp.Durable (commit-first) follow the same six-stage rhythm, differing mainly in whether intermediate state is held in memory or committed to storage first.
The six stages of a turn
Section titled “The six stages of a turn”| Stage | What happens | In PiSharp.Agent |
In PiSharp.Durable |
|---|---|---|---|
| 1. Input & request build | User/custom input joins the transcript; system sections, active tool declarations, thinking level and request-only transforms are assembled. | AgentHooks.BeforePrompt → PrepareRequestBoundary → PrepareRequest → TransformRequestMessages → FinalTransformRequestMessages. |
SubmitAsync commits the input first; the generation task resolves sections and tool schemas from the live Registry. |
| 2. Stream & partial parse | PiSharp.AI streams text deltas, reasoning/thinking blocks and partial tool-call JSON fragments. |
IAgentEventSink.EmitAsync is awaited in strict order (AssistantMessageStarted → TurnStreamObserved → AssistantMessageEnded). |
Streamed partial output persists on ProgressSettings.PersistWithinMs (~1 s default) and folds to WatchAsync observers. |
| 3. Validate & preflight | Tool arguments are parsed and validated; policy and preflight hooks inspect each concrete call before execution. | IToolHooks.BeforeExecutionAsync can block; built-in tools run IPreparedToolAdapter → IToolActionPolicy.AuthorizeAsync. |
Harness validates against JSON Schema (pi-schema). From 1.6.0, BeforeTool hooks can park or block calls. |
| 4. Tool batch execution | Tool calls from the assistant reply execute in parallel (default) or sequentially, streaming optional progress. | ToolBatchScheduler executes Parallel unless any tool in the batch sets Sequential; results always append in call order. |
Each call runs as a durable child task (pi.tool) tracking ToolReplay.Safe vs. Unsafe and OnceAsync checkpoints. |
| 5. Commit & steering check | Tool results join the transcript; any pending steering input is drained before the next model turn starts. | ToolResultMessageEnded → drains agent.Steer(...) (OneAtATime or All) → AgentHooks.PrepareNextTurn. |
Tool results commit atomically; inbox steering messages attach to the commit before the next generation schedules. |
| 6. Continue or settle | If tools ran or steering arrived, another turn starts. If the model replied with no tool calls, follow-ups run or the run ends. | FinishTurn / FinishTurnDecision → drains agent.FollowUp(...) → returns AgentLoopResult. |
Resolves SubmissionRecord (Done, Unanswered). In 1.6.0, AfterTools and OnYield hooks can launch review tasks. |
Stage details in PiSharp.Agent
Section titled “Stage details in PiSharp.Agent”Transcript & tool declarations
Section titled “Transcript & tool declarations”In PiSharp.Agent, tool executors (ToolDefinition) and tool declarations (JSON Schemas) are deliberately separate:
- Executors are supplied in
AgentConfiguration.Tools. - Declarations travel inside the
toolsAddedarray of asystemtranscript entry, matching Pi’s wire model. - A plain
IToolExecutorreceives rawJsonDataarguments; validate them inside your executor (or usePiSharp.Contracts.Schema.JsonSchemaValidator,PiSharp.Tools, orPiSharp.CustomAgents’sAgentTool.Create<TArgs>which validates automatically).
Parallel vs. sequential tool batches
Section titled “Parallel vs. sequential tool batches”When a model emits three tool calls in a single reply:
- If all three tools allow
ToolExecutionMode.Parallel(the default),ToolBatchSchedulerstarts them concurrently. - If any tool in that batch specifies
ToolExecutionMode.Sequential(for example, a payment refund or stateful file writer), the entire batch runs sequentially in call order. - Either way,
toolResultmessages are appended to the transcript in the exact order the model emitted the calls. - If every tool result in a batch sets
Terminate = true, the loop stops cleanly after recording those results.
Steering vs. follow-up queues
Section titled “Steering vs. follow-up queues”You can queue input while PromptAsync is still running:
agent.Steer(message)is checked after each tool batch and before the next LLM call. Use it to redirect an agent mid-task without aborting in-flight file writes.agent.FollowUp(message)is checked only when the model finishes with no tool calls. Use it to queue the next task once the current one settles.SteeringModeandFollowUpModechoose betweenOneAtATime(default) andAll.
Stop reasons and bounds
Section titled “Stop reasons and bounds”Unlike an unbounded while (true) loop, AgentOptions enforces explicit ceilings so runaway loops fail predictably:
AgentLoopStopReason |
When it happens |
|---|---|
Completed |
The model finished without tool calls (or a terminating tool batch ran) and the follow-up queue is empty. |
ChatFailure |
A provider request failed, or Abort() settled the run under default CancellationBehavior.SettleAborted. |
TurnLimit |
The run reached AgentLoopOptions.MaximumTurns (default 16). |
TranscriptLimit |
The transcript reached AgentLoopOptions.MaximumTranscriptMessages (default 1,000,000). |
What changes in PiSharp.Durable
Section titled “What changes in PiSharp.Durable”In PiSharp.Durable, there is no single in-memory for loop that disappears if the process exits:
- Submissions (
root.SubmitAsync) commit an inbox record before returning. - Generation tasks (
pi.generation) run one model turn, commit the assistant entry, and spawn onepi.toolchild task per tool call. - Crash recovery resets any task that was
runningback topendingwhenHarness.OpenAsyncreopens the store. Tasks that crash the process five times in a row halt cleanly instead of looping forever. - Hooks run at durable boundaries:
BeforeRun,AfterTools,OnYield, andAfterRunobserve or modify execution across restarts.
Related guides and patterns
Section titled “Related guides and patterns”- Agent loop SDK reference — full
Agent,AgentHooks,IAgentEventSink, andAgentLoopRunnerAPI. - Steering, follow-ups & cancellation — runnable queue and abort recipes.
- Streaming UIs, watches & telemetry — wiring live sinks and folded durable views.
- Tool errors, validation & typed repairs — keeping tool failures recoverable.