TheDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/earendil-works/pi/llms.txt
Use this file to discover all available pages before exploring further.
Agent class emits typed events for every phase of LLM interaction. Subscribe to these events to drive streaming UI, track tool progress, and perform barrier work at the end of a run.
Event types
| Event | Description |
|---|---|
agent_start | Agent begins processing a prompt or continuation |
agent_end | Final event for the run; awaited subscribers count toward settlement |
turn_start | A new turn begins (one LLM call plus its tool executions) |
turn_end | Turn completes with the assistant message and collected tool results |
message_start | Any message begins — user, assistant, or toolResult |
message_update | Assistant only. Contains assistantMessageEvent with streaming delta |
message_end | A message is complete |
tool_execution_start | A tool call begins |
tool_execution_update | A tool streams progress |
tool_execution_end | A tool call completes |
message_update and assistantMessageEvent
message_update wraps the underlying pi-ai streaming event in assistantMessageEvent. Check its type field to handle each delta kind:
Event sequence diagrams
prompt() without tool calls
prompt() with tool calls
When the assistant calls tools, the loop adds a second turn for the follow-up LLM response:When using the
Agent class, message_end processing acts as a barrier before tool preflight begins. beforeToolCall therefore sees agent state that already includes the assistant message that requested the tool call.subscribe() and agent_end settlement
subscribe() returns an unsubscribe function. Listeners are awaited in registration order.
agent_end signals that no more loop events will be emitted. await agent.waitForIdle() and await agent.prompt(...) only settle after all awaited agent_end listeners finish.
Tool execution modes
Tool execution is configurable globally and per-tool.- Parallel (default)
- Sequential
- Per-tool override
Tools in a batch are preflighted sequentially, then executed concurrently.
tool_execution_end events fire in completion order. toolResult messages and turn_end.toolResults follow assistant source order.beforeToolCall hook
Runs aftertool_execution_start and after argument validation. Return { block: true, reason } to prevent execution.
afterToolCall hook
Runs after execution finishes, beforetool_execution_end and final tool result message events are emitted.
{ terminate: true }— hint that the agent should stop after this batch (only takes effect when every result in the batch terminates){ details: {...} }— replace or augment the result’s details objectundefined— no change
shouldStopAfterTurn
The low-levelagentLoop() API exposes shouldStopAfterTurn for graceful loop exit after any turn.
shouldStopAfterTurn runs after turn_end is emitted and after the assistant response and tool executions have completed normally. If it returns true, the loop emits agent_end and exits before polling steering or follow-up queues, and before starting another LLM call. It does not abort the provider stream, cancel running tools, or alter the assistant message stop reason.
Low-level API
UseagentLoop() and agentLoopContinue() when you need direct control without the Agent class.
Related pages
pi-agent-core overview
Agent class constructor, state, methods, and proxy usage.
Defining agent tools
AgentTool interface, execute signature, and tool error handling.