Skip to main content

Plug-ins for agents

Plug-ins play a role in enhancing the capabilities and robustness of agents. They help enable custom behavior to be easily added to an agent’s processing flow, allowing modifications to incoming input or outgoing output. This customization is essential for applications where agents must comply with safety, security, and regulatory requirements. Plug-ins protect the agent from problematic inputs by filtering or sanitizing content and enforce compliance by applying guardrails to sensitive or restricted information. They also improve the reliability and trustworthiness of outputs by masking sensitive data or transforming results to meet specific standards.

Input and output plug-ins overview

The agent workflow includes two types of plug-ins:
  • Input plug-ins - operate before the agent processes a request, inspecting and potentially modifying incoming messages.
  • Output plug-ins - run after the agent generates a response, refining or changing the final output before it returns to the user.
Both types of plug-ins receive the message text and the agent run context, which provides additional information that can influence their behavior. Plug-ins offer multiple options for interacting with messages. They can check or validate content, rewrite, or mask parts of messages, and even stop further processing if certain criteria are met. This flexibility makes plug-ins powerful tools for enforcing business rules, safety protocols, or compliance policies dynamically during agent execution. Plug-ins are implemented as Python tools with a type that determines their invocation phase. Specify the type with the tool decorator: kind=PythonToolKind.AGENTPREINVOKE for pre-invoke plug-ins or kind=PythonToolKind.AGENTPOSTINVOKE for post-invoke plug-ins. For example, a pre-invoke plug-in is declared inline like this:
PYTHON
Developers can register or update these Python tools in the watsonx Orchestrate environment, consistent with the overall tool lifecycle in the platform. This setup helps developers extend and modify agent behavior by deploying new or updated plug-ins as needed. Plug-ins access the incoming message context through AgentPreInvokePayload and decide whether to accept or reject processing by setting continue_processing in AgentPreInvokeResult. This approach provides precise control over agent execution. Pre-invoke plug-ins address two primary situations. First, they control agent access by integrating with identity providers for authentication and authorization. Second, they process each message to accept it as-is, reject it entirely (for example, blocking unsafe content), or reformulate the input for better handling. This dual capability helps ensure robust security and input quality before the agent generates responses.

Implementation details

A plug-in receives two key pieces of information and must return a result when it finishes processing them:
  • Context – Contains details about the current state and user environment.
  • Payload – Includes the data that is required for the plug-in to perform its task.
Schemas such as PluginContext, and AgentPostInvokePayload, can be imported from the ibm_watsonx_orchestrate library. The content, displayed as JSON for illustrative purposes, typically contains details about the current state and user environment, for example:

Collaborator access control

To check access to a collaborator agent, bind a pre-invoke plug-in to the collaborator agent.
The plug-in is invoked during the parent agent pre-invoke execution.
When checking collaborator access using a pre-invoke plug-in (AgentPreInvokeType.RBAC_ONLY), if access to a specific collaborator agent is denied, that agent is removed from the parent agent’s list of collaborators. Code example:
PYTHON
Within the plug-in, use the appropriate conditions to ensure access checks are performed only when necessary, avoiding repeated calls to the access-check API for the same thread.
The payload usually contains the agent ID and the list of messages to process. For agent_pre_invoke_payload, the messages list contains only one message, such as:
A common plug-in pattern is to copy the input messages to the output and optionally modify them. The plug-in controls whether the agent continues processing by returning a flag such as continue_processing = True or False. This mechanism helps enable the plug-in to accept, reject, or modify the agent’s workflow dynamically.

Email agent

Use this configuration to include an optional tool that the agent can call, such as a tool that simulates sending an email. Other plug-ins in this example run automatically, regardless of the agent’s actions. Code example:
YAML

Email masking

The post-invoke plug-in masks email addresses after the agent completes its response. The plug-in runs automatically and modifies the final output before it is sent back to the user. The next utterance will use the updated context or conversation produced by the post-invoke plug-in processing of the previous utterance. Code example:
PYTHON

Guardrail plug-in

The pre-invoke plug-in applies guardrails before the agent processes the request. It can strip or adjust content to enforce compliance or safety requirements. Code example:
PYTHON

Import Python tools

Use this command to import Python-based tools for use as plug-ins. The tools can then be configured as pre-invoke or post-invoke plug-ins in the agent workflow. Command example:
BASH

Import Python toolkits

You can configure Python toolkit tools as plugins. You define each tool individually in the plugins section of the configuration. To reference a specific tool, use the following format:
Example
YAML
Before you reference a Python toolkit as a plugin, import the toolkit into watsonx Orchestrate. You can complete this task by using the orchestrate toolkits add or orchestrate toolkits import commands. For more information, see Add Python toolkits using the ADK CLI. Command examples