How much does it cost to build on top of Open AI API: building👩‍💻 + covering cost💰
Tags: openai, api, ai
Published: 2023-07-19 15:57:33

Over the past two months, I've developed five distinct micro-products using the OpenAI API. So called, ChatGPT wrappers. The goal was to leverage the power of ChatGPT and understand if it is worth to build on top of it's API.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r6cmdoup1418q4gv452k.gif)

## What you will find in this article? 

1. Projects I build
2. How to build on top of Open AI API 
3. How much it costs for different projects
4. How to cover costs 

## 1.The Projects I've Built/Building 

**LinkedIn Post Generator:** This open-source tool employs ChatGPT to create engaging LinkedIn posts. https://github.com/shnai0/linkedin-post-generator

**Startup Funding:** With the help of ChatGPT, this tool generates a tailored list of funds and investors for your startup, considering factors like your location, funding round, and industry sector. https://www.startupfunding.to

**Posts-to-Blog:** This mmicro-tool transforms a tweet or LinkedIn post into a comprehensive blog article, utilizing ChatGPT's writing capabilities.https://post-to-blog.vercel.app

**ReportX:** An AI teaching assistant that employs ChatGPT to generate informative teacher reports.https://www.aiteacherassistant.co

**Infinite UI**: 1000+ UI components which can be regenerated 
https://infiniteui.co

This one is not public yet so not costs. 


## 2.How to build on top of OpenAI API? 

### Step 1: Create an OpenAI Account

The first step to using OpenAI's GPT-3 is to create an account on the OpenAI website. This will give you access to the API key needed to make requests.

Got to API https://platform.openai.com/apps

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1rhsopbld9u1tq5tucxz.png)


### Step 2: Generate API Key

After creating an account, you'll have access to the OpenAI Dashboard. From here, you can generate an API Key, which you will use to authenticate your requests to the API.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pjku36x3imxa6mvrqdvm.png)
Go to API Keys and generate new 


### Step 3: Create API folder and ts file which handling it


```ts
import { OpenAIStream, OpenAIStreamPayload } from "@/utils/OpenAIStream";

if (!process.env.OPENAI_API_KEY) {
  throw new Error("Missing env var from OpenAI");
}

export const config = {
  runtime: "edge",
};

const handler = async (req: Request): Promise<Response> => {
  const { prompt } = (await req.json()) as {
    prompt?: string;
  };

  if (!prompt) {
    return new Response("No prompt in the request", { status: 400 });
  }

  const payload: OpenAIStreamPayload = {
    model: "gpt-3.5-turbo",
    messages: [{ role: "user", content: prompt }],
    temperature: 0.7,
    top_p: 1,
    frequency_penalty: 0,
    presence_penalty: 0,
    max_tokens: 500,
    stream: true,
    n: 1,
  };

  const stream = await OpenAIStream(payload);
  return new Response(stream);
};

export default handler;

```

### Step 4. Change env.file

If you are using Open source project, or you can use my project, there is file generate.ts/optomixe.ts which handling api. So it all will be set except your own key in env. 


Create new or add in existing Key you get from Open AI api 


![Secret Key](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ycne19m7x8q4dtsd02hg.gif)

It is secret key. Don't share it with anyone. 

### Step 5. Change prompts 

In the index file for most simple apps and for me ,I was handling prompt. 

```tsx
 const handlePrompt = () => {
    let prompt;
    switch (vibe) {
      case "Student":
        prompt = `Your Prompts`;
 break;
    }
    return prompt;
  };
```


## 3. How much does it cost to build with OpenAI? 

If you're building something new, it's definitely worth considering. 

The initial costs will be relatively low while you establish your user base.


**So, 0 users 0 costs **


To give you some idea of costs, I connected ChatGPT API to my five tools and currently spend less than a dollar per day.

**

## 1000 request = 1 dollar

**
On a peak day, when I launched the LinkedIn Post Generator and received 6,053 requests, the cost was around $6. This translates to roughly one dollar for 1,000 requests.

![April expenses](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vdr5o9lx7nh1yygc8r6y.jpeg)


![July costs](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fjwateztfibqq8l6zczv.png)

90% of costs come from Linkedin Post Generator 



## How to avoid paying , prepare to it or cover expenses? 

You might be wondering, how do you manage these costs or prepare for the expenses? Here are some strategies that have worked for me:

1️⃣ Put limit: in case your API got leacked you can always put the limit on it


![Add usage limit](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2f26vjh3v7mdexxw47g2.png)

2️⃣ Evaluate useage: If you addigin functionality with ChatGPT in existing tool with exisitng users or traffic, consider that there could be much more requests, than new tool

3️⃣ Introduce subscribtion: Think on subscribtion model, which would work that cover your costs 

4️⃣ Coffee donation: If you want to keep the tool free, think about adding small coffee donation. I added this one in Linkedin Post generator and it covered the expenses on ChatGpt for couple of months. 

5️⃣ API key from users: Offer users to add their own API key. In that case they connect to your product there API key and you dont need to pay anything there. 


## And is it worth it or not ?

If you are building smth new, i think it is definitely worth it as costs will be very low for the beggining while you get your user base.

Benefits: you will be able to build smth cool, faster, and explore AI power. 


So the idea was to test and see how it will work and how much it will cost. 


Are you building on top of OpenAI or adding new feature with it? 


## Looking for stars 

I am growing Papermark.io - open source document insfrastructure.

[Place to leave your precious stars ⭐️](https://github.com/mfts/papermark)


![Star storm](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vqwrv7a9clwoazuo89te.gif)



Share with me your developer journey:) Happy to meet more exciting friends here.

Here my Twitter for new projects update https://twitter.com/shnai0