What it is
An AI agent platform runs tasks that an LLM carries out step by step: the model decides the next action, the platform executes it (usually a tool call), the result goes back into the model's context, and the loop repeats until the task is done or a limit is hit. A multi-tenant platform does this for many users and teams at once, with shared LLM capacity, shared tools and strict isolation between tenants.
As a design exercise it is a job system wrapped around an expensive, rate-limited, non-deterministic dependency. Most of the hard parts are classic: queues, leases, checkpoints, budgets, idempotency, observability. The new parts are the tool boundary and the cost model.
Why it exists
A chat request is one model call. A task like "triage the new issues and open fixes for the trivial ones" is tens of model calls and tool calls over minutes or hours, some of which change real systems. Running that inside a web request fails on every axis: connections time out, restarts lose the work, a burst of tasks gets rejected, nobody can approve a risky step, and nobody can say afterwards what the agent did or what it cost. The platform exists to make those runs durable, bounded, safe and observable.
How it works
Requirements and estimates. Functional: submit a task, follow its progress, approve or reject sensitive actions, get the result; tools added by teams without changing the runtime. Non-functional: tasks survive crashes, one tenant cannot starve others, every action is auditable, spend is capped. An illustrative estimate (the numbers in the lesson): 12 new tasks per second at peak, ~10 LLM calls and ~20k tokens per task, so ~120 LLM calls and ~240k tokens per second. Most of those tokens are context re-sent on every call, so prompt caching and context trimming matter more than server count. With 60 s per run, Little's law gives 12 × 60 ≈ 720 runs in progress.
The agent loop. A runner holds the task's message history, calls the LLM with the available tool definitions, receives either a final answer or a tool-use request, executes the tool, appends the result and calls the LLM again. The loop is sequential per task, so a run spends most of its wall-clock time waiting on the model.
Tools through MCP. The Model Context Protocol standardises how an application (the MCP client, here the runtime) talks to tool providers (MCP servers). Messages are JSON-RPC 2.0; the client lists tools with tools/list and invokes one with tools/call; servers can also expose resources (readable data) and prompts. Transports are stdio for local servers and Streamable HTTP for remote ones, where the protocol specifies OAuth-based authorization. A platform typically runs a registry of approved MCP servers and decides per tenant and per task which tools an agent may see.
The tool boundary. The model only proposes a call. The runtime validates it against the task's allow-list and argument schema, obtains a short-lived credential scoped to this task and tool (from a secrets vault or token exchange), and the MCP server executes code in a sandbox: a fresh container or microVM with CPU, memory and time limits, a read-only base image and no network egress unless the tool requires specific destinations. Tool results and fetched documents are untrusted input; a prompt injection in a web page must not be able to grant new tools or exfiltrate credentials.
Accept, queue, run. The API validates the task, stores it, enqueues it and answers 202 Accepted with a task id; progress streams to the client (SSE, WebSocket) or arrives by webhook. Runner pools take tasks from the queue under a lease, renew the lease with heartbeats while they work, and write a checkpoint after each step: plan, message history, tool results, pending actions. Durable-execution engines (workflow systems that persist every step) are a common way to get this without building it.
Capacity is the LLM quota. Providers limit requests and tokens per minute, or sell provisioned throughput. Since each run mostly waits on the model, the number of runs you can usefully execute at once is set by that quota, not by runner CPU. Runner concurrency should follow the quota (per model, per region), with a token-aware admission check before each call. Past it, extra calls receive 429 Too Many Requests; retries spend the same quota again.
Budgets and loop detection. Each task carries limits: maximum steps, tokens, cost and wall-clock time, plus loop detection (the same tool with the same arguments several times in a row, no progress between steps). Hitting a limit stops the run cleanly: checkpoint, partial result, or escalation to a human. Per-tenant budgets and quotas keep one customer's runaway agents from consuming the shared LLM capacity.
Human approval. Sensitive actions (merging, sending, paying, deleting) require approval. The runner checkpoints the state and the exact pending action, emits an approval request and releases its slot. The approval (or rejection) is an event that re-enqueues the task; any runner resumes from the checkpoint and executes exactly the approved action, with the approver recorded in the audit log.
Failure handling. If a runner dies, its lease expires and the queue redelivers the task; another runner resumes from the last checkpoint. Delivery is at least once, so the step that was running may run again: side-effecting tools take an idempotency key, or the runtime records the intent before the call and checks it on resume. Tasks that keep failing go to a dead letter queue for inspection instead of retrying forever.
Observability and evaluation. Each task is a trace: a span per LLM call and per tool call with model, tokens, cost, latency, tool name and arguments (with sensitive values redacted). Dashboards show cost per task and tenant, steps per task, budget hits, 429 rate and tool error rates. Recorded tasks become an evaluation set: before a model or prompt change ships, replay them and compare success rate, steps and cost.
When to use it
- Tasks need many dependent steps with tools and take longer than a request should.
- Several teams or tenants share LLM capacity and tools, so isolation, quotas and audit are required.
- Some actions are risky enough to need approval, and you must be able to show afterwards who approved what.
When not to
- A single model call (classification, summarisation, extraction) answers the request: call the model from the service, with a timeout and a rate limiter.
- The workflow is fixed and known in advance: a regular workflow or a pipeline with one or two model calls is cheaper, faster and easier to test than an agent deciding the steps.
- Hard real-time latency is required: an agent loop is seconds to minutes by nature.
Trade-offs
| Choice | You gain | You pay |
|---|---|---|
| Queue + background runners instead of synchronous requests | Durable tasks, absorbed bursts, fast acceptance | Status APIs, streaming progress, more moving parts |
| Checkpoint after every step | Cheap crash recovery and approvals, replayable runs | Storage, a little latency per step, schema for state |
| Tight budgets | Bounded cost and capacity per task | Hard tasks stop early or escalate more often |
| More autonomy (fewer approvals) | Faster, less human effort | Larger blast radius when the agent is wrong |
| Shared LLM quota | Higher utilisation, lower cost | Noisy neighbours unless per-tenant limits exist |
| Fresh sandbox per tool call | Strong isolation, no leftover state | Start-up latency and cost; warm pools need care |
Alternatives
- Direct model calls in the service for single-step features, with a timeout, a rate limiter and a fallback.
- A workflow engine with model steps: fixed control flow, model calls as activities. Predictable and testable; the model does not choose the steps.
- Batch APIs for offline, non-interactive work: much higher throughput per dollar, results within hours.
- Hosted agent services from model providers: less to run, but less control over tools, data residency and observability.
Failure modes
- Runaway loops: a prompt or model change makes agents repeat steps; capacity and spend explode. Budgets, loop detection and per-tenant quotas contain it.
- Quota exhaustion and retry storms: 429s retried without backoff burn more quota. Admit calls against the quota, back off with jitter, shed or delay low-priority tasks.
- Lost or duplicated side effects after a crash: missing checkpoints lose work; missing idempotency repeats a payment or an email.
- Prompt injection through tool results: untrusted content steers the agent. Keep permissions outside the model's reach, isolate tools, require approval for sensitive actions.
- Credential leakage: long-lived or broad tokens in the agent's context. Use short-lived, scoped credentials injected at the tool boundary, never shown to the model.
- Stuck approvals: tasks waiting forever. Give approvals a deadline and a default (usually reject).
- Silent quality regressions after a model update: no evaluation set, no alert. Replay recorded tasks before rollout.
In production
Route model calls through an LLM gateway that enforces per-tenant quotas, tracks tokens and cost, and fails over between models or regions. Keep runner concurrency per model in line with the purchased quota. Store checkpoints in a durable store with the task id as key and write them before side effects. Run tools from a curated MCP server registry, each server with its own identity and least-privilege credentials, in sandboxes with no default egress. Emit traces with GenAI semantic conventions (OpenTelemetry has them) so LLM and tool spans line up with the rest of your telemetry. Alert on cost per task, budget-hit rate, 429 rate and tool error rate, not only on latency.
Common misconceptions
- "More runners make agents faster." Only until the LLM quota is used; after that they add 429s.
- "A queue fixes overload." It absorbs bursts. If tasks arrive faster than runners finish them, the backlog grows until the queue is full.
- "The model decides which tools it may use." The model proposes; the runtime authorises. Permissions must not depend on what the prompt says.
- "Checkpoints mean exactly-once." A crash between a side effect and its checkpoint repeats the step. Tools need idempotency.
- "Waiting for approval is just a long tool call." Holding a runner for hours wastes a slot and loses the wait on restart. Checkpoint and release.
Interview angle
Start with requirements and numbers: tasks per second, steps and tokens per task, run duration, concurrent runs, cost per task. Draw the accept-queue-run shape early and explain why. Interviewers then probe the scarce resource (how you size against the LLM quota and handle 429s), safety (tool allow-lists, sandboxing, scoped credentials, prompt injection, approvals), durability (leases, checkpoints, idempotent tools, dead letters) and cost control (budgets per task and tenant, caching). Close with observability and evaluation: how you would detect that a new model loops more before users do.
Model assumptions
The simulation is a simplified model, not a benchmark, and durations are compressed: one agent run, minutes in reality, takes about 600 ms of simulated time (lognormal, cv 0.3–0.4; 1500 ms with cv 1.0 for looping agents). The engine has no LLM or tool model. LLM capacity is represented by the runner slot count (one slot = one concurrent run within the quota); 429s, token accounting and costs are described, not simulated. Runner pools are queue consumers; the task queue redelivers a task whose lease (visibility timeout, 2 s in the crash step) expires without an acknowledgement. The agent loop, MCP tool calls, credentials, sandboxing, checkpoints and the approval flow are scripted illustrations over scenery nodes, not simulated behaviour. Task arrivals are Poisson. Estimates in the text (tasks per second, LLM calls and tokens per task) are illustrative orders of magnitude.