To address the efficiency and scalability limitations of agent systems based on large-scale language models (LLMs), this paper proposes Polymath, a multilayered workflow agent with self-optimization capabilities. Polymath leverages the flexibility of workflow graphs and the expressive power of code-based workflows to solve a variety of real-world problems. It improves workflows by integrating multi-grid-based graph optimization and self-reflection-based evolutionary algorithms, even without labeled data. Experimental results on six benchmark datasets, including coding, mathematics, and multi-round question-answering, show that Polymath outperforms state-of-the-art baseline models by an average of 8.1%.