Agent Rules are the decision-making engine of your StateSet agents. They enable sophisticated, context-aware behaviors that adapt to customer needs, business policies, and real-time conditions. This guide will show you how to create rules that make your agents truly intelligent.
Rules define how your agents respond to specific situations. They work on an “if-then” basis but can incorporate complex logic, multiple conditions, and sophisticated actions. Think of rules as your agent’s decision tree—guiding every interaction toward the best possible outcome.
const routingRule = await client.rules.create({ agent_id: agentId, name: 'intelligent_routing', priority: 300, conditions: { evaluate: 'routing_logic' }, routing_logic: ` // Technical issues go to tech support if (message.text.match(/error|bug|crash|not working/i)) { return 'technical_support'; } // Billing issues to finance team if (message.text.match(/charge|bill|payment|invoice/i)) { return 'billing_team'; } // Sales inquiries to sales team if (message.text.match(/pricing|discount|upgrade|plan/i) && !customer.is_existing) { return 'sales_team'; } // VIP customers always get premium support if (customer.tier === 'vip' || customer.lifetime_value > 10000) { return 'vip_support'; } // Default to general support return 'general_support'; `, actions: [ { type: 'route_to_team', team: '{{routing_result}}', transfer_context: true, message: 'I\'m connecting you with our {{routing_result.friendly_name}} team who can best assist with your request.' } ]});
const sequencedRule = await client.rules.create({ actions: [ { type: 'execute_function', function: 'check_order_status', store_result: 'order_status' }, { type: 'conditional_action', condition: 'order_status.status === "shipped"', then: { type: 'send_message', message: 'Great news! Your order has shipped and is on its way.' }, else: { type: 'send_message', message: 'Your order is being prepared and will ship soon.' } } ]});
const robustRule = await client.rules.create({ error_handling: { on_condition_error: 'skip_rule', on_action_error: 'continue_next_action', fallback_message: 'I encountered an issue, but I\'m still here to help!', log_errors: true }});