Design an AI Chatbot Like ChatGPT at Scale A developer published a system design for a ChatGPT-style chat service that targets sub-second time-to-first-token and sub-cent per-message cost at 100,000 daily users sending 500,000 messages a day. The architecture separates a stateless chat service from a Postgres conversation store, a memory service, a moderation classifier, a semantic cache, a model router, and a hosted LLM API with prompt caching, with every figure sourced from published prices or vendor documents and estimates labeled as such. ChatGPT serves more than 800 million people a week and answers over 2.5 billion messages a day. This part designs a system that can do the same job: take a message, remember the conversation, stream back a useful answer in under a second, and keep the bill under control. Every number below comes from a published price, a vendor document, or a public announcement, and every estimate is labelled as one. Build a chat service where millions of people talk to one large language model, each inside their own long conversation, with replies that start appearing in under a second and a cost per message measured in fractions of a cent. Functional. A user sends a text message and gets a streamed reply. Conversations persist across devices, so a chat started on a phone continues on a laptop. The system remembers facts the user shared earlier, moderates harmful input and output, and lets a user start a new chat, rename it, or delete it. Voice, files, and images are out of scope for version one. Non-functional. Time to first token under one second is the design target, measured per model rather than assumed. The service stays up when one zone fails. A single user cannot take the system down, so every user gets a token budget. Cost per message is tracked as a first-class metric, not discovered at the end of the month. Scale assumptions. We design for 100,000 daily users sending 5 messages each, which is 500,000 messages a day, with an average conversation of 20 turns and an evening peak three times the average. For reference, ChatGPT itself handles more than 2.5 billion messages a day, about 29,000 every second, and OpenAI reported processing over 6 billion tokens a minute on its API in October 2025. Our design must grow toward that shape without a rewrite. One request touches most of these boxes, but no box holds the whole conversation in memory. The chat service is stateless, so any instance can serve any user. All state lives in the conversation store, the memory service, and the cache, which is what lets the system scale by adding machines. Edge and API gateway. Terminate TLS, block abuse, enforce login and per-user limits before any expensive work starts. A chatbot is a public endpoint, and the first defence is cheap. Chat service stateless . The only component that talks to the user and the model. Stateless means it holds no conversation in memory, so it scales horizontally and a crashed instance loses nothing. All state is fetched per request. Conversation store. A database keyed by user and conversation, holding every message and a running summary. Postgres works here; the read pattern is one conversation at a time, not a global scan. Memory service. The model itself remembers nothing between requests. Memory is an app layer: saved facts, preferences, and summaries of past chats, pulled into each new request as context. OpenAI describes ChatGPT memory the same way, and warns it is for high-level preferences, not exact text. Moderation. A separate classifier that scores input and output for harm. It is free on OpenAI's API, and the cost of skipping it is a safety incident, not a saving. Semantic cache. A vector store of past questions and answers. Common questions repeat a lot in a chatbot, and a cache hit skips the model entirely. Treat hit rates quoted by cache vendors as claims, not measurements, and measure your own. Model router. Not every message needs the flagship model. A small model answers greetings, short factual questions, and reformatting for a fraction of the price. The router is a classifier plus a set of rules, and it needs its own evaluation, or it will quietly send hard questions to a weak model. LLM API and prompt cache. The hosted model, reached over HTTPS. The provider caches the longest matching prefix of the prompt, so a system prompt and conversation history sent on every turn is billed at the cached rate. On GPT-6.2 Sol, cached input costs $0.10 per million tokens against $2.00 uncached, a 95 percent discount. Caches are typically cleared after 5 to 10 minutes of inactivity and always within one hour. Compaction worker. A background job that summarises long conversations before they hit the context limit. Anthropic's approach is the model to copy: compact near the limit, keep decisions, open bugs, and implementation details, drop redundant tool output, and continue from the summary. Their automatic compaction triggers at 50,000 tokens by default and runs at 150,000. Observability. Time to first token, tokens per message, cache hit rate, moderation flag rate, and cost per message, all on one dashboard. If cost per message is not measured, it will surprise you. GPT-6.2 Sol offers a 1.05 million token context window, which sounds like the problem is solved. It is not. A token is about four characters, or about three quarters of a word, so a 20-turn conversation of 150 words a turn is only about 4,000 tokens, but long sessions grow, tool output piles up, and Anthropic's own engineers warn that as context grows, the model's ability to recall information from it falls. Their rule is to send the smallest set of high-signal tokens that still answers the question. The working rule is simple. Each request sends three things: the system prompt, a compact summary of the conversation so far, and the last few turns in full. When the token count crosses a threshold, the compaction worker writes a fresh summary and the old turns are dropped from the request. The full history stays in the store for the user to scroll back through, but the model never sees all of it at once. Sending the full history is the simplest design and the most expensive one. Every turn re-sends every past token, and the bill grows with the square of the conversation length. The alternative is aggressive summarisation, which is cheaper but loses detail, and a bad summary can make the model forget something the user cares about. Decision: cap the working history and compact. Keep the last 10 turns verbatim plus a summary, and rewrite the summary every 20 turns. The full history stays in storage, so a user can always read it, but the model is only billed for what helps the current answer. Streaming makes the reply feel instant, but it makes output moderation harder, because a partial reply is difficult to judge. Waiting for the full reply before showing anything is safest and feels slow. OpenAI's own streaming guide warns about exactly this tension. Decision: moderate the input before the model call, always, and stream the output. Input moderation is free and fast, and it stops the worst abuse before any generation happens. Output is checked on the completed reply for logging and flagging, and high-risk accounts get a short delay buffer instead of live streaming. Speed for normal users, control where it matters. One model for everything is simple to build and easy to reason about. It is also the most expensive option, because the flagship answers greetings at flagship prices. A router sends easy turns to a small model: on OpenAI's current pricing, the small GPT-6 Luna costs $0.10 in and $0.50 out per million tokens, against $2 and $10 for Sol. Decision: route. The saving is large enough to justify the extra component, but the router gets a held-out evaluation set and a weekly error review, because a router that misroutes is worse than no router at all. Every layer scales on its own. The chat service is stateless, so add instances behind the load balancer. The conversation store is sharded by user id, so one user's chats live on one shard. The semantic cache is a Redis cluster, and compaction runs on a queue that absorbs bursts. The hard limit is the provider's rate limit: tokens per minute, not requests per minute. On OpenAI's Build tier the limit is 5,000 requests and 1,000,000 tokens per minute, and the token limit binds first, because one chat request carries thousands of tokens. At ChatGPT's scale of 29,000 messages a second, the same shape holds, with more shards, more regions, and request queues in front of the model. Nothing in this design needs a single central machine. The first thing to break is the token budget. On the Build tier, 1,000,000 tokens a minute supports only about 400 full chat requests a minute, because each request carries roughly 2,500 tokens of input and output. The fix is to move up the tiers, set a per-user token budget, and make the cache hit rate a team metric. The second break is context in very long chats, fixed by compaction before the window fills, not after. The third is cost runaway from long outputs, fixed by a per-reply output cap and by routing easy turns to the small model. This is a worked estimate built only from published prices, using GPT-6.2 Sol at $2 per million input tokens, $0.10 cached, and $10 per million output tokens. Two notes on the numbers. Output tokens cost five times input tokens, so the biggest lever on the bill is shorter answers and the small-model router, not shorter histories. And GPT-5 and GPT-4o are being retired from the OpenAI API, GPT-5 on 11 December 2026 and GPT-4o on 14 January 2027, which is why this design prices the current GPT-6.2 family instead of the older flagships. Time to first token per model, tokens per message, prompt cache hit rate, semantic cache hit rate, moderation flag rate, router error rate, and cost per message. A chatbot that does not measure cost per message will learn its cost from the invoice. Originally published on LogicStack https://logicstack.org/blog/design-ai-chatbot-like-chatgpt . Run the code examples right in your browser with our free interactive Python https://logicstack.org/python-playground and SQL https://logicstack.org/sql-playground playgrounds — no setup, no sign-up.