Best Practices
Follow these recommendations to get the most out of CID222 while maintaining security, performance, and reliability.
Security
API Key Management
- Never expose keys in client code — Always use server-side proxies
- Use environment variables — Never hardcode keys in source code
- Rotate keys regularly — Generate new keys periodically
- Separate environments — Use different keys for dev/staging/production
Secure Key Usage
// Good: Server-side API route// pages/api/chat.tsexport default async function handler(req, res) {const response = await fetch('https://api.cid222.ai/chat/completions', {headers: {'Authorization': `Bearer ${process.env.CID222_API_KEY}`},// ...});}// Bad: Client-side exposure// const API_KEY = 'sk-abc123'; // Never do this!
Input Validation
While CID222 validates and sanitizes content, always validate user input before sending:
Input Validation
function validateMessage(content) {// Check lengthif (content.length > 10000) {throw new Error('Message too long');}// Check for empty contentif (!content.trim()) {throw new Error('Message cannot be empty');}return content;}const validatedMessage = validateMessage(userInput);await sendToApi(validatedMessage);
Performance
Use Streaming for Better UX
Streaming responses improve perceived performance by showing content as it's generated:
Streaming reduces time-to-first-byte from several seconds to under 500ms, significantly improving user experience.
Connection Reuse
Reuse HTTP connections when making multiple requests:
Connection Pooling
// Node.js with keep-aliveconst https = require('https');const agent = new https.Agent({ keepAlive: true });async function makeRequest(body) {return fetch('https://api.cid222.ai/chat/completions', {agent,// ...});}
Use Sessions for Conversations
Sessions are more efficient than sending full history with each request:
- Reduced payload size
- Automatic context management
- Built-in token optimization
Reliability
Implement Proper Error Handling
Error Handling
async function safeChatRequest(message) {try {const response = await fetch('https://api.cid222.ai/chat/completions', {// ...});if (!response.ok) {const error = await response.json();switch (response.status) {case 401:throw new Error('Invalid API key');case 403:// Authorization failure — content blocks are NOT a 403,// they arrive as SSE events (type: "content_rejected")throw new Error('Insufficient role or scope');case 429:throw new Error('Rate limit exceeded');default:throw new Error(error.message || 'Unknown error');}}// /chat/completions always streams SSE — collect the filtered answerconst reader = response.body.getReader();const decoder = new TextDecoder();let answer = '';while (true) {const { done, value } = await reader.read();if (done) break;for (const line of decoder.decode(value).split('\n')) {if (!line.startsWith('data: ')) continue;const data = line.slice(6);if (data === '[DONE]') return answer;const event = JSON.parse(data);// Input blocked (type: "content_rejected")if (event.error) throw new Error('Blocked: ' + event.error);if (event.type === 'output_content_rejected') {throw new Error('Blocked: ' + event.reason);}// Full filtered answer arrives as one event (finish_reason: "stop")if (!event.type && typeof event.content === 'string') {answer = event.finish_reason === 'stop' ? event.content : answer + event.content;}}}return answer;} catch (error) {console.error('Chat request failed:', error);throw error;}}
Implement Retry Logic
Retry with Exponential Backoff
async function withRetry(fn, maxRetries = 3) {for (let attempt = 0; attempt < maxRetries; attempt++) {try {return await fn();} catch (error) {// Don't retry client errors (4xx)if (error.status >= 400 && error.status < 500) {throw error;}if (attempt === maxRetries - 1) {throw error;}// Exponential backoffconst delay = Math.pow(2, attempt) * 1000;await new Promise(r => setTimeout(r, delay));}}}// Usageconst result = await withRetry(() => chatRequest(message));
Monitoring
- Track detection rates — Monitor what's being detected and masked
- Monitor latency — Set alerts for response time degradation
- Review blocked requests — Regularly audit rejected content
- Token usage — Track consumption to avoid unexpected costs
Content Guidelines
- Set clear system prompts — Define expected behavior upfront
- Use appropriate models — Don't use gpt-4o when gpt-4o-mini suffices
- Limit output length — Set max_tokens to prevent runaway responses
- Test your filters — Use the testing tool before deploying changes