Build a Chatbot with an LLM API

Call a language model over HTTP, keep conversation memory, stream the reply, protect your API key behind a backend, handle errors and cost, and ship a web chat UI — built step by step with Node.js.

ជំពូកនេះមិនទាន់ត្រូវបានបកប្រែជាភាសាខ្មែរនៅឡើយទេ — សូមអានជាភាសាអង់គ្លេសខាងក្រោម។

You have used AI chat apps. Now you will build one. This series shows how any app talks to a large language model (LLM): it sends an HTTP request with the conversation so far, and the model sends text back. That is the whole trick. Once you can do it, you can add AI to a Telegram bot, a school project, a customer-support page, or a backend service at work.

You will learn the parts that tutorials often skip. A model has no memory, so your code must keep the conversation and resend it. Replies arrive faster if you stream them word by word. An API key is money, so it must never reach the browser; it lives on your own backend, and the browser talks to your backend instead. Real networks fail, so you need timeouts, retries, and a spending limit. And a chat is only useful if people can open it, so you will finish with a small web page.

Across eight chapters you build chatlab: an API client, a conversation manager, a streaming parser, a command-line chatbot, a secure backend proxy, a retry and budget layer, and a browser chat UI. To keep everything free and runnable anywhere, chapter 1 gives you a tiny mock LLM server (about 90 lines, no dependencies) that speaks the same style of API as a hosted model. Every chapter works against it with no account, no key, and no bill. Then, with two environment variables, the same code can talk to a real provider when you are ready.

You need the basics of JavaScript, including async/await, from JavaScript & TypeScript Fundamentals, and the terminal from Command Line Basics. It helps to have read How LLMs Work (for Developers), especially the chapters on tokens and context windows, because this series is where those ideas become code. You need Node.js (the LTS version) and a code editor such as Visual Studio Code.

Every chapter ends with a Quick Review: Common Questions section: short, honest answers to the questions students and developers most often ask about that chapter's topic.

  1. 01What an LLM API IsA chatbot is just an HTTP request that carries a list of messages and gets text back: set up the chatlab project, run a free mock LLM server, and learn the anatomy of a request, a response, roles, tokens and errors.
  2. 02Your First API CallCall a chat API from JavaScript with a bare fetch, then refactor it into a small reusable client with settings in environment variables, a clear ApiError for failures, and a safe way to switch to a real provider.
  3. 03Conversation and MemoryProve that a chat API is stateless, then build a small Conversation class that keeps the history, resends it every turn, sets a persona with a system prompt, trims old turns, watches the cost grow, and saves the chat to a JSON file.
  4. 04Streaming ResponsesShow the reply word by word as the model writes it: read the server-sent events format, fix the chunk-boundary bug that breaks a naive parser, build chatStream(), print tokens with process.stdout.write, read usage from the last event, and cancel with AbortController.
  5. 05A Command-Line ChatbotPut the client, the conversation and the streaming parser together into a real chatbot you can type to in the terminal, with slash commands, a token tally, error recovery, Ctrl+C to stop a reply, and a test that runs without a keyboard.
  6. 06API Keys and a Backend ProxyWhy an API key must never reach the browser, and how to build a small Node server that keeps the key secret, checks every request, limits abuse, streams replies through, and shows visitors only safe error messages.
  7. 07Errors, Retries and CostMake a chatbot survive real networks and real bills: read error codes, retry only what can succeed with backoff and jitter, set timeouts, track tokens against a hard budget, and show people friendly messages while logging safely.
  8. 08A Web Chat UIBuild the browser page for your chatbot: a form that streams the reply token by token from your backend proxy, renders it safely with textContent, supports a Stop button, handles errors, and then learn how to deploy and what to check before sharing it.