Create domain-specific AI assistants with custom system prompts and instructions.
Overview
This tutorial shows you how to:
- Build custom system prompts
- Create domain-specific assistants
- Use prompt templates
- Validate prompts
Step 1: Create a Custom Prompt
Use the buildSystemPrompt utility:
import { buildSystemPrompt } from "@/lib/ai/prompts";
const businessPrompt = buildSystemPrompt({
systemPrompt: "You are a professional business consultant.",
context: "The user is a startup founder seeking advice.",
instructions: [
"Provide actionable, specific advice",
"Be concise and direct",
"Use business terminology appropriately",
"Always consider scalability and growth"
],
examples: [
{
user: "How do I raise seed funding?",
assistant: "To raise seed funding, you'll need: 1) A clear pitch deck..."
}
]
});
Step 2: Use Custom Prompt in Chat
Pass the custom prompt to your chat endpoint:
const response = await fetch("/api/ai/chat", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
message: userInput,
systemPrompt: businessPrompt,
messages: messageHistory,
}),
});
Step 3: Create Domain-Specific Assistants
Medical Information Assistant
const medicalPrompt = buildSystemPrompt({
systemPrompt: "You are a medical information assistant.",
instructions: [
"Provide general health information only",
"Always include disclaimers",
"Never provide diagnoses",
"Always recommend consulting healthcare professionals",
"Be empathetic and clear"
],
examples: [
{
user: "What are symptoms of flu?",
assistant: "Common flu symptoms include fever, cough, and fatigue. However, I'm not a doctor. Please consult a healthcare professional for medical advice."
}
]
});
Legal Information Assistant
const legalPrompt = buildSystemPrompt({
systemPrompt: "You are a legal information assistant.",
instructions: [
"Provide general legal information only",
"Never provide specific legal advice",
"Always recommend consulting an attorney",
"Clarify jurisdiction-specific differences",
"Be precise with legal terminology"
]
});
Code Review Assistant
const codeReviewPrompt = buildSystemPrompt({
systemPrompt: "You are an expert code reviewer.",
instructions: [
"Review code for bugs, security issues, and best practices",
"Suggest improvements with explanations",
"Provide code examples when helpful",
"Consider performance and maintainability",
"Be constructive and educational"
]
});
Step 4: Build Message History
Use buildMessageHistoryWithTier (recommended) or buildMessageHistory to include system prompts:
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 prompts
const messages = buildMessageHistoryWithTier(
[
{ role: "user", content: "Hello" },
{ role: "assistant", content: "Hi! How can I help?" },
{ role: "user", content: "I need business advice" }
],
{ systemPrompt: businessPrompt, tier }
);
// Or use custom prompt only
import { buildMessageHistory } from "@/lib/ai/prompts";
const messages = buildMessageHistory(
[
{ role: "user", content: "Hello" },
{ role: "assistant", content: "Hi! How can I help?" },
{ role: "user", content: "I need business advice" }
],
businessPrompt
);
Step 5: Validate Prompts
Always validate prompts before sending:
import { validatePrompt } from "@/lib/ai/prompts";
const validation = validatePrompt(messages);
if (!validation.valid) {
console.error("Invalid prompt:", validation.error);
return;
}
// Proceed with AI request
Step 6: Create Reusable Prompt Templates
Create a prompts file for your app:
// src/lib/prompts.ts
import { buildSystemPrompt } from "@/lib/ai/prompts";
export const PROMPTS = {
business: buildSystemPrompt({
systemPrompt: "You are a business consultant...",
// ...
}),
medical: buildSystemPrompt({
systemPrompt: "You are a medical information assistant...",
// ...
}),
legal: buildSystemPrompt({
systemPrompt: "You are a legal information assistant...",
// ...
}),
};
Use in your components:
import { PROMPTS } from "@/lib/prompts";
const response = await fetch("/api/ai/chat", {
body: JSON.stringify({
message: input,
systemPrompt: PROMPTS.business,
}),
});
Step 7: Dynamic Context
Add dynamic context based on user data:
const userContext = `User is a ${user.role} working in ${user.industry}.`;
const personalizedPrompt = buildSystemPrompt({
systemPrompt: "You are a helpful assistant.",
context: userContext,
instructions: [
"Tailor your advice to the user's role and industry",
// ...
]
});
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 and iterate - Refine prompts based on results
- Validate inputs - Always validate before sending
- Keep prompts focused - One prompt per use case
Common Patterns
Multi-Step Instructions
const analysisPrompt = buildSystemPrompt({
systemPrompt: "You are an analysis assistant.",
instructions: [
"Step 1: Analyze the input",
"Step 2: Identify key points",
"Step 3: Provide recommendations",
"Step 4: Summarize findings"
]
});
Conditional Behavior
const conditionalPrompt = buildSystemPrompt({
systemPrompt: "You are a helpful assistant.",
instructions: [
user.isPremium
? "Provide detailed, comprehensive responses"
: "Provide concise, helpful responses",
// ...
]
});
Security Considerations
- Never trust user input - Always validate
- Sanitize context - Remove sensitive information
- Limit prompt length - Prevent abuse
- Monitor usage - Track prompt patterns
Next Steps
- Learn about Content Moderation
- Explore Subscription Access Control
- Check out AI Components