How foundationagents/metagpt works
1. Overall shape
MetaGPT is a Python multi-agent framework centered on the idea that a “software company” of LLM-driven roles can turn a natural-language requirement into a structured project deliverable: PRD, design, tasks, code, tests, docs, and sometimes a generated repo. The core package is metagpt/; examples/ and tests/ demonstrate and validate the subsystems.
2. Primary orchestration layer
The main coordination boundary is team.py, software_company.py, roles/, and actions/. A user request enters through CLI/library startup, is assigned to roles such as product manager, architect, project manager, engineer, QA, researcher, etc., and each role executes a sequence of actions. The team/role/action layering is the key architectural spine.
3. Role-action execution model
Roles encapsulate behavior and state; actions are the atomic LLM-powered or tool-powered steps. Many actions are domain-specific pipelines like requirement analysis, API design, code generation, testing, code review, doc generation, and repo parsing. The architecture is intentionally modular so the same orchestration shell can drive different workflows.
4. LLM provider abstraction
provider/ is the model boundary. It contains adapters for OpenAI, Azure OpenAI, Anthropic, Bedrock, Gemini, Ollama, Groq/other providers, plus a human provider. This layer normalizes requests/responses, streaming, retries, post-processing, and provider-specific auth/config. All higher layers depend on this abstraction rather than calling model APIs directly.
5. Prompting and skill assets
Prompt templates live in prompts/ and skills/. Skills are structured prompt packages with configs and templates, while prompts are role/action-specific instructions. These assets are treated as first-class inputs to the agent workflows, not just static text files.
6. Memory and context
memory/, context.py, and context_mixin.py manage short-term conversation state, longer-term memory, and role-specific persistence. This is the main state boundary between steps in a multi-turn workflow. Some extensions add specialized memory stores, such as the Stanford Town simulation memory and role-zero memory.
7. Document and repository intelligence
The framework can inspect source repositories and documents through repo_parser.py, document.py, and utilities under utils/. It converts existing codebases, README files, PDFs, docx, HTML, and other inputs into structured context for analysis, summarization, requirement extraction, and incremental development. This is a major ingestion boundary feeding the agent pipeline.
8. RAG subsystem
rag/ is a separate retrieval-augmented generation stack with parsers, retrievers, rankers, factories, and engines. It supports BM25, FAISS, Chroma, Elasticsearch, and hybrid retrieval, plus document parsing via OmniParse. This subsystem is used when agents need grounded retrieval over external corpora instead of pure prompt context.
9. Tools and automation adapters
tools/ wraps external capabilities such as web search, browser automation, shell/terminal execution, git, code editing, linting, deployment, text-to-image, OCR, translation, and TTS. These tools are consumed by actions and roles, especially engineering-oriented flows like SWE-agent-style tasks and data interpretation. This layer is the boundary between LLM reasoning and real-world side effects.
10. Environment simulators and execution worlds
environment/ contains multiple simulated or integrated execution environments: software projects, Android, Minecraft, Stanford Town, Werewolf, and an API-driven environment. These are not just utilities; they are alternative “worlds” in which agent roles act, observe, and update state. Some environments include external runtimes, such as the JavaScript/mineflayer bridge for Minecraft.
11. Evaluation, benchmarking, and experience pooling
exp_pool/, strategy/, and extension packages such as ext/aflow, ext/spo, and ext/sela support experimentation, optimization, and benchmarking. They provide context builders, scorers, judges, planners, search/solver components, and benchmark runners. This is the research/optimization boundary separate from the main agent runtime.
12. Extension packages
metagpt/ext/ contains packaged vertical solutions built on the core framework: Android assistant, code review, Stanford Town simulation, Werewolf game, AFLOW optimization, SPO, and SELA. These extensions reuse the same core abstractions—roles, actions, memory, prompts, tools—but specialize them for distinct domains and workflows.
13. Configuration and deployment
Runtime behavior is driven by YAML configs in config/ and user-level ~/.metagpt/config2.yaml. The project also includes Docker/devcontainer support, CI workflows, and generated docs/OpenAPI artifacts. Configuration governs LLM selection, search providers, embeddings, storage backends, and environment-specific behavior.
14. Typical end-to-end flow
A request enters via CLI or library API, is routed through startup/config into a team of roles, each role invokes a chain of actions, those actions call LLM providers and tools, consult memory/RAG/repo context as needed, and finally emit documents, code, or a generated project repository. The important architectural boundaries are: orchestration, model access, memory/context, retrieval, tools, and environment execution.
Main entry points: role.py Role base · action.py Action base · __init__.py Extensions · write_prd.py Action suites
Components 20
Core runtime
Main agent orchestration and execution spine.
- Startup · Loads config and bootstraps the runtime.
metagpt/startup.py - Software company · High-level company-style workflow assembly.
metagpt/software_company.py - Team · Coordinates roles and task execution.
metagpt/team.py - Role base · Shared role state and lifecycle.
metagpt/roles/role.py - Action base · Atomic LLM/tool-powered unit of work.
metagpt/actions/action.py - Core roles · Primary agent personas such as product, architect, engineer, QA, research, and support.
metagpt/roles/product_manager.py - Action suites · Major action pipelines for PRD, design, code, tests, docs, review, parsing, and analysis.
metagpt/actions/write_prd.py
State & prompts
Memory, context, prompt, and skill assets used by roles and actions.
- LLM providers · Unified boundary for OpenAI-like, Anthropic, Bedrock, Gemini, Ollama, human, and other backends.
metagpt/provider/__init__.py - Prompts · Role- and action-specific instruction templates.
metagpt/prompts/__init__.py - Skills · Packaged prompt skills with configs and templates.
metagpt/skills/SummarizeSkill/Summarize/skprompt.txt - Memory · Short-term, long-term, and role-specific memory.
metagpt/memory/memory.py - Context · Shared step-to-step context and persistence helpers.
metagpt/context.py
Knowledge systems
Repository/document ingestion and retrieval-augmented generation.
- Repo parser · Parses repositories and project artifacts into structured context.
metagpt/repo_parser.py - Documents · Handles document and content normalization for downstream reasoning.
metagpt/document.py - RAG stack · Retrieval, ranking, parsing, and engine layer for grounded generation.
metagpt/rag/__init__.py
Tools & environments
External tools and simulated worlds used for side effects and execution.
- Tools · Browser, shell, git, editor, search, deployment, OCR, TTS, and other side-effecting tools.
metagpt/tools/__init__.py - Environments · Software, Android, Minecraft, Stanford Town, Werewolf, and API environments.
metagpt/environment/__init__.py
Extensions & experiments
Packaged vertical solutions and evaluation/optimization subsystems.
- Exp pool · Context builders, scorers, judges, and serializers for experimentation.
metagpt/exp_pool/__init__.py - Strategy · Planning and solver components for search-driven optimization.
metagpt/strategy/__init__.py - Extensions · Packaged solutions like Android assistant, code review, Stanford Town, Werewolf, AFLOW, SPO, and SELA.
metagpt/ext/__init__.py
Connections 25
- Startup → Software company: bootstraps · Startup initializes the main application flow.
- Startup → Team: creates · Startup can construct teams for workflows.
- Software company → Team: orchestrates · Software company coordinates the team boundary.
- Team → Role base: manages · Teams operate through role instances.
- Role base → Action base: executes · Roles run ordered actions.
- Core roles → Role base: extends · Concrete domain roles build on the shared role base.
- Action suites → Action base: extends · Concrete workflows specialize the action primitive.
- Role base → LLM providers: calls · Roles invoke LLM providers through the abstraction.
- Action base → LLM providers: calls · Actions use provider adapters for model access.
- Role base → Prompts: uses · Roles consume prompt templates.
- Action base → Prompts: uses · Actions are guided by prompt assets.
- Role base → Skills: uses · Roles may load reusable skills.
- Role base → Memory: persists · Roles read and write memory across steps.
- Role base → Context: shares · Roles carry workflow context.
- Action suites → Repo parser: ingests · Repository-aware actions parse source projects.
- Action suites → Documents: ingests · Document-focused actions normalize external files.
- Action suites → RAG stack: retrieves · Some actions use retrieval-backed grounding.
- Action base → Tools: operates · Tool-using actions connect reasoning to side effects.
- Role base → Environments: acts in · Roles may operate inside simulated or integrated environments.
- Exp pool → Strategy: feeds · Evaluation artifacts support search and optimization workflows.
- Extensions → Role base: reuses · Extensions are built from the core role abstraction.
- Extensions → Action base: reuses · Extensions specialize the same action model.
- Extensions → Memory: reuses · Extensions reuse memory and state handling.
- Extensions → Tools: reuses · Extensions call into shared tool adapters.
- Extensions → Environments: targets · Many extensions are tied to specific environments.