Agent Core & Loop

Conversation Main Loop Decision Mechanism & Concurrency Control

Decrypting the run_conversation() lifecycle state machine in run_agent.py & agent/conversation_loop.py
Session Loop State Machine
📊 Figure 2-1-1: AI Agent Decision Flow State Machine & API Lock Transition

🔄 1. Session Main Loop Deep Dive

In the run_conversation() function in agent/conversation_loop.py, the agent maintains a state machine. It does not simply execute a single LLM request but enters an adaptive tool-calling loop. The specific steps include:

  • State Initialization: Load session context and long-term memory from the database, assembling the initial messages array.
  • Interrupt Pre-check: At the start of each loop iteration, check agent._interrupt_requested. The user sending a new message or a cancel signal from the gateway can trigger an interrupt.
  • LLM Negotiation: Invoke client.chat.completions.create with api_messages and the tool list.
AIAgent Decision Flow and Cache Optimization
📊 Figure 2-1-2: AIAgent Decision Flow & Cache Optimization Overall Sequence Diagram

🛡️ 2. Concurrency Throttling & Grace Call Mechanism

Key design to handle token budget exhaustion and runaway multi-agent concurrency:

🚦 Graceful Exit (grace_call)

When iteration_budget.remaining reaches zero, to prevent the agent from abruptly halting, the system activates _budget_grace_call. This grants the model one final read-only interaction, clearing the tools definition and forcing the LLM to generate only a concluding summary reply to the user.

🛑 Sub-Agent Truncation (_cap_delegate_task_calls)

When the LLM dispatches too many parallel delegate_task calls, high local resource consumption occurs. The system truncates concurrent sub-tasks to the MAX_CONCURRENT_CHILDREN limit and feeds overflow tasks back to the LLM as warning-format feedback, preventing runaway execution.