mirror of
https://github.com/open-webui/open-webui.git
synced 2026-05-06 10:58:17 -05:00
[PR #20480] [CLOSED] fix: prevent system prompt duplication in native function calling #41270
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/20480
Author: @jvadura
Created: 1/8/2026
Status: ❌ Closed
Base:
dev← Head:fix/system-prompt-duplication📝 Commits (9)
fe6783cMerge pull request #19030 from open-webui/devfc05e0aMerge pull request #19405 from open-webui/deve3faec6Merge pull request #19416 from open-webui/dev9899293Merge pull request #19448 from open-webui/dev140605eMerge pull request #19462 from open-webui/dev6f1486fMerge pull request #19466 from open-webui/devd95f533Merge pull request #19729 from open-webui/deva7271530.6.43 (#20093)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
Contributor License Agreement
By submitting this pull request, I confirm that I have read and fully agree to the Contributor License Agreement (CLA), and I am providing my contributions under its terms.
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.