執行期

對話輪次上下文

每輪一次組裝的指令、歷史、工具、記憶和護欄的綜合視圖。

對話輪次上下文

這是什麼

每輪一次組裝的指令、歷史、工具、記憶和護欄的綜合視圖。

白話解釋

攝影師在拍攝前會打包一個背包——針對特定工作準備合適的鏡頭、備用電池和記憶卡——一旦快門開始按下,就不會停下來翻找更多裝備。Hermes 在每輪對話開始時組裝一個等效的包裹:對話歷史、活躍指令、檢索到的記憶以及當前開啟的工具,在模型被呼叫之前就固定下來。

為什麼重要

build_turn_context 每輪決定一次模型在這一輪中允許看到什麼——對話歷史、活躍的系統提示、檢索到的記憶、啟用的工具以及任何活躍的護欄。一個在儀表板中看起來「已安裝」但從未出現在模型選項中的工具集,通常是在這裡失敗,而不是在工具層本身。輪次中的每個後續步驟(包括工具執行)都是從這個函數產生的快照中運作,而不是自己重新推導上下文。

How it works

Defined in agent/turn_context.py, build_turn_context runs once at the very top of a turn, before any model call happens. It restores or rebuilds the cached system prompt only when one is missing, runs a cheap character-based estimate to decide whether the expensive full token count is even worth paying for, and compresses older conversation history when that estimate crosses the configured threshold. It also refreshes any newly-connected MCP tool server into this turn’s tool snapshot and runs the pre_llm_call plugin hook so installed plugins can inject extra context. Everything produced — the sanitized message, working message list, active system prompt, and a fresh turn_id — is packaged into one TurnContext object that the turn loop’s run_conversation function reads from for the rest of the turn.

A concrete example

A session has grown to roughly 300 messages and is edging toward the model’s context window.

  1. A new message arrives, and build_turn_context runs before any model call.
  2. It estimates the current token count from message lengths and decides compression is warranted.
  3. Older exchanges are compressed into a condensed summary while recent messages stay intact.
  4. The resulting TurnContext fits within the model’s window, and the conversation continues without losing the thread.