Agent Loop¶
The agent loop is the low-level multi-turn execution engine that powers the Agent class in pi-llm-agent. It manages the cycle of calling the LLM, executing tool calls, and repeating until the task is complete.
Overview¶
The loop operates at a lower level than the Agent class. While Agent provides state management, event dispatch, and message queueing, the agent loop focuses purely on the turn-by-turn execution logic.
Key functions¶
run_agent_loop(messages, context, config, event_sink, cancellation, stream_fn)-- Run the loop with new input messages.run_agent_loop_continue(context, config, event_sink, cancellation, stream_fn)-- Continue from existing context.agent_loop()/agent_loop_continue()-- Lower-level async generators.
Turn structure¶
A single turn consists of:
- Build context from system prompt, messages, and tools.
- Call the LLM via the
stream_fn. - Consume the event stream, building the assistant message.
- If the message contains tool calls, execute them and append results.
- Poll for steering messages.
- If there are pending tool results or steering messages, start a new turn.
AgentLoopConfig¶
Content coming soon.
Next steps¶
- Agent Lifecycle -- The Agent class that wraps the loop
- Events -- Events emitted during the loop
- Steering & Follow-up -- Message injection between turns