MetaGPT: Multi-Agent Framework that Write Code Efficiently
MetaGPT is an LLM-based multi-agent framework designed to mirror established software engineering practices. Modern software engineering typically decompose complex workflows into distinct processes to manage complexity, enhance productivity, and ensure software quality. MetaGPT replicates this process by: (i) explicitly assigning specialized roles to agents; (ii) structuring detailed planning and task decomposition; (iii) leveraging tools for automated code generation; and (iv) facilitating iterative debugging through inter-agent cooperation.
We will now use examples and prompt templates to illustrate how MetaGPT leverages role-play, planning, cooperation, and tool use to ship software systematically.
Role-play
Role-play serves as a foundational capability within MetaGPT, shaping how individual agents function and collaborate. In MetaGPT, roles include Product Manager, Architect, Project Manager, Engineer, and QA Engineer, where the Product Manager defines requirements, the Architect designs system structure, the Project Manager oversees workflows, the Engineer implements code, and the QA Engineer ensures quality. Specifically, role-play defines the agent's identity, responsibilities, and interaction constraints through four components: (i) Name, (ii) Profile, detailing the agent's professional identity, such as a Product Manager or Software Architect, along with their area of expertise, (iii) Goal, clarifying the agent's primary tasks within the team, for instance, generating product requirements or performing code reviews; and (iv) Constraint, specifying explicit standards, such as adhering to coding conventions and ensuring modularity, readability, and maintainability. To illustrate this clearly, we take the Product Manager as an example.
Example of role-playing as a product manager
Name: Alice
Profile: Product Manager
Goal: Create a Product Requirement Document or market research/competitive product research
Constraint: Utilize the same language as the user requirements for seamless communication
Planning
In MetaGPT, planning involves systematically decomposing complex software engineering tasks into structured, actionable procedures clearly assigned to specific agent roles. For example, the Product Manager analyzes user requirements and produces a detailed Product Requirements Document (PRD) that explicitly defines product goals, user stories, and a prioritized requirement pool. This structured PRD serves as a foundational functional breakdown, guiding subsequent agents through detailed system design, task distribution, and code implementation.
Example of planning for a product requirement document
INPUT:
Agent description: You are Alice, a Product Manager responsible for analyzing user requirements and creating clear, structured Product Requirement Documents (PRDs).
High-level user requirements: Make the 2048 sliding tile number puzzle game
Task: Generate a structured Product Requirement Document (PRD) clearly including well-defined product goals that describe the intended outcomes and value proposition, detailed user stories explicitly covering core functionalities from the user's perspective, a comprehensive competitive analysis systematically identifying existing solutions and analyzing their strengths and weaknesses, and a prioritized requirement pool organizing all identified tasks by priority levels
OUTPUT:
Product Goals: Simple, responsive, user-friendly 2048 puzzle game for desktop and mobile.
User Stories: Quickly start games, slide tiles to merge numbers, clearly view scores, easily reset.
Competitive Analysis:Original 2048 is simple but limited; mobile versions have good UI but intrusive ads; Threes! is polished yet paid.
Prioritized Requirement Pool: Core gameplay mechanics (high), score tracking and reset functionality (medium), responsive design and customization (low).
Cooperation
Cooperation within MetaGPT revolves around the shared message pool mechanism, which enables structured and efficient collaboration among specialized agents. Agents publish their structured outputs, such as product requirement documents and system designs, directly into a global message pool, allowing access by other agents. Agents selectively subscribe to messages relevant to their roles, reducing information overload and ensuring efficient collaboration. This publish-subscribe approach ensures efficient division of labor. Below, we illustrate how the Product Manager assigns tasks to the Architect.
Example of the product manager assigning tasks to the architect
INPUT:
Agent description: You are Alice, a Product Manager responsible for analyzing user requirements and creating clear, structured Product Requirement Documents (PRDs)
Task: You are the Product Manager, responsible for assigning tasks to the Architect. You
must ensure all necessary information is included in your message because you are the sole source of
information for your team. When publishing messages: Clearly specify the task. Provide relevant paths,
links, environment details, programming languages, frameworks, requirements, and constraints. Address the
message explicitly to the receiving agent. Wrap the parameters in JSON format: content for
task details, and send_to for the receiving agent's name.
OUTPUT:
{
"content": "Bob (Architect), the PRD is ready at docs/PRD.md. Please draft the system design accordingly.",
"send_to": "Bob"
}
Tool use
Tool use is essential in software development, enabling efficient interaction with external resources and real-world development environments. In MetaGPT, specialized agents employ dedicated tools aligned with their roles to streamline workflows and reduce errors. Tools are strictly aligned with agent roles: Product Managers employ market analysis tools, Architects utilize design visualization tools, and Engineers leverage coding tools (Editor, Terminal, Code Review) to streamline task execution and reduce errors. Below, we provide a concrete example illustrating how the Engineer agent utilizes these tools in practice.
Example of the engineer reviewing the system design using the Editor.open_file tool
INPUT:
Agent description: You are Alex, an autonomous Engineer who writes elegant, production-ready code for game, app, and web projects. You implement features following system designs, prioritizing code quality, maintainability, and deployment readiness. <System Design provided by the Architect>
Current Task: Implement game.py according to the system design, which
includes
the Game class with grid management, scoring logic, and tile movement methods. While coding, you have
access to specialized tools such as:
Tools for Editor:
open_file(path): Opens a file and returns its content.write(path, content): Creates or overwrites a file with complete content. ...- ...
Tools for Terminal: run_command(cmd, parameters) executes shell commands
and returns output. The cmd parameter accepts any valid shell command (e.g., ls,
cat, cd).
Tools for CodeReview: review(patch_path, output_file) analyzes code changes
and generates review comments.
...
Analyze the current task, then select the most appropriate tool to
proceed. Your response must include: (1) Reason - explain why you need this tool, (2)
Tool - specify the
exact tool name (e.g., Editor.open_file), and (3) Tool Parameters - provide all required
parameters in
JSON format.
OUTPUT:
Reason: Before implementinggame.py, I need to carefully review the
complete system design. Opening the system design document allows me to reference these specifications
while coding. Tool: Editor.open_file.
Tool Parameters:
{ "path": "docs/system_design.json" }
After the LLM determines the required tool and specifies its parameters, the agent runs a shell command to execute the tool and retrieves the requested file's contents. The obtained file data is then incorporated into the agent's prompt, thereby improving the reliability of subsequent coding tasks.
If you find this work helpful, please consider citing our paper:
@article{hu2025hands,
title={Hands-on LLM-based Agents: A Tutorial for General Audiences},
author={Hu, Shuyue and Ren, Siyue and Chen, Yang and Mu, Chunjiang and Liu, Jinyi and Cui, Zhiyao and Zhang, Yiqun and Li, Hao and Zhou, Dongzhan and Xu, Jia and others},
journal={Hands-on},
volume={21},
pages={6},
year={2025}
}