Skip to content

A research team

This tutorial composes two agents. A research agent searches a set of notes. A writer agent writes summaries, and gets its facts by calling the research agent as a tool. The team runs offline with scripted models, and on Claude with a change to two lines.

The complete code is docs/examples/research_team.py.

flowchart TB
    User(["Task"]) --> Writer["writer_agent<br/>writes summaries"]
    Writer -->|"research_agent(task)"| Research["research_agent<br/>searches the notes"]
    Research -->|"search_notes(word, limit)"| Notes[("Notes")]

1. The tool

The research agent needs one tool: a search over the notes. It takes a word and an optional limit on the number of notes returned. It only reads, so it is marked read-only and concurrent.

NOTES = [
    "The budget limits steps, tokens, cost and time.",
    "A run stops when the model answers or the budget runs out.",
    "A sub-agent's spending is charged to its parent's budget.",
    "Tool calls can be denied or changed by the event bus.",
]


class SearchNotes(Tool):
    readonly = concurrent = True

    def __init__(self, notes: list[str]):
        self.notes = notes

    @property
    def definition(self):
        return {
            "name": "search_notes",
            "description": "Find notes that mention a word. Returns at most `limit` notes.",
            "input_schema": {
                "type": "object",
                "properties": {"word": {"type": "string"}, "limit": {"type": "integer"}},
                "required": ["word"],
            },
        }

    async def execute(self, input, ctx=None):
        found = [n for n in self.notes if input["word"].lower() in n.lower()][: input.get("limit", 3)]
        return text_output(text=" ".join(found) or "no notes found")

2. The research agent

The research agent is described by a blueprint, so it can run on its own or serve as a tool. A function builds it from a model, which keeps the choice of model outside the composition.

def research_agent(model: Model) -> Blueprint:
    return Blueprint(
        name="research_agent",
        description="searches the notes and reports what they say.",
        model=model,
        harness=Harness(
            loop=StandardLoop(),
            budget=TreeBudget(limit={"steps": 5}),
            tools=StaticToolSet(tools=[SearchNotes(notes=NOTES)]),
            events=GuardBus(
                TranscriptBus(label="research_agent"),
                clamp(tool="search_notes", field="limit", lo=1, hi=2),
            ),
        ),
        initial=InitialState(system="Search the notes and report what they say about the topic. Quote the notes."),
    )

Its harness:

  • Loop: the standard loop.
  • Budget: 5 steps per run, enough for a few searches and an answer.
  • Tools: the search over the notes.
  • Events: a transcript of every call, wrapped in a guard. clamp() keeps the limit of every search between 1 and 2 by changing the call, so the agent never pulls in more notes than needed, whatever the model asks for.

3. The writer agent

The writer agent has one tool: the research agent, turned into a tool with as_tool(). Every call of that tool runs the research agent with a fresh conversation, and the research agent's spending is charged to the writer's budget.

def writer_agent(model: Model, research: Blueprint) -> Blueprint:
    return Blueprint(
        name="writer_agent",
        description="writes short summaries.",
        model=model,
        harness=Harness(
            loop=StandardLoop(),
            budget=TreeBudget(limit={"steps": 10}),
            tools=StaticToolSet(tools=[research.as_tool()]),
            events=TranscriptBus(label="writer_agent"),
        ),
        initial=InitialState(system="Write one-line summaries. Ask the research agent for the facts first."),
    )

4. Running the team

run_team() builds the team from two models, prints its composition and runs one task. The session budget is charged for every model call in the tree, so it shows what the whole team used.

async def run_team(research_model: Model, writer_model: Model, task: str) -> None:
    team = writer_agent(model=writer_model, research=research_agent(model=research_model))
    print(team.tree(), end="\n\n")

    session = TreeBudget(limit={"tokens": 100_000})  # every model call in the tree is charged here
    async with team.agent(session_budget=session) as agent:
        outcome = await agent.run(task)
    print(f"\n{message_text(outcome.final)}")
    print(f"whole team: {session.spent['steps']} model calls, {session.spent['tokens']} tokens")

5. Offline, with scripted models

Two playbook models play the parts. The research model searches with a limit of 10, which the guard turns down to 2. The writer model asks the research agent and writes the summary from its answer.

research_script = PlaybookModel(ref="playbook:research", playbooks={
    "budget": [call("search_notes", word="budget", limit=10), say(lambda results: results[-1])],
})
writer_script = PlaybookModel(ref="playbook:writer", playbooks={
    "summary": [
        call("research_agent", task="Find what the notes say about the budget."),
        say(lambda results: f"Summary: {results[-1]}"),
    ],
})

if __name__ == "__main__":
    asyncio.run(run_team(research_script, writer_script, "Write a one-line summary of how the budget works."))
python docs/examples/research_team.py

Output:

writer_agent [playbook:writer]: writes short summaries.
  harness: StandardLoop, budget steps=10, TranscriptBus
  └─ research_agent [playbook:research]: searches the notes and reports what they say.
       harness: StandardLoop, budget steps=5, GuardBus(TranscriptBus)
       tools: search_notes

[writer_agent] → research_agent(task='Find what the notes say about the budget.')
    [research_agent] → search_notes(word='budget', limit=10)
    [research_agent] ⚑ guard: search_notes limit=10 is out of range, changed to 2
    [research_agent] ← search_notes: The budget limits steps, tokens, cost and time. A run stops when the mod…
    [research_agent] ■ done (end_turn)
[writer_agent] ← research_agent: The budget limits steps, tokens, cost and time. A run stops when the model a…
[writer_agent] ■ done (end_turn)

Summary: The budget limits steps, tokens, cost and time. A run stops when the model answers or the budget runs out.
whole team: 4 model calls, 434 tokens

The transcript shows the tree at work: the writer delegated to the research agent, the guard changed the search, and the research agent's answer became the writer's tool result.

6. On Claude

The composition does not depend on the models, so running the team on Claude takes only other models. A small, fast model searches and a stronger model writes:

"""Tutorial: the research team on Claude. Requires ANTHROPIC_API_KEY."""

import asyncio

from modular_agents.composites.model.anthropic_model import AnthropicModel
from research_team import run_team

asyncio.run(run_team(
    research_model=AnthropicModel(ref="claude-haiku-5-5"),  # a small, fast model searches
    writer_model=AnthropicModel(ref="claude-sonnet-5-5"),  # a stronger model writes
    task="Write a one-line summary of how the budget works.",
))
export ANTHROPIC_API_KEY=...
python docs/examples/research_team_claude.py

The exact answer depends on the model, but the structure is the same: the writer calls the research agent, and the research agent searches under the same guard and budget.

What to change next

Each change touches one block:

  • Another model for the research agent: one argument of run_team().
  • A second source of facts, such as a web search tool: one more tool in the research agent's tool set.
  • A stricter limit: the budget of either harness, or a tokens limit on the session budget.
  • A rule that the writer may ask at most three questions: a custom guard rule on the writer's event bus.
  • A team that works in parallel: a lead agent with several research agents as tools, which the loop runs in parallel when the model calls them in one turn.