KaireonAI includes a safe, sandboxed formula engine for computing personalized values at decision time. The engine follows a strict pipeline:
Tokenizer — Breaks the formula string into tokens (numbers, strings, identifiers, operators, punctuation)
Parser — Recursive-descent parser builds an Abstract Syntax Tree (AST) from the token stream
Evaluator — Walks the AST and computes the result structurally
The engine uses no eval, Function, or vm — all evaluation is structural, making it safe by design. Any formula that fails to parse or evaluate returns null rather than throwing an error.Formulas are defined on computed custom fields within Categories. At decision time, the Recommend API evaluates each formula per candidate offer and merges the results into the response.
Formulas can reference three namespaces of variables:
Namespace
Source
Example
When Available
fieldName
Other custom field values on the offer
base_rate, offer_price
Always — values come from the offer’s custom fields
customer.*
Enriched data from schema tables via the Enrich stage
customer.loan_amount, customer.balance
When an Enrich stage is configured in the Decision Flow
attributes.*
Request-time attributes from the Recommend API body
attributes.tier, attributes.channel
When the caller passes attributes in the Recommend request
Variables are resolved as a flat key-value map. Dot notation (e.g., customer.loan_amount) is part of the identifier — the engine treats customer.loan_amount as a single variable name, not an object property access.
Computed fields declare an outputType of either number or text. This is a metadata hint used for validation and display — the formula engine itself returns whatever type the expression produces:
Arithmetic operations return numbers
String operations (+ on two strings, concat) return strings
Comparison operators always return 1 or 0 (numbers)
coalesce returns the type of the first non-null argument
When defining a computed field, choose the outputType that matches your formula’s expected result. The Categories API rejects any computed field that omits either a formula string or an outputType of number or text.
The formula engine is designed to fail gracefully. Every error condition returns null rather than throwing:
Condition
Behavior
Division by zero (10 / 0)
Returns null
Modulo by zero (10 % 0)
Returns null
Missing or undefined variable
Returns null (use coalesce to provide a default)
null variable value
Returns null (null propagates through all operations)
Syntax error or unparseable formula
Returns null
Mismatched parentheses
Returns null
Empty formula
Returns null
Unknown function name
Returns null
Wrong argument count for a function
Returns null
Type mismatch (e.g., arithmetic on strings)
Returns null
Unterminated string literal
Returns null
Null propagation rule: If any operand in a binary operation is null, the entire operation returns null. This means 5 + null is null, not 5. Use coalesce to guard against missing values.
The Business Hierarchy page (Category editor) includes a Validate button next to each computed field. Clicking it parses the formula in place and returns either a green check or a red error message describing exactly what failed (unbalanced parentheses, unknown function, missing operand, and so on), so authors can fix mistakes before saving.