Implement an AI agent loop with tool calls: parse the model's tool calls, execute them, and run the loop.
Asked in the coding round (AI allowed) stage. Candidate said you just need to implement the tool call parsing and the loop itself.
Implement run_agent(initial_messages, model_outputs, tools, max_turns). model_outputs contains the assistant messages returned on successive model turns. Each tool call has id, name, and arguments, where arguments is a JSON object or JSON string. Each tools[name] entry specifies an operation: echo, add, or multiply. Append tool results as messages with role, tool_call_id, name, and content. Return the complete message list, stopping after an assistant message without tool calls or after max_turns turns.
tools.def run_agent(initial_messages, model_outputs, tools, max_turns):