PromptSmith enables powerful composition patterns through .extend() and .merge(), allowing you to create reusable prompt components and specialized variants without duplication.
The .extend() method creates a copy of a builder that you can then specialize:
1
Create a Base Prompt
2
import { createPromptBuilder } from 'promptsmith-ts/builder';const baseSupport = createPromptBuilder() .withIdentity('You are a customer support assistant') .withCapabilities([ 'Answer product questions', 'Help with account issues', 'Provide general assistance' ]) .withGuardrails() .withTone('Professional and friendly');
3
Extend for Specialization
4
// Create specialized variant for technical supportconst technicalSupport = baseSupport.extend() .withIdentity('You are a technical support specialist') // Overrides base identity .withCapabilities([ 'Debug technical issues', 'Explain complex technical concepts', 'Guide through troubleshooting steps' ]) // Adds to existing capabilities .withContext(`Technical Knowledge Base:- Product: CloudSync Pro- Tech Stack: React, Node.js, PostgreSQL- Common issues: sync failures, authentication errors, database timeouts `);// Original remains unchangedconst basePrompt = baseSupport.build();const techPrompt = technicalSupport.build();
5
Create Multiple Variants
6
// Billing support variantconst billingSupport = baseSupport.extend() .withIdentity('You are a billing support specialist') .withCapabilities([ 'Explain billing statements', 'Process refund requests', 'Update payment information' ]) .withContext('Payment processor: Stripe. Billing cycle: monthly on the 1st.');// Returns specialist variantconst returnsSupport = baseSupport.extend() .withIdentity('You are a returns and exchanges specialist') .withCapabilities([ 'Process return requests', 'Arrange exchanges', 'Issue refunds' ]) .withContext('Return window: 30 days. Free return shipping on all orders.');
// templates/security.tsexport function securityTemplate() { return createPromptBuilder() .withGuardrails() .withConstraint('must', 'Always verify user identity before sharing sensitive data') .withConstraint('must_not', 'Never log or store personally identifiable information') .withForbiddenTopics(['Internal system details', 'Other users\' data']);}// templates/professional-tone.tsexport function professionalTone() { return createPromptBuilder() .withTone('Maintain a professional, courteous tone. Be clear and concise.') .withConstraint('should', 'Use formal language and avoid slang') .withConstraint('should', 'Address users respectfully');}// templates/error-handling.tsexport function robustErrorHandling() { return createPromptBuilder() .withErrorHandling(`Error Handling:- When uncertain, ask clarifying questions- For errors, explain issue in user-friendly terms- Always suggest next steps or alternatives- Acknowledge user frustration empathetically `.trim()) .withConstraint('must', 'Never leave users without a path forward');}// templates/multilingual.tsexport function multilingualSupport() { return createPromptBuilder() .withCapability('Communicate in multiple languages') .withCapability('Detect user language and respond accordingly') .withConstraint('should', 'Match user\'s language preference automatically');}
import { createPromptBuilder } from 'promptsmith-ts/builder';import { securityTemplate, professionalTone, robustErrorHandling, multilingualSupport} from './templates';const customerService = createPromptBuilder() .withIdentity('You are a global customer service assistant') .withCapabilities([ 'Answer product questions', 'Process orders and returns', 'Provide technical support' ]) .merge(securityTemplate()) // Add security .merge(professionalTone()) // Add professional communication .merge(robustErrorHandling()) // Add error handling .merge(multilingualSupport()); // Add language support// Final prompt has all capabilities from all templatesconst prompt = customerService.build();
const variantA = baseAgent.extend() .withTone('Casual and friendly');const variantB = baseAgent.extend() .withTone('Professional and formal');// Test which performs better