Reduce the number of turns (~20 min)
fewer_model_calls.py is a weather agent whose bugs are already fixed. It works, but it's slow:
python -m fewer_model_calls
The prompt "What is the average temperature in Tokyo and Paris right now?" currently costs several model calls (look up each city, then add, then divide, then answer).
Goal: the correct average (14.5 °C) in at most 2 model calls (one round of tool calls, then the final answer), without breaking error handling.
Constraints:
- Raw SDK only, and
run_agent(prompt, client=None, max_turns=10) -> strkeeps its signature. - You can change the tools, their schemas, the system prompt, the request parameters and the loop, but keep
get_weatherandcalculateworking (add tools rather than replacing them). - An unknown city must still come back to the model as an
is_errorresult rather than crash the agent.
Before you change anything, count the calls and print response.content each turn. Then be ready to explain each lever you used, what it costs (composability, risk of wrong parallelism), and why you picked it.
Running the module with a key prints a trace of every model call to stderr: tool calls with their ids and inputs, the results sent back, and token counts (ABH_TRACE=0 turns it off).
Check it: pytest tests/test_fewer_model_calls.py. The offline tests check the loop and request, not your tool design: parallel calls not disabled, results batched in tool_use order, errors surfaced, one round plus an answer fitting max_turns=2, and every advertised tool having a well-formed schema and a handler. The 2-call goal itself is checked by the live test, which needs an API key (see Settings).