Agent Loop Internals
كيف تعمل حلقة الوكيل من الداخل
Start with meaning, then move to detail.
This lesson explains Agent Loop Internals as part of Hermes internals and extension points. You will learn what it does, when it matters, and the smallest safe test that proves it works.
If you are new, do not memorize names. Focus on three questions: what problem does this solve, what access does it need, and how can you verify the result?
For practice, inspect the first example, identify its effects, run it on test data, and compare the result with the source claim.
For advanced readers, inspect Core Responsibilities, Two Entry Points, API Modes, then verify failure modes and version compatibility.
Know Python, Git, and basic project structure before changing code.
A clear outcome before you read.
- Understand Agent Loop Internals without assumed prior knowledge.
- Separate the source description from what still needs testing in your environment.
- Read the first command and identify its inputs and outputs before copying it.
Short definitions before the details.
- Tool
- A structured action the agent can call to read or change something.
Detailed walkthrough of AIAgent execution, API modes, tools, callbacks, and fallback behavior
What does the source say, and in what order?
- 01Core Responsibilities
Start here to understand the core idea or structure.
- 02Two Entry Points
Read this after the foundation, then connect it to the previous step.
- 03API Modes
Read this after the foundation, then connect it to the previous step.
- 04Turn Lifecycle
Read this after the foundation, then connect it to the previous step.
- 05Message Format
Read this after the foundation, then connect it to the previous step.
- 06Message Alternation Rules
Read this after the foundation, then connect it to the previous step.
- 07Interruptible API Calls
Read this after the foundation, then connect it to the previous step.
- 08Tool Execution
Read this after the foundation, then connect it to the previous step.
- 09Sequential vs Concurrent
Read this after the foundation, then connect it to the previous step.
- 10Execution Flow
Finish here to verify the result and special cases.
Copy only after you understand the effect.
`chat()` is a thin wrapper around `run_conversation()` that extracts the `final_response` field from the result dict.
## API Modes
Hermes supports three API execution modes, resolved from provider selection, explicit args, and base URL heuristics:
| API mode | Used for | Client type |
|----------|----------|-------------|
| `chat_completions` | OpenAI-compatible endpoints (OpenRouter, custom, most providers) | `openai.OpenAI` |
| `codex_responses` | OpenAI Codex / Responses API | `openai.OpenAI` with Responses format |
| `anthropic_messages` | Native Anthropic Messages API | `anthropic.Anth### Message Format
All messages use OpenAI-compatible format internally:Reasoning content (from models that support extended thinking) is stored in `assistant_msg["reasoning"]` and optionally displayed via the `reasoning_callback`.
### Message Alternation Rules
The agent loop enforces strict message role alternation:
- After the system message: `User → Assistant → User → Assistant → ...`
- During tool calling: `Assistant (with tool_calls) → Tool → Tool → ... → Assistant`
- **Never** two assistant messages in a row
- **Never** two user messages in a row
- **Only** `tool` role can have consecutive entries (parallel tool results)
Providers validate these sequenceRead the first command and identify its inputs and outputs before copying it.
Match every command to your installed Hermes version, review the files and accounts it can reach, and use non-sensitive data for the first test. If this explanation differs from the source, the official source wins.