In-the-Flow Agentic System Optimization for Effective Planning and Tool Use