# foundationagents/metagpt architecture

> Architecture diagram of the GitHub repository foundationagents/metagpt, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-06-25). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/foundationagents/metagpt
- Repository: https://github.com/foundationagents/metagpt

## Overview

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: [`metagpt/roles/role.py`](https://github.com/foundationagents/metagpt/blob/main/metagpt/roles/role.py) (Role base), [`metagpt/actions/action.py`](https://github.com/foundationagents/metagpt/blob/main/metagpt/actions/action.py) (Action base), [`metagpt/ext/__init__.py`](https://github.com/foundationagents/metagpt/blob/main/metagpt/ext/__init__.py) (Extensions), [`metagpt/actions/write_prd.py`](https://github.com/foundationagents/metagpt/blob/main/metagpt/actions/write_prd.py) (Action suites)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_core["Core runtime"]
  node_startup["Startup<br/>entry/config<br/>[startup.py]"]
  node_company["Software company<br/>orchestrator"]
  node_team["Team<br/>coordination<br/>[team.py]"]
  node_role_base["Role base<br/>agent runtime<br/>[role.py]"]
  node_action_base["Action base<br/>step primitive<br/>[action.py]"]
  node_role_spine["Core roles<br/>domain roles<br/>[product_manager.py]"]
  node_action_pipelines["Action suites<br/>workflow steps<br/>[write_prd.py]"]
end

subgraph group_state["State & prompts"]
  node_provider[("LLM providers<br/>model adapter<br/>[__init__.py]")]
  node_prompts["Prompts<br/>prompt assets<br/>[__init__.py]"]
  node_skills["Skills<br/>skill assets<br/>[skprompt.txt]"]
  node_memory[("Memory<br/>state storage<br/>[memory.py]")]
  node_context["Context<br/>conversation state<br/>[context.py]"]
end

subgraph group_knowledge["Knowledge systems"]
  node_repo_parser["Repo parser<br/>code ingestion<br/>[repo_parser.py]"]
  node_document["Documents<br/>document ingestion<br/>[document.py]"]
  node_rag["RAG stack<br/>retrieval pipeline<br/>[__init__.py]"]
end

subgraph group_runtime["Tools & environments"]
  node_tools["Tools<br/>automation adapters<br/>[__init__.py]"]
  node_envs["Environments<br/>simulated worlds<br/>[__init__.py]"]
end

subgraph group_extensions["Extensions & experiments"]
  node_exp_pool["Exp pool<br/>evaluation framework<br/>[__init__.py]"]
  node_strategy["Strategy<br/>search/solver<br/>[__init__.py]"]
  node_ext_packages["Extensions<br/>vertical modules<br/>[__init__.py]"]
end

node_startup -->|"bootstraps"| node_company
node_startup -->|"creates"| node_team
node_company -->|"orchestrates"| node_team
node_team -->|"manages"| node_role_base
node_role_base -->|"executes"| node_action_base
node_role_spine -->|"extends"| node_role_base
node_action_pipelines -->|"extends"| node_action_base
node_role_base -->|"calls"| node_provider
node_action_base -->|"calls"| node_provider
node_role_base -->|"uses"| node_prompts
node_action_base -->|"uses"| node_prompts
node_role_base -->|"uses"| node_skills
node_role_base -->|"persists"| node_memory
node_role_base -->|"shares"| node_context
node_action_pipelines -->|"ingests"| node_repo_parser
node_action_pipelines -->|"ingests"| node_document
node_action_pipelines -->|"retrieves"| node_rag
node_action_base -->|"operates"| node_tools
node_role_base -->|"acts in"| node_envs
node_exp_pool -->|"feeds"| node_strategy
node_ext_packages -->|"reuses"| node_role_base
node_ext_packages -->|"reuses"| node_action_base
node_ext_packages -->|"reuses"| node_memory
node_ext_packages -->|"reuses"| node_tools
node_ext_packages -->|"targets"| node_envs

click node_startup "https://github.com/foundationagents/metagpt/blob/main/metagpt/startup.py"
click node_company "https://github.com/foundationagents/metagpt/blob/main/metagpt/software_company.py"
click node_team "https://github.com/foundationagents/metagpt/blob/main/metagpt/team.py"
click node_role_base "https://github.com/foundationagents/metagpt/blob/main/metagpt/roles/role.py"
click node_action_base "https://github.com/foundationagents/metagpt/blob/main/metagpt/actions/action.py"
click node_role_spine "https://github.com/foundationagents/metagpt/blob/main/metagpt/roles/product_manager.py"
click node_action_pipelines "https://github.com/foundationagents/metagpt/blob/main/metagpt/actions/write_prd.py"
click node_provider "https://github.com/foundationagents/metagpt/blob/main/metagpt/provider/__init__.py"
click node_prompts "https://github.com/foundationagents/metagpt/blob/main/metagpt/prompts/__init__.py"
click node_skills "https://github.com/foundationagents/metagpt/blob/main/metagpt/skills/SummarizeSkill/Summarize/skprompt.txt"
click node_memory "https://github.com/foundationagents/metagpt/blob/main/metagpt/memory/memory.py"
click node_context "https://github.com/foundationagents/metagpt/blob/main/metagpt/context.py"
click node_repo_parser "https://github.com/foundationagents/metagpt/blob/main/metagpt/repo_parser.py"
click node_document "https://github.com/foundationagents/metagpt/blob/main/metagpt/document.py"
click node_rag "https://github.com/foundationagents/metagpt/blob/main/metagpt/rag/__init__.py"
click node_tools "https://github.com/foundationagents/metagpt/blob/main/metagpt/tools/__init__.py"
click node_envs "https://github.com/foundationagents/metagpt/blob/main/metagpt/environment/__init__.py"
click node_exp_pool "https://github.com/foundationagents/metagpt/blob/main/metagpt/exp_pool/__init__.py"
click node_strategy "https://github.com/foundationagents/metagpt/blob/main/metagpt/strategy/__init__.py"
click node_ext_packages "https://github.com/foundationagents/metagpt/blob/main/metagpt/ext/__init__.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_startup,node_company,node_team,node_role_base,node_action_base,node_role_spine,node_action_pipelines toneBlue
class node_provider,node_prompts,node_skills,node_memory,node_context toneAmber
class node_repo_parser,node_document,node_rag toneMint
class node_tools,node_envs toneRose
class node_exp_pool,node_strategy,node_ext_packages toneIndigo
```

## Components

### Core runtime

Main agent orchestration and execution spine.

- [Startup](https://github.com/foundationagents/metagpt/blob/main/metagpt/startup.py): entry/config · Loads config and bootstraps the runtime. · `metagpt/startup.py`
- [Software company](https://github.com/foundationagents/metagpt/blob/main/metagpt/software_company.py): orchestrator · High-level company-style workflow assembly. · `metagpt/software_company.py`
- [Team](https://github.com/foundationagents/metagpt/blob/main/metagpt/team.py): coordination · Coordinates roles and task execution. · `metagpt/team.py`
- [Role base](https://github.com/foundationagents/metagpt/blob/main/metagpt/roles/role.py): agent runtime · Shared role state and lifecycle. · `metagpt/roles/role.py`
- [Action base](https://github.com/foundationagents/metagpt/blob/main/metagpt/actions/action.py): step primitive · Atomic LLM/tool-powered unit of work. · `metagpt/actions/action.py`
- [Core roles](https://github.com/foundationagents/metagpt/blob/main/metagpt/roles/product_manager.py): domain roles · Primary agent personas such as product, architect, engineer, QA, research, and support. · `metagpt/roles/product_manager.py`
- [Action suites](https://github.com/foundationagents/metagpt/blob/main/metagpt/actions/write_prd.py): workflow steps · 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](https://github.com/foundationagents/metagpt/blob/main/metagpt/provider/__init__.py): model adapter · Unified boundary for OpenAI-like, Anthropic, Bedrock, Gemini, Ollama, human, and other backends. · `metagpt/provider/__init__.py`
- [Prompts](https://github.com/foundationagents/metagpt/blob/main/metagpt/prompts/__init__.py): prompt assets · Role- and action-specific instruction templates. · `metagpt/prompts/__init__.py`
- [Skills](https://github.com/foundationagents/metagpt/blob/main/metagpt/skills/SummarizeSkill/Summarize/skprompt.txt): skill assets · Packaged prompt skills with configs and templates. · `metagpt/skills/SummarizeSkill/Summarize/skprompt.txt`
- [Memory](https://github.com/foundationagents/metagpt/blob/main/metagpt/memory/memory.py): state storage · Short-term, long-term, and role-specific memory. · `metagpt/memory/memory.py`
- [Context](https://github.com/foundationagents/metagpt/blob/main/metagpt/context.py): conversation state · Shared step-to-step context and persistence helpers. · `metagpt/context.py`

### Knowledge systems

Repository/document ingestion and retrieval-augmented generation.

- [Repo parser](https://github.com/foundationagents/metagpt/blob/main/metagpt/repo_parser.py): code ingestion · Parses repositories and project artifacts into structured context. · `metagpt/repo_parser.py`
- [Documents](https://github.com/foundationagents/metagpt/blob/main/metagpt/document.py): document ingestion · Handles document and content normalization for downstream reasoning. · `metagpt/document.py`
- [RAG stack](https://github.com/foundationagents/metagpt/blob/main/metagpt/rag/__init__.py): retrieval pipeline · 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](https://github.com/foundationagents/metagpt/blob/main/metagpt/tools/__init__.py): automation adapters · Browser, shell, git, editor, search, deployment, OCR, TTS, and other side-effecting tools. · `metagpt/tools/__init__.py`
- [Environments](https://github.com/foundationagents/metagpt/blob/main/metagpt/environment/__init__.py): simulated worlds · Software, Android, Minecraft, Stanford Town, Werewolf, and API environments. · `metagpt/environment/__init__.py`

### Extensions & experiments

Packaged vertical solutions and evaluation/optimization subsystems.

- [Exp pool](https://github.com/foundationagents/metagpt/blob/main/metagpt/exp_pool/__init__.py): evaluation framework · Context builders, scorers, judges, and serializers for experimentation. · `metagpt/exp_pool/__init__.py`
- [Strategy](https://github.com/foundationagents/metagpt/blob/main/metagpt/strategy/__init__.py): search/solver · Planning and solver components for search-driven optimization. · `metagpt/strategy/__init__.py`
- [Extensions](https://github.com/foundationagents/metagpt/blob/main/metagpt/ext/__init__.py): vertical modules · Packaged solutions like Android assistant, code review, Stanford Town, Werewolf, AFLOW, SPO, and SELA. · `metagpt/ext/__init__.py`

## Connections

- 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.
