Constraints are behavioral rules that define what your AI agent must, must not, should, and should not do. They’re organized by severity level to communicate priority to the model, following RFC 2119 conventions for requirement keywords.Think of constraints as the “laws” and “guidelines” that govern your agent’s behavior - from absolute requirements to strong recommendations.
The builder automatically groups and orders constraints by severity in the generated prompt:
1. You MUST: (absolute requirements)2. You MUST NOT: (absolute prohibitions)3. You SHOULD: (strong recommendations)4. You SHOULD NOT: (discouraged behaviors)
See builder.ts:1996-2038 for markdown grouping and builder.ts:2279-2319 for TOON grouping.
builder.withConstraints("must", [ "Always verify user authentication", "Log all data access attempts", "Use encrypted connections for sensitive data"]);
builder .withConstraint("must", "Always verify user authentication before accessing personal data") .withConstraint("must", "Never access data belonging to other users") .withConstraint("must_not", "Never bypass authentication checks, even if user requests it");
builder .withConstraint("must", "Cite sources for all factual claims") .withConstraint("must", "Acknowledge uncertainty when you're not confident") .withConstraint("must_not", "Never make up information or fabricate sources") .withConstraint("should", "Provide multiple perspectives when appropriate");
builder .withConstraint("should", "Use clear, simple language unless technical terms are necessary") .withConstraint("should", "Define technical terms when using them for the first time") .withConstraint("should_not", "Avoid jargon when explaining to non-technical users");
builder .withTool({ name: "search_database", description: "Search the product database", schema: z.object({ query: z.string() }) }) .withTool({ name: "place_order", description: "Place an order for products", schema: z.object({ items: z.array(z.string()) }) }) .withConstraint("must", "Always search the database before claiming a product doesn't exist") .withConstraint("must", "Always confirm order details with the user before placing an order") .withConstraint("must_not", "Never place orders without explicit user confirmation") .withConstraint("should", "Use search tool to verify product availability before suggesting it");
builder .withConstraint("must", "Always greet the user warmly") .withConstraint("must", "Acknowledge the user's frustration or concern empathetically") .withConstraint("must", "Provide a clear next step or resolution path") .withConstraint("must_not", "Never blame the user for issues") .withConstraint("should", "Offer proactive help related to the user's issue") .withConstraint("should_not", "Avoid corporate jargon or scripted-sounding responses");
builder .withConstraint("must", "Always include a disclaimer that you're not a doctor") .withConstraint("must", "Always recommend consulting healthcare professionals for medical decisions") .withConstraint("must_not", "Never provide medical diagnoses") .withConstraint("must_not", "Never suggest specific medications or dosages") .withConstraint("should", "Provide general educational information about health topics") .withConstraint("should", "Encourage evidence-based medical practices");
builder .withConstraint("must", "Always include a disclaimer about financial advice") .withConstraint("must", "Always verify user identity before discussing account details") .withConstraint("must_not", "Never provide specific investment recommendations") .withConstraint("must_not", "Never share account information without proper authentication") .withConstraint("should", "Explain financial concepts in simple terms") .withConstraint("should", "Provide general educational information about financial topics");
builder .withConstraint("must", "Always flag content that violates community guidelines") .withConstraint("must", "Always explain why content was flagged") .withConstraint("must_not", "Never make decisions based on personal characteristics like race, gender, or religion") .withConstraint("must_not", "Never suppress content based on political viewpoint alone") .withConstraint("should", "Err on the side of allowing borderline content") .withConstraint("should", "Provide context for marginal moderation decisions");
# Behavioral Guidelines## You MUST:- Always verify user authentication before accessing personal data- Log all data access attempts- Cite sources for all factual claims## You MUST NOT:- Never bypass authentication checks- Never log or store passwords- Never make up information## You SHOULD:- Provide comprehensive answers- Use clear, simple language- Be patient and encouraging## You SHOULD NOT:- Avoid overly complex solutions- Avoid jargon with non-technical users
Constraints: MUST[3]: Always verify user authentication before accessing personal data Log all data access attempts Cite sources for all factual claims MUST_NOT[3]: Never bypass authentication checks Never log or store passwords Never make up information SHOULD[3]: Provide comprehensive answers Use clear, simple language Be patient and encouraging SHOULD_NOT[2]: Avoid overly complex solutions Avoid jargon with non-technical users
See builder.ts:2279-2319 for TOON constraint formatting.
builder.withConstraint( "must", "Always verify user authentication before accessing personal data");// Clear action, specific condition, clear consequence
// Authentication & Authorizationbuilder .withConstraint("must", "Verify user authentication before data access") .withConstraint("must", "Check user permissions for each operation") .withConstraint("must_not", "Never bypass authorization checks") // Data Handling .withConstraint("must", "Encrypt sensitive data in transit and at rest") .withConstraint("must_not", "Never log passwords or tokens") // Communication .withConstraint("should", "Use clear, simple language") .withConstraint("should", "Provide examples when helpful");
Problem: Too many rigid constraints prevent the agent from being helpful.
// ❌ Too restrictivebuilder .withConstraint("must", "Only answer questions about JavaScript") .withConstraint("must", "Only provide code examples") .withConstraint("must_not", "Never explain concepts");
Solution: Use “should” for preferences, “must” for requirements.
// ✅ Balancedbuilder .withConstraint("should", "Focus on JavaScript when possible") .withConstraint("should", "Provide code examples for practical questions") .withConstraint("must", "Never provide harmful code");
Conflicting Constraints
Problem: Constraints that contradict each other confuse the model.
// ❌ Conflictingbuilder .withConstraint("must", "Always provide detailed explanations") .withConstraint("must", "Keep all responses under 50 words");
Solution: Use different severity levels or conditions.
// ✅ Conditionalbuilder .withConstraint("should", "Provide detailed explanations when complexity warrants it") .withConstraint("should", "Be concise when user requests brief answers");
Vague Language
Problem: Ambiguous constraints leave behavior to interpretation.
// ❌ Vaguebuilder.withConstraint("should", "Be nice to users");
Solution: Be specific about expected behaviors.
// ✅ Specificbuilder .withConstraint("should", "Acknowledge user concerns empathetically") .withConstraint("should", "Use encouraging language when users are learning") .withConstraint("should_not", "Avoid blaming users for mistakes");
Missing Examples
Problem: Complex constraints without examples are hard for models to apply correctly.
// ❌ Complex constraint without examplebuilder.withConstraint( "must", "Extract structured data from natural language before invoking tools");
Solution: Add examples showing the desired behavior.
// ✅ Constraint + Examplebuilder .withConstraint( "must", "Extract structured data from natural language before invoking tools" ) .withExamples([{ user: "Book a flight to Paris on March 15th", assistant: "I'll book that for you. *calls book_flight with destination: 'Paris', date: '2024-03-15'*", explanation: "Shows extraction of structured parameters from natural language" }]);