TL;DR: Treat a turn's calls as a small dependency graph: a call depends on any earlier call it references, and writes to the same resource are ordered. Run each call as soon as its dependencies finish, with independent reads concurrent, each under a timeout. Collect results keyed by call id and return them in issue order, with failures and timeouts as structured results so one slow call cannot take the turn down. Test with fake tools that record start and end times.
How to approach it
Agree the call shape (id, tool, args, optional after list and a resource key for writes), choose asyncio for concurrency, implement scheduling by dependencies, then timeouts and ordering of results, then tests that prove overlap and order.
A strong answer
import asyncio
async def dispatch(calls, tools, timeout=1.0):
done = {c["id"]: asyncio.Event() for c in calls}
results, last_write = {}, {}
deps = {}
for c in calls: # explicit + same-resource writes
d = set(c.get("after", []))
if c.get("writes"):
if c["writes"] in last_write:
d.add(last_write[c["writes"]])
last_write[c["writes"]] = c["id"]
deps[c["id"]] = d
async def run_one(c):
for d in deps[c["id"]]:
await done[d].wait()
if "error" in results[d]:
results[c["id"]] = {"error": "dependency_failed", "dependency": d}
done[c["id"]].set()
return
try:
out = await asyncio.wait_for(tools[c["tool"]](**c["args"]), timeout)
results[c["id"]] = {"ok": out}
except asyncio.TimeoutError:
results[c["id"]] = {"error": "timeout", "retryable": True}
except Exception as e:
results[c["id"]] = {"error": "tool_failed", "type": type(e).__name__}
done[c["id"]].set()
await asyncio.gather(*(run_one(c) for c in calls))
return [(c["id"], results[c["id"]]) for c in calls] # issue order
log = []
def tool(name, delay):
async def fn(**args):
log.append(("start", name))
await asyncio.sleep(delay)
log.append(("end", name))
return name
return fn
tools = {"a": tool("a", 0.05), "b": tool("b", 0.05), "w": tool("w", 0.01),
"slow": tool("slow", 0.5)}
calls = [{"id": "1", "tool": "a", "args": {}}, {"id": "2", "tool": "b", "args": {}},
{"id": "3", "tool": "w", "args": {}, "after": ["1"]},
{"id": "4", "tool": "slow", "args": {}}]
out = asyncio.run(dispatch(calls, tools, timeout=0.2))
print(out[3]) # => ('4', {'error': 'timeout', 'retryable': True})
print(log.index(("start", "b")) < log.index(("end", "a"))) # => True
print(log.index(("start", "w")) > log.index(("end", "a"))) # => True
Reads a and b overlap; w waits for a because it declared the dependency; the slow call times out without holding up the others; results return in issue order with their ids.
What to test and say.
- Overlap of independent calls (start of one before end of another).
- Order for dependencies and same-resource writes.
- A failed dependency produces
dependency_failedrather than running the dependent call. - Timeouts produce a structured result; the turn completes.
- "In production I would cap concurrency per tool and per downstream service, and refuse two writes to the same resource in one turn if the model decided both before seeing either result."
What interviewers probe next
"How do you know which calls depend on each other if the model does not say?" Treat writes as dependent on earlier calls in the turn and reads of the same resource after a write as dependent on it; or require the model's API to express dependencies.
"What about cancellation?" Pass a cancellation signal and cancel pending tasks; results for cancelled calls say so.
"Threads instead of asyncio?" Fine for blocking tools: a thread pool with futures and the same dependency logic.
What if?
What if · interview follow-ups
Hold your answer as the constraints change
Your dispatcher schedules calls by dependencies with timeouts and returns results in issue order.
Start here
What if the dependency list contains a cycle?
What does the scheduler do?
Detect it before scheduling (a topological sort fails) and return an error for the calls in the cycle instead of waiting forever.
Carry it forwardValidate the graph before running it.
Weak answerLetting the turn hang on mutual waits.
Constraint changes
What if 30 independent reads hit the same API?
Is unlimited concurrency fine?
Add a semaphore per downstream service so only a few calls run at once, protecting the service and its rate limits while independent calls to other services still overlap.
Carry it forwardParallelism needs per-service limits.
Weak answerGathering all 30 at once.
Constraint changes
What if a write times out?
Did it happen?
Its outcome is unknown, not failed. Mark it unknown, do not retry blindly, and rely on idempotency keys and a lookup of the target system before any retry.
Carry it forwardTimeouts on writes are unknown outcomes.
Weak answerMarking the write failed and retrying it.
Now defend it
One-line summary?
Build a per-turn dependency graph including same-resource writes, run calls as their dependencies finish with per-call timeouts, propagate dependency failures, and return structured results by id in issue order, tested for overlap, order and timeouts.
How it's scored
Weak: Runs everything sequentially, or everything in parallel including writes.
Hire: Parallel independent calls, ordered dependent ones, results keyed by id.
Strong Hire: Dependency graph including same-resource writes, per-call timeouts, dependency-failure propagation, issue-ordered results, tests proving overlap, order and timeouts, and production notes on concurrency limits, cycles and unknown write outcomes.
Common mistakes
- Results returned in completion order without ids.
- Writes racing each other.
- One timeout blocking the whole turn.
- Tests that cannot show calls actually overlapped.