---
title: "The Agent Loop"
type: concept
tags: [agent-loop, sessions, lifecycle, tools]
updated: 2026-06-19
confidence: high
sources: [raw/github_doc-docs-concepts-agent-loop-md.md, raw/github_doc-docs-concepts-agent-md.md]
---

# 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

1. **Intake** — a message arrives from a [channel](channels.md) and is routed to a session.
2. **Context assembly** — the [context engine](context-and-memory.md) builds what the model sees: which messages to include, how to summarize older history, injected memory.
3. **Model inference** — the chosen [provider](model-providers.md) 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](agent-runtime.md); long-running work it kicks off is tracked as [background tasks](automation.md).
