Design AI Code Completion
Suggest the next lines of code as a developer types. It must feel instant and be cheap enough to run on every pause.
Like a friend reading over your shoulder who finishes your sentence when you pause. They must be quick, or you have already typed it. They must also stop talking the moment you start typing again.
Where this analogy stops working: A friend knows what you are building. The model sees only the code it is sent, so it can suggest code that looks right but does not run.
- Editor
- Editor plugin
- Gateway
- Nearest region gateway
- Routing
- Sticky router
- Model
- Small code model
- Telemetry
- Telemetry
1. Requirements and scope
- Show a grey suggestion for the next few lines when the developer pauses typing.
- Use the code before and after the cursor, plus related open files.
- Accept with Tab; typing anything else hides the suggestion.
- Let each company turn off storage of its code.
- A suggestion within about 300 ms of the pause, or the developer has moved on.
- Cost per suggestion low enough to run on almost every pause.
- Never keep or train on code without the company's consent.
- Measure usefulness by how many suggestions are kept.
2. Back-of-the-envelope numbers
3. API
4. Data model and storage
ORG_SETTINGS(org_id, model, store_code, enabled) · SUGGESTION_EVENT(id, org_id, language, shown_at, accepted, chars_kept, latency_ms)
5. The design, in three versions
Version 1: Call a large chat model on every keystroke
The editor sends the whole file to a large hosted model after each keystroke and shows whatever comes back.
Completion v1: A Large Model on Every Keystroke Version 1 of 3
The picture follows one request through the parts of this version, in order. Under it, switch a part off to see what the request does without it.
One request, step by step
Each numbered arrow is one step. Press "Simulate Request Flow" to follow them one at a time.
- The order of the steps
- A request or its reply
The parts: switch one off
1. Editor
2. Gateway
3. Model
Example figures
- Time budget
- 2–5 s
The steps in words0 of 3 steps shown
Editor to Gateway
POST the whole file after a keystroke
Gateway to Model
Reads the whole file from scratch
Model to Editor
Arrives after the developer has moved on
Write path, read path and failures
Each path follows one request through the final design (version 3), one hop at a time.
Write pathLearn from what developers keep
- Editor plugin
- Telemetry
- Model tests
- Sticky router
Editor plugin to Telemetry
The plugin reports whether each suggestion was kept, changed or ignored. The code itself is not sent unless the company agreed.
Telemetry to Model tests
Keep rates per language feed the tests that each new model version must pass.
Model tests to Sticky router
A version that is kept less often does not reach more developers.
Read pathSuggest code at a pause
- Editor plugin
- Gateway
- Sticky router
- Code model
Editor plugin to Gateway
After a short pause in typing, the plugin sends the code around the cursor and parts of related files. Typing again cancels it.
Gateway to Sticky router
The gateway in the nearest region checks the key and passes the request on.
Sticky router to Code model
The router sends this developer to the same GPU as last time, so the earlier context is still cached there.
Code model to Editor plugin
The small model streams a short suggestion, and the plugin shows it as grey text.
What breaks, and what happens then
The developer keeps typing
- What users see
- A suggestion would arrive for code that has changed.
- What the design does
- The plugin cancels the old request, and the server stops work on it.
The developer's usual GPU is busy or gone
- What users see
- The cached context is lost, so the next suggestion is slower.
- What the design does
- The router picks another GPU, which builds the context again from the prompt.
The region is far from the developer
- What users see
- Network time alone uses up the 300 ms goal.
- What the design does
- Serve from several regions near developers.
6. Deep dives
7. Follow-up questions
- What happens to GPU work when the developer keeps typing? (The editor cancels the request, and the server stops generating at once, freeing that slot in the batch.)
- A company does not allow its code to leave its region. What changes? (Serve it from a region inside its borders, store no code, and keep only counts in telemetry.)
What it costs
The request count is the cost: about 2,300 a second, and many suggestions are never accepted. Wait for a pause before asking, cancel a request when the developer types again, and use a small model.
Another way to design it
A small model can run on the developer's own machine. Nothing leaves the laptop, and there is no server bill. The suggestions are weaker, and every laptop needs enough memory.
8. How answers are judged
A passing answer at Mid level. A complete design with numbers: about 2,300 requests a second, about 5,000 at peak, each within about 300 milliseconds. It explains waiting for a pause and cancelling stale requests.
| What is judged | Junior | Mid | Senior | Staff |
|---|---|---|---|---|
| BreadthHow much of the system does the answer cover? | Names the core parts: clients, servers, a database and a cache. | Covers the whole main flow, from the request to the stored data and back. | Builds a complete design without being led through it. | Covers the system, the systems around it and who owns each part. |
| DepthHow far does it go into the hard parts? | Explains what each part does and why it is there. | Goes deep on one part, such as the database or the cache. | Goes deep on the two or three riskiest parts. | Goes deepest where the risk is largest, and explains why the risk is there. |
| DrivingWho leads the conversation? | Follows the interviewer, step by step. | Moves forward alone, with an occasional nudge. | Leads the conversation from start to finish. | Changes the problem when the question asked is not the right one. |
| Trade-offsAre choices compared and justified? | Names more than one option. | Says why one option was picked over another. | Compares options in detail, with what each one costs. | Ties each choice to cost, speed and risk for the business. |
| Unclear questionsWhat happens when the question is vague? | Answers the questions that are asked. | Asks questions to make the problem clear. | Sets the scope and says what is left out. | Questions the assumptions behind the problem itself. |
| ScopeWhat does it consider beyond the normal path? | Covers the normal path, when everything works. | Adds what happens when a part fails. | Adds growth: what breaks at ten times the load, and the fix. | Adds abuse, cost and the daily work of running the system. |
Requirements and scope
Before I design, I want to know the budget. A suggestion that arrives after the developer has typed on is wasted, so I will assume about 300 milliseconds from the pause. Can company code be stored, or must nothing leave the request? How do we count a suggestion as useful?
The best suggestions come from the biggest model. I will use the largest model we can get, even if answers take a few seconds.
Shows: Unclear questionsThe difference: A slow suggestion is never seen, because the developer has already typed the line. Speed decides usefulness before quality does.
Estimates
200,000 developers making about 1,000 requests a day is 200 million requests, about 2,300 a second and 5,000 at peak. Each sends about 2,000 tokens of context but gets back only about 30, so reading the context uses more GPU work than writing the answer.
Developers type about 300 lines a day, so one request per line is about 60 million requests a day.
Shows: DepthThe difference: Requests follow pauses, not finished lines, so there are several per line. Counting by lines gives 60 million, about three times too few.
Core design
The editor waits for a short pause, then sends the code before and after the cursor, so a small code model fills in the middle. Answers stream, so the first line shows quickly. When the developer types again, the editor cancels the request and the server stops generating.
The editor sends a request on every keystroke and shows whichever answer comes back last.
Shows: DepthThe difference: Most of those requests are stale before they return, yet each still uses GPU time. Waiting for a pause and cancelling stale work saves both.
Deep dive
The hard part is the 300 milliseconds. About 75 go to waiting for the pause and 50 to the network. I would aim for about 100 for the first token, which leaves about 75 spare. I would send each developer to the same GPU, so the context processed last time is still cached.
I would spread every request evenly across all GPUs with round robin, so the load stays balanced.
Shows: DepthThe difference: Round robin sends the next request to a GPU that has none of this developer's context cached, so it rereads about 2,000 tokens every time.
Failures and trade-offs
If the model is slow or down, the editor shows nothing, since no suggestion is better than a late one. If one region fails, editors move to the next nearest region and accept slower answers. I would alert when suggestions kept per developer drop.
If the model is slow, the editor waits for the answer and shows it whenever it arrives, even a few seconds later.
Shows: Trade-offsThe difference: A late suggestion lands on code the developer has already changed. Showing nothing keeps the editor calm, and the next pause tries again.
We wrote this guide in our own words, informed by public interview guides such as AlgoMaster and Hello Interview. We are not connected to either site. Interviewers differ. The guide and the example answers show the direction each level takes, not a checklist.
10. Check your design with your own AI
Write your design first. Then copy this prompt into the AI chat you already use, and add your design at the end. It asks for a score in each area, one fix for each, and two follow-up questions.
The site sends nothing anywhere. An AI review can be wrong, so check what it says against sections 8 and 9.
See the prompt
You are a system design interviewer. Review my design for the interview problem below. Problem: Design AI Code Completion Suggest the next lines of code as a developer types. It must feel instant and be cheap enough to run on every pause. What users must be able to do: - Show a grey suggestion for the next few lines when the developer pauses typing. - Use the code before and after the cursor, plus related open files. - Accept with Tab; typing anything else hides the suggestion. - Let each company turn off storage of its code. How well it must work: - A suggestion within about 300 ms of the pause, or the developer has moved on. - Cost per suggestion low enough to run on almost every pause. - Never keep or train on code without the company's consent. - Measure usefulness by how many suggestions are kept. Numbers to assume: - 200,000 active developers, each causing about 1,000 suggestion requests a day. - Each request sends about 2,000 tokens of context and gets back about 30 tokens. - The busiest hour is about twice the average. Judge my design at the Mid level. A passing answer at this level: A complete design with numbers: about 2,300 requests a second, about 5,000 at peak, each within about 300 milliseconds. It explains waiting for a pause and cancelling stale requests. For each area below, say whether my design is strong, fine or weak. Give one reason and one fix for each: 1. Requirements and scope 2. Estimates 3. Core design 4. Deep dive 5. Failures and trade-offs Then ask me the two follow-up questions an interviewer would ask next. Do not write a full model answer for me. My design: [Describe your parts, how one request moves through them, and why you chose each one.]