LovableOpenAIIntermediate9 min read

How to Add AI Features to Your Lovable App with OpenAI

Add ChatGPT-powered features to your Lovable app: chatbots, content generation, summarization, and more using the OpenAI API.

Before you start

  • A Lovable app with Supabase connected
  • An OpenAI account with API key
  • Basic understanding of how prompts work

Step by step

1

Get your OpenAI API key

Go to platform.openai.com → API Keys → Create new secret key. Copy it.

OPENAI_API_KEY=sk-your-key-here
2

Add the key to Supabase secrets

Store the API key securely in Supabase Edge Functions secrets.

# Supabase Dashboard → Edge Functions → Secrets
# Name: OPENAI_API_KEY
# Value: sk-your-key-here
3

Create the AI Edge Function

Build an Edge Function that calls the OpenAI API.

Paste this into Lovable:

Create a Supabase Edge Function called 'ai-chat' that:
1. Accepts a 'messages' array in the request body
2. Calls the OpenAI Chat Completions API using fetch
3. Uses gpt-4o-mini model for speed and cost
4. Sets a system prompt: 'You are a helpful assistant for [your app description]'
5. Returns the assistant's response
6. Limits max_tokens to 500 to control costs
7. Has proper CORS headers
4

Build the chat interface

Create a chat UI that communicates with your Edge Function.

Paste this into Lovable:

Create a chat component that:
1. Shows a message list with user and assistant bubbles
2. Has a text input with a send button at the bottom
3. Sends user messages to the ai-chat Edge Function
4. Shows a typing indicator while waiting for response
5. Scrolls to the latest message automatically
6. Stores conversation history in state
7. Has a 'Clear conversation' button
5

Add streaming responses

Make the AI response appear word-by-word like ChatGPT for a better user experience.

Paste this into Lovable:

Update the ai-chat Edge Function and chat component to support streaming:
1. Set stream: true in the OpenAI API call
2. Return the response as a ReadableStream
3. In the frontend, read the stream chunk by chunk
4. Update the message as each token arrives
5. Show a blinking cursor at the end while streaming
6

Add rate limiting and cost controls

Protect your API budget by limiting how many messages users can send.

Paste this into Lovable:

Add rate limiting to the AI chat:
1. Track message count per user in the database
2. Free users: 10 messages per day
3. Pro users: unlimited
4. Show remaining messages count in the chat UI
5. Show an upgrade prompt when the limit is reached

Common errors

429 Too Many Requests from OpenAI

You've exceeded your OpenAI API rate limit or run out of credits.

Fix: Check your usage at platform.openai.com/usage. Add a payment method or wait for the rate limit to reset.

Response takes too long

GPT-4 is slow for real-time chat.

Fix: Switch to gpt-4o-mini for faster responses. It's 10x cheaper and good enough for most use cases.

CORS error calling Edge Function

The browser is blocked from calling your Supabase function.

Fix: Add Access-Control-Allow-Origin headers to the Edge Function response.

Related guides

Weekly Newsletter

Get next week's fix before you need it.

Join developers getting weekly vibe coding tips, error fixes, and tool updates.

Subscribe on Substack →