Delegate to a sub-agent (~20 min)
subagent.py is the planner again, with two tools (search_flights, search_hotels) and a working loop. The planner should also know what a destination is like on the travel date (weather, what's worth doing in that weather), but that research takes several lookups, and you don't want all of it in the planner's context.
The module already has a researcher: its own tools (SUB_TOOLS: get_weather, search_attractions), handlers (SUB_HANDLERS), system prompt (SUB_SYSTEM), model (SUB_MODEL) and turn budget (SUB_MAX_TURNS). run_subagent is a stub.
Goal: give the planner a research_destination tool that hands a question to the researcher and gets back only its answer.
- Add a tool named
research_destinationto the planner's tools. Its input:cityandquestion(both strings, both required). - Running it starts a fresh researcher conversation whose first user message contains the city and the question. Researcher calls use
model=SUB_MODEL,system=SUB_SYSTEM,tools=SUB_TOOLS, and run the usual tool loop (everytool_usegets atool_result, failures come back asis_error). - The researcher's final text is the
research_destinationtool result. None of the researcher's own messages, tool calls or tool results appear in the planner's history. - The researcher gets at most
SUB_MAX_TURNSmodel calls. If it runs out, or anything inside it raises (including an API error), the planner gets anis_errorresult for thatresearch_destinationcall and carries on. The planner never crashes because of the researcher. - The researcher can't call
research_destinationitself, and every model call (planner and researcher) goes through the sameclientthatrun_agentwas given. search_flights,search_hotels,max_turnsand theis_errorhandling keep working, andrun_agent(prompt, client=None, max_turns=10) -> strkeeps its signature.
Be ready to explain what you gain from a sub-agent here (context size, cost, a cheaper model, focused instructions) and what you lose (latency, the planner can't see the raw data, errors are summarized), and when you'd just give the planner the tools directly.
Try it: python -m subagent
Check it: pytest tests/test_subagent.py