The Message Validator Hook is a feature in Mahilo that enforces policies on messages exchanged between agents. This allows you to define rules that messages must adhere to, and messages that violate these policies will be rejected and sent back to the sender with appropriate feedback.
In multi-agent systems, agents can sometimes engage in unproductive or harmful communication patterns. The Message Validator Hook helps prevent these issues by:
- Enforcing user-defined policies on all messages
- Preventing endless loops where agents keep sending similar messages back and forth
- Ensuring messages adhere to guidelines (e.g., professional tone, no sensitive information)
- Providing feedback to agents when their messages violate policies
Mahilo supports two types of policies:
- Heuristic Policies: Rule-based, programmatic checks defined as Python functions
- Natural Language Policies: Policies defined in natural language and evaluated using an LLM
When a message is sent between agents, it goes through the following validation process:
- The message is passed to the
MessageValidatorwhich calls thePolicyManager - Policies are evaluated in order of priority (higher priority policies are evaluated first)
- If a high-priority policy (priority >= 100) is violated, evaluation stops immediately
- A list of all policy violations is returned
- If there are no violations, the message is delivered; otherwise, it's rejected
Natural language policies require an LLM to evaluate messages. The model is selected in the following order:
- The model specified when creating the
AgentManagerviavalidator_model_name - The model specified in the
MAHILO_POLICY_MODELenvironment variable - The model specified in the
MAHILO_LLM_MODELenvironment variable - The default model from
llm_config
from mahilo.agent_manager import AgentManager
# Create an agent manager with an LLM model for natural language policies
agent_manager = AgentManager(
secret_key="your_secret_key",
db_path="messages.db",
service_name="your_service",
validator_model_name="gpt-4o-mini" # Optional, specify the model to use for policy validation
)from typing import Dict, Optional, Tuple
from mahilo.message_protocol import MessageEnvelope
def no_keyword_policy(message: MessageEnvelope, context: Dict) -> Tuple[bool, Optional[str]]:
"""Policy that prevents messages containing a specific keyword."""
if "forbidden" in message.payload.lower():
return False, "Messages containing the word 'forbidden' are not allowed."
return True, None
agent_manager.add_heuristic_policy(
name="no_keyword",
description="Prevents messages containing the word 'forbidden'",
policy_function=no_keyword_policy,
priority=80 # Higher priority policies are evaluated first
)agent_manager.add_natural_language_policy(
name="professional_tone",
description="Ensures messages maintain a professional tone",
policy_text=(
"All communication between agents should maintain a professional tone. "
"Agents should not use slang, informal language, or emojis. "
"Messages should be clear, concise, and focused on the task at hand."
),
priority=70
)Mahilo comes with several built-in policies that are enabled by default:
- Name:
anti_loop - Type: Heuristic
- Priority: 90 (High)
- Description: Prevents agents from falling into repetitive conversation patterns
- Implementation:
- Detects when a message is >80% similar to the sender's previous message
- Identifies ping-pong patterns (A→B→A→B) where messages from each agent are >70% similar
- Requires at least 4 messages in the conversation history
- Error message: "Your message is too similar to your previous message. Please provide new information or a different approach."
- For ping-pong patterns: "It seems you're in a repetitive conversation pattern. Try a different approach or provide new information."
- Name:
message_length - Type: Heuristic
- Priority: 50 (Medium)
- Description: Ensures messages aren't too long or too short
- Implementation:
- Rejects messages shorter than 10 characters with: "Your message is too short. Please provide more information."
- Rejects messages longer than 4,000 characters with: "Your message is too long. Please be more concise."
- Name:
relevance - Type: Natural Language (requires LLM)
- Priority: 70 (Medium-high)
- Description: Ensures messages are relevant to the task or topic
- Policy Text:
The message must be relevant to the current task or topic of conversation. It should not introduce completely unrelated topics without clear justification.
- Name:
toxicity - Type: Natural Language (requires LLM)
- Priority: 100 (Highest)
- Description: Prevents harmful or inappropriate content
- Policy Text:
The message must not contain harmful, offensive, or inappropriate content. This includes but is not limited to: hate speech, personal attacks, explicit content, or anything that could be considered harmful to individuals or groups.- Being the highest priority policy (100), if it's violated, no further policies are evaluated
# Get all policies
policies = agent_manager.get_policies()
# Enable/disable a policy
agent_manager.enable_policy("no_keyword")
agent_manager.disable_policy("no_keyword")
# Remove a policy
agent_manager.remove_policy("no_keyword")
# Get policy violations
violations = agent_manager.get_policy_violations(limit=10)
# Get violations for a specific policy
violations = agent_manager.get_policy_violations(limit=10, policy_name="toxicity")When policies are evaluated, they receive a context dictionary that can include:
conversation_history: Previous messages exchanged between the agents- Other key-value pairs that might be relevant for policy evaluation
For natural language policies, this context is serialized and included in the prompt sent to the LLM.
See the policy_example.py file for a complete example of how to use the Message Validator Hook.
The Message Validator Hook is integrated with the existing message broker architecture. When a message is sent, it is first validated against all enabled policies. If it passes validation, it is delivered to the recipient. If it fails validation, an error message is sent back to the sender with information about which policy was violated and suggestions for how to modify the message.
- Heuristic policies are fast and have minimal impact on message processing
- Natural language policies require an LLM call for each message, which can add latency
- Policies are evaluated in order of priority, with higher priority policies evaluated first
- High-priority policy violations (priority >= 100) stop further evaluation
- Failed policy evaluations are logged but do not prevent other policies from being evaluated