Design a platform that runs AI agents which call tools on behalf of users
An agent platform runs language-model agents that take multi-step actions with tools: search, read documents, call APIs, write and run code, send messages. The model decides what to try next, but the platform must decide what is allowed. The hard parts are durability of long runs, security against prompt injection, approvals for risky actions, cost control and making agent behaviour observable and testable.
AI & Machine Learning, 10 components, about 50 min
1. Scope
In scope
- • The agent loop and its state
- • Tool registry, authorization and sandboxing
- • Human approval for risky actions
- • Budgets and caps
- • Tracing and evaluation
Out of scope
- • Training the model
- • Building each third-party integration
- • End-user chat UI details
2. Functional requirements
Must have
- • Start an agent run with a goal and a set of allowed tools
- • Stream progress (steps, tool calls, partial answers) to the user
- • Execute tool calls with the user's permissions only
- • Pause for human approval on actions that send, pay or delete
- • Resume after crashes without repeating completed side effects
Nice to have
- • Scheduled and background runs
- • Sub-agents working in parallel
- • Replay a run for debugging
3. Non-functional requirements
4. Back-of-envelope estimation
Assumptions
- • 1M agent runs per day, 12 steps on average
- • Each step sends ~8K input tokens (growing context) and gets ~300 output tokens
- • About 10 tool calls per run; 20% of runs execute code
- • Checkpoint ~20 KB per step
Derived numbers
5. API design
Body { goal, tools[], budget } → { run_id }; progress over SSE
Approve or reject a pending action (bound to its exact arguments)
Stop the run at the next step boundary
Every step: prompt hash, tool calls, results, tokens, cost
6. High-level design
What each box in the reference diagram is responsible for:
7. Data model
8. Deep dives
The model asks, code decides
The model only proposes tool calls. Before any call executes, the policy engine checks it in code: is this tool allowed for this run, does this user have rights to this resource, are the arguments within rules (for example email only to the user's own domain). Tools run with the user's scoped credentials, so even a fully fooled model cannot reach what the user cannot.
Prompt injection is a capability problem
Any text the agent reads (web pages, emails, documents, tool output) can contain instructions. Filtering helps but is not a boundary. Limit damage instead: narrow tools, no open network from the sandbox, approvals bound to exact arguments for anything that sends, pays or deletes, and never let untrusted content choose a destination for data. Test by planting instructions in the channels they would really arrive through.
Durable execution
Runs last minutes to hours and must survive deploys. Model each step as an activity in a durable workflow engine (Temporal-style) or checkpoint after each step. Tool calls with side effects carry an idempotency key derived from (run_id, step), so a replay after a crash does not send the same email twice.
Budgets and caps
Every run has hard limits in code: steps, tokens, wall time and dollars, plus a per-user daily cap. Context grows each step, so cost grows faster than linearly; summarize or drop old tool outputs and cache the stable prompt prefix. Hitting a cap stops the run with a clear message, never a silent loop.
Evaluating agents
Unit tests for tools, then task-level eval suites: realistic goals with checkable end states graded by code where possible (did the file contain X, was the API called with Y), by a separate model otherwise. Run them before any prompt, tool or model change, and track success rate, steps, cost and safety violations.
9. Bottlenecks
- Context growth makes long runs slow and expensive → summarize, cache prefixes, cap steps
- Third-party API rate limits → per-tool queues and backoff
- Approval latency (humans) → batch approvals, clear expiry, let the run continue on other branches
- Sandbox cold starts → warm pools of microVMs
10. Trade-offs
One general agent or specialized agents
Choose: One agent with a small, well-described tool set per task type; sub-agents only for parallel, independent work.
Instead of: Many cooperating agents: flexible, but more cost, more failure points and harder debugging.
Each extra agent multiplies tokens and coordination; most tasks do not need it.
Where tools run
Choose: Tools run server-side with scoped, short-lived user credentials; code runs in an isolated sandbox.
Instead of: Give the agent a broad service account: simpler, and catastrophic when injected.
The tool permissions are the real security boundary.
Approval granularity
Choose: Approve each risky action with its exact arguments, single use.
Instead of: Approve the whole plan up front: faster, but the plan can change after approval.
An approval that does not bind arguments can be redirected by injected content.
11. Failure modes
Prompt injection in a fetched web page
Agent tries to send data to an attacker.
Mitigation: No tool lets untrusted content pick a recipient; sending requires approval; egress allowlist for the sandbox.
Agent loops without progress
Cost runs away.
Mitigation: Step, token and dollar caps; detect repeated identical tool calls and stop.
Worker crash mid-run
Run lost or side effects repeated.
Mitigation: Durable checkpoints and idempotency keys per (run, step) on side-effecting tools.
12. What interviewers probe
Answer each one yourself before opening it.
How do you stop the agent from emailing the wrong person?Show a strong answer
The send tool validates recipients against rules in code (for example only contacts the user has emailed before), and sending needs an approval bound to the recipient and body.
How do you measure whether a prompt change made the agent better?Show a strong answer
Run the task eval suite before and after: success rate on checkable end states, steps, cost and safety violations, with a known-good and a no-op baseline to validate the grader.
How do you keep long runs affordable?Show a strong answer
Prompt caching on the stable prefix, summarizing old tool outputs, smaller models for simple steps, and hard caps.
How is the sandbox isolated?Show a strong answer
A microVM per session (not just a container), no network or an allowlist, read-only base image, CPU, memory and time limits, destroyed after use.
Sign in to track this guide in your curriculum progress.