System prompts and prompt templates help you customize AI behavior and maintain consistent responses.
Overview
The prompt system (src/lib/ai/prompts.ts) provides:
- System prompt templates - Pre-built prompts for common use cases
- Prompt builders - Utilities to construct custom prompts
- Message history management - Build conversation context
- Prompt validation - Ensure prompts meet safety requirements
Default System Prompts
The boilerplate includes several pre-built system prompts:
General Assistant
import { DEFAULT_SYSTEM_PROMPT } from "@/lib/ai/prompts";
// Default: Helpful, friendly, professional assistant
Code Assistant
import { CODE_ASSISTANT_PROMPT } from "@/lib/ai/prompts";
// Optimized for coding tasks, debugging, explanations
Creative Writer
import { CREATIVE_WRITER_PROMPT } from "@/lib/ai/prompts";
// Optimized for creative writing, content improvement
Tiered System Prompts
The boilerplate includes tiered prompts based on subscription status:
Basic Tier Prompt
Used for free tier users (if free tier is enabled):
import { BASIC_TIER_PROMPT, getTierPrompt } from "@/lib/ai/prompts";
// Friendly, approachable, fundamental concepts
const prompt = getTierPrompt("free");
Characteristics:
- Friendly and approachable tone
- Fundamental concepts explained simply
- Practical, actionable advice
- Step-by-step guidance
Advanced Tier Prompt
Used for premium subscribers:
import { ADVANCED_TIER_PROMPT, getTierPrompt } from "@/lib/ai/prompts";
// Professional, technical, comprehensive
const prompt = getTierPrompt("premium");
Characteristics:
- Professional and technical tone
- In-depth technical analysis
- Advanced concepts and methodologies
- Comprehensive solutions
Using Tiered Prompts
import { buildMessageHistoryWithTier } from "@/lib/ai/prompts";
import { getSubscriptionTier } from "@/features/ai/server";
// Get user's tier
const tier = await getSubscriptionTier(userId);
// Build message history with tier-appropriate prompt
const messages = buildMessageHistoryWithTier(
[{ role: "user", content: "Hello" }],
{ tier }
);
Building Custom Prompts
Using buildSystemPrompt()
import { buildSystemPrompt } from "@/lib/ai/prompts";
const customPrompt = buildSystemPrompt({
systemPrompt: "You are a helpful assistant.",
context: "User is working on a React project.",
instructions: [
"Always provide code examples",
"Explain your reasoning",
"Suggest best practices"
],
examples: [
{
user: "How do I use hooks?",
assistant: "React hooks let you use state..."
}
]
});
Parameters
systemPrompt- Base system promptcontext- Additional context about the user or taskinstructions- Array of specific instructionsexamples- Array of example conversations
Message History
Building Message History (Legacy)
import { buildMessageHistory } from "@/lib/ai/prompts";
const messages = buildMessageHistory(
[
{ role: "user", content: "Hello" },
{ role: "assistant", content: "Hi there!" }
],
"You are a helpful assistant."
);
// Result includes system prompt at the beginning
Building Message History with Tier (Recommended)
import { buildMessageHistoryWithTier } from "@/lib/ai/prompts";
import { getSubscriptionTier } from "@/features/ai/server";
// Get user's subscription tier
const tier = await getSubscriptionTier(userId);
// Build with tier-appropriate prompt
const messages = buildMessageHistoryWithTier(
[
{ role: "user", content: "Hello" },
{ role: "assistant", content: "Hi there!" }
],
{ tier } // Automatically uses BASIC_TIER_PROMPT or ADVANCED_TIER_PROMPT
);
// Or use custom system prompt
const messages = buildMessageHistoryWithTier(
[{ role: "user", content: "Hello" }],
{ systemPrompt: "You are a custom assistant." }
);
Message Format
interface ChatMessage {
role: "user" | "assistant" | "system";
content: string;
}
Prompt Validation
Validate Prompts
import { validatePrompt } from "@/lib/ai/prompts";
const validation = validatePrompt([
{ role: "system", content: "..." },
{ role: "user", content: "..." }
]);
if (!validation.valid) {
console.error(validation.error);
}
Validation checks:
- Maximum message length
- Maximum conversation length
- Prohibited content patterns
- Injection attempt detection
Examples
Custom AI Assistant
const assistantPrompt = buildSystemPrompt({
systemPrompt: "You are a professional business consultant.",
context: "The user is a startup founder.",
instructions: [
"Provide actionable advice",
"Be concise and direct",
"Use business terminology"
]
});
const response = await generateChatCompletion({
messages: buildMessageHistory(
[{ role: "user", content: "How do I raise funding?" }],
assistantPrompt
)
});
Domain-Specific Prompts
// Medical assistant (with disclaimers)
const medicalPrompt = buildSystemPrompt({
systemPrompt: "You are a medical information assistant.",
instructions: [
"Always include disclaimers",
"Do not provide diagnoses",
"Recommend consulting healthcare professionals"
]
});
// Legal assistant
const legalPrompt = buildSystemPrompt({
systemPrompt: "You are a legal information assistant.",
instructions: [
"Provide general information only",
"Always recommend consulting an attorney",
"Do not provide specific legal advice"
]
});
Best Practices
- Be specific - Clear instructions produce better results
- Include examples - Show the AI what you want
- Set boundaries - Define what the AI should and shouldn't do
- Test prompts - Iterate to find what works best
- Validate inputs - Always validate user prompts before sending
Security Considerations
- Never trust user input - Always validate and sanitize
- Use moderation - Check prompts for harmful content
- Limit length - Prevent prompt injection attacks
- Monitor usage - Track prompt patterns for abuse
Next Steps
- Learn about Content Moderation
- Explore AI Client for API usage
- Check out AI Tutorials for examples