PromptSmith provides a fluent API for building type-safe system prompts. This guide walks you through creating prompts from basic to advanced configurations.
Every prompt starts with the createPromptBuilder() factory function and follows a fluent, chainable API pattern.
1
Create a Builder Instance
2
Start by importing and creating a builder:
3
import { createPromptBuilder } from 'promptsmith-ts/builder';const builder = createPromptBuilder();
4
Define the Agent’s Identity
5
The identity establishes who or what your agent is. This appears first in the generated prompt and frames all subsequent instructions.
6
const builder = createPromptBuilder() .withIdentity('You are an expert travel assistant specializing in European destinations');
7
Write identities in second person (“You are…”) for clarity. Be specific about the agent’s expertise and domain.
8
Add Capabilities
9
Capabilities define what your agent can do. Add them one at a time or in bulk:
10
const builder = createPromptBuilder() .withIdentity('You are a financial analysis assistant') .withCapabilities([ 'Analyze financial statements and identify trends', 'Calculate investment returns and risk metrics', 'Generate detailed financial reports', 'Compare performance across different time periods' ]);
11
Build the Prompt
12
Generate the final prompt string with .build():
13
const prompt = builder.build();console.log(prompt);// # Identity// You are a financial analysis assistant//// # Capabilities// 1. Analyze financial statements and identify trends// 2. Calculate investment returns and risk metrics// 3. Generate detailed financial reports// 4. Compare performance across different time periods
Provide domain-specific background knowledge that helps your agent understand its operating environment:
const builder = createPromptBuilder() .withIdentity('You are a medical appointment scheduler') .withContext(`Our clinic operates Monday-Friday, 9 AM to 5 PM.We have three doctors:- Dr. Smith (general medicine, 30-min appointments)- Dr. Jones (cardiology, 45-min appointments)- Dr. Lee (pediatrics, 30-min appointments)Emergency appointments can be accommodated with 24-hour notice.Standard appointments require 48-hour notice for cancellation. `);
Context differs from identity (who the agent is) and capabilities (what it can do). Context is what the agent needs to know about its domain.
const builder = createPromptBuilder() .withIdentity('You are a customer support assistant') .withTone('Be empathetic and patient. Use a warm, conversational tone. Acknowledge user frustration and always end responses positively.');
const builder = createPromptBuilder() .withIdentity('You are a technical troubleshooting assistant') .withOutput(`Structure all responses in this format:1. **Problem Summary**: Brief restatement of the issue (1-2 sentences)2. **Diagnosis**: What's likely causing the problem3. **Solution Steps**: Numbered, actionable steps to resolve4. **Prevention**: How to avoid this in the futureUse code blocks for commands or configuration snippets. `);
import { createPromptBuilder } from 'promptsmith-ts/builder';const ecommerceAssistant = createPromptBuilder() .withIdentity( 'You are a knowledgeable e-commerce assistant for TechGear, ' 'an online retailer specializing in consumer electronics' ) .withCapabilities([ 'Help customers find products matching their needs', 'Answer questions about product specifications', 'Explain shipping options and delivery times', 'Assist with order tracking and status inquiries', 'Process return and exchange requests', ]) .withContext(`Store Information:- Free shipping on orders over $50- Standard delivery: 3-5 business days- Express delivery: 1-2 business days (additional fee)- 30-day return policy for all items- Extended warranty available for purchase- Customer service available 24/7Product Categories:- Laptops & Computers- Smartphones & Tablets- Audio Equipment- Smart Home Devices- Gaming Accessories `) .withTone( 'Be friendly, helpful, and enthusiastic about our products. ' 'Use clear, jargon-free language. Always prioritize customer satisfaction.' ) .withOutput( 'Keep responses concise but complete. Use bullet points for lists. ' 'Include relevant product links when recommending items.' ) .build();// Use with your AI providerimport { generateText } from 'ai';import { openai } from '@ai-sdk/openai';const response = await generateText({ model: openai('gpt-4'), system: ecommerceAssistant, prompt: 'I need a laptop for video editing under $1500'});
All builder methods return this, enabling fluent chaining:
const builder = createPromptBuilder() .withIdentity('You are a code review assistant') .withCapability('Identify potential bugs and security issues') .withCapability('Suggest performance optimizations') .withCapability('Recommend better design patterns') .withTone('Be constructive and educational, not critical') .withOutput('Provide specific file paths and line numbers with suggestions');