Schedule tool calls under global and per-tool concurrency limits
Why it matters Agent runtimes cap parallel tool calls overall and per API so one burst does not trip a provider's rate limit.
An agent fires many tool calls, but the runtime allows only G calls at once overall and some tools have their own lower limits (for example, a rate-limited search API).
Write schedule_calls(calls, global_limit, per_tool). calls is a list of (tool, duration) in submission order; per_tool maps a tool name to its limit (tools not listed have no per-tool limit). Calls start in order: call i cannot start before call i - 1 started. A call occupies a slot during [start, end), so a slot freed at time t can be reused at t. Call i starts at the earliest time t >= start of call i - 1 (t >= 0 for the first) at which fewer than global_limit calls are running and, if its tool is limited, fewer than per_tool[tool] calls of that tool are running. Only the previous start time and end times of earlier calls need to be checked as candidates. Return (list of (start, end) pairs, makespan), with makespan = max end (0 for no calls).
def schedule_calls(calls, global_limit, per_tool)Example
schedule_calls([("search", 2), ("search", 2), ("calc", 1)], 2, {"search": 1}) gives ([(0, 2), (2, 4), (2, 3)], 4).
Understand the idea
An agent that runs tool calls in parallel finishes faster, but providers have limits. A runtime usually enforces two caps: a global one (how many calls in flight in total) and per-tool ones (for example only one call at a time to a slow search API). Calls are started in the order they were submitted, which keeps behaviour predictable.
Example: global limit 2, search limited to 1. The first search runs 0 to 2. The second search must wait for the first, so it runs 2 to 4. The calc call cannot start before the second search started (order), so it starts at 2 and ends at 3. Makespan 4.
Rule: start_i = first t >= start_(i-1) where running(t) < G and running_tool(t) < L_tool; a slot is busy on [start, end).
Production agent runners and API gateways use this kind of admission control in front of LLM and tool providers.