Track 04 · Multi-agent & integrationLesson 03 of 11
Fan-out and sandboxed parallel work
Process thousands of records fast, isolated, and inside the budget.
- Lesson library · reference
- 45 min read
- 75 min hands-on
- Pro
What you'll be able to do
01- Explain fan-out/fan-in and map-reduce over record sets, and calculate what parallelism does and does not buy: wall-clock time falls, tokens and cost do not.
- Implement bounded concurrency in Python with a semaphore per shared resource, per-attempt timeouts, retries with backoff and jitter, request coalescing and a partial-failure report.
- Design result aggregation that survives out-of-order completion, duplicates and partial failure, with a batch-level success threshold.
- Choose between synchronous fan-out and a provider batch API for a workload, citing the discount, size limits and completion window.
- Threat-model execution of model-generated code and choose an isolation level (process, container, gVisor, microVM) with egress, resource and filesystem controls that a security team will accept.
Before you start
- Supervisor, router or swarm?: fan-out is the parallel branch of a supervisor or pipeline.
- Retries, fallbacks and failing gracefully: retry, timeout and idempotency mechanics inside one agent. This file applies them across many items.
- Workflows that survive restarts: large batches must resume after a crash without redoing finished items.
- Subagents, skills and code mode: code mode is why you need a sandbox.
- Working knowledge of Python
asyncio(async/await, tasks).
The short version
When an agent must process hundreds or thousands of independent records (exceptions at a port cut-off, bills of lading in a backlog, invoices at month end), doing them one after another is the latency problem. Fan-out runs many at once and fan-in collects the results. The speed-up is capped by the tightest shared resource (the carrier portal's session limit, the customer's SAP rate limit, your provider's tokens per minute), so you bound concurrency per resource, time out each attempt, retry transient failures with jittered backoff, coalesce duplicate requests, and report partial failures instead of failing the batch. Parallelism cuts wall-clock time, not tokens: the bill is the same or higher. For work that can wait hours, provider batch APIs trade latency for a 50% discount. When agents write and run code as part of the work, that code is untrusted: it runs in an isolated, ephemeral sandbox with no secrets, no network by default and hard resource limits.
Key takeaways:
- Speed-up ≈ min(concurrency limit, number of items) for uniform work, eroded by tail latency and retries.
- One semaphore per shared resource, sized from that resource's real limit, not one global number.
- Every item returns a typed result with a status. The batch reports successes, failures by reason, and whether it met its threshold.
- Batch APIs (Anthropic, OpenAI) cost half and complete within 24 hours; they are for backfills and evals, not for cut-off night.
- Model-generated code runs in a sandbox: fresh filesystem, no credentials, egress denied by default, CPU, memory and time limits.
Pro lesson
The rest of this lesson is in Pro.
The mental model, worked example, field note, failure table, exercise and self-check, with the voice tutor.
- Every stage and specialisation, in full
- Every lab and every role project
- Halden weeks 2–10, on your record
- Every drill, live in the playground
- All solutions, plus the server judge
- Lab Assistant and every Teach with AI