Keel by ChimpLabs
Jump to page or section Esc
Primitives

Flag

Schema-typed feature flags with typed evaluation contexts, boolean helpers, and safe default fallbacks.

Flag defines schema-validated feature flags for booleans, strings, numbers, and structured experiment variants. Each flag evaluates against an optional typed context such as tenant ID, user tier, or region and falls back to defaultValue when no rule matches.

Use Flag for progressive rollouts, kill switches, tenant-specific features, and typed configuration experiments.


Declaring a Flag

Plain TypeScript SDK (@chimp-labs/keel/sdk)

import { k } from "@chimp-labs/keel/sdk"

export const NewCheckoutFlag = k.flag("new-checkout", {
  value: k.boolean(),
  defaultValue: false,
  context: k.object({
    tenantId: k.string(),
    plan: k.string(),
  }),
})

export const MaxBatchSizeFlag = k.flag("max-batch-size", {
  value: k.int(),
  defaultValue: 50,
})

Configuration options

Option Type Required Description
value SchemaLike Yes Schema validating the flag’s evaluated value.
defaultValue InferSchema<Val> Yes Fallback value returned when no override/rule matches.
context SchemaLike No Optional schema describing caller attributes required during evaluation.

Effect v4 (@chimp-labs/keel/flag)

import { Schema } from "effect"
import { Flag } from "@chimp-labs/keel/flag"

const UserCtx = Schema.Struct({
  tenantId: Schema.String,
  plan: Schema.String,
})

export class NewCheckout extends Flag("new-checkout", {
  value: Schema.Boolean,
  defaultValue: false,
  context: UserCtx,
}) {}

Attaching to a service

export const CheckoutService = k.service("checkout", {
  resources: [OrdersTable, NewCheckoutFlag],
  operations: { /* ... */ },
})

Operations reference

Evaluating flags

When a flag declares context, TypeScript requires a matching context object. When context is omitted, no argument is needed:

// Boolean helper (only available on boolean flags)
const enabled = await keel.newCheckout.isEnabled({
  tenantId: "acme",
  plan: "enterprise",
})

// General typed evaluation (booleans, numbers, strings, or objects)
const batchSize = await keel.maxBatchSize.evaluate()

Configuring rules in memory and tests

await using keel = await createKeel({
  backend: "memory",
  flags: {
    "new-checkout": (ctx: { plan: string }) => ctx.plan === "enterprise",
    "max-batch-size": 200,
  },
  resources: {
    newCheckout: NewCheckoutFlag,
    maxBatchSize: MaxBatchSizeFlag,
  },
})

SDK and Effect API mapping

SDK method (keel.<flag>.*) Effect function (@chimp-labs/keel/flag) Description
evaluate(context?) evaluateFlag(flag, context?) Evaluates the flag, decodes the typed value, and falls back to defaultValue.
isEnabled(context?) isEnabled(flag, context?) Boolean wrapper that returns Promise<boolean>.

Errors and failure modes

Error tag Category HTTP status Retryable When it occurs
FlagEvaluationError internal 500 Yes Underlying feature flag provider threw an error.
SchemaError invalid 400 No Evaluation context or returned flag value failed schema validation.

Backends and infrastructure

  • FeatureFlagsMemory supports static values and rule predicates (context) => value in memory.
  • PostgreSQL 17 (FeatureFlagsPostgresLive, included in KeelPostgresLive and backend: "postgres") stores flags in a keel_flags JSONB table and includes setPostgresFlag(key, value) for runtime updates.
  • SurrealDB 4 (FeatureFlagsSurrealLive, included in KeelSurrealLive) stores durable flag records in SurrealDB.
  • To integrate LaunchDarkly, OpenFeature, or config files without changing application code, implement the FeatureFlags Effect service tag.

Testing in memory

await using keel = await createKeel({
  backend: "memory",
  flags: {
    "new-checkout": (ctx: { tenantId: string }) => ctx.tenantId === "beta-co",
  },
  resources: { newCheckout: NewCheckoutFlag },
})

const beta = await keel.newCheckout.isEnabled({ tenantId: "beta-co", plan: "pro" }) // true
const prod = await keel.newCheckout.isEnabled({ tenantId: "other", plan: "pro" })   // false