Design AI Code Completion
Understanding the Problem
⌨️ What is AI code completion? AI code completion suggests the next few tokens, lines or a whole block of code as gray "ghost text" at the cursor while a developer types. Pressing Tab accepts it; typing anything else dismisses it. GitHub Copilot's inline suggestions, Cursor Tab and JetBrains' full-line completion are well-known examples. Under the hood a code language model predicts what belongs at the cursor, given the code before it, the code after it, and other relevant code from the project.
It looks like a small feature next to a coding agent, and it's a very different system. An agent runs for minutes, makes dozens of model calls and can afford a frontier model. Completion runs on almost every pause in typing, has a budget of a few hundred milliseconds, and most of its suggestions are never shown or are rejected. The design is dominated by latency, by cost per request at enormous volume, and by choosing context well in a tiny token budget.
We'll design it as a hosted service for a large population of developers using several IDEs.
Functional Requirements
Core Requirements
- While a developer types, the IDE should show a completion at the cursor: from the rest of a line up to a multi-line block (a function body, a test case).
- Completions should use the code after the cursor (fill-in-the-middle), not just the code before it.
- Completions should use context from the rest of the repository: other open files, imported modules, similar code, and symbols the language server knows about.
- The system should learn from accept and reject signals and from offline evaluation, and improve models over time.
Below the line (out of scope)
- Chat and agentic edits across files (see Design a Coding Agent).
- Training the base model from scratch, beyond the code-specific objectives that matter here.
- On-premises deployments in air-gapped environments, though we'll note what changes.
Non-Functional Requirements
Ask how many developers, how fast suggestions must appear, and whether code may be retained.
Core Requirements
- Latency: a suggestion should appear within about 250 ms at p50 and 500 ms at p90 from the moment the developer pauses, measured in the IDE. Later than that and the developer has typed past it.
- Scale: 1 million daily active developers.
- Quality: suggestions must be syntactically valid in context, and the product should show nothing rather than something bad. We'll track acceptance and retention, not just "was something shown."
- Privacy: customer code is used only to serve the request. It isn't retained or used for training unless an organization opts in. Secrets in the context are never sent to the model or logged.
- Cost: the cost per developer per month must be well under the subscription price.
Below the line (out of scope)
- Billing and seat management.
- Licensing policy beyond a filter for suggestions that match public code verbatim.
Back-of-the-envelope:
- A developer actively writes code for maybe 4 hours a day. After debouncing (waiting ~75 ms of no typing), the IDE sends a request on the order of 200 times an hour. That's 800 requests per developer per day, or 800 million a day for 1M developers: about 9,000 QPS on average and perhaps 25,000 QPS at the peak hour.
- A prompt is about 1,500-2,000 tokens of context, and a completion is short: 20-60 tokens.
- Prefill compute for a 7B-parameter model is about 2 x 7B x 2,000 = 2.8 x 10^13 FLOPs per request. At a realistic ~4 x 10^14 FLOP/s on an H100-class GPU, that's ~70 ms of GPU time. At 9,000 QPS, prefill alone would need on the order of 600+ GPUs before any caching.
- But consecutive requests from one developer share almost all their prefix, and a large share (often a third or more) are cancelled because the developer kept typing.
That arithmetic sets the design. A small model, prefix caching that exploits consecutive keystrokes, and cancelling stale requests are worth more than any quality trick, because they turn an impossible GPU bill into a reasonable one. Say this before you draw anything.