Master the OpenClaw Agent Loop: End-to-End Execution Guide
Ever felt like your AI agent is a black box? You send a message, wait, and hope it doesn’t get stuck or break something. Understanding the actual execution path—the “Agent Loop”—is the key to building reliable agentic workflows that you can actually trust in production.
An agentic loop is the full run of an agent: intake, context assembly, model inference, tool execution, streaming replies, and persistence. In OpenClaw, a loop is a single, serialized run per session that emits lifecycle and stream events as the model thinks and acts. This is how that loop is wired end-to-end.
Entry points
Section titled “Entry points”You can start an agent loop through a few different doors. The Gateway RPC uses agent and agent.wait. If you are working in the terminal, you can use the CLI agent command.
How it works (high-level)
Section titled “How it works (high-level)”The process follows a specific sequence to turn your input into a finished response:
- The
agentRPC validates your parameters and resolves the session using asessionKeyorsessionId. It saves the session metadata and returns{ runId, acceptedAt }immediately. - The
agentCommandruns the agent. It picks the model and thinking defaults, loads your skills snapshot, and callsrunEmbeddedPiAgent. It also handles lifecycle end or error events if the core loop misses them. runEmbeddedPiAgenthandles the heavy lifting. It serializes runs through session and global queues, builds the session, and subscribes to events. It also tracks usage and enforces timeouts.subscribeEmbeddedPiSessionbridges internal events to the OpenClawagentstream. It maps tool events tostream: "tool", assistant deltas tostream: "assistant", and lifecycle events tostream: "lifecycle".agent.waituseswaitForAgentJobto stay active until therunIdhits a lifecycle end or error. It returns the final status and timing data.
Queueing + concurrency
Section titled “Queueing + concurrency”To keep your session history consistent, runs are serialized per session key. This “session lane” prevents tool or session races. Messaging channels can choose different queue modes like collect, steer, or followup to feed this system. You can find more details in the Command Queue documentation.
Session + workspace preparation
Section titled “Session + workspace preparation”Before the model starts thinking, the system prepares the environment. It resolves the workspace and might redirect sandboxed runs to a specific sandbox root. It loads skills from a snapshot and injects them into the environment. Finally, it acquires a session write lock and opens the SessionManager before any streaming begins.
Prompt assembly + system prompt
Section titled “Prompt assembly + system prompt”The system prompt is a combination of several parts: the OpenClaw base prompt, skills prompts, bootstrap context, and any overrides for that specific run. The system also enforces model-specific limits and reserves tokens for compaction. You can see exactly what the model sees in the System prompt guide.
Hook points (where you can intercept)
Section titled “Hook points (where you can intercept)”OpenClaw gives you two ways to jump into the loop and change how things work.
Internal hooks (Gateway hooks)
Section titled “Internal hooks (Gateway hooks)”These are event-driven scripts for commands and lifecycle events.
agent:bootstrap: This runs while building bootstrap files. You can use it to add or remove context files before the system prompt is finished.- Command hooks: These handle events like
/new,/reset, or/stop.
Check the Hooks doc for setup details.
Plugin hooks (agent + gateway lifecycle)
Section titled “Plugin hooks (agent + gateway lifecycle)”These hooks run inside the agent loop or the gateway pipeline:
before_model_resolve: Runs before the session starts so you can override the provider or model.before_prompt_build: Runs after the session loads. You can useprependContextfor dynamic text or use system-context fields for stable guidance.before_agent_start: A legacy hook for compatibility.before_agent_reply: Runs after actions but before the LLM call. A plugin can use this to send a synthetic reply or silence the turn.agent_end: Lets you inspect the final message list and metadata.before_compaction/after_compaction: Used to observe compaction cycles.before_tool_call/after_tool_call: Lets you intercept tool parameters or results.before_install: Inspects built-in scan findings to block skill or plugin installs.tool_result_persist: Transforms tool results before they are written to the transcript.message_received/message_sending/message_sent: Hooks for inbound and outbound messages.session_start/session_end: Marks session boundaries.gateway_start/gateway_stop: Marks gateway lifecycle events.
If you are building guards, remember these rules:
- In
before_tool_callorbefore_install, setting{ block: true }stops the process. - In
message_sending, setting{ cancel: true }stops the message.
Detailed API info is in the Plugin hooks documentation.
Streaming + partial replies
Section titled “Streaming + partial replies”Assistant deltas stream from the core and show up as assistant events. The system can emit partial replies on text_end or message_end. Reasoning can also be sent as a separate stream or as part of the block replies. You can read more about chunking in the Streaming guide.
Tool execution + messaging tools
Section titled “Tool execution + messaging tools”When a tool runs, you get start, update, and end events on the tool stream. The system cleans up tool results by checking size and image payloads before logging them. It also tracks messaging tools to make sure the assistant doesn’t send duplicate confirmations.
Reply shaping + suppression
Section titled “Reply shaping + suppression”The final output is built from assistant text, reasoning, and tool summaries. If the model errors, the system includes that error text. The system filters out NO_REPLY tokens so they don’t reach the user. It also removes duplicate messaging tool payloads. If there is nothing to show but a tool failed, it sends a fallback error reply.
Compaction + retries
Section titled “Compaction + retries”When a conversation gets too long, auto-compaction triggers. This emits a compaction event and can start a retry. During a retry, the system resets in-memory buffers and tool summaries to prevent double outputs. See the Compaction guide for the full pipeline.
Event streams (today)
Section titled “Event streams (today)”The system currently supports these stream types:
lifecycle: Handles start, end, and error phases.assistant: Sends streamed deltas from the model.tool: Sends streamed tool events.compaction: Notifies you of compaction cycles.
Chat channel handling
Section titled “Chat channel handling”For chat interfaces, assistant deltas are gathered into delta messages. A final chat message is sent once the lifecycle hits an end or error state.
Timeouts
Section titled “Timeouts”The agent.wait call has a default timeout of 30 seconds, which you can change with the timeoutMs parameter. The actual agent runtime is much longer, defaulting to 48 hours, and is managed by an abort timer in the core runtime.
Where things can end early
Section titled “Where things can end early”A run can stop for a few reasons:
- The agent hits its runtime timeout.
- An
AbortSignalcancels the run. - The gateway disconnects or the RPC times out.
- The
agent.waitcall times out (though this doesn’t stop the agent itself).
Related
Section titled “Related”- Tools — available agent tools
- Hooks — event-driven scripts
- Compaction — conversation summarization
- Exec Approvals — shell command gates
- Thinking — reasoning configuration
OpenClaw Expert
Still stuck?
If this page didn't answer your case, ask OpenClaw Expert for step-by-step guidance.