mirror of
https://github.com/open-webui/open-webui.git
synced 2026-05-06 10:58:17 -05:00
[PR #20479] [CLOSED] fix: prevent system prompt duplication in native function calling #64495
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/open-webui/open-webui/pull/20479
Author: @jvadura
Created: 1/8/2026
Status: ❌ Closed
Base:
main← Head:fix/system-prompt-duplication📝 Commits (1)
42b731cfix: prevent system prompt duplication in native function calling📊 Changes
1 file changed (+18 additions, -0 deletions)
View changed files
📝
backend/open_webui/utils/misc.py(+18 -0)📄 Description
Summary
Prevents system prompt content from being duplicated during native function calling with MCP tools, which was causing quadratic token growth and excessive API costs.
Problem
When using native function calling mode with MCP tools, each tool call iteration triggers
update_message_content()which prepends the system prompt to the existing system message. This causes the same prompt to be duplicated multiple times:Impact: A 20k token conversation can balloon to 3M+ tokens over multiple tool call iterations, causing massive unnecessary API costs.
Root Cause
The bug occurs in the agentic tool call loop:
apply_system_prompt_to_body()withreplace=Truegenerate_chat_completion()againapply_system_prompt_to_body()withreplace=False(default)update_message_content()withappend=False, which prepends the contentFix
Add a check in
update_message_content()to skip the update if the content is already present at the start of the existing message. This prevents duplicate prepending while preserving the ability to append genuinely new content.Test Plan
Verified: Token count remains stable at ~12k tokens after 3 tool calls (previously would have grown to ~36k+).
Related: #19656
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.