SID-2 Swarm
Multi-agent loop
Answer tool calls, end agents, fork document caches and fuse the agents' reports.
Your client is a loop over the run's events. For each tool call, run the tool and send the result back; for each fork, fork the parent's document cache; when an agent reports, end it. The run is done at response.completed.
Answering tool calls
Send each result as a function_call_output in a response.inject event, with the call's call_id and the run's response_id. output is a string, rendered exactly as for SID-2.
await conn.send({
"type": "response.inject",
"response_id": response_id,
"input": [{"type": "function_call_output", "call_id": item["call_id"], "output": observation}],
})You can answer calls in any order and from parallel workers; SID puts the outputs back in call order before the agent's next turn. One inject may carry several outputs. Each inject is acknowledged with response.inject.created or response.inject.failed.
Run the backend part of each call (the search itself) concurrently: many agents search at once, and a turn often makes several calls. Don't let the event loop wait for a search; start a task per call and keep reading events.
Render each agent's results serially, in the order its calls arrived, against that agent's own document cache (below). The seen-ledger is stateful, as in Executing a turn's tool calls. The reference implementation does both: it fetches concurrently and renders each agent's calls in order.
While an agent has calls you haven't answered, it waits. Other agents keep going.
Ending an agent
When an agent calls report_helpful_ids, end it by injecting end_agent: true on that call. output isn't needed; anything you send is not shown to the model.
await conn.send({
"type": "response.inject",
"response_id": response_id,
"input": [{"type": "function_call_output", "call_id": item["call_id"], "end_agent": True}],
})The agent stops, and any other call from the same turn that you haven't answered is dropped. An empty ids list is the agent abstaining: end it the same way and leave it out of the fusion.
To turn a report down instead, for example when ids isn't a list of strings, answer the call with an error message as a normal output. The agent continues and can report again.
An agent also ends on its own when its turn budget runs out (max_turns) or a turn is cut off (truncated). Calls from a truncated turn are not sent to you.
Document caches across agents
Agents share one set of model-facing document IDs, so an agent can read an ID that another agent found and sent it. Each agent has its own seen-ledger, starting from its parent's at the fork. DocumentCache.fork(n) gives you exactly this: n new caches that share the parent's documents and IDs, each with a copy of the parent's seen-ledger.
Keep one cache per agent path. Create the root's as for SID-2, and fork on every spawn_agent output:
caches = {"/root": DocumentCache(range_mode="lenient")}
def on_spawn(parent: str, output_item: dict) -> None:
children = json.loads(output_item["output"][0]["text"])["agents"]
for path, cache in zip(children, caches[parent].fork(len(children)), strict=True):
caches[path] = cacheA fork's output always arrives before any call from the new agents, and after the parent's earlier calls have been answered, so the parent's cache is in the right state when you fork it.
Fusing the reports
Each agent's report_helpful_ids is its own ranking. Process each one as in Reporting results, against that agent's cache, then fuse them with reciprocal rank fusion, k = 60:
def reciprocal_rank_fusion(rankings: list[list[str]], k: int = 60) -> list[str]:
scores: dict[str, float] = {}
for ranking in rankings:
for rank, doc_id in enumerate(ranking):
scores[doc_id] = scores.get(doc_id, 0.0) + 1.0 / (k + rank + 1)
return sorted(scores, key=scores.get, reverse=True)Truncate the fused list to the number of documents you want, for example to match a recall@<n> reward.