Skip to main content

withConstraint

Adds a behavioral constraint or guideline for the agent. Constraints define rules that govern the agent’s behavior with different levels of severity. They are organized by type in the generated prompt to communicate priority to the model:
  • must: Absolute requirements that cannot be violated
  • must_not: Absolute prohibitions
  • should: Strong recommendations to follow when possible
  • should_not: Strong recommendations to avoid
Use constraints to enforce security, privacy, quality standards, tone, and interaction patterns.
withConstraint(type: ConstraintType, rule: string): this
type
ConstraintType
required
The constraint severity level: "must", "must_not", "should", or "should_not"
rule
string
required
The constraint rule text. Should be clear, specific, and actionable. Use imperative mood (“Do X”, “Never Y”).
Returns
this
The builder instance for method chaining

Example

builder
  .withConstraint("must", "Always verify user authentication before accessing personal data")
  .withConstraint("must_not", "Never store or log sensitive information")
  .withConstraint("should", "Provide concise responses unless detail is requested")
  .withConstraint("should_not", "Avoid using technical jargon with non-technical users");

withConstraints

Adds one or more behavioral constraints with the same type. This overloaded method accepts either a single constraint rule or an array of rules, making it more convenient to add multiple constraints of the same type.
withConstraints(type: ConstraintType, rules: string | string[]): this
type
ConstraintType
required
The constraint severity level
rules
string | string[]
required
Single rule string or array of rule strings
Returns
this
The builder instance for method chaining

Examples

builder.withConstraints("must", "Always verify user authentication");

withConstraintIf

Conditionally adds a behavioral constraint based on a condition. This method only adds the constraint if the condition evaluates to true, making it easier to build prompts with conditional logic without breaking the fluent chain.
withConstraintIf(condition: boolean, type: ConstraintType, rule: string): this
condition
boolean
required
Boolean condition that determines if the constraint is added
type
ConstraintType
required
The constraint severity level
rule
string
required
The constraint rule text
Returns
this
The builder instance for method chaining

Example

const isProd = process.env.NODE_ENV === 'production';
const hasAuth = config.authEnabled;

builder
  .withConstraintIf(isProd, "must", "Log all security events")
  .withConstraintIf(hasAuth, "must", "Verify user identity before data access")
  .withConstraintIf(!isProd, "should", "Provide verbose debug information");

Build docs developers (and LLMs) love