Blog
    15 min read

    Integrating AI Into Your Application: Practical Patterns for Production

    Integrating AI Into Your Application: Practical Patterns for Production

    Technical Note: This article includes production code examples in TypeScript, SQL, and React. While the concepts are universally applicable, the implementation patterns assume familiarity with modern web development.


    The AI Integration Reality Check

    Everyone wants to add AI to their application. Few understand what that actually means in production.

    AI APIs are fundamentally different from traditional REST endpoints:

    • Response times are measured in seconds, not milliseconds
    • Costs scale with usage in unpredictable ways
    • Outputs are non-deterministic;the same input can produce different results
    • Rate limits and downtime are common

    This isn't a warning to avoid AI;it's a call to implement it properly. Here's how we build AI features that work reliably in production.

    Architecture Pattern: The AI Gateway

    We never call AI APIs directly from frontend code. Instead, we route all AI requests through a backend gateway:

    // Edge function: AI Gateway
    Deno.serve(async (req) => {
      const corsHeaders = {
        'Access-Control-Allow-Origin': '*',
        'Access-Control-Allow-Methods': 'POST, OPTIONS',
        'Access-Control-Allow-Headers': 'authorization, content-type',
      };
    
      if (req.method === 'OPTIONS') {
        return new Response(null, { headers: corsHeaders });
      }
    
      // Authenticate the request
      const authHeader = req.headers.get('Authorization');
      const supabase = createClient(
        Deno.env.get('SUPABASE_URL') ?? '',
        Deno.env.get('SUPABASE_ANON_KEY') ?? '',
        { global: { headers: { Authorization: authHeader ?? '' } } }
      );
    
      const { data: { user }, error: authError } = await supabase.auth.getUser();
      if (authError || !user) {
        return new Response(
          JSON.stringify({ error: 'Unauthorized' }),
          { status: 401, headers: corsHeaders }
        );
      }
    
      // Check rate limits per user
      const { data: usage } = await supabase
        .from('ai_usage')
        .select('requests_today')
        .eq('user_id', user.id)
        .single();
    
      const DAILY_LIMIT = 50;
      if (usage && usage.requests_today >= DAILY_LIMIT) {
        return new Response(
          JSON.stringify({ error: 'Daily AI limit reached' }),
          { status: 429, headers: corsHeaders }
        );
      }
    
      // Parse and validate the request
      const { prompt, model = 'google/gemini-2.5-flash' } = await req.json();
      
      if (!prompt || typeof prompt !== 'string' || prompt.length > 10000) {
        return new Response(
          JSON.stringify({ error: 'Invalid prompt' }),
          { status: 400, headers: corsHeaders }
        );
      }
    
      // Call the AI API
      const response = await fetch(
        'https://ai.gateway.lovable.dev/v1/chat/completions',
        {
          method: 'POST',
          headers: {
            'Authorization': `Bearer ${Deno.env.get('LOVABLE_API_KEY')}`,
            'Content-Type': 'application/json',
          },
          body: JSON.stringify({
            model,
            messages: [{ role: 'user', content: prompt }],
            max_tokens: 2000,
          }),
        }
      );
    
      if (!response.ok) {
        console.error('AI API error:', await response.text());
        return new Response(
          JSON.stringify({ error: 'AI service temporarily unavailable' }),
          { status: 503, headers: corsHeaders }
        );
      }
    
      const aiResult = await response.json();
      const content = aiResult.choices?.[0]?.message?.content;
    
      // Track usage
      await supabase.rpc('increment_ai_usage', { p_user_id: user.id });
    
      return new Response(
        JSON.stringify({ content }),
        { headers: { ...corsHeaders, 'Content-Type': 'application/json' } }
      );
    });
    

    Why the Gateway Pattern?

    1. Security: API keys never touch the frontend
    2. Rate limiting: Control costs by limiting per-user requests
    3. Fallback handling: Route to backup providers on failures
    4. Logging: Track usage for billing and debugging
    5. Caching: Store frequent responses to reduce costs

    Frontend Integration: Handling Slow Responses

    AI responses take 2-10 seconds. Users need feedback during that time:

    // Custom hook for AI requests
    export function useAI() {
      const [isLoading, setIsLoading] = useState(false);
      const [progress, setProgress] = useState(0);
    
      const generateContent = useCallback(async (prompt: string) => {
        setIsLoading(true);
        setProgress(0);
    
        // Simulate progress during the wait
        const progressInterval = setInterval(() => {
          setProgress((prev) => Math.min(prev + 10, 90));
        }, 500);
    
        try {
          const response = await fetch('/functions/v1/ai-gateway', {
            method: 'POST',
            headers: {
              'Authorization': `Bearer ${session?.access_token}`,
              'Content-Type': 'application/json',
            },
            body: JSON.stringify({ prompt }),
          });
    
          if (!response.ok) {
            const error = await response.json();
            throw new Error(error.message || 'AI request failed');
          }
    
          const result = await response.json();
          setProgress(100);
          return result.content;
        } finally {
          clearInterval(progressInterval);
          setIsLoading(false);
        }
      }, [session]);
    
      return { generateContent, isLoading, progress };
    }
    

    UI Pattern: Progressive Disclosure

    function ContentGenerator() {
      const { generateContent, isLoading, progress } = useAI();
      const [result, setResult] = useState<string | null>(null);
    
      const handleGenerate = async () => {
        const content = await generateContent(prompt);
        setResult(content);
      };
    
      return (
        <div className="space-y-4">
          <Textarea
            value={prompt}
            onChange={(e) => setPrompt(e.target.value)}
            placeholder="Describe what you want to generate..."
            disabled={isLoading}
          />
          
          <Button onClick={handleGenerate} disabled={isLoading}>
            {isLoading ? 'Generating...' : 'Generate'}
          </Button>
    
          {isLoading && (
            <div className="space-y-2">
              <Progress value={progress} />
              <p className="text-sm text-muted-foreground">
                AI is thinking... This usually takes 5-10 seconds.
              </p>
            </div>
          )}
    
          {result && (
            <Card>
              <CardContent className="pt-6">
                <div className="prose">{result}</div>
              </CardContent>
            </Card>
          )}
        </div>
      );
    }
    

    Prompt Engineering: The Hidden Complexity

    The prompt isn't just what the user types. It includes system instructions, context, and formatting requirements:

    function buildPrompt(userInput: string, context: ProjectContext): string {
      return `You are a professional content writer for a ${context.industry} company.
    
    TASK: Generate marketing copy based on the user's request.
    
    CONTEXT:
    - Company name: ${context.companyName}
    - Target audience: ${context.targetAudience}
    - Brand voice: ${context.brandVoice}
    - Key differentiators: ${context.differentiators.join(', ')}
    
    USER REQUEST:
    ${userInput}
    
    REQUIREMENTS:
    1. Match the brand voice exactly
    2. Include at least one mention of a key differentiator
    3. Keep the response under 200 words
    4. Use active voice
    5. End with a clear call to action
    
    OUTPUT FORMAT:
    Return only the marketing copy, no explanations or metadata.`;
    }
    

    Prompt Template Management

    For production applications, we store prompts in the database:

    CREATE TABLE public.ai_prompts (
      id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
      name TEXT NOT NULL UNIQUE,
      template TEXT NOT NULL,
      model TEXT NOT NULL DEFAULT 'google/gemini-2.5-flash',
      max_tokens INTEGER NOT NULL DEFAULT 1000,
      temperature DECIMAL(3,2) NOT NULL DEFAULT 0.7,
      is_active BOOLEAN NOT NULL DEFAULT true,
      created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
      updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
    );
    

    This allows us to:

    • Update prompts without code deployments
    • A/B test different prompt versions
    • Track which prompts perform best

    Cost Management: Staying Profitable

    AI costs can spiral quickly. Here's how we keep them under control:

    1. Usage Tracking

    CREATE TABLE public.ai_usage (
      id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
      user_id UUID NOT NULL REFERENCES auth.users(id),
      requests_today INTEGER NOT NULL DEFAULT 0,
      requests_total INTEGER NOT NULL DEFAULT 0,
      tokens_used_today INTEGER NOT NULL DEFAULT 0,
      last_request_at TIMESTAMPTZ,
      reset_at DATE NOT NULL DEFAULT CURRENT_DATE,
      created_at TIMESTAMPTZ NOT NULL DEFAULT now()
    );
    
    -- Function to increment usage
    CREATE OR REPLACE FUNCTION public.increment_ai_usage(p_user_id UUID)
    RETURNS void AS $$
    BEGIN
      INSERT INTO public.ai_usage (user_id, requests_today, requests_total)
      VALUES (p_user_id, 1, 1)
      ON CONFLICT (user_id) 
      DO UPDATE SET
        requests_today = CASE 
          WHEN ai_usage.reset_at < CURRENT_DATE THEN 1 
          ELSE ai_usage.requests_today + 1 
        END,
        requests_total = ai_usage.requests_total + 1,
        reset_at = CURRENT_DATE,
        last_request_at = now();
    END;
    $$ LANGUAGE plpgsql SECURITY DEFINER SET search_path = public;
    

    2. Tiered Limits by Plan

    const PLAN_LIMITS = {
      free: { daily: 10, maxTokens: 500 },
      pro: { daily: 100, maxTokens: 2000 },
      enterprise: { daily: 1000, maxTokens: 4000 },
    };
    
    async function checkPlanLimits(userId: string, plan: string) {
      const limits = PLAN_LIMITS[plan] || PLAN_LIMITS.free;
      
      const { data: usage } = await supabase
        .from('ai_usage')
        .select('requests_today')
        .eq('user_id', userId)
        .single();
    
      return {
        allowed: !usage || usage.requests_today < limits.daily,
        remaining: limits.daily - (usage?.requests_today || 0),
        maxTokens: limits.maxTokens,
      };
    }
    

    3. Response Caching

    For repeated queries, cache results:

    async function getCachedOrGenerate(prompt: string, userId: string) {
      const promptHash = await hashString(prompt);
      
      // Check cache
      const { data: cached } = await supabase
        .from('ai_cache')
        .select('response')
        .eq('prompt_hash', promptHash)
        .gt('expires_at', new Date().toISOString())
        .single();
    
      if (cached) {
        return cached.response;
      }
    
      // Generate new response
      const response = await callAIAPI(prompt);
    
      // Cache for 24 hours
      await supabase.from('ai_cache').insert({
        prompt_hash: promptHash,
        response,
        expires_at: new Date(Date.now() + 24 * 60 * 60 * 1000),
      });
    
      return response;
    }
    

    Model Selection: Matching Task to Model

    Different models excel at different tasks:

    | Model | Best For | Speed | Cost | |-------|----------|-------|------| | gemini-2.5-flash | General tasks, good balance | Fast | Low | | gemini-2.5-pro | Complex reasoning, analysis | Medium | Medium | | gpt-5 | Nuanced writing, creative tasks | Slow | High | | gpt-5-mini | Simple classification, extraction | Fast | Low |

    function selectModel(taskType: string): string {
      switch (taskType) {
        case 'summarize':
        case 'classify':
          return 'openai/gpt-5-nano';
        case 'generate_content':
        case 'answer_question':
          return 'google/gemini-2.5-flash';
        case 'analyze_document':
        case 'complex_reasoning':
          return 'google/gemini-2.5-pro';
        case 'creative_writing':
          return 'openai/gpt-5';
        default:
          return 'google/gemini-2.5-flash';
      }
    }
    

    Error Handling: Graceful Degradation

    AI services fail. Your application shouldn't:

    async function generateWithFallback(prompt: string) {
      const models = [
        'google/gemini-2.5-flash',
        'openai/gpt-5-mini',
        'google/gemini-2.5-flash-lite',
      ];
    
      for (const model of models) {
        try {
          return await callAIAPI(prompt, model);
        } catch (error) {
          console.error(`Model ${model} failed:`, error);
          // Continue to next model
        }
      }
    
      // All models failed - return graceful fallback
      return {
        error: true,
        message: 'AI is temporarily unavailable. Please try again later.',
        fallbackContent: generateRuleBased(prompt),
      };
    }
    

    Testing AI Features

    AI outputs are non-deterministic. Testing requires a different approach:

    describe('AI Content Generator', () => {
      test('generates content within expected length', async () => {
        const result = await generateContent('Write a product description');
        expect(result.length).toBeGreaterThan(50);
        expect(result.length).toBeLessThan(500);
      });
    
      test('handles rate limiting gracefully', async () => {
        // Exhaust rate limit
        for (let i = 0; i < 55; i++) {
          await generateContent('test');
        }
        
        // Next request should fail gracefully
        await expect(generateContent('test')).rejects.toThrow(/limit/i);
      });
    
      test('respects prompt constraints', async () => {
        const result = await generateContent(
          'Write a 2-sentence product description'
        );
        const sentences = result.split(/[.!?]+/).filter(Boolean);
        expect(sentences.length).toBeLessThanOrEqual(3);
      });
    });
    

    The Reality of AI in Production

    AI is powerful, but it's not magic. For every successful AI feature, there's:

    • Careful prompt engineering
    • Robust error handling
    • Cost monitoring
    • User education about limitations

    The best AI features feel invisible. Users don't care that AI generated their product description;they care that it sounds good and saved them time.

    Build AI features like you build any other feature: with attention to reliability, performance, and user experience. The AI part is just the implementation detail.