# Design a Code Assistant

> How to design a code assistant that finds the right code, edits safely and stays fast and affordable, explained simply for your AI interview.

**In short:** A code assistant finds the few files that matter, asks a model for a change, checks it by running tests in a sandbox, and shows a diff that a person accepts. Context, speed, safety and cost decide the design.

AI and LLM interview question Q349, LLM system design. Advanced, common (our estimate). From Venkat R. Murty's free Interview Kit: https://designervenkat.online/interview/ai/design-a-code-assistant

## Say it in 30 seconds

A code assistant reads the parts of a codebase that matter, asks a model for a change, runs the tests in a locked sandbox, and shows a diff for a person to accept. The hard parts are picking the right code within a fixed context budget, keeping outside text from giving orders, and keeping answers fast and cheap. I'd start with simple search and a strict permission model, then measure it on real past bugs.

## Say it in 2 minutes

Start by asking what kind of assistant this is. Inline completion has to answer in well under a second, so it sees only the code near the cursor and uses a small, fast model. A chat question can take a few seconds and needs more context. An agent that edits several files and runs tests can take minutes, so it needs approvals and a sandbox. I'd build one shared context builder for all three, with different time limits.

The biggest problem is context. A repository is far too large for one prompt, and a model recalls less as the prompt grows. So I build a map of function names and files once, search on demand, and pack the strongest files into a fixed budget, with the most relevant ones at the start and end. Every call costs money and time, so the stable part of the prompt, the rules and the map, goes first, where caching can reuse it.

Edits go in as diffs on a scratch branch. Tests and checks run in a sandbox that can only touch the repo and approved hosts. Anything from the repo, such as comments or issue text, is treated as data, because it can carry instructions. Pushes, deletes and merges need a person.

To know it works, I replay real past bugs with known fixes and track whether the tests pass, how often people accept the diff, and how often it is reverted. Then I roll out read-only first, and widen access only when those numbers hold.

## What is a code assistant, in plain words?

A code assistant is a program that reads a codebase, answers questions about it, and proposes changes that a developer then reviews. Three kinds are common. An inline one finishes the line you are typing, in well under a second. A chat one answers questions about the code. An agent takes a ticket, edits several files, runs the tests and reports back. A repo, short for repository, is all the code for one project.

Think of a new engineer on a payments team. They don't read all 900 files. They search for the refund code, open the files that call it, check the tests and send a small change for review. A code assistant does the same walk in seconds. It can also forget what it read if the file list is too long.

> "Claude Code is an agentic coding tool that lives in your terminal, understands your codebase" (Anthropic, Claude Code README)

Two real examples are Claude Code from Anthropic and Codex CLI from OpenAI. Codex CLI runs on the developer's own computer.

- **Token:** A small chunk of text, often part of a word, that a model reads and bills for.
- **Context window:** The text a model can see in one call. It is limited, and every token costs money.
- **Diff:** The before and after of a change, line by line, so a person can review it.
- **Sandbox:** A locked box the tool runs in, so a bad command can't touch the rest of the machine.

## How does it find the right code?

A repository is far too big for one prompt, and a bigger window only goes so far. Anthropic's engineers describe the trade-off this way:

> "as the number of tokens in the context window increases, the model's ability to accurately recall information from that context decreases" (Anthropic, context engineering guide)

So treat context as a budget. A prebuilt map of function names and files is fast to read but can go stale. Search on demand reads the live code. Anthropic describes this "just in time" style as avoiding stale indexing, at the cost of slower runtime exploration.

Research favours retrieving the right code over more code. RepoCoder, a 2023 study, uses iterative retrieval and generation, and it improves on the in-file baseline by over 10% in all the settings it tested.

Position matters too. A 2023 study, Lost in the Middle, found that performance can degrade significantly when the relevant information moves within a long input. Performance is often highest at the beginning or end. That study used documents, not code, so treat the placement advice as a guide.

1. **Map the repo once:** Index the names of functions and classes, the files, and who calls what. Refresh the map when files change.
2. **Search for the task:** Match the ticket's words and error messages against files, and use meaning search where the words differ.
3. **Read only what matters:** Open the files the matches point to, plus their tests and recent edits.
4. **Pack to a fixed budget:** Fill a set number of tokens, strongest files at the ends, and leave out the rest.

*Diagram, Packing the right code into the prompt: The repo is too big to send whole. A map and a search find candidates, and the packer fills a fixed budget with the strongest files. The rest is dropped.*
1. The map is built once and refreshed when files change.
2. Search runs only when the task needs it.
3. The map's best matches are packed in until the budget is full.
4. The search's best matches are packed in the same way.
5. The packed files go to the model, strongest at the ends.
6. Files that don't fit are left out, not squeezed in.

Take a made-up refund bug and a budget of 8,000 tokens. The packer keeps the three most relevant files, about 7,300 tokens, and drops the big legacy export, which alone would use more than half the budget.

## How do edits get applied, and how do you keep it safe?

The model proposes a diff, and the tool applies it to a scratch branch, runs the tests and a lint check (a tool that flags messy or risky code), and shows the result. If the tests fail, the error goes back to the model. I'd cap it at three tries, which is my own design choice.

*Diagram, One change, from ticket to review: A ticket goes in, and the model and its tools loop until the tests pass or the tries run out. A person then sees the diff. The one loop back is the model reading each tool result.*
1. The ticket goes in, and the assistant starts searching.
2. The best matching code, tests and recent edits are packed in.
3. The model asks to read a file or run the tests.
4. The tool's result goes back so the model can try again.
5. The diff and its test result go to a person.

Assume the text in the repo can be hostile. A comment, a README or an issue title can carry an order, and the model may follow it. OWASP's 2026 list of LLM risks names a database row or an issue title as a way in for prompt injection.

In its October 2025 sandboxing post, Anthropic said Claude Code is read-only by default, and asks for permission before changes or commands. A sandbox gives fixed walls instead: filesystem isolation limits the tool to specific directories, and network isolation limits it to approved servers. In Anthropic's internal use, sandboxing cut permission prompts by 84%.

Keep secrets out of the packer. Passwords, keys and .env files (files that hold secrets) go on a deny list, and the model never holds keys. OWASP says to hold credentials and state-changing power in application code, not the model, with least privilege per operation.

## How do you keep it fast, cheap and tested?

Each call costs money and time, so the design uses three levers: a small model for inline help, short context for it, and a cache for the stable part of the prompt. Anthropic's caching guide says prompt caching works by letting the API resume from specific prefixes in your prompts. So put the rules and the repo map first and the ticket last.

A design target I'd set, with made-up numbers: 95% of inline suggestions back in under a second, and a monthly cost cap per developer that alerts before it is reached.

Build an offline test set from your own past bugs, each with a known fix and its test. Replay each ticket, let the assistant try, and count how many fixes pass. Then track what happens after a merge: how often people accept the diff, how much they edit it first, and how often it is reverted within a week.

A passing test only shows the checks pass. The team still has to want the change. Log what was packed for each run too, since a better model can't fix a file it never saw.

## What does the interviewer want to hear?

**A strong answer covers**

- [ ] A plain picture: the model proposes, tools act, and a person approves.
- [ ] Which kind of assistant it is, inline, chat or agent, and why the limits differ.
- [ ] Context as a budget: map, search, read just in time, then pack.
- [ ] Edits land in a branch, checked by tests in a sandbox.
- [ ] Repo text treated as untrusted input, with a deny list for secrets.
- [ ] Evaluation on past real bugs, plus speed and cost targets.

**Common traps**

- Trap: "Put the whole repo in the prompt." Better: A big window still loses detail. Retrieve the few files that matter and pack them to a budget.
- Trap: "Let the agent push straight to main." Better: Edits land in a branch, checks run in a sandbox, and a person merges.
- Trap: "A bigger model fixes it." Better: A stronger model can't use code that never reached the prompt, and it can't undo permissions you gave it.

## Can you spot the risk?

**Spot the risk:** Which setting is the risky one for an agent on a payments repo?

A. write: repo folder only, network: package mirror only
B. run: sandbox, push: needs a person's approval
C. write: whole home folder, network: any host, read: .env files
D. edits: feature branch, merge: after tests and review

**Answer:** C. The third line lets the agent write anywhere, reach any host and read secret files, so one hostile comment could leak keys. The others limit writes or put a person on pushes.

**Quick check:** A developer reports a bug in a 900-file service. What should the packer send first?

A. The whole 900-file repo, so nothing is missed
B. The failing test, its code path and recent edits to both
C. The README, which describes the project in general
D. The 20 largest files, so the model sees the most code

**Answer:** B. The failing test and the code it calls are the strongest signal. Whole repos bury detail, a README is too general, and the largest files are not the ones the bug lives in.

## Can you answer these from memory?

**Flash cards**

- **What is a code assistant?** A tool that reads a codebase, proposes changes and checks them with tests, with a person approving.
- **Why not send the whole repo?** Context is finite. Recall drops as the prompt grows, and the middle of a long prompt is used least well.
- **What is just-in-time retrieval?** The agent searches and opens files when it needs them, not all up front.
- **What does a sandbox limit?** The directories and hosts a command can touch, so fewer prompts are needed.
- **Why cache the prompt prefix?** Stable rules and the repo map come first, so the API can reuse them and cut cost and latency.
- **How do you measure it?** Replay past real bugs with known fixes. Track passing tests, accepted diffs and reverts.

## Likely follow-up questions

- Q352 Design a work copilot that never leaks data the user can't access. https://designervenkat.online/interview/ai/design-a-work-copilot-that-respects-permissions
- Q353 Design an LLM gateway. https://designervenkat.online/interview/ai/design-an-llm-gateway
- Q356 Design rate limiting. https://designervenkat.online/interview/ai/design-rate-limiting
- Q357 Scale to millions of users. https://designervenkat.online/interview/ai/scale-to-millions-of-users
- Q189 How do coding agents work? https://designervenkat.online/interview/ai/how-do-coding-agents-work

## Learn it in depth

- [AI coding tools](https://designervenkat.online/learn/ai-engineering/ai-coding-tools)
- [Context Engineering](https://designervenkat.online/learn/ai-engineering/context-engineering)
- [Agentic AI security](https://designervenkat.online/learn/ai-guardrails/agentic-ai-security)
