SDK Integration
Integrate optimized prompts directly into your application code using the Handit.ai SDK. Fetch the latest optimized prompts automatically and ensure your application always uses the best-performing prompt versions.
The SDK provides seamless integration with your existing codebase. Simply replace static prompts with SDK calls to automatically receive optimized versions as they become available.
Overview
The optimization SDK enables:
Automatic Prompt Fetching
Retrieve the latest optimized prompts for any LLM node using simple function calls
Fallback Support
Gracefully handle cases where optimizations aren’t available with default prompts
Caching & Performance
Built-in intelligent caching to minimize API calls and improve response times
Version Control
Access specific prompt versions or always get the latest optimized version
Installation & Setup
Core Function
Basic Implementation
Production-Ready Implementation
Framework-Specific Integrations
React Integration
React Hook for Optimized Prompts:
import { useState, useEffect } from 'react';
import { HanditClient } from '@handit/sdk';
const handit = new HanditClient({
apiKey: process.env.REACT_APP_HANDIT_API_KEY,
projectId: process.env.REACT_APP_HANDIT_PROJECT_ID
});
export function useOptimizedPrompt(modelId, fallbackPrompt = '') {
const [prompt, setPrompt] = useState(fallbackPrompt);
const [loading, setLoading] = useState(true);
const [error, setError] = useState(null);
const [isOptimized, setIsOptimized] = useState(false);
useEffect(() => {
let isMounted = true;
async function fetchPrompt() {
try {
setLoading(true);
setError(null);
const optimizedPrompt = await handit.fetchOptimizedPrompt({ modelId });
if (isMounted) {
setPrompt(optimizedPrompt);
setIsOptimized(true);
}
} catch (err) {
if (isMounted) {
setPrompt(fallbackPrompt);
setIsOptimized(false);
setError(err.message);
}
} finally {
if (isMounted) {
setLoading(false);
}
}
}
fetchPrompt();
return () => {
isMounted = false;
};
}, [modelId, fallbackPrompt]);
return { prompt, loading, error, isOptimized };
}
// Usage in component
function CustomerSupportChat() {
const fallbackPrompt = 'You are a helpful customer service assistant...';
const { prompt, loading, isOptimized } = useOptimizedPrompt(
'customer-support-llm',
fallbackPrompt
);
if (loading) {
return <div>Loading optimized prompt...</div>;
}
return (
<div className="chat-interface">
<div className="prompt-status">
{isOptimized ? (
<span className="optimized">✅ Using optimized prompt</span>
) : (
<span className="fallback">⚠️ Using fallback prompt</span>
)}
</div>
{/* Chat interface implementation */}
</div>
);
}
Next.js API Route
API Route with Optimization:
// pages/api/chat.js
import { HanditClient } from '@handit/sdk';
import OpenAI from 'openai';
const handit = new HanditClient({
apiKey: process.env.HANDIT_API_KEY,
projectId: process.env.HANDIT_PROJECT_ID
});
const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
export default async function handler(req, res) {
if (req.method !== 'POST') {
return res.status(405).json({ error: 'Method not allowed' });
}
const { message, modelId = 'customer-support-llm' } = req.body;
try {
// Fetch optimized prompt
let systemPrompt;
try {
systemPrompt = await handit.fetchOptimizedPrompt({ modelId });
} catch (error) {
systemPrompt = 'You are a helpful customer service assistant.';
console.warn('Using fallback prompt:', error.message);
}
// Generate response
const completion = await openai.chat.completions.create({
model: 'gpt-4',
messages: [
{ role: 'system', content: systemPrompt },
{ role: 'user', content: message }
]
});
res.status(200).json({
response: completion.choices[0].message.content
});
} catch (error) {
console.error('Error:', error);
res.status(500).json({ error: 'Internal server error' });
}
}
Next Steps
Ready to integrate optimized prompts into your application?
- Install the SDK for your preferred language
- Replace static prompts with
fetch_optimized_prompt()
calls - Implement fallback handling for production reliability
- Set up caching for optimal performance
- Monitor optimization usage and performance
- Start using Handit.ai to begin optimizing your prompts
SDK integration complete! Your application can now automatically fetch and use optimized prompts. The SDK handles caching, error recovery, and real-time updates seamlessly. Visit beta.handit.ai to get started with prompt optimization today.