How to Build an AI Chatbot Using OpenAI API

Short answer: Get an API key, send the conversation to a chat model along with a system prompt that defines the bot's job, store the message history yourself, and add your own documents for answers about your business. Cost is per token: on the official pricing page in December 2026, small models start at cents per million tokens and flagship models cost tens of dollars.
A chatbot is a loop: take the user's message, add context, call a model, show the reply, remember the exchange. The model is the easy part. The quality comes from the instructions you give it, the knowledge you connect and the limits you set. This guide walks the build, with model prices checked against OpenAI's official API pricing page in December 2026.
How the pieces fit
- System prompt: the standing instructions: who the bot is, what it may answer, what tone to use, what to refuse.
- Message history: the API does not remember past turns. Your app sends the relevant history each time.
- Knowledge: documents the bot should answer from, such as FAQs, policies and product pages.
- Guardrails: rules for when to hand off to a human.
Step 1: Get access and pick a model
Create an API key in the OpenAI platform and keep it on your server, never in browser code. Then choose a model by job. The pricing page lists prices per 1 million tokens, input and output separately. Examples from the page in December 2026: gpt-6-luna at $0.10 input and $0.50 output, gpt-6.1-terra at $1.00 and $5.00, gpt-6.1-sol at $2.00 and $10.00, and gpt-6.2-astra at $8.00 and $40.00. Older models such as gpt-4o-mini ($0.15 and $0.60) are still listed. Start with a cheaper model and move up only if answers are not good enough.
Step 2: Write the system prompt
State the role, the audience, the scope and the exit. For example: "You are the support assistant for a dental clinic. Answer only from the provided information. If you do not know, say so and offer to connect the user with reception. Never give medical advice." Short and specific beats long and vague.

Step 3: Manage history
Keep the last several turns and send them with each request. Long histories cost more because input tokens are billed every call, so summarise older turns rather than resending everything. Cached input is billed far lower than fresh input on the pricing page, which rewards a stable system prompt placed first.
Step 4: Add your own knowledge
Put the bot's source material in front of it. For small knowledge bases, paste the relevant section into the prompt. For larger ones, split documents into chunks, search for the best matches to each question, and include only those. Tell the bot to answer from the supplied text and cite which document it used.
Step 5: Control cost and risk
- Set a maximum reply length and a monthly budget alert.
- Log conversations (with privacy notices) so you can see where the bot fails.
- Add a visible human handoff.
- Test with awkward questions, off-topic requests and attempts to override your instructions.
A realistic cost sketch
Cost is tokens times price. A support chat of a few hundred words each way is a small fraction of a cent on the cheaper models and more on flagship ones. Estimate with your own typical conversation length and the current prices rather than guessing, and re-check the pricing page, since model lists and prices change.
Testing before real users
Build a list of twenty questions: ten the bot should answer well, five it should decline, and five that try to push it off script, such as asking it to ignore its instructions or to reveal its prompt. Run all twenty after every change to the system prompt or the knowledge base, and keep the results. A bot that passes this list is not perfect, but you know where it is weak. Read real conversation logs weekly in the first month. The questions customers actually ask are rarely the ones you predicted, and each surprise is a new line for your knowledge base or a new rule for the prompt.
When to use a no-code tool instead
If your goal is a support bot on a website and you do not want to maintain a server, a no-code platform that wraps a model API can get you live in an afternoon. The trade-off is less control over memory, cost and data handling. If you handle personal or sensitive information, read the provider's data terms before you connect anything, and keep identifying details out of prompts wherever possible. For custom behaviour, unusual integrations or tight cost control, the direct API route repays the extra effort.
Handing over to a human
Design the exit first. Decide which topics always go to a person, such as refunds, complaints and anything legal or medical. Make the handoff one click, pass the conversation along so the customer does not repeat themselves, and tell the user plainly when they are talking to an AI. A bot that knows its limits builds more trust than one that bluffs.

FAQ
Do I need to code to build this?
Some code, yes: a small server that calls the API. No-code builders and tools like n8n can wrap the same API if you prefer.
Does the API remember previous messages?
No. You send the history each time, which is why you control what the bot remembers.
How do I stop it making things up?
Tell it to answer only from supplied information, give it good source text, and test with questions it should not be able to answer.
What does it cost?
Per token, input and output priced separately. Prices on OpenAI's page in December 2026 range from $0.10 to $40.00 per million tokens depending on model.
Related guides
- How to Build a Website Using AI Without Hiring a Developer
- How to Use Supabase with AI-Generated Applications
- How to Deploy an AI Application on Google Cloud
Sources
- OpenAI API Pricing: https://platform.openai.com/docs/pricing
Key takeaways
- A chatbot is a loop: system prompt, history, model call, reply.
- You manage memory; the API does not.
- Ground answers in your own documents and add a human handoff.
- Start on a cheaper model and check the pricing page before you budget.
Want a chatbot that answers from your own business content? Book a free 30 minute call with Tech AI Labs and we will scope it.
FAQ
Quick answers
Do I need to code to build this?
Some code, yes: a small server that calls the API. No-code builders and tools like n8n can wrap the same API if you prefer.
Does the API remember previous messages?
No. You send the history each time, which is why you control what the bot remembers.
How do I stop it making things up?
Tell it to answer only from supplied information, give it good source text, and test with questions it should not be able to answer.
What does it cost?
Per token, input and output priced separately. Prices on OpenAI's page in December 2026 range from $0.10 to $40.00 per million tokens depending on model.


