Lovable App's OpenAI Bill Growing: Find and Fix It
Your Lovable app's OpenAI bill keeps climbing. Here's how to find what's driving it and cut it, with real model prices for November 2026.
Your Lovable app's OpenAI bill grows because almost every app starts with a flagship model hardcoded for every request, no prompt caching, and conversations that grow longer with every turn. The fix is straightforward: find which edge function calls what model and how often, then match the model to the actual work. This article walks through both steps, with real November 2026 prices.
Two Ways a Lovable App Calls OpenAI
Before you can diagnose the bill, you need to know which billing path your app is on.
Path 1: Lovable's built-in AI connector. If you told Lovable to add a chatbot or summarization feature without specifying an API key, Lovable created the backend function and manages the key itself. Those calls draw from your Lovable workspace's AI credit balance, not from your OpenAI account. You can see per-request costs in the More → AI dashboard inside Lovable. If this is your setup and you're seeing a rising OpenAI bill, you either switched paths at some point or have a second integration running.
Path 2: Your own OpenAI key in a Supabase Edge Function. This is the path that shows up on your OpenAI invoice. When you asked Lovable to build an AI feature using your own key (stored in Cloud → Secrets), Lovable generated a Supabase Edge Function that calls OpenAI directly. Your OpenAI account gets the bill; Lovable only charges Cloud usage for running the function. This is where most growing bills come from.
For the rest of this article, the assumption is Path 2.
Step 1: Find What's Driving the Bill
Pull the usage breakdown from OpenAI
Go to platform.openai.com/usage and filter by model. The model column tells you immediately if your app is using a flagship model for every call. Check whether the code still uses the model it was first built with, such as gpt-4o, which may be more than the task needs.
Then look at the token counts. OpenAI shows input and output tokens separately. If input tokens dwarf output tokens by a large ratio (say, 10:1 or higher), the main cost driver is probably context being resent on every turn, not the replies themselves.
Read the edge function code
In your Lovable project, open the Supabase Edge Function that handles AI calls. Look for three things:
- Which model is specified. A line like
model: "gpt-4o"ormodel: "gpt-4.1"tells you exactly what you're paying for. - How the messages array is built. If the function takes the full conversation history and sends it as the
messagesparameter on every request, the input token count grows with every user turn. A 20-message conversation resends all 20 messages each time. - What the system prompt looks like. A long system prompt (500+ tokens) that repeats on every call adds up. At GPT-4o's $2.50 per million input tokens, 1,000 tokens of repeated system prompt costs $0.0025 per call, or $2.50 for every 1,000 calls. At scale that matters.
Map cost to features
If your app has multiple AI features (a chat interface, a summarizer, a document analyzer), and each routes to a separate edge function, check each one separately. The LLM cost attribution guide covers how to tag requests so you can see per-feature spend on the OpenAI usage page.
Let each request use the cheapest model that's just as good
PromptUnit checks each request your app sends. When a cheaper model is just as good for that request, it uses it; otherwise your model answers. Free until it has saved you money.
Try it free →Step 2: Lower the Bill Without Breaking the App
Match the model to the task
This is where the biggest savings come from. The table below shows what the main OpenAI models cost today. Three of the models listed (GPT-4o, GPT-4.1 and GPT-4o mini), and GPT-4.1 mini, are deprecated, with an announced shutdown date of January 14, 2027.
| Model | Input ($/1M tokens) | Output ($/1M tokens) | Cached input ($/1M tokens) |
|---|---|---|---|
| GPT-6 Astra | $10.00 | $50.00 | $1.00 |
| GPT-5.5 | $5.00 | $30.00 | $0.50 |
| GPT-5.4 | $2.50 | $15.00 | $0.25 |
| GPT-4o | $2.50 | $10.00 | $1.25 |
| GPT-4.1 | $2.00 | $8.00 | $0.50 |
| GPT-5.4 mini | $0.75 | $4.50 | $0.075 |
| GPT-4o mini | $0.15 | $0.60 | $0.075 |
| GPT-6 Luna | $0.10 | $0.50 | $0.01 |
| GPT-5 nano | $0.05 | $0.40 | $0.005 |
Note: GPT-4o, GPT-4o mini, GPT-4.1, and GPT-4.1 mini are deprecated and will be shut down on January 14, 2027. If your edge function uses any of those model strings, plan the migration now. The recommended replacement for all four is gpt-6-luna.
A chatbot that answers simple questions about your product does not need GPT-5.5 at $5.00/$30.00. GPT-6 Luna at $0.10/$0.50 handles most FAQ-style interactions and costs 50x less per input token. The guide to where GPT-4o mini holds up and where it breaks has the framework for deciding which tier fits your use case.
To change the model in a Lovable app, open the edge function in Lovable's editor and ask it: "Change the model in the ai-chat edge function from gpt-4o to gpt-6-luna." Lovable will update the function. Test a sample of your real queries before redeploying.
Cap the conversation history
If your edge function sends the full conversation history with every request, add a window. Keeping the last 6-10 messages instead of the entire history cuts input tokens significantly in long sessions. Ask Lovable: "Update the ai-chat edge function to only send the last 8 messages from the conversation history, keeping the system prompt."
Enable prompt caching
For requests where the system prompt is long and consistent (the same instructions on every call), OpenAI's prompt caching kicks in automatically for prompts over 1,024 tokens. The cached portion costs $1.25 per million tokens for GPT-4o (versus $2.50 for uncached), and just $0.01 per million for GPT-6 Luna (versus $0.10). You don't need to change code to get it; you need to structure your prompts so the static part comes first and the dynamic part (the user's message) comes at the end. The prompt caching guide covers how to structure this.
Add per-user rate limiting
An unthrottled public chatbot can generate thousands of requests if users discover they can send unlimited messages, or if a buggy retry loop runs. Add a simple per-user limit in the edge function: count requests against a Supabase table row and reject calls above a threshold per hour. Ask Lovable: "Add rate limiting to the ai-chat edge function so each user can send a maximum of 20 messages per hour, returning an error if they exceed that."
Set max_tokens explicitly
If your edge function doesn't set max_tokens, OpenAI will let the model generate until it decides to stop. For conversational replies, a limit of 400-600 tokens is usually more than enough. Setting it to 500 prevents the occasional runaway response from inflating your bill. The token ceiling also limits your exposure if someone crafts a prompt designed to generate a very long response.
Switching to Your Own Key vs. Lovable's Built-in Connector
If you're currently on Path 2 (your own key) and your usage is low, consider whether the built-in Lovable AI connector would be simpler. Per the Lovable docs, it handles the API key, and the More → AI dashboard shows per-request costs. The tradeoff is that you're billed in Lovable credits rather than on your OpenAI account, so cost visibility and model control are more limited.
If you need control over which model runs and want the costs on your own OpenAI invoice, stay on Path 2 and use the techniques above.
Automated Routing as the App Matures
Once your app is handling real traffic, manually picking the right model for each edge function becomes a maintenance problem. Models get deprecated (four models are going away on January 14, 2027), new cheaper options appear, and the best choice for a given task can shift. Hardcoding a specific model per step creates its own problems over time.
A proxy layer that sits between your edge function and OpenAI can handle this automatically. PromptUnit works this way: you point the edge function's base URL at the proxy and add your PromptUnit key as a header alongside your existing OpenAI key. Each request is checked: when a cheaper model is just as good for it, PromptUnit uses it; otherwise the request goes to the model you asked for. The dashboard shows every call: what model was asked for, what model answered, and what it cost. For a Lovable app, PromptUnit's setup provides a ready message you paste into Lovable's chat, and Lovable makes the two-setting change. Details at promptunit.ai.
The AI cost optimization checklist covers all 15 steps in priority order if you want a broader view beyond just model selection.