How to integrate ChatGPT into your app: API setup, streaming, real cost math ($50-$2,000/month), and when you need an agent instead of a single call.

To integrate ChatGPT into your app, you call the OpenAI API from your backend: send a system prompt and the user's message to the chat completions endpoint, stream the response back, and keep your API key server-side. The basic version takes an afternoon. Making it reliable, affordable, and safe at scale is the real work.
We've shipped 30+ AI projects to production since 2024, and almost every one started with some version of this integration. The pattern below is what survives contact with real users. This guide covers the setup, streaming, the cost math nobody shows you, and the failure modes that only appear after launch.
Three things, in order of difficulty:
Before you write a line of code, get the prerequisites out of the way: an OpenAI platform account with billing enabled, an API key stored in your secrets manager (never in frontend code, never in a git repo), and a hard spending limit set in the dashboard. That last one is not optional. A runaway loop or a scraped key can burn through hundreds of dollars overnight, and the dashboard limit is the only brake that works while you sleep.

Model names change quarterly, so we'll give you the durable heuristic instead of a table that's stale in three months: every provider ships a flagship model and a small, fast, cheap model, and the price gap between them is usually 10-20x per token.
| Task type | Model tier | Why |
|---|---|---|
| Complex reasoning, content generation, multi-step instructions | Flagship | Quality failures cost you users |
| Classification, extraction, routing, summaries | Small/fast | The cheap model is good enough, at a fraction of the cost |
| High-volume background jobs | Small/fast, batched | Batch APIs typically cut the price by half again |
The single biggest cost lever in any OpenAI API integration is routing: send the easy 80% of requests to the cheap model and reserve the flagship for the 20% that needs it. We've seen this one change cut a client's inference bill by more than half before touching anything else.
One more durable opinion: buy the outcome, not the model. Wrap your provider calls in a thin abstraction layer from day one so you can swap models, or providers, without rewriting your product. The Anthropic API docs use a nearly identical messages format to OpenAI's, so a well-structured integration can add Claude as a fallback or an A/B arm in a day.
Here's the minimal server-side pattern. The key details are the ones people skip: a timeout, a max token cap, and the key loaded from the environment.
import OpenAI from 'openai';
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
timeout: 30_000,
maxRetries: 2,
});
export async function chat(userMessage, history = []) {
const response = await openai.chat.completions.create({
model: process.env.CHAT_MODEL, // config, not hardcoded
messages: [
{ role: 'system', content: SYSTEM_PROMPT },
...history,
{ role: 'user', content: userMessage },
],
max_tokens: 800,
});
return response.choices[0].message.content;
}
Rules that come from shipping this pattern repeatedly:

A flagship model can take 10-30 seconds to finish a long response. A spinner for 20 seconds reads as broken; users refresh, resend, and double your costs. Streaming shows the first token in under a second and changes the perceived speed entirely.
The OpenAI API reference supports server-sent events natively: set stream: true, iterate the chunks, and forward them to your client over SSE or WebSockets. Two production details worth knowing:
If you're on Next.js or React, the Vercel AI SDK handles most of this plumbing and is what we reach for on client work rather than hand-rolling SSE handlers.
Here's the math most tutorials skip. API pricing is per token (roughly 4 characters per token), billed separately for input and output, and output tokens cost several times more than input.
Across the production systems we run and maintain, inference for a typical AI feature lands between $50 and $2,000 per month. Where you fall in that range depends less on user count than on engineering discipline. Good engineering (caching, model routing, and tight prompt design) routinely cuts inference costs 3-10x. Concretely:
Set a per-user rate limit and a per-user daily token budget from day one. Every consumer AI product without one eventually finds the user who treats your chat feature as their personal free API.
A chat completion answers questions. It cannot check your database, update an order, or take any action. If your roadmap says "and then the assistant actually does the thing", you've crossed from a chatbot into agent territory, and the architecture changes: tool calls, multi-step loops, permissions, and an evaluation suite.
The line matters commercially too. A well-built chat integration is a feature. An agent is a system. We wrote up the distinction in detail in our AI agent vs chatbot comparison, and if you're heading that way, our practical guide to building AI agents covers the architecture end to end. As a budget anchor: a single-purpose production agent typically runs $8,000-$20,000 and ships in 3-4 weeks.
Our honest advice: don't jump to an agent because it sounds more impressive. Most products should ship the single-call version first, learn from real usage, and add tools only where users hit a wall.
Every failure below comes from real integrations we've either built or been called in to fix.
The demo-to-production gap. The recurring pattern in our client work: a team arrives with a demo that impressed everyone in a meeting and fell apart on real data. It handled the five hand-picked test questions perfectly and then met real users, who paste in 8,000-word documents, ask questions in Spanish, and try to jailbreak it within the first hour. Prototypes lie. Moving from 90% to 99% reliability is where the engineering lives, and it's most of what we get hired for.
No evaluation suite. If you can't measure output quality, you can't safely change your prompt, your model, or your provider. An AI feature without an evaluation suite is a liability with a chat interface. Even 50 real test cases with expected properties, run on every prompt change, puts you ahead of most teams.
Prompt injection. Users will tell your assistant to ignore its instructions, and sometimes it will. Never let model output trigger privileged actions directly, strip or fence user-provided documents in the prompt, and treat the model's output as untrusted input to the rest of your system.
Rate limits and outages. OpenAI enforces per-minute request and token limits, and the API has bad days. Production integrations need exponential backoff, request queuing, and ideally a second provider behind that abstraction layer you built in week one.
Silent quality drift. Models get updated and deprecated. Pin specific model versions where the API allows it, and let your evaluation suite tell you whether the shiny new version is actually better for your use case before you switch.
The build cost for a solid chat feature (backend integration, streaming, rate limiting, cost controls) is typically 1-3 weeks of engineering. Running costs land between $50 and $2,000 per month for most production features, and disciplined caching and model routing can cut that 3-10x. Agent-grade builds start around $8,000.
No. Any key shipped in frontend or mobile code is effectively public and will be extracted and abused. Route every call through your own backend, keep the key in a secrets manager, and enforce authentication and per-user rate limits at your API layer.
Both offer mature APIs with similar message formats, so the switching cost is low if you build a thin abstraction layer. Rather than betting on a provider, structure your code so you can test both against your own evaluation cases and route to whichever wins on your workload. The answer changes as models update.
Three layers: per-user rate limits and daily token budgets at your API layer, a moderation check on inputs before they reach the model, and hard spending caps in the provider dashboard as the final backstop. Log everything so you can identify and block abusive accounts quickly.
If the feature only needs to answer questions from your content, a single-call integration with retrieval is enough, and it's cheaper and faster to ship. You need an agent when the assistant must take actions: querying live systems, updating records, or completing multi-step workflows with tools.
Arsalan Amin drafted this guide with AI assistance and edited it against Codestreaks' own project-scoping and pricing history: 30+ shipped projects since 2024, fixed-price engagements from $8,000 to $60,000+. The pricing tiers and timelines above are what we've actually quoted and delivered, not industry averages.
If you'd rather skip the failure modes above, this is the work we do. Codestreaks has delivered 30+ AI products to production since 2024, most in 4-8 weeks, and every client gets 100% code ownership, so the integration is yours, not ours.
Book a free 30-minute scoping call and we'll map your use case to an architecture and a fixed price. We respond within two business days, and if a single API call solves your problem, we'll tell you that instead of selling you an agent. Start your project or see how we approach chatbot and AI assistant development.