Skip to content

Agent Main Loop

源码版本v2026.7.20

Responsibility

run_conversation is the heart of Hermes: given a user message, it repeatedly calls the model + tools until the iteration budget is exhausted or the task completes. It coordinates context construction, tool dispatch, stop-condition handling, and fallback on failure.

The file is ~5800 lines and dense — this page covers only the main thread; details go to sub-pages.

Key Files

Data Flow

  1. run_agent.py AIAgent is a thin forwarder; actual dependency injection lives in init_agent.
  2. The caller passes user_message; run_conversation first runs build_turn_context to assemble the turn context (api_messages).
  3. Enters the while main loop: interrupt check → decrement budget (iteration_budget.consume()) → build/sanitize api_messages → pre-compression → drain /steer text → MoA aggregation → enter the API call retry sub-loop.
  4. After the model responds, validate the response and handle finish_reason; on failure, call agent._try_activate_fallback().
  5. When the compression threshold is hit, run context_compressor to reclaim space.
  6. When the loop exits (budget exhausted / task complete / interrupted), the final message is returned and delivered by the gateway (see Gateway).

Summary

The main loop only orchestrates: turn driving, budget control, retry and fallback. The real capability comes from tools and skills; multi-platform delivery is handled by the gateway; self-improvement is driven by the learning loop.

For the official Chinese intro, see README.zh-CN and website/i18n/zh-Hans/.

Unofficial community learning site. Content based on the MIT-licensed NousResearch/hermes-agent source.