How-To Guides
How do I create a percentage rollout?
Section titled “How do I create a percentage rollout?”from lexigram.features.types import Flag, FlagType, FlagContext
flag = Flag( name="new_checkout", type=FlagType.PERCENTAGE, enabled=True, percentage=25, # 25% of users)provider.add_flag(flag)
# Provide user_id for deterministic bucketingctx = FlagContext(user_id="user-42")enabled = await manager.is_enabled("new_checkout", ctx)How do I set up an A/B test with variants?
Section titled “How do I set up an A/B test with variants?”from lexigram.features.types import Flag, FlagType
flag = Flag( name="checkout_theme", type=FlagType.VARIANT, enabled=True, variants={"classic": 50, "modern": 50}, default_variant="classic",)provider.add_flag(flag)
# Evaluate variantctx = FlagContext(user_id="user-42")variant = await manager.get_variant("checkout_theme", ctx)How do I gate an async function with a flag?
Section titled “How do I gate an async function with a flag?”from lexigram.features import feature_flag
@feature_flag("new_checkout", fallback=legacy_handler)async def handle_checkout(request): """New checkout flow. Falls back to legacy_handler when disabled."""How do I use an environment-based flag provider?
Section titled “How do I use an environment-based flag provider?”from lexigram.features.backends.env import EnvProvider
env = EnvProvider(prefix="LEX_FLAG_")
export LEX_FLAG_NEW_CHECKOUT=trueHow do I chain multiple providers?
Section titled “How do I chain multiple providers?”from lexigram.features.backends.local import LocalProviderfrom lexigram.features.backends.env import EnvProviderfrom lexigram.features.backends.chained import ChainedProvider
local = LocalProvider(flags)env = EnvProvider(prefix="LEX_FLAG_")chained = ChainedProvider([local, env])
# Env overrides local when the env var is setHow do I listen for flag override changes?
Section titled “How do I listen for flag override changes?”def on_flag_change(name: str, old: bool, new: bool) -> None: logger.info("flag_changed", name=name, old=old, new=new)
manager.add_listener_sync(on_flag_change)How do I use flags from a cache backend?
Section titled “How do I use flags from a cache backend?”from lexigram.features.backends.cache import CacheBackendFlagProviderfrom lexigram.contracts.infra.cache import CacheBackendProtocol
cache = await container.resolve(CacheBackendProtocol)cached_provider = CacheBackendFlagProvider(inner=LocalProvider(flags), cache=cache)