Asked at
URL Shortener (TinyURL)
Asked from Junior level upFoundations & Storage
Distributed Rate Limiter
Asked from Mid level upAPI Gateway & Security
Distributed Key-Value Store
Asked from Senior level upFoundations & Storage
Distributed Notification Service
Asked from Mid level upMessaging & Queues
Distributed Web Crawler
Asked from Mid level upDistributed Systems
Metrics & Monitoring Platform
Asked from Senior level upObservability & Time-Series
Real-Time Chat App (like WhatsApp or Slack)
Asked from Mid level upReal-Time & WebSockets
Social News Feed (like X/Twitter)
Asked from Mid level upSocial & Feed Systems
Ride-Hailing App (like Uber)
Asked from Senior level upGeospatial & Real-Time
Low-Latency Stock Exchange Matching Engine
Asked from Staff level upFintech & Low Latency
Global Video Streaming Platform (like YouTube or Netflix)
Asked from Senior level upMedia & Streaming
Distributed Hotel & Ticket Reservation System
Asked from Mid level upE-Commerce & Transactions
Shared Document Editor (like Google Docs)
Asked from Staff level upReal-Time & Distributed State
Game Leaderboard
Asked from Junior level upCaching & Data Structures
File Sync Service (Dropbox / Google Drive)
Asked from Mid level upStorage
Payment System
Asked from Senior level upPayments & Transactions
Ad Click Aggregation
Asked from Senior level upStream Processing
AI Support Assistant (RAG)
Asked from Senior level upAI Systems
LLM Inference Platform
Asked from Senior level upAI Systems
Enterprise Semantic Search
Asked from Mid level upAI Systems
AI Code Completion
Asked from Senior level upAI Systems
LLM Gateway
Asked from Senior level upAI Systems
AI Agent That Takes Actions
Asked from Senior level upAI Systems
LLM Evaluation Platform
Asked from Senior level upAI Systems
AI Document Processing Pipeline
Asked from Mid level upAI Systems

Design AI Code Completion

Asked from Senior level upEmerging topicPart of the AI track: start with AI basicsDomain: AI Systems
Concepts to know first:

Suggest the next lines of code as a developer types. It must feel instant and be cheap enough to run on every pause.

An analogy

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.

Where this design ends up. This is version 3 of 3. Part 5 builds it up from version 1 and says why each part was added.
  1. Editor
    • Editor plugin
  2. Gateway
    • Nearest region gateway
  3. Routing
    • Sticky router
  4. Model
    • Small code model
  5. Telemetry
    • Telemetry

1. Requirements and scope

Functional Requirements
  • 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.
Non-Functional Requirements & SLAs
  • 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.
Key Traffic Assumptions: 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.

2. Back-of-the-envelope numbers

Write Throughput
Telemetry: one shown or accepted event per suggestion, batched in the editor
Read Throughput
200,000 × 1,000 = 200M requests a day ÷ 86,400 ≈ 2,300/s on average, about 4,600/s at peak (twice the average)
Storage Growth
No code is stored by default; acceptance events ≈ 200M/day × 100 B ≈ 7 TB a year
RAM Cache Footprint
When the router sends a developer to the same GPU each time, their recent context is often still in its KV cache. Then only the tokens after the first changed line are read again

3. API

POST/v1/completeBody: prefix, suffix, language and related snippets. The suggestion streams back, and the editor cancels it if the developer types
POST/v1/telemetry{ suggestionId, shown, accepted, charsKept } in batches
GET/v1/settingsThe company's model, privacy and on/off settings

4. Data model and storage

Storage Strategy: The service keeps almost no state. Company settings live in a relational database, and telemetry goes through a queue into a warehouse.
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.

Editor pluginGatewayLarge chat model123
  • The order of the steps
  • A request or its reply

The parts: switch one off

1. Editor

Editor pluginSends every keystroke

2. Gateway

GatewayCompany settings

3. Model

Large chat modelSeconds per answer
Example figures
Time budget
2–5 s

The steps in words0 of 3 steps shown

  1. Editor to GatewayHTTPS · 50ms

    POST the whole file after a keystroke

  2. Gateway to ModelgRPC · 3000ms

    Reads the whole file from scratch

  3. Model to EditorStream · 30ms

    Arrives after the developer has moved on

Why this version
A demo in a day.
Next bottleneck
Answers take seconds, most are out of date by the time they return, and every keystroke pays for a large model.

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

  1. Editor plugin
  2. Telemetry
  3. Model tests
  4. Sticky router
  1. 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.

  2. Telemetry to Model tests

    Keep rates per language feed the tests that each new model version must pass.

  3. Model tests to Sticky router

    A version that is kept less often does not reach more developers.

Read pathSuggest code at a pause

  1. Editor plugin
  2. Gateway
  3. Sticky router
  4. Code model
  1. 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.

  2. Gateway to Sticky router

    The gateway in the nearest region checks the key and passes the request on.

  3. 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.

  4. 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

The 300 ms budget
Spend about 75 ms waiting for the pause, 50 ms on the network and 100 ms to the first token. That leaves a little room. A small model, cached context and a nearby region together make it fit.
Measuring usefulness
Count suggestions shown, suggestions accepted, and characters still in the file a minute later. A model that writes longer suggestions can lower acceptance even when each line is good.

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.

How each level is judged
What is judgedJuniorMidSeniorStaff
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

Strong answer
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?
Weak answer
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

Strong answer
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.
Weak 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

Strong answer
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.
Weak answer
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

Strong answer
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.
Weak answer
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

Strong answer
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.
Weak answer
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.]