This page describes the internal mechanics of the AIAgent conversation loop, detailing how the agent orchestrates iterative interactions, including system prompt construction, message assembly, integration of tool calls, iteration budget management, context compression, and interrupt handling.
All user interactions with Hermes Agent are funneled into the pivotal method:
The core logic of this method is implemented in agent/conversation_loop.py agent/conversation_loop.py1-6 It manages an iterative loop that continues until the model returns a final answer, the iteration budget is exhausted, or an interrupt is received agent/conversation_loop.py4-6 The function run_conversation is the primary driver for one user turn, handling model calls, tool dispatch, retries, and post-turn hooks agent/conversation_loop.py3-6
Conversation Loop — Code Entity Map
Sources: agent/conversation_loop.py1-15 agent/chat_completion_helpers.py1-14 agent/agent_runtime_helpers.py1-18 agent/iteration_budget.py33-35
The loop is regulated by a turn-based budget and an interrupt mechanism for safety and control.
The IterationBudget class agent/iteration_budget.py33 manages the maximum allowed tool-calling steps:
consume() is called after tool execution to decrement the remaining count agent/iteration_budget.py84Hermes Agent uses a non-blocking interrupt system to stop long-running API calls or tool executions:
_interruptible_streaming_api_call (and its non-streaming variant) monitors an interrupt flag agent/conversation_loop.py90-93INTERRUPT_WAITING_FOR_MODEL_PREFIX is used to signal cancellations while waiting on a provider agent/conversation_loop.py93_cancelled_tool_result helper, which returns a structured error to the model agent/tool_executor.py202-205Sources: agent/iteration_budget.py33-84 agent/conversation_loop.py90-93 agent/tool_executor.py202-205
Before each LLM call, the agent must determine if the conversation history fits within the model's context window.
The compression logic is triggered based on context usage:
compress_context manages the reduction of history while protecting essential context agent/conversation_compression.py15-18try_shrink_image_parts_in_messages is used as a recovery helper to reduce payload size agent/conversation_compression.py20-23The loop performs several cleanup steps on messages:
_repair_tool_call_arguments fixes malformed JSON in model responses agent/message_sanitization.py39-40repair_message_sequence enforces the role alternation required by many providers agent/agent_runtime_helpers.py11_sanitize_surrogates and _sanitize_messages_non_ascii remove characters that cause API failures agent/message_sanitization.py51-59Sources: agent/conversation_loop.py31-39 agent/conversation_compression.py15-23 agent/agent_runtime_helpers.py11 agent/message_sanitization.py39-59
The agent handles tool calls through a dispatch system that includes error classification and smart failover.
tool_calls in the assistant response._execute_tool_calls_sequential or _execute_tool_calls_concurrent dispatches calls agent/tool_executor.py3-5 Concurrent execution is limited to _MAX_TOOL_WORKERS (default 8) agent/tool_executor.py95classify_api_error determines the FailoverReason (e.g., rate_limit, billing, context_overflow) agent/error_classifier.py33-51Sources: agent/tool_executor.py1-12 agent/error_classifier.py33-51 agent/conversation_loop.py1-15
| Reason | Recovery Action |
|---|---|
context_overflow | Triggers context compression or image stripping agent/conversation_loop.py31-49 |
rate_limit | Triggers adaptive_rate_limit_backoff or jittered_backoff agent/conversation_loop.py75-81 |
billing | Triggers recover_with_credential_pool to rotate keys agent/agent_runtime_helpers.py13 |
image_too_large | _image_error_max_dimension extracts provider ceilings for reactive resizing agent/conversation_loop.py162-180 |
Sources: agent/conversation_loop.py31-180 agent/agent_runtime_helpers.py13 agent/error_classifier.py33-51
Once the model provides a final answer (indicated by finish_reason: stop), the agent wraps up the turn:
extract_reasoning pulls native thinking blocks or XML scratchpads from the response agent/agent_runtime_helpers.py17convert_to_trajectory_format normalizes messages for persistent storage, including converting tool results to text-only summaries agent/agent_runtime_helpers.py76-91_flush_session_db_after_tool_progress to ensure transcript survival agent/tool_executor.py138-155Sources: agent/agent_runtime_helpers.py17-91 agent/tool_executor.py138-155 agent/conversation_loop.py30
Refresh this wiki
This wiki was recently refreshed. Please wait 2 days to refresh again.