The system prompt of the main agent gets overridden when using sub-agents as tools.
>>> [!note] Migrated issue
<!-- Drupal.org comment -->
<!-- Migrated from issue #3573049. -->
Reported by: [akhil babu](https://www.drupal.org/user/3632866)
Related to !236 !235
>>>
<h3 id="summary-problem-motivation">Problem/Motivation</h3>
<p>While testing <span class="drupalorg-gitlab-issue-link project-issue-status-info project-issue-status-7"><a href="https://www.drupal.org/project/canvas/issues/3548718" title="Status: Closed (fixed)">#3548718: Orchestrator and sub-agents produce duplicate final messages</a></span> I saw that when an agent uses other agents as tools, in the final loop, the system prompt of the main agent gets overriden with the system prompt of the last sub agent tool it executed.</p>
<h4 id="summary-steps-reproduce">Steps to reproduce</h4>
<ul>
<li>In drupal cms 2, install the AI assistant recipe</li>
<li>Install ai agents explorer module</li>
<li>Open a Canvas page and ask the agent 'I am creating a page about global warming. Generate a title for it'</li>
<li>Once the task is completed, Go to '/admin/content/ai-agent-decision'</li>
<li>Check the first log. It corresponds to the orchestrator. The system prompt logged is the exact system prompt of the agent.</li>
<li>Check the last log. It should be also related to the orchestrator agent. But the logged system prompt is different. Its the system prompt of the previous sub agent that was executed</li>
</ul>
<h3 id="summary-proposed-resolution">Proposed resolution</h3>
<p>In <code>AiAgentEntityWrapper::determineSolvability()</code>, move the <code>$this->aiProvider->setChatSystemRole($system_prompt);</code> call immediately before calling <code>$this->aiProvider->chat()</code>, so that it always loads the correct system prompt</p>
<p>Tested with OpenAI provider, gpt-4.1</p>
<h3 id="summary-remaining-tasks">Remaining tasks</h3>
<h3 id="summary-ui-changes">User interface changes</h3>
<h3 id="summary-api-changes">API changes</h3>
<h3 id="summary-data-model-changes">Data model changes</h3>
issue