Learning When to Plan: Efficiently Allocating Test-Time Compute for LLM Agents