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.
Key takeaways
- The model is stateless. Memory, history, and safety are separate services built around it.
- Output tokens cost five times input tokens on GPT-6.2 Sol, so long answers dominate the bill, not long histories.
- Prompt caching cuts the input bill by about half in our worked example, because the system prompt and history are the same on every turn.
- Streaming over SSE is what makes replies feel instant. The first token matters more than the last one.
- The binding limit at scale is tokens per minute, not requests per minute.
The problem in one line
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.
Requirements
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.
High-level architecture
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.
The request path, step by step
- The user message arrives over HTTPS and passes the edge layer, which blocks bots and bad traffic.
- The API gateway checks the login token and the per-user rate limit, then routes to a chat service instance.
- The chat service runs the message through the moderation API. OpenAI's moderation model is free, so there is no cost reason to skip this step.
- The service checks the semantic cache: has a near-identical question been answered before? A hit returns the stored answer in tens of milliseconds and the model is never called.
- On a miss, the service loads the conversation: a short summary, the last few turns, and any saved facts from the memory service. It never sends the full raw history.
- The model router picks a model. Simple turns go to a small cheap model, hard turns to the flagship.
- The LLM API streams tokens back over server-sent events. The service forwards each token to the user as it arrives.
- The finished reply is saved to the conversation store, and the summary is updated in the background.
Each component and why it exists
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.
Context is the hard part
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.
Tradeoff 1: remember everything vs pay for it
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.
Tradeoff 2: stream now vs moderate first
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.
Tradeoff 3: one flagship model vs a router
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.
How it scales
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.
What breaks first, and the fix
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.
What it costs
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.
- Assumptions: 100,000 daily users, 5 messages each, 500,000 messages a day. Each message carries 2,000 tokens of history and system prompt, mostly cached, plus 200 new input tokens and a 300-token reply.
- Per message: cached input 2,000 tokens at $0.10 per million is $0.0002. New input 200 tokens at $2 per million is $0.0004. Output 300 tokens at $10 per million is $0.003. Total $0.0036 per message.
- Per day: 500,000 messages at $0.0036 is $1,800, about $54,000 a month.
- Without prompt caching the same message costs $0.0074, or $3,700 a day. Caching cuts the bill roughly in half.
- Peak load: 500,000 messages a day is about 6 a second on average, and about 17 a second at a three-times evening peak. A 300-token reply streaming at an assumed 50 tokens a second takes 6 seconds, so the peak needs about 100 replies generating at once. That is a small fleet of stateless servers, not a supercomputer.
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.
What we would measure
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. Run the code examples right in your browser with our free interactive Python and SQL playgrounds — no setup, no sign-up.