[agent_instructions] token not getting updated
>>> [!note] Migrated issue
<!-- Drupal.org comment -->
<!-- Migrated from issue #3523523. -->
Reported by: [akhil babu](https://www.drupal.org/user/3632866)
Related to !124
>>>
<h3 id="summary-problem-motivation">Problem/Motivation</h3>
<p>While testing the content type triage agent and some other custom agents that I've created, I observed that the [agent_instructions] is not getting replaced. The text '[agent_instructions]' is directly sent to the llm as system prompt.<br>
Ideally it should be replaced with the 'Agent instruction' of each agent, if no default value is set</p>
<h4 id="summary-steps-reproduce">Steps to reproduce</h4>
<p>1. Enable AI logging, AI agents (1.1 version), AI API Explorer and AI Agents Explorer and Latest release of any AI provider that supports function calling<br>
2. Visit:/admin/config/ai/agents/explore?agent_id=content_type_agent_triage<br>
3. Give 'How many content types are there' as promt and wait for the final answer<br>
4. Go to '/admin/content/ai-agent-decision' and check the last decision<br>
5. The prompt used value will be</p>
<pre>[agent_instructions]<br><br>This is the first time that this agent has been run. <br>The following is information that is important as context: <br>-----------------------------------------------<br>Values: Node Types<br>Description of values: The existing node types on the system</pre><h3 id="summary-proposed-resolution">Proposed resolution</h3>
<p>The core token service <code>Drupal\Core\Utility\Token::replace</code> is not replacing the token [agent_instructions] as it expects tokens to be in the format [name:value] so either update the token name or use str_replace to replace the system prompt.</p>
<p>Drupal\ai_agents\PluginBase\AiAgentEntityWrapper::applyTokens</p>
<div class="codeblock">
<pre><span style="color: #000000"><span style="color: #0000BB"><?php<br> </span><span style="color: #007700">public function </span><span style="color: #0000BB">applyTokens</span><span style="color: #007700">(</span><span style="color: #0000BB">string $prompt</span><span style="color: #007700">): </span><span style="color: #0000BB">string </span><span style="color: #007700">{<br> </span><span style="color: #FF8000">// Handle agent_instructions token directly.<br> </span><span style="color: #007700">if (</span><span style="color: #0000BB">strpos</span><span style="color: #007700">(</span><span style="color: #0000BB">$prompt</span><span style="color: #007700">, </span><span style="color: #DD0000">'[agent_instructions]'</span><span style="color: #007700">) !== </span><span style="color: #0000BB">FALSE</span><span style="color: #007700">) {<br> </span><span style="color: #0000BB">$prompt </span><span style="color: #007700">= </span><span style="color: #0000BB">str_replace</span><span style="color: #007700">(</span><span style="color: #DD0000">'[agent_instructions]'</span><span style="color: #007700">, </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">aiAgent</span><span style="color: #007700">-></span><span style="color: #0000BB">get</span><span style="color: #007700">(</span><span style="color: #DD0000">'system_prompt'</span><span style="color: #007700">), </span><span style="color: #0000BB">$prompt</span><span style="color: #007700">);<br> }<br><br> </span><span style="color: #0000BB">$tokens </span><span style="color: #007700">= [<br> </span><span style="color: #DD0000">'user' </span><span style="color: #007700">=> </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">currentUser</span><span style="color: #007700">,<br> </span><span style="color: #DD0000">'ai_agent' </span><span style="color: #007700">=> </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">aiAgent</span><span style="color: #007700">,<br> ];<br> </span><span style="color: #FF8000">// Add dynamical tokens.<br> </span><span style="color: #0000BB">$tokens </span><span style="color: #007700">= </span><span style="color: #0000BB">array_merge</span><span style="color: #007700">(</span><span style="color: #0000BB">$tokens</span><span style="color: #007700">, </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">tokens</span><span style="color: #007700">);<br> return </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">token</span><span style="color: #007700">-></span><span style="color: #0000BB">replace</span><span style="color: #007700">(</span><span style="color: #0000BB">$prompt</span><span style="color: #007700">, </span><span style="color: #0000BB">$tokens</span><span style="color: #007700">);<br> }<br></span><span style="color: #0000BB">?></span></span></pre></div>
<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