guides

Feature Flags and A/B Testing

Implement feature toggles and experiment logic with compiled code.

Published May 30, 2026

Simple Feature Flag

def is_enabled(flag: str, context: dict) -> bool:
    rules = {
        'new_algorithm': lambda ctx: ctx.get('user_id', 0) % 2 == 0,
        'beta_feature': lambda ctx: ctx.get('tier') == 'beta',
    }
    rule = rules.get(flag)
    return rule(context) if rule else False

A/B Test Assignment

import hashlib

def ab_variant(user_id: str, experiment: str, variants: int = 2) -> int:
    h = hashlib.md5(f"{experiment}:{user_id}".encode()).hexdigest()
    return int(h, 16) % variants

Percent Rollout

def rollout(user_id: str, feature: str, percent: int) -> bool:
    h = hashlib.md5(f"{feature}:{user_id}".encode()).hexdigest()
    bucket = int(h[:8], 16) % 100
    return bucket < percent