Coding Question 45 minutesHard
Stock Research Agent
Repair a tool-using research agent, then make its execution loop cheaper and faster.
Question
Repair the stock-research implementation in research_agent.py. It must answer the requested historical-price and YTD-return question while following the model's multi-tool message protocol exactly and efficiently.
Function parameters
- price_api
- For get_stock_price: exposes close_on(symbol, date); the requested date must be forwarded unchanged.
- first_close / latest_close
- For get_ytd_return: numeric first trading close of the year and latest close.
- client
- For run_agent: model client called as create(system=SYSTEM_PROMPT, messages=messages).
- tools
- For run_agent: dictionary from requested tool name to a callable accepting the supplied input fields.
- user_request
- For run_agent: original question placed in the initial user message.
Inputs and API responses
Messages API
| Entity | Fields |
|---|---|
| Response | stop_reason, content[] |
| ToolUseBlock | id, type, name, input |
| ToolResultBlock | type, tool_use_id, content |
- One assistant response may request several tools.
- Stop only when stop_reason is end_turn.
Market data
| Entity | Fields |
|---|---|
| PriceAPI | close_on(symbol, date) |
| Tools | get_stock_price, get_ytd_return |
- Historical date arguments are meaningful.
- Return percentages rounded to two decimals.
Agent contract
- tool_result.tool_use_id must match the originating ToolUseBlock.id
- Execute all tool calls from one response before the next model call
- The loop continues until the model returns end_turn
Requirements
- get_stock_price must use the requested date.
- get_ytd_return must calculate (latest - first) / first * 100.
- Append the complete assistant response before returning tool results.
- Return one tool_result for every tool_use block, preserving order.
- Use tool_use_id, not id, in tool-result dictionaries.
- Do not call the model once per tool; batch results into the next turn.
- A program can run without exceptions and still violate the Messages API protocol or silently answer a different financial question.
Editable
Test output
Run the challenge to validate the agent protocol, calculations, and model-turn count.