Delegate to subagents
Let one model plan the job and hand each routine part to a worker that sees only its own task.
- Pick the planner and the worker.
- Give the planner a delegate tool.
- Write each task so it stands alone.
- Run the tasks side by side.
- Read what the job cost.
1. Pick the planner and the worker
The planner reads the whole job, splits it, and writes the final answer. The worker does one part at a time, from a task written for it, and never sees the rest. The routine parts are where most of the words are written, so they go to the smaller model.
| Seat | Does | Model in this recipe |
|---|---|---|
| planner | splits the job, hands out the parts, writes the answer | anthropic/claude-sonnet-4.5 |
| worker | does one part: a summary, an extraction, a draft | anthropic/claude-haiku-4.5 |
The samples below put anthropic/claude-sonnet-4.5 in both seats, so they run as they stand. Set WORKER to the worker's model IDModel IDThe name a request calls a model by, lab/model, copied from Models.More on Models →, from the table or from Models.
2. Give the planner a delegate tool
The worker is a tool in the planner's tools, like any function of yours (Tools and function calling). The planner calls delegate with a task_name and a task_description. Your code sends the description to the worker's model, in a request of its own, and returns the worker's answer as the call's result.
Nadia runs a marketing agency in Oran. Every Sunday, her agent reads the week's client briefs, has each one summarised, and writes the weekly report.
import json
import os
from concurrent.futures import ThreadPoolExecutor
from openai import OpenAI
API_KEY = os.environ["OPENDUNES_API_KEY"]
BASE_URL = "https://opendunes.com/v1"
MODEL = "anthropic/claude-sonnet-4.5"
WORKER = "anthropic/claude-sonnet-4.5"
PLANNER_BRIEF = (
"You plan the job and write the final answer. Hand each routine part "
"to delegate, one call per part, all at once. The worker sees only "
"task_description: put in it everything the part needs."
)
WORKER_BRIEF = "Do the task exactly as described. Return only the result."
BRIEFS = [
"Sahel Dates wants twelve Instagram posts for Ramadan, in Arabic.",
"Oran Auto moved the launch of its used-car page to May.",
"Tassili Travel asks for a brochure in French and English.",
]
MAX_STEPS = 6
client = OpenAI(base_url=BASE_URL, api_key=API_KEY)
spent = {"planner": 0.0, "worker": 0.0}
def delegate(call):
args = json.loads(call.function.arguments)
result = {"status": "ok", "task_name": args["task_name"]}
try:
reply = client.chat.completions.create(
model=WORKER,
max_tokens=500,
messages=[
{"role": "system", "content": WORKER_BRIEF},
{"role": "user", "content": args["task_description"]},
],
)
except Exception as error:
result.update(status="error", error=str(error))
return call.id, result, 0.0
result["outcome"] = reply.choices[0].message.content
return call.id, result, reply.usage.cost
delegate_tool = {
"type": "function",
"function": {
"name": "delegate",
"description": "Hand one routine task to a worker model.",
"parameters": {
"type": "object",
"properties": {
"task_name": {"type": "string"},
"task_description": {"type": "string"},
},
"required": ["task_name", "task_description"],
},
},
}
job = "Write the client report for this week.\n\n" + "\n".join(BRIEFS)
messages = [
{"role": "system", "content": PLANNER_BRIEF},
{"role": "user", "content": job},
]
for step in range(MAX_STEPS):
reply = client.chat.completions.create(
model=MODEL,
messages=messages,
tools=[delegate_tool],
)
spent["planner"] += reply.usage.cost
message = reply.choices[0].message
if not message.tool_calls:
break
messages.append(message)
with ThreadPoolExecutor() as pool:
done = list(pool.map(delegate, message.tool_calls))
for call_id, result, cost in done:
spent["worker"] += cost
messages.append(
{
"role": "tool",
"tool_call_id": call_id,
"content": json.dumps(result),
}
)
print(message.content)
print(spent)The cURL sample is the planner's first turn: its answer carries the delegate calls your code runs.
The same split can run in one request, with the subagent helperHelperA tool run for you when a model reaches for it mid-answer: a web search, a page read, a sandbox. Named opendunes:‹name›.More on Helpers →, opendunes:subagent: OpenDunes runs the worker for the planner, and you write no loop. Helpers shows how. The loop above is the version you own: you see every task, and the worker can use your own functions.
3. Write each task so it stands alone
The worker reads its brief and the task_description, and nothing else. It has no conversation, no job, no other task. This is the request delegate sends, with the worker's model in place.
{
"model": "anthropic/claude-haiku-4.5",
"max_tokens": 500,
"messages": [
{
"role": "system",
"content": "Do the task exactly as described. Return only the result."
},
{
"role": "user",
"content": "Two lines, for a report: Oran Auto moved its launch to May."
}
]
}A task_description holds | Example |
|---|---|
| what to do | two lines, for a report |
| everything it works on | the brief itself, pasted in |
| the shape of the result | two lines |
Don't write "summarise the brief above". The worker has no "above": it answers from nothing, or makes the brief up.
Say in the planner's brief that the worker sees only task_description, and paste every input the part needs into it.
4. Run the tasks side by side
The planner can ask for several calls in one message. The tasks do not wait on each other, so the samples run them at once: a pool of threads in Python, Promise.all in JavaScript, a goroutine per call in Go. The results go back in the order of the calls, one tool message each.
A worker call that fails goes back too, as {"status": "error", ...}: the planner reads it, and hands the task out again or works around it.
Each task is a request of its own. Many at once can meet a 429 when the provider limits its rate: the library waits and sends it again, as Errors and retries shows.
5. Read what the job cost
Each request is charged on its own, at its model's rates, and usage.cost on each answer carries its charge in DADAThe Algerian dinar, the one currency the docs show.More on Balance and charges →. The samples keep two sums: planner, every turn of the planner, and worker, every task. max_tokens on the worker's request caps each task's answer. Activity, in the app, lists each request under its model.
The other way round, a worker that asks a stronger model: Review by a stronger model.
Last modified on