[agent_instructions] token not getting updated

Created on 9 May 2025, 6 days ago

Problem/Motivation

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.
Ideally it should be replaced with the 'Agent instruction' of each agent, if no default value is set

Steps to reproduce

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
2. Visit:/admin/config/ai/agents/explore?agent_id=content_type_agent_triage
3. Give 'How many content types are there' as promt and wait for the final answer
4. Go to '/admin/content/ai-agent-decision' and check the last decision
5. The prompt used value will be

[agent_instructions]

This is the first time that this agent has been run. 
The following is information that is important as context: 
-----------------------------------------------
Values: Node Types
Description of values: The existing node types on the system

Proposed resolution

The core token service Drupal\Core\Utility\Token::replace 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.

Drupal\ai_agents\PluginBase\AiAgentEntityWrapper::applyTokens

  public function applyTokens(string $prompt): string {
    // Handle agent_instructions token directly.
    if (strpos($prompt, '[agent_instructions]') !== FALSE) {
      $prompt = str_replace('[agent_instructions]', $this->aiAgent->get('system_prompt'), $prompt);
    }

    $tokens = [
      'user' => $this->currentUser,
      'ai_agent' => $this->aiAgent,
    ];
    // Add dynamical tokens.
    $tokens = array_merge($tokens, $this->tokens);
    return $this->token->replace($prompt, $tokens);
  }

Remaining tasks

User interface changes

API changes

Data model changes

🐛 Bug report
Status

Active

Version

1.1

Component

Code

Created by

🇮🇳India Akhil Babu Chengannur

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.71.5 2024