TLDR: GasAgent is a multi-agent AI framework that automates the optimization of smart contract Gas fees. It uses four specialized agents (Seeker, Innovator, Executor, Manager) to identify existing Gas waste patterns, discover new ones, validate changes, and manage the process in a closed loop. Experiments show it successfully optimizes a high percentage of real-world and LLM-generated contracts, significantly reducing deployment Gas costs and demonstrating a new approach to efficient smart contract development.
Smart contracts are the backbone of decentralized applications on the blockchain, enabling trustworthy and automatic execution of predefined logic. However, their operation isn’t free; it requires a mechanism called ‘Gas’ to ensure efficiency and fairness. Unfortunately, many smart contracts are not written with optimal coding practices, leading to significant ‘Gas waste’ – unnecessary costs incurred during deployment and execution.
Traditionally, optimizing these Gas costs has been a manual, labor-intensive process, relying on human experts to identify and fix inefficiencies. While recent advancements have explored using large language models (LLMs) to find new Gas waste patterns, these models often struggle with compatibility, produce redundant suggestions, and require extensive manual validation.
To bridge this gap, researchers Jingyi Zheng, Zifan Peng, Yule Liu, Junfeng Wang, Yifan Liao, Wenhan Dong, and Xinlei He from The Hong Kong University of Science and Technology (Guangzhou) have introduced a groundbreaking solution called GasAgent. This innovative framework is the first multi-agent system designed for automated Gas optimization in smart contracts, combining the ability to work with existing optimization patterns and automatically discover and validate new ones, offering an end-to-end solution.
How GasAgent Works: A Collaborative Approach
GasAgent operates through a sophisticated collaboration of four specialized AI agents, working in a continuous loop to identify, validate, and apply Gas-saving improvements:
-
Seeker: This agent acts like an experienced inspector. It identifies known Gas inefficiencies by comparing the target smart contract against a continuously updated library of existing Gas waste patterns. It uses both code and natural language similarity to find relevant patterns and generates reports on potential improvements.
-
Innovator: The creative developer of the team, the Innovator focuses on discovering new or improved Gas-saving patterns that aren’t yet in the library. It uses the context provided by the Seeker to propose novel optimizations, ensuring they don’t duplicate existing suggestions and are actionable.
-
Executor: This agent is the reliable craftsman. It applies the suggested changes from both the Seeker and Innovator by refactoring the contract code. Crucially, it then rigorously verifies every change through security audits, consistency checks (to ensure functionality remains intact), and actual Gas cost comparisons. Only safe and effective optimizations are kept.
-
Manager: The team leader, the Manager oversees the entire process. It handles interactions, collects results from the other agents, and decides when the optimization loop should terminate. It also generates comprehensive reports for human review, making the process transparent.
This closed-loop system allows GasAgent to continuously learn and adapt, integrating newly discovered and validated patterns into its library, much like an experienced developer refines their knowledge over time.
Impressive Results in the Real World
The effectiveness of GasAgent was put to the test on 100 verified real-world smart contracts. The results were compelling: GasAgent successfully optimized 82% of these contracts, achieving an average deployment Gas savings of 9.97%. Some contracts saw savings of over 30%.
Beyond applying known patterns, the Innovator agent automatically proposed 68 new Gas waste patterns. These included entirely original patterns, such as “Bitmap Role Management” (which efficiently packs multiple roles into a single storage slot), and “Sub-patterns” that refined existing optimization concepts, like “Immutable Metadata Fields” (declaring certain data as unchangeable to save storage costs). These discoveries highlight GasAgent’s ability to go beyond predefined rules and find novel ways to save Gas.
GasAgent also demonstrated strong compatibility with prior research, effectively recalling 92.5% of existing pattern instances while significantly reducing the computational effort needed for detection.
Also Read:
- LLMs Learn to Think Smarter with Hierarchical Budget Policy Optimization
- Automating Challenging Traffic Scenarios with LLM Agents
Optimizing AI-Generated Contracts
In a further evaluation, GasAgent was applied to 500 smart contracts generated by five different large language models. It successfully optimized nearly 80% of these contracts, with average savings ranging from 4.79% to 13.93% depending on the LLM and task complexity. This shows GasAgent’s practical utility as an automated optimization layer for LLM-assisted smart contract development, helping to clean up residual inefficiencies left by AI-generated code.
In conclusion, GasAgent represents a significant leap forward in smart contract optimization. By combining the strengths of multiple AI agents, it automates a complex and crucial task, making blockchain applications more cost-effective and efficient. This framework promises to be a valuable tool for developers aiming to build high-performance smart contracts. You can learn more about this research in the paper: GasAgent: A Multi-Agent Framework for Automated Gas Optimization in Smart Contracts.


