Home / AI Architecture & System Design / AI System Design
Design an autonomous coding agent that takes an issue and opens a pull request.
30-second answer
Per-task microVM sandboxes from warm snapshots (repo and dependencies), with no credentials and egress limited to a package proxy. The harness runs the loop outside the sandbox with search, read, edit (exact replace), run and todo tools; trims tool output; keeps the prompt prefix stable so ~90% of input is cached (~$1 instead of ~$4.40 per task); compacts rarely. Limits on steps, cost and time, loop detection, and the harness re-runs tests before finishing. A separate git service pushes the diff with a branch-scoped token and opens a PR for human review. Checkpoints allow resuming from review comments; a benchmark of past issues measures every change.
Try answering out loud before reading on. The walkthrough below is paced for a 40-minute interview round.
New to these terms? 10 terms on this page are explained in the glossary
0–5 min Functional requirements
A coding agent is a model in a loop with tools, but the system around it decides whether it's safe and useful: the sandbox, what the agent can reach, how context is managed over dozens of steps, and how its work gets reviewed.
You What's the input and output? An issue in, a pull request out?
Interviewer Yes. A developer assigns an issue to the agent; it should come back with a PR that passes the tests.
You Does it run in the developer's IDE, or in the cloud in the background?
Interviewer In the cloud, asynchronously. Developers check back later.
You Can it touch anything besides the repo: production, databases, secrets?
Interviewer No production access. It may need to install packages.
You Can developers give feedback on the PR and have the agent continue?
Interviewer Yes.
| # | Functional requirement | Why it matters |
|---|---|---|
| F1 | Accept a task (issue text, or a prompt) for a repository and branch | Entry point |
| F2 | Work autonomously: explore code, edit multiple files, run commands and tests | The core loop |
| F3 | Open a PR with a description of the change and test evidence | The output humans review |
| F4 | Continue from review comments on the PR | Real tasks need iteration |
| F5 | Live progress: plan, steps taken, commands run; ability to stop | Trust and debugging |
| F6 | Repository instructions (build commands, conventions) honoured | Quality on real repos |
Out of scope: IDE inline completion, merging without review, deploying.
5–10 min Non-functional requirements and estimates
| Requirement | Target | What it forces |
|---|---|---|
| Isolation | Agent code execution can't reach other tenants, production or secrets | Per-task microVM sandboxes, egress allowlist, credentials outside the sandbox |
| Task success | Measured on an internal benchmark of past issues with hidden tests; tracked per model and harness version | An eval harness built in from day one |
| Time to PR | Most tasks within ~30 min | Warm sandboxes with dependencies preinstalled |
| Cost | Cost cap per task (e.g. $5) and visible cost per merged PR | Prompt caching, context compaction, step limits |
| Durability | A task survives worker crashes and can resume days later on review feedback | Checkpointed transcript and workspace |
| Observability | Every model call, tool call and command recorded | Debugging and audit |
Back-of-envelope estimates:
- Volume: 50,000 tasks/day, ~20 min each → on average 50,000 × 20 / 1,440 ≈ 700 concurrent tasks; ~2,000 at peak. With 2 vCPU and 4 GB per sandbox, that's ~4,000 vCPUs at peak.
- Tokens per task: ~40 model calls; context grows from ~8k to ~60k tokens, averaging ~35k. Each call resends the whole context, so input totals 40 × 35k ≈ 1.4M tokens; output ~40 × 300 = 12k tokens.
- Cost per task at illustrative prices ($3 per million input tokens, $0.30 per million cached input tokens, $15 per million output tokens):
without prompt caching 1.4M × $3/M + 12k × $15/M ≈ $4.38
with ~90% cached 0.14M × $3/M + 1.26M × $0.30/M + 12k × $15/M ≈ $0.98
- Prompt caching is the biggest cost lever for agents, because each step appends to a long, unchanged prefix. The harness must keep that prefix byte-stable.
- Wall clock: 40 steps × (~6 s model + ~15 s average tool time, dominated by tests) ≈ 14 min.
10–18 min High-level design
You A control plane takes tasks and schedules them. Each task gets its own sandbox. The agent harness runs the loop outside the sandbox, calling the model and sending tool commands into the sandbox. Credentials and the git push stay outside, so nothing the agent runs can leak them.
Scroll sideways to see the whole diagram.
Components:
- Task API and scheduler: accepts tasks from the UI, an issue label or a PR comment; queues them with per-tenant concurrency limits; tracks state (
queued → running → pr_opened → waiting_review → done/failed). - Sandbox pool: microVMs (Firecracker, gVisor-style isolation) started from snapshots with the repo checked out and dependencies installed, so a task starts in seconds. Network egress limited to package registries and the git host.
- Agent harness: runs the loop: builds the prompt, calls the model, executes tool calls in the sandbox, truncates outputs, compacts context, enforces limits, records everything.
- Tools:
search(ripgrep),read_file(with line ranges),edit_file(exact string replace),run(shell with timeout),todo(the agent's own plan),finish(with a summary). - Model gateway: routes model calls, applies prompt caching, tracks tokens and cost per task.
- Checkpoint store: the transcript and a workspace snapshot after each step, so a crashed task resumes and a review comment days later continues from the same state.
- Git integration (outside the sandbox): takes the final diff, commits to a branch named for the task, pushes with a short-lived token scoped to that branch, opens the PR, and listens for review comments.
- Trace store and eval harness: every step logged; nightly runs on a benchmark of past issues.
One task:
Scroll sideways to see the whole diagram.
How it works: a worked example
A study aid: one real-looking task traced through the harness, step by step, with token counts and cost.
The task. Issue #482 in acme/billing: "CSV export drops or splits rows when a customer name contains a comma." A developer adds the agent label.
1. Queue and sandbox (~3 s). The webhook creates task t_9c1. The scheduler takes a warm microVM from the pool for acme/billing, restored from a snapshot built at main@a1b2c3 with dependencies installed. Without the snapshot, pip install alone would take ~3 minutes.
2. Initial prompt (~8k tokens).
system prompt: role, rules, tool definitions ~6,000 tokens
AGENTS.md from the repo (test command, style) ~1,500 tokens
issue #482 title and body ~400 tokens
3. The loop. Each line is one model call followed by one tool call:
1 search "csv" in billing/ → 14 matches (output trimmed to 2k tokens)
2 read_file billing/export/csv_writer.py:1-180
3 (model notices rows are built with ",".join(fields): no quoting)
todo: [write failing test] [fix writer] [run full suite]
4 run "pytest tests/export -q" → 12 passed (no test covers commas)
5 edit tests/export/test_csv.py → add test_name_with_comma
6 run "pytest tests/export -q" → 1 failed, as expected
7 edit billing/export/csv_writer.py → use csv.writer(..., quoting=csv.QUOTE_MINIMAL)
8 run "pytest tests/export -q" → 13 passed
9 run "pytest -q" → 1 failed: tests/reports/test_summary.py expects unquoted output
10 read_file tests/reports/test_summary.py:40-75
11 (model reasons: the expected string has a name with a comma; quoting is correct, test was wrong)
edit tests/reports/test_summary.py → update expected row
12 run "pytest -q" → 418 passed
13 run "ruff check . && mypy billing" → clean
14 run "git diff --stat" → 3 files changed, +24 −6
15 finish(summary="…")
4. Context and cost. Context grew from 8k to ~41k tokens; the 15 calls sent ~370k input tokens in total, ~93% of them cache hits, plus ~6k output tokens:
uncached input ~26k × $3/M ≈ $0.08
cached input ~344k × $0.30/M ≈ $0.10
output 6k × $15/M ≈ $0.09
total ≈ $0.27 wall clock ≈ 9 minutes
5. Pull request (~5 s). The harness copies the diff out of the sandbox. The git service, outside the sandbox, commits it to agent/482-csv-quoting, pushes using a token that can only push that branch, and opens a PR: what changed, why the report test changed, and the test output. CODEOWNERS are requested as reviewers. The agent can't merge.
6. Review feedback (two days later). A reviewer comments: "Also handle newlines inside names." The webhook resumes t_9c1: a new sandbox is restored from the step-15 snapshot, the transcript is reloaded (compacted to a summary plus recent steps), and the comment is appended as a new user message. Four more steps add a test and confirm csv.writer already handles newlines; the agent pushes a commit and replies on the PR.
Where each piece runs:
| Component | Runs as | Example technology | If it fails |
|---|---|---|---|
| Task API + scheduler | Stateless service + durable queue | Postgres-backed job queue, Temporal | Tasks wait in queue |
| Sandboxes | MicroVMs on a dedicated fleet | Firecracker, gVisor, Kata | Task resumes in a new sandbox from the last checkpoint |
| Harness | Worker process per running task, outside the sandbox | Python/TypeScript agent loop | Another worker resumes from the checkpoint |
| Model gateway | Internal proxy | LLM API with prompt caching | Retry with backoff; fallback model; pause the task |
| Checkpoints | Object storage | Transcript JSON + filesystem snapshot | Resume from an earlier checkpoint |
| Git integration | Service holding the GitHub App credentials | GitHub App with per-repo installation tokens | PR delayed; diff kept |
| Traces + evals | Log store and batch jobs | OpenTelemetry, warehouse, nightly benchmark | No impact on tasks |
18–32 min Deep dive
You I'd go deeper on the sandbox and security model, context management, keeping the agent on track, and evaluation.
Deep dive 1 · Sandbox and security
- Assume the agent will run hostile code. The repo, its dependencies and the issue text are all untrusted. An issue could contain a prompt injection telling the agent to upload secrets.
- So remove the secrets. The sandbox has no credentials. The git push and PR creation happen outside, from the diff. The model API key lives in the harness, also outside.
- Egress allowlist: package registries (through a caching proxy) and nothing else. This blocks exfiltration even if the agent is manipulated.
- Resource limits: CPU, memory, disk, process count and a wall-clock limit per command.
- Human review is the last gate. The agent opens PRs; CI and humans decide what merges.
Deep dive 2 · Context management
- Trim tool output. A test run can print 50,000 lines. Keep the head and tail plus failures, and tell the model how to read more (
read_filewith an offset). - Files are the memory. The agent doesn't need to keep whole files in context; it can re-read them. The
todotool keeps the plan explicit. - Compaction: when context passes ~70% of the window, replace older steps with a summary (what's done, what was learned, what's next) and keep recent steps verbatim. This resets the cache once, so do it rarely.
- Stable prefix: system prompt and tool definitions never change mid-task; new content only appends. That's what makes the 90% cache hit rate possible.
Deep dive 3 · Keeping the agent on track
- Limits: max steps (e.g. 100), max cost ($5), max wall clock (60 min). On hitting one, stop and report what was done.
- Loop detection: the same command with the same result three times, or no file changes in 15 steps → inject a "you seem stuck; reconsider" message, then stop.
- Verification before finishing: the harness runs the repo's test and lint commands itself after
finish; if they fail, the agent gets the output and continues (within limits). - Plan first for big tasks: for vague issues, the agent posts a plan and waits for a thumbs-up before editing.
Deep dive 4 · Evaluation and improvement
- Offline benchmark: a few hundred past issues from internal repos with the fix's tests hidden (SWE-bench-style). Resolved rate, cost and time per task for every change of model, prompt or tool.
- Online metrics: PR merge rate, review rounds per PR, share of PRs closed without merge, cost per merged PR.
- Trace review: sample failed tasks weekly; most fixes are in tools and prompts (e.g. a better
edit_fileerror message), not the model.
32–37 min Trade-offs
| Decision | Chosen | Alternative | Why |
|---|---|---|---|
| Where the harness runs | Outside the sandbox | Inside, with the code | Keeps model keys and git credentials away from untrusted code |
| Isolation | MicroVM per task | Containers on shared hosts | Stronger isolation for arbitrary code; similar start time with snapshots |
| Editing tool | Exact string replace | Whole-file rewrite, or unified diffs | Precise, cheap in tokens, clear errors when the match fails |
| Start-up | Warm snapshots with dependencies | Clone and install per task | Seconds instead of minutes |
| Context | Append-only with rare compaction | Rebuild the prompt each step | Keeps the cache prefix stable: ~4× cheaper |
| Autonomy | PRs with mandatory review | Auto-merge when tests pass | Tests don't cover everything; humans own the merge |
Build first: harness with the five tools, a container sandbox, PR creation, step and cost limits, traces. Then microVM snapshots, egress allowlist, checkpoints and resume-on-review, compaction, and the benchmark.
37–40 min Wrap-up and likely follow-ups
Close with: "Each task gets a microVM from a warm snapshot with no credentials and an egress allowlist. The harness runs the model loop outside the sandbox with search, read, edit, run and todo tools, trims tool output, keeps the prompt prefix stable for caching and compacts rarely. Limits and loop detection stop runaway tasks, the harness re-runs tests before finishing, and a separate service turns the diff into a PR that humans review. Checkpoints let the task resume from review comments, and a benchmark of past issues measures every change."
Likely follow-ups:
- Several agents in parallel on one task? Run N attempts in separate sandboxes and pick the one whose tests pass with the smallest diff; costs N× but raises the success rate.
- Sub-agents? A main agent can delegate searches or isolated sub-tasks to sub-agents with their own context, receiving only their summaries, which keeps the main context small.
- Private package registries? The caching proxy authenticates to them; the sandbox never sees the credentials.
Further reading
- SWE-bench: Can Language Models Resolve Real-World GitHub Issues?: the evaluation pattern used above
- SWE-agent: Agent-Computer Interfaces Enable Automated Software Engineering: why tool design (search, edit, trimmed output) matters
- Building effective agents (Anthropic): workflows, agents and when to use each
- On this site: Papers · Harnesses and Agent loop safeguards
What interviewers listen for
- A threat model: untrusted code and text, with credentials kept out of the sandbox.
- Context management: trimming, compaction, stable prefix and its cost effect.
- Limits, loop detection and verification before finishing.
- Durable, resumable tasks.
- An evaluation loop on real past issues.
Red flags
- Running the agent with the developer's own credentials or production access.
- Unlimited steps or cost.
- No plan for 50,000-line tool outputs.
- Auto-merging agent changes.
Discussion
Help us improve our content — give feedback. Spotted an error, have a better explanation, or got asked a follow-up in a real interview? Share it here.