Skip to main content

Engine functions

Every function the engine exposes, on the policy at root.policy. Call them directly in Stof (root.policy.allow(...), or self.policy.allow(...) from the document root), or from an SDK: the TypeScript SDK wraps most of them as methods, and limitr.docCall('policy.<name>', ...args) calls any of them. <Limitr>.load() types root.policy as a Limitr (a policy parsed from JSON, YAML, or TOML has no type) and returns it; the SDKs do this for you.

id is a customer ID or alt ID. Where noted, it can also be a plan ID (plan-level reads). Values are in the credit's units unless they say runes. Optional parameters (name?) default to null. Stof calls can name arguments: policy.add_customer_cap('org_1', 50, id = 'monthly', resets = true, reset_sch = 'monthly:1').

Metering​

FunctionReturnsNotes
allow(id, entitlement, value = 0, event = true, cap = null, overhead?, force = false)boolDecide and meter. See Entitlements.
check(id, entitlement, value = 0, cap = null, context = null, overhead?)boolSame decision, no changes.
features(id, context = null)listEntitlement names the customer can use now (plan, less taken, plus given, enabled). Access, not capacity. Null if no customer.
increment(id, entitlement, event = true, cap = null)boolallow with the limit's increment.
decrement(id, entitlement, event = true, cap = null)boolallow with minus the increment.
check_increment(id, entitlement, cap = null) / check_decrement(...)bool

value can be a string with units. event is true, false, an object, or a Stof/JSON string (event data and cost function context). cap is a Cap or a map of credit to ceiling. overhead is the actual cost in runes. check, allow (unless force), and features apply an entitlement's enabled rule to usage (value >= 0) and send feature-checked for track: true entitlements.

Reservations and estimates​

FunctionReturnsNotes
reserve(id, entitlement, value?, context?, basis?, segment?, ttl?, quantile = 0.9, overhead?)strHold ID, or null if not allowed now.
settle(id, entitlement, hold_id, value = 0, event = true, overhead?, basis?, segment?)boolDrop the hold, allow(force = true), learn.
release(id, entitlement, hold_id)boolDrop a hold. False if there was none.
held(id, entitlement)floatRoom held by unexpired holds.
estimate(id, entitlement, basis?, segment?, quantile = 0.9, context?)mapvalue, overhead, samples, source, scope, or null.
observe(id, entitlement, value, overhead?, basis?, segment?, context?)boolLearn from a call without a hold.

context / event (the call's event data) supplies the entitlement's estimate_basis and estimate_segment fields. basis and segment arguments override them.

Reads​

These meter nothing and show the current period (0 after a pending reset).

FunctionReturnsNotes
value(id, entitlement, percent = false, grants = true)floatMeter value this period (percent of the limit).
remaining(id, entitlement, percent = false, grants = true)floatLimit minus value, plus grants when grants. Null for observe limits.
limit(id, entitlement, grants = true)floatThe limit in meter units, plus converted grants when grants. id can be a plan.
allowance(id, entitlement, grants = true)floatWhat can run right now: remaining, capped by governor tokens.
cost(id, entitlement)floatThe standard increment. id can be a plan.
resets(id, entitlement)msWhen the meter next resets, or null.
rate(id, entitlement) / acceleration(...)floatConsumption per ms, and its change.
projected_exhaustion(id, entitlement, smoothed = false, grants = true)msWhen the limit is reached at the current rate, or null.
meter_obj(id, entitlement) / limit_obj(...)Meter / LimitThe live objects (scope and overrides resolved). Don't mutate.
entitlement(id, entitlement)EntitlementFor a customer: as it has it (gives and takes resolved, enabled not applied). id can be a plan.
customer_entitlements(id)list(name, Entitlement) pairs the customer has (plan, less taken, plus given).
credit(id) / credit_for(id, entitlement)Credit
credit_exchange(in, out, value = 1)floatConvert between credits and currencies.
set_exchange_pair(name, value, currency)boolAdd or replace a pair. False, with nothing changed, for rune, a negative value, or a pair that can't reach runes.
remove_exchange_pair(name)boolA priced credit goes back to its own price. rune and usd stay.
credit_remaining(customer_id, credit)floatGrant balance in credit (other credits converted).

Customers​

FunctionReturnsNotes
create_customer(id, plan = '', type = 'user', label = 'User', refs?, alts?, metadata?)CustomerCreates meters and included topup grants. An existing ID is updated. Null if no plan resolves, or the ID contains a period.
set_customer(stof, event = true)CustomerReplace a customer from its Stof/JSON (Ex. loaded from your database). Null if the ID contains a period.
customer(id) / customer_metadata(id) / customer_refs(id)
set_customer_refs(id, refs)bool
set_customer_plan(id, plan, overwrite_meters = false)boolKeeps meters unless overwrite_meters. Moves included topup balances by plan_change (Grants and topups).
delete_customer(id)bool
set_alt_customer_id(id, alt, event = true) / delete_alt_customer_id(alt, event = true)boolFalse for an alt ID with a period.
create_customer_override(id, entitlement, expires?, credit?, mode?, value?, increment?, resets?, reset_inc?, reset_sch?)LimitPer-customer limit. Unset fields copy the current limit.
remove_customer_override(id, entitlement)bool
give_entitlement(id, entitlement, plan?, expires?)boolGive an entitlement whatever the plan says, defined by plan or the closest plan that has it. False if no plan has it.
take_entitlement(id, entitlement, expires?)boolDeny it even though the plan has it. Meters and overrides are kept.
restore_entitlement(id, entitlement)boolUndo a give or take. False when there was neither.
closest_plan_with(customer, entitlement)PlanThe plan a give without plan uses.
apply_customer_topup(id, topup)boolGrant a plan topup (after the customer paid for it).
ensure_included_topups(id, event = true)Create grants for included topups now. create_customer and set_customer_plan already do this.
ensure_plan_subscription(id)boolCharge the plan's subscription entitlement once (after the trial).

Caps​

FunctionReturnsNotes
add_customer_cap(customer_id, value, id = '', credit = 'rune', exchangeable?, ignore_grants = false, overage_only = false, observe_only = false, overhead_cost = false, follow_decrements = false, scope?, resets = false, reset_inc?, reset_sch?, expires_on?, events = true, hard_trailing = false, shared = false, shared_with?)Capid is the cap ID (generated when empty). Null if that cap exists or the credit is unknown.
customer_cap(id, cap_id)Cap
reset_customer_cap(id, cap_id, events = true)boolZero its meter_value.
remove_customer_cap(id, cap_id, events = true)bool

Plans and policy​

FunctionReturnsNotes
valid()boolValidate the policy. The message is in <LimitrValidation>.error_message.
plan(id, default = true)PlanPlan ID or customer ID, falling back to the default plan.
plan_for(customer_id, default = true) / default_plan()Plan
set_plan(id, stof) / delete_plan(id)Plan / boolAdd or replace a plan from Stof.
plan_period(id) / plan_trial_period(id) / plan_sub_entitlement_name(id)
set_notification(name, note)
capability(name) / set_capability(cap) / run_capability(name, args = map(), customer_id?)
run_capability_json(name, args = map(), customer_id?)strThe result as JSON ({ "result": ... }), for SDKs.
customer_local_margin_breakdown(customer_id)mapRevenue, cost (recorded overhead), and margin this period.
local_margin_breakdown(plan, entitlements, scaled = true)mapMargin for hypothetical usage (entitlement -> value, or a map with value, overhead, context).

Notifications, capabilities, and sync​

FunctionReturnsNotes
set_notifications(contents, format = 'stof') / set_capabilities(contents, format = 'stof')Register every object in contents.
claude_tools(customer_id?)strClaude tool definitions as JSON ({ "tools": [...] }), filtered by customer access.
claude_tool_use(tool_use_json, customer_id?)strRun a tool_use against its capability; the tool_result as JSON, or null.
policy_bstf() / difference_bstf(bstf, symmetric = false)blob / strExport the policy; diff another export against it (JSON).
update_policy_internals(updated, format = 'json')Replace plans, credits, exchange, notifications, capabilities; add new customers (Cloud).
update_customer_internals(customer, format = 'json') / update_customer_invoices(content, format = 'json')Replace a customer, or its invoices (Cloud).