import OpenAI from 'openai'
const openai = new OpenAI()
async function generateSummary(text: string) {
const response = await openai.chat.completions.create({
model: 'gpt-4',
messages: [
{
role: 'system',
content: 'Summarize the following text in 3 bullet points.'
},
{ role: 'user', content: text }
],
max_tokens: 200
})
return response.choices[0].message.content
}// Agent loop with tool use
async function runAgent(userRequest: string) {
const tools = [searchDatabase, sendEmail, createTask]
while (true) {
const response = await llm.generate({
prompt: userRequest,
tools: tools.map(t => t.schema)
})
if (response.type === 'tool_call') {
const result = await executeTool(response.tool, response.args)
userRequest += \nTool result: ${result}
} else {
return response.content
}
}
}const SYSTEM_PROMPT = You are a helpful assistant that analyzes customer feedback.
RULES:
1. Identify sentiment (positive/negative/neutral)
2. Extract key topics mentioned
3. Suggest actionable improvements
OUTPUT FORMAT: JSON with fields:
- sentiment: string
- topics: string[]
- suggestions: string[]
Keep suggestions actionable and specific.async function safeAICall(
fn: () => Promise,
fallback: T
): Promise {
try {
const result = await fn()
if (!isValidOutput(result)) {
logger.warn('Invalid AI output, using fallback')
return fallback
}
return result
} catch (error) {
logger.error('AI call failed', error)
return fallback
}
} async function* streamResponse(prompt: string) {
const stream = await openai.chat.completions.create({
model: 'gpt-4',
messages: [{ role: 'user', content: prompt }],
stream: true
})
for await (const chunk of stream) {
yield chunk.choices[0]?.delta?.content || ''
}
}async function cachedAICall(prompt: string) {
const cacheKey = createHash('sha256').update(prompt).digest('hex')
const cached = await redis.get(cacheKey)
if (cached) return JSON.parse(cached)
const result = await aiService.generate(prompt)
await redis.setex(cacheKey, 3600, JSON.stringify(result)) // 1 hour TTL
return result
}- Unit tests for prompt templates
- Evaluation datasets with expected outputs
- A/B tests for prompt variations
- Edge case testing (empty input, long input, adversarial)
- Output validation before use
| Guardrail | Purpose | Implementation |
|---|---|---|
| Input Validation | Prevent injection attacks | Sanitize before sending to LLM |
| Output Filtering | Block harmful content | Content moderation API |
| Rate Limiting | Control costs and abuse | Per-user quotas |
| Human Review | High-stakes decisions | Approval workflow |
Ready to Add AI Features to Your Product?
We help teams integrate AI features that delight users—from concept to production, with responsible implementation practices.
Discuss Your AI Integration →
