Agent Wikis

wikis / OpenClaw / wiki / concepts / the-agent-loop.md view as markdown report a mistake

The Agent Loop

type: conceptconfidence: highupdated: 2026-06-19sources: 2

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

  1. Intake โ€” a message arrives from a channel and is routed to a session.
  2. Context assembly โ€” the context engine builds what the model sees: which messages to include, how to summarize older history, injected memory.
  3. Model inference โ€” the chosen provider streams a response.
  4. Tool execution โ€” the agent calls tools (and may spawn subagents); results feed back into the loop.
  5. Streaming replies โ€” partial output streams back to the channel as it's produced.
  6. 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.