wikis / OpenClaw / wiki / concepts / the-agent-loop.md view as markdown report a mistake
The Agent Loop
The agentic loop is the authoritative path that turns an incoming message into actions and a final reply while keeping session state consistent.
The stages
- Intake โ a message arrives from a channel and is routed to a session.
- Context assembly โ the context engine builds what the model sees: which messages to include, how to summarize older history, injected memory.
- Model inference โ the chosen provider streams a response.
- Tool execution โ the agent calls tools (and may spawn subagents); results feed back into the loop.
- Streaming replies โ partial output streams back to the channel as it's produced.
- Persistence โ the transcript and session state are written durably.
Sessions
Each conversation is a session with persisted transcript and state. The runtime enforces a session write lock and queueing so concurrent messages to the same session don't corrupt state โ a message that arrives mid-run is queued rather than racing the in-flight loop.
Why it matters
Understanding the loop matters when you change session queueing, transcript writes, or lifecycle behavior, and when reasoning about how a single user message can produce many tool calls, subagent runs, and streamed updates before the final reply. The loop is implemented by the agent runtime; long-running work it kicks off is tracked as background tasks.
