AI system design · Orchestration, loops, cost per step

How to design a multi-agent AI system

An agent system completes a task by running a language model in a loop. Its failure modes are new: loops that never end, context that grows every step, and cost that scales with every token. At 1,000 tasks a minute that is roughly 550,000 tokens a second.

Updated · 4 min read

The orchestrator takes tasks off a queue and dispatches sub-tasks to agent workers. Workers call models and tools through gateways and save progress after every step.

AI Agent System architecture diagram

The orchestrator takes tasks off a queue and dispatches sub-tasks to agent workers. Workers call models and tools through gateways and save progress after every step.

The short version

  • An orchestrator plans each task and dispatches sub-tasks to agent workers through a queue. Tasks run asynchronously.
  • The orchestrator, not the model, enforces a step limit, a token budget and a deadline on every task.
  • Save state after every step and make every step safe to re-run, so a crash resumes instead of restarting.
  • Models and tools sit behind gateways that own retries, limits, credentials and cost accounting.

Clarifying questions to ask first

  • How many steps per task? (Assume about 10 model calls, using tools such as web search, code execution and internal APIs.)
  • How many tasks? (Assume 1,000 tasks a minute.)
  • Does the user wait on the request? (Assume not. A task takes about 20 seconds, so it runs asynchronously and the client polls or receives events.)
  • Can agents take actions with side effects? (Assume yes, such as sending email, so irreversible actions need human approval.)
  • Is there a limit on cost and time per task? (Assume yes: a step limit, a token budget and a deadline.)

Numbers that shape the design

QuantityAssumptionResult
Steps per task10 model calls-
Tokens per step3,000 input + 300 output≈ 33,000 tokens per task
Task load1,000 tasks per minute≈ 17 tasks/s ≈ 550,000 tokens/s
Latency≈ 2 s per step, run sequentially≈ 20 s per task

Latency is a sum of sequential steps, so parallelism matters more than raw speed. If every step re-sends the whole conversation, input grows each step and total tokens per task grow roughly with the square of the step count.

High-level design

  • The API accepts a task immediately and puts it on a queue. The client polls or receives events.
  • The orchestrator plans the task, splits it into sub-tasks, dispatches them and combines the results.
  • Each agent worker runs a model loop for one sub-task, with its own instructions and allowed tools.
  • The model gateway owns provider routing, retries with backoff, rate limits, quotas, caching and cost accounting.
  • The tool gateway owns timeouts, sandboxing for code execution, credentials and idempotency keys for side effects.
  • The state store holds task state, step history and intermediate results, written after every step.

Durable execution

A twenty-second task with ten external calls will regularly hit a timeout, a deploy or a crashed worker. Treat each task as a workflow: checkpoint each completed step with its result before planning the next, and make every step safe to re-run. A workflow engine such as Temporal, or a simple state machine over a database and a queue, lets a task resume where it stopped instead of paying for every token twice.

Keeping cost and latency bounded

  • Summarise or trim history between steps. Keep large tool outputs in the state store and put only references in the prompt.
  • Run independent sub-tasks in parallel to cut end-to-end latency.
  • Retry in one place, the gateway, with backoff and a cap, and put circuit breakers on failing tools.
  • Cache deterministic tool results and repeated model calls, and route simple steps to smaller, cheaper models.
  • Give each tool only the authority its operation needs. A sandbox holding production secrets still exposes them.

Observability and evaluation

Trace every step with inputs, outputs, tokens, latency and cost, grouped by task. Track task success rate, cost per successful task and the distribution of steps per task; a long tail of step counts usually means loops. Keep a set of reference tasks and re-run it on every change to prompts, tools or models.

Common traps

  • Trusting the model to stop. An agent can keep calling the same tool or re-planning without progress; the orchestrator must enforce steps, tokens and a deadline.
  • Re-sending the full history every step. Context grows until it overflows the window or makes each call slow and expensive.
  • Retrying at every layer. A failing tool or provider gets its load multiplied while it is already down.
  • Checkpointing only when the whole task finishes. A crash then repeats external work that already succeeded.
  • Giving a retried tool call a new operation ID. The receiver can't recognise the earlier call, so the same email goes out twice.
  • Adding up child agents' token use after they finish. Agents spawning agents can blow past the parent limit first; reserve a shared task budget before dispatch.

Follow-up questions interviewers ask

  • "How do you stop an agent looping forever?" The orchestrator enforces a maximum step count, a token budget and a deadline.
  • "What happens if a worker crashes mid-task?" Another worker resumes from the last saved step.
  • "How do you avoid sending the same email twice?" Idempotency keys on side-effecting tool calls, and human approval for irreversible actions.
  • "How do you make tasks finish faster?" Run independent sub-tasks in parallel and route simple steps to smaller models.

Frequently asked questions

What is an AI agent system design interview?

+

It asks you to architect a system where language-model agents complete multi-step tasks with tools, such as a research assistant or coding agent. You design the orchestrator, workers, tool and model access, state and observability, and reason about tokens, latency, cost per task and runaway loops.

How is AI system design different from classic system design?

+

The components are familiar: queues, coordinators, workers, caches and storage. The units change. Throughput is measured in tokens, memory is the context window, cost is per token, and agent loops and context growth join the classic failure modes.

What does the orchestrator do?

+

It receives a task, plans it, splits it into sub-tasks, dispatches them to specialised agents, enforces budgets and combines the results. It is the coordinator from classic distributed systems, plus context and control flow for the agents.

Now fix it yourself.

Run the design under real traffic and see what breaks first. No signup.