OpenViking icon indicating copy to clipboard operation
OpenViking copied to clipboard

[Question]: ContextBuilder.build_messages appends a trailing system message after the user message?

Open pluiez opened this issue 8 months ago • 3 comments

Your Question

In bot/vikingbot/agent/context.py, the build_messages method constructs the message list in this order:

# Line 264-283
messages = []
messages.append({"role": "system", "content": system_prompt})   # 1. system
messages.extend(history)                                         # 2. history (user/assistant pairs)
messages.append({"role": "user", "content": user_content})       # 3. current user message
messages.append({"role": "system", "content": user_info})        # 4. system (viking memory context)

An extra {"role": "system", ...} message after the final user message is appended to the messages after each new user message. A similar pattern exists in bot/vikingbot/agent/loop.py line 353, where a system message "Reflect on the results and decide next steps." is injected after tool results during the agent loop.

The resulting messages array looks like:

[
  {"role": "system", "content": "<identity + profile>"},      # context.py:268
  ...history (user/assistant turns)...
  {"role": "user", "content": "<current message>"},            # context.py:277
  {"role": "system", "content": "<viking memory context>"},    # context.py:281  ← trailing system msg
]

And during tool-use iterations:

  {"role": "tool", ...},
  {"role": "system", "content": "Reflect on the results and decide next steps."},  # loop.py:353

Since VikingBot is designed to work with multiple LLM providers, I'm wondering if this pattern is a commonly-adopted practice or if it will fail or produce undefined behavior (e.g., Anthropic Claude, or Gemini, etc.).

Context

No response

Code Example (Optional)


Related Area

Session Management

Before Asking

pluiez avatar Mar 16 '26 10:03 pluiez

Thank you for your great suggestions! This is indeed an issue that requires attention when using multiple LLM providers. We have not yet fully verified whether this approach works correctly across all providers. {"role": "system", "content": "<viking memory context>"} is retrieved in real time with the current query. The original intention of placing it at the end of the messages is to make use of the model's cache mechanism as much as possible for the unchanged system message.

yeshion23333 avatar Mar 16 '26 13:03 yeshion23333

Thank you for your great suggestions! This is indeed an issue that requires attention when using multiple LLM providers. We have not yet fully verified whether this approach works correctly across all providers. {"role": "system", "content": "<viking memory context>"} is retrieved in real time with the current query. The original intention of placing it at the end of the messages is to make use of the model's cache mechanism as much as possible for the unchanged system message.

Thanks for the explanation — the caching rationale makes sense.

That said, I think the trailing system message pattern could be problematic for cross-provider compatibility. For example, Anthropic's Claude API expects the system parameter to be separate from the messages array entirely, and doesn't support system role messages interleaved in the conversation. Other providers may silently ignore, misinterpret them, or generate unexpected behavior.

Is there any plan to adopt a more portable approach for injecting dynamic context (like memory) mid-conversation while still preserving caching benefits? Would be curious to hear if your have any ideas or candidate solutions on this.

pluiez avatar Mar 17 '26 04:03 pluiez

Thank you for your great suggestions! This is indeed an issue that requires attention when using multiple LLM providers. We have not yet fully verified whether this approach works correctly across all providers. {"role": "system", "content": "<viking memory context>"} is retrieved in real time with the current query. The original intention of placing it at the end of the messages is to make use of the model's cache mechanism as much as possible for the unchanged system message.感谢你的宝贵建议!这确实是使用多个 LLM 提供者时需要关注的问题。我们还没有完全验证这种方法是否在所有供应商中都能正常工作。 {"role": "system", "content": "<viking memory context>"} 是实时检索的,包含当前查询。将它放在消息末尾的最初意图是尽可能利用模型的缓存机制来保存未更改的系统消息。

Thanks for the explanation — the caching rationale makes sense.谢谢你的解释——缓存的理由很有道理。

That said, I think the trailing system message pattern could be problematic for cross-provider compatibility. For example, Anthropic's Claude API expects the system parameter to be separate from the messages array entirely, and doesn't support system role messages interleaved in the conversation. Other providers may silently ignore, misinterpret them, or generate unexpected behavior.不过,我认为尾随系统消息模式可能会影响跨服务商兼容性。例如,Anthropic 的 Claude API 期望系统参数完全独立于消息数组,不支持在对话中交错的系统角色消息。其他提供者可能默默无视、误解,或引发意外行为。

Is there any plan to adopt a more portable approach for injecting dynamic context (like memory) mid-conversation while still preserving caching benefits? Would be curious to hear if your have any ideas or candidate solutions on this.有没有计划采用更便携的方式,在对话中注入动态上下文(比如内存),同时保留缓存的好处?很想知道你有没有什么想法或候选解决方案。

As of now, we are still exploring to find the optimal solution. We would highly appreciate and welcome any valuable suggestions you may have.

yeshion23333 avatar Mar 17 '26 06:03 yeshion23333