import json
import os
import requests
BASE = "https://api.swarms.world"
HEADERS = {"x-api-key": os.environ["SWARMS_API_KEY"], "Content-Type": "application/json"}
# Before the nightly data room run: check the credit balance and that the
# models the swarm needs are available, then submit every agreement as one
# batch. Each item becomes its own run in the account's swarm logs, named
# after the matter and the document.
credits = requests.get(f"{BASE}/v1/account/credits", headers=HEADERS, timeout=30).json()
models = requests.get(f"{BASE}/v1/models/available", headers=HEADERS, timeout=30).json()
print("credits:", credits)
print("models:", models)
with open("data_room_index.json") as f:
documents = json.load(f) # [{"doc_id": "DR-0412", "title": "...", "text": "..."}, ...]
MATTER = "M-4471"
AGENTS = [
{
"agent_name": "Clause Locator",
"system_prompt": (
"Locate the term, termination, change of control, assignment, "
"exclusivity, most-favoured-customer and governing law clauses. Return "
"the section number and the full clause text for each; write ABSENT "
"when a clause is not present."
),
"model_name": "gemini-2.5-flash", # fast, long context, cheap across a data room
"max_loops": 1,
},
{
"agent_name": "Term Extractor",
"system_prompt": (
"From the located clauses extract parties, effective date, term end, "
"renewal mechanics, notice periods, change of control consequences and "
"exclusivity scope. Quote the source text for every value."
),
"model_name": "claude-haiku-5-5",
"max_loops": 1,
},
{
"agent_name": "Abstract Writer",
"system_prompt": (
"Fill the diligence abstract template from the extracted terms and list "
"issues for the deal team. Leave fields blank where the extractor found "
"nothing. A paralegal and an attorney review every abstract."
),
"model_name": "gpt-5.4-mini",
"max_loops": 1,
},
]
batch = [
{
"name": f"{MATTER} abstract {doc['doc_id']}",
"description": "Nightly data room abstraction for the deal team's review.",
"swarm_type": "SequentialWorkflow",
"task": f"Document {doc['doc_id']}: {doc['title']}\n\n{doc['text']}",
"agents": AGENTS,
"max_loops": 1,
}
for doc in documents
]
results = requests.post(
f"{BASE}/v1/swarm/batch/completions", headers=HEADERS, json=batch, timeout=900
).json()
os.makedirs("abstracts", exist_ok=True)
for doc, result in zip(documents, results):
with open(f"abstracts/{doc['doc_id']}.json", "w") as out:
json.dump(result, out, indent=2)
print(doc["doc_id"], result.get("usage", {}).get("total_tokens"), "tokens")
print(len(results), "abstracts ready for the deal team")