Optimizing Cost And Latency In Agent Loops