Source - Hermes Agent Loop (Nous Research Developer Guide)

Source - Hermes Agent Loop (Nous Research Developer Guide)

URL: https://hermes-agent.nousresearch.com/docs/developer-guide/agent-loop
Author/Org: Nous Research
Ingested: 2026-04-14

Summary

Developer guide for the AIAgent class (run_agent.py) at the core of Nous Research's Hermes Agent system. Covers the full agent loop: prompt assembly, provider selection, turn lifecycle, tool dispatch, compression, budget tracking, and callback surfaces.

Key Takeaways

Scale

The AIAgent class is ~10,700 lines and handles prompt assembly, tool dispatch, and provider failover in a single orchestration engine.

API Mode Abstraction

Three execution modes with automatic resolution:

Mode priority: explicit constructor arg → provider detection → base URL heuristics → default (chat_completions).

Turn Lifecycle (10 steps)

  1. Generate task_id if needed
  2. Append user message to history
  3. Build or reuse cached system prompt
  4. Check if compression needed (>50% context)
  5. Build API messages from conversation history
  6. Inject ephemeral prompt layers
  7. Apply Anthropic prompt caching markers if applicable
  8. Make interruptible API call
  9. Parse response → execute tools or return final response

Interruptible Calls

API requests run in background threads; main thread monitors for interrupts. On interrupt: thread abandoned, partial results discarded — never injected into history.

Tool Execution

Agent-level tools intercepted before the registry: todo, memory, session_search, delegate_task (spawns subagents).

Iteration Budget

Compression

Fallback Model

On primary model failure (rate limits, server errors, auth failures): attempts fallback providers in configured order, continues conversation with successful provider.

Message Format

All messages use OpenAI-compatible format internally:

{"role": "system", "content": "..."}
{"role": "user", "content": "..."}
{"role": "assistant", "content": "...", "tool_calls": [...]}
{"role": "tool", "tool_call_id": "...", "content": "..."}

Reasoning content stored in assistant_msg["reasoning"].

Alternation rules: strict user/assistant alternation; only tool role allows consecutive entries.

Callback Surfaces

tool_progress_callback, thinking_callback, reasoning_callback, clarify_callback, step_callback, stream_delta_callback, status_callback.

Key Files

File Purpose
run_agent.py AIAgent class (~10,700 lines)
prompt_builder.py System prompt assembly
context_engine.py Pluggable context management
context_compressor.py Lossy summarization algorithm
prompt_caching.py Anthropic caching markers
auxiliary_client.py Auxiliary LLM for side tasks
model_tools.py Tool schema collection and dispatch