Skip to content

Conversation

@DeJeune
Copy link
Collaborator

@DeJeune DeJeune commented Jan 29, 2026

What this PR does

Before this PR:
When using prompt-based tool calling (for models without native function call support), the System Prompt containing MCP tool definitions was duplicated on each recursive call after tool execution. This caused token consumption to multiply with each tool call.

After this PR:
The System Prompt remains unique throughout the conversation, regardless of how many tool calls are made. Tool definitions are only added once during the initial call.

Fixes #12638

Why we need it and why it was done in this way

The following tradeoffs were made:

  • Added a simple isRecursiveCall check in transformParams to skip system prompt rebuilding on recursive calls. This is the minimal change needed to fix the issue.

The following alternatives were considered:

  • Storing the original user system prompt in context and restoring it during recursive calls - more complex and requires additional context management.
  • Modifying buildRecursiveParams to exclude the system field - would require changes to StreamEventManager and could affect other behaviors.

The chosen approach is the simplest fix that directly addresses the root cause.

Breaking changes

None. This is a bug fix that corrects unintended behavior.

Special notes for your reviewer

Key changes:

  1. packages/aiCore/src/core/plugins/built-in/toolUsePlugin/promptToolUsePlugin.ts - Added check for context.isRecursiveCall before rebuilding system prompt
  2. Added unit tests to verify the fix and prevent regression

Checklist

  • PR: The PR description is expressive enough and will help future contributors
  • Code: Write code that humans can understand and Keep it simple
  • Refactor: You have left the code cleaner than you found it (Boy Scout Rule)
  • Upgrade: Impact of this change on upgrade flows was considered and addressed if required
  • Documentation: A user-guide update was considered and is present (link) or not required.

Release note

fix: Fixed an issue where System Prompt was duplicated when using MCP tools with prompt method, causing excessive token consumption (#12638)

When using prompt-based tool calling (for models without native function
call support), the system prompt containing MCP tool definitions was being
duplicated on each recursive call after tool execution.

Root cause: In `transformParams`, the system prompt was rebuilt even on
recursive calls, causing tool definitions to be appended repeatedly.

Fix: Check `context.isRecursiveCall` before rebuilding the system prompt.
On recursive calls, skip the system prompt transformation to preserve the
already-processed prompt.

Fixes #12638

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@DeJeune DeJeune added this to the v1.7.16 milestone Jan 29, 2026
@DeJeune DeJeune merged commit c53d6f3 into main Jan 29, 2026
5 checks passed
@github-actions github-actions bot deleted the fix/recursive-system-prompt branch January 29, 2026 16:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Multiple MCP tool calls cause System Prompt duplication (v1.7.15)

4 participants