📜 Constitution

Define fundamental rules for your AI system

Constitution Hierarchy

📜 Constitution
Top-level governance document
⚖️ Laws
Specific rules and policies
📋 Rules
Concrete conditions to check
🤖 Agents
Must comply with all rules

Why Constitution?

🛡️

Safety

Prevent agents from making costly mistakes or violating policies

💰

Cost Control

Enforce token limits and budget constraints automatically

📊

Compliance

Ensure agents follow industry regulations and company policies

🎯

Consistency

All agents follow the same rules, ensuring predictable behavior

Basic Constitution

constitution DataManagement {
    description: "Rules for data processing"
    
    laws: [
        {
            name: "Data Size Limit",
            description: "Data must be less than 1000 bytes",
            enforcement: mandatory,
            rules: ["data_size < 1000"]
        },
        {
            name: "Temperature Control",
            description: "Temperature must be between 0 and 1",
            enforcement: mandatory,
            rules: [
                "temperature >= 0",
                "temperature <= 1"
            ]
        }
    ]
}

💡 Automatic Enforcement

Constitution rules are checked automatically before every agent call.

Enforcement Types

🚫 Mandatory

Violations are blocked

enforcement: mandatory

// Blocks execution
// Throws error

⚠️ Advisory

Violations show warning

enforcement: advisory

// Shows warning
// Continues execution

📝 Logging

Violations are logged

enforcement: logging

// Logs to file
// Continues execution

Real-World Example

Financial system with strict rules:

constitution FinancialGovernance {
    description: "Financial transaction rules"
    
    laws: [
        {
            name: "Transaction Limit",
            description: "Single transaction cannot exceed $10,000",
            enforcement: mandatory,
            rules: ["transaction_amount <= 10000"]
        },
        {
            name: "Approval Required",
            description: "Transactions over $1,000 need approval",
            enforcement: mandatory,
            rules: [
                "transaction_amount <= 1000 OR approval_granted == true"
            ]
        },
        {
            name: "Business Hours",
            description: "Large transactions only during business hours",
            enforcement: advisory,
            rules: [
                "transaction_amount < 5000 OR is_business_hours == true"
            ]
        },
        {
            name: "Audit Trail",
            description: "All transactions must be logged",
            enforcement: logging,
            rules: ["log_transaction == true"]
        }
    ]
}

agent FinancialAgent {
    provider: "openai"
    constitution: "FinancialGovernance"
    
    task process_transaction(amount) {
        // Constitution automatically checks:
        // 1. Amount <= $10,000 (mandatory)
        // 2. Approval if > $1,000 (mandatory)
        // 3. Business hours if >= $5,000 (advisory)
        // 4. Logging (logging)
        
        return this.call(`Process $${amount} transaction`)
    }
}

// This will SUCCEED
FinancialAgent.process_transaction(500)

// This will FAIL (no approval)
// FinancialAgent.process_transaction(5000)

// This will FAIL (exceeds limit)
// FinancialAgent.process_transaction(15000)

✅ Automatic Protection

No manual checks needed - constitution enforces rules automatically!

Mnemonics in Constitution

Save tokens by defining mnemonics in your constitution:

constitution DataAnalysisSystem {
    description: "Data analysis with mnemonics"
    
    // Token-saving mnemonics
    mnemonics: {
        "DA1": "Validate data format and check for missing values",
        "DA2": "Calculate mean, median, standard deviation",
        "DA3": "Create histogram and scatter plot",
        "DA4": "Detect outliers using IQR method",
        "DA5": "Write summary report with recommendations"
    }
    
    laws: [
        {
            name: "Token Budget",
            enforcement: mandatory,
            rules: ["tokens_per_call < 1000"]
        }
    ]
}

agent DataAnalyst {
    provider: "openai"
    constitution: "DataAnalysisSystem"
    
    task analyze(data) {
        // Old way: 500 tokens
        // let result = this.call("You are a data analyst...")
        
        // New way: 50 tokens (90% savings!)
        let result = this.call("Execute: DA1→DA2→DA3→DA4→DA5")
        
        return result
    }
}

💰 Cost Savings

Mnemonics can reduce token usage by 80-95%, saving thousands of dollars!

Governance Models

HudHudScript supports 12 different governance models:

🗳️ Democracy

Equal voting rights

👑 Monarchy

Single ruler

🎓 Technocracy

Expert-based

👥 Oligarchy

Small group rules

🏛️ Republic

Representative voting

⚖️ Meritocracy

Merit-based power

constitution DemocraticSystem {
    model: "democracy"
    
    // All agents have equal voting power
    voting: {
        type: "majority",
        quorum: 0.5
    }
}

constitution MonarchySystem {
    model: "monarchy"
    
    special_roles: {
        "king": {
            bypass_constitution: true,
            modify_laws: true,
            voting_weight: 10.0
        }
    }
}