Part 1
Specialists as tools
The analyst is the orchestrator, and it delegates by calling each specialist as a tool. A specialist is a tool whose run is its own agent loop, so the analyst calls it like any other tool. The 3 agents share one context, a list of findings, which all 3 write to and which each specialist is handed when it starts.
Study first
- Look again at the agent loop from Day 11,
runAgent. It takes a system prompt, a tool list, adonetool that ends the task, and limits, and a specialist reuses it with its own prompt and tools. - Note that each agent keeps its own messages, and only the findings list is shared.
Build
- In
src/lib/investigate.ts, define 3 agents,analyst,knowledgeandweb, each with its own system prompt. The analyst asks the knowledge base first, and it asks the web only for what the documents do not cover or when the question names a site. It ends withwrite_report. - Make
save_findingthe shared tool, which appends a finding's text and source to the one list. - Write a
specialist(agent, name, description, system, tools)helper that returns a tool. ItsruncallsrunAgenton the small model with the specialist's tools plussave_finding, and the task is the question plus "Known so far" with the findings to date. The tool returns the specialist's answer in text. - Give each specialist limits of 8 steps, $0.20 and 90 seconds. The analyst keeps the loop's defaults of 15 steps and $0.50, with 300 seconds.
- Build
ask_knowledge_basewith the Day 13 tools (find,ls,cat,grep), andask_webwithread_url,browse_siteand, when a search key is set,web_search. - Write
investigate(question, { analyst, specialist }, { on, limits, browser }), where the second argument holds the 2 models. It returns the report, the findings, the log, the cost by agent and the total.
Done when
- A test runs a scripted analyst that calls
ask_knowledge_baseand thenwrite_report, and a scripted specialist that callsfind, saves a finding and answers. The report cites the finding's source. - The log is in this order: the analyst's thought, the specialist's 2 calls and its answer, the specialist's finish, the analyst's 2 calls, and the analyst's finish.
Technical notes
- A specialist that stops on a limit returns a sentence that says so, and the findings it saved stay in the list.
- The analyst and the specialists run on different models,
chatModel()andchatModel("small"), because the large model decides and writes while the small one reads. In the terminal, both models come from the session's provider. - A specialist runs inside the analyst's tool call, so the analyst's time limit bounds it too. A specialist that outlives the deadline fails as a tool, and the analyst continues with the tool error as the result.
- Day 12's orchestrator on the Claude Agent SDK has the same shape, but the SDK runs its loops. On Day 15 the loops are your repository's own, so a test can script every agent.

