# tsinghuaise/alchemy architecture

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

- Interactive diagram: https://gitdiagram.com/tsinghuaise/alchemy
- Repository: https://github.com/tsinghuaise/alchemy

## Overview

1. **Overall split: two layers**
   - The repo is intentionally divided into **`ai_scientist/`** and **`research_environment/`**.
   - `ai_scientist` is the reasoning/iteration layer: it proposes hypotheses, writes code, selects hyperparameters, and learns from results.
   - `research_environment` is the execution layer: it runs experiments, schedules GPUs, executes inside containers, and collects metrics.

2. **Scientist-side control loop**
   - The entrypoint is `python -m ai_scientist` via `ai_scientist/__main__.py` and `ai_scientist/main.py`.
   - The loop described in the README is: **load seed baseline → generate hypothesis → generate code + hyperparameters → run evaluation → ingest feedback → iterate**.
   - This layer is where the LLM is used, so the architecture depends on prompt generation and response parsing more than on classical training code.

3. **Scientist-side internal subsystems**
   - `ai_scientist/agent/` contains the agent orchestration logic.
   - Key pieces are `agent.py` for the main agent flow, `context.py` for state, `evaluator.py` for judging outputs, and `response_parser.py` for extracting structured results from model responses.
   - `ai_scientist/utils/llm_client.py` is the external model boundary; it is the main dependency on an LLM provider.

4. **Prompting and knowledge memory**
   - Prompt templates live in `ai_scientist/agent/prompts/*.jinja` and are assembled by `ai_scientist/agent/prompt.py`.
   - The agent can consume **domain knowledge** from `ai_scientist/domain_knowledge/**/knowledge.md`.
   - There is also a memory/learning path under `ai_scientist/agent/memory/domain_knowledge/` and `ai_scientist/tools/knowledge_learner.py`, which suggests a feedback loop where experiment outcomes can be distilled into reusable task knowledge.

5. **Seed baseline as the starting corpus**
   - `ai_scientist/seed_baseline/` stores prior implementations for supported domains and tasks.
   - Each seed baseline typically has `algorithm.py`, `hyperparameter.yaml`, and metadata files.
   - This is the main source from which the agent derives new candidate algorithms rather than starting from scratch.

6. **Research environment orchestration**
   - `research_environment/orchestrator.py` is the high-level runtime coordinator.
   - The environment is plugin-driven: each task area has a `plugin.py` and task-local `config.yaml`.
   - Task-specific logic is separated from the core scheduler/executor so new domains can be added without changing the scientist layer.

7. **Execution boundary: containers**
   - The environment can execute through either **Docker** or **Singularity** (`research_environment/executor/docker.py`, `singularity.py`).
   - Each task ships a `container/` directory with a Dockerfile and environment definition, making runtime dependencies explicit and isolated.
   - This is a strong architectural boundary: the scientist outputs code/config, but the environment owns reproducible execution.

8. **Scheduling and resource management**
   - `research_environment/scheduler/gpu_scheduler.py` handles GPU allocation and concurrency control.
   - `research_environment/config.yaml` controls executor choice, GPU IDs, and `max_per_gpu`.
   - This means the system is built for batch/parallel experimentation rather than single-model execution.

9. **Task collector and result flow**
   - Results are gathered by `research_environment/collector/result_table.py` and task-specific parsing logic in plugins.
   - The README notes that each task plugin must implement `expand_hp()` and `parse_output()`, which is the interface between raw experiment output and the scientist’s feedback loop.
   - So the data flow is: **candidate code/config → container run → stdout/files/metrics → parser → structured results**.

10. **Task families and pipelines**
   - The repo currently includes two major published task families: **Recsys/MMRec** and **TimeSeries**.
   - TimeSeries is further split into **anomaly detection, classification, imputation, long-term forecasting, short-term forecasting**.
   - Each task family contains its own pipeline, configs, scripts, models, and dataset wiring, but follows the same orchestration pattern.

11. **Task pipelines are self-contained experiment frameworks**
   - Under each `research_environment/tasks/{Domain}/{Task}/pipeline/`, you can see:
     - dataset configs,
     - model configs,
     - experiment classes in `exp/`,
     - data loading in `data_provider/`,
     - model implementations in `models/`,
     - shared utilities in `utils/`.
   - For TimeSeries, the pipelines are broad enough to support many model families, including transformer variants, Mamba-like models, and task-specific baselines.

12. **Script-to-config conversion layer**
   - Many task pipelines include large `scripts/` trees of `.sh` launchers and `sh2yaml/` converters.
   - This indicates the project preserves an older shell-script experiment style but normalizes it into YAML configs for orchestration.
   - Architecturally, this acts as an adapter layer between legacy benchmarking workflows and the newer managed environment.

13. **External dependencies that matter**
   - The scientist layer depends on an external LLM endpoint configured in `ai_scientist/config.yaml`.
   - The environment layer depends on local GPU resources and container runtime support.
   - The repository is therefore a split-brain system: **cloud/remote inference for idea generation**, **local/HPC-style infrastructure for experiment execution**.

14. **Key diagram boundaries to show**
   - A useful architecture diagram should draw a hard boundary between:
     - **LLM-driven agent loop** on the left,
     - **task execution platform** on the right.
   - Inside the platform, show shared orchestration components first, then task plugins, then task pipelines, then containers/datasets/models.
   - The critical feedback arrow is from **experiment results back to the agent memory/evaluator**, because that is what drives the next round of algorithm generation.

Main entry points: [`ai_scientist/agent/agent.py`](https://github.com/tsinghuaise/alchemy/blob/main/ai_scientist/agent/agent.py) (Agent core), [`research_environment/collector/result_table.py`](https://github.com/tsinghuaise/alchemy/blob/main/research_environment/collector/result_table.py) (Result table), [`ai_scientist/agent/prompt.py`](https://github.com/tsinghuaise/alchemy/blob/main/ai_scientist/agent/prompt.py) (Prompt builder), [`research_environment/executor/base.py`](https://github.com/tsinghuaise/alchemy/blob/main/research_environment/executor/base.py) (Executor base)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_scientist["Scientist layer"]
  node_scientist_cli["Scientist CLI<br/>entrypoint<br/>[__main__.py]"]
  node_scientist_main["Main loop<br/>control loop<br/>[main.py]"]
  node_agent_core["Agent core<br/>orchestrator<br/>[agent.py]"]
  node_agent_state["Agent state<br/>context store<br/>[context.py]"]
  node_agent_prompts["Prompt builder<br/>prompt assembly<br/>[prompt.py]"]
  node_agent_parse["Response parser<br/>structured parsing<br/>[response_parser.py]"]
  node_agent_eval["Evaluator<br/>judge<br/>[evaluator.py]"]
  node_llm_client["LLM client<br/>model boundary<br/>[llm_client.py]"]
  node_seed_baseline[("Seed baseline<br/>starter corpus")]
  node_domain_knowledge["Domain knowledge<br/>knowledge base"]
  node_knowledge_learner["Knowledge learner<br/>memory writer"]
end

subgraph group_environment["Execution layer"]
  node_environment_orch["Orchestrator<br/>runtime coordinator<br/>[orchestrator.py]"]
  node_plugin_api["Plugin API<br/>task extension point<br/>[plugin.py]"]
  node_gpu_scheduler["GPU scheduler<br/>resource manager<br/>[gpu_scheduler.py]"]
  node_executor_base["Executor base<br/>runtime abstraction<br/>[base.py]"]
  node_docker_exec["Docker exec<br/>container backend<br/>[docker.py]"]
  node_singularity_exec["Singularity exec<br/>container backend<br/>[singularity.py]"]
  node_result_table["Result table<br/>aggregation<br/>[result_table.py]"]
end

subgraph group_recsys["Recsys tasks"]
  node_recsys_plugin["MMRec plugin<br/>task plugin<br/>[plugin.py]"]
  node_recsys_pipeline["MMRec pipeline<br/>benchmark pipeline<br/>[main.py]"]
end

subgraph group_timeseries["TimeSeries tasks"]
  node_ts_plugin["TimeSeries plugin<br/>task plugin<br/>[plugin.py]"]
  node_ts_pipeline["TS pipeline<br/>benchmark pipeline<br/>[run.py]"]
end

node_scientist_cli -->|"starts"| node_scientist_main
node_scientist_main -->|"drives"| node_agent_core
node_agent_core -->|"reads/writes"| node_agent_state
node_agent_core -->|"builds"| node_agent_prompts
node_agent_core -->|"queries"| node_llm_client
node_agent_core -->|"parses"| node_agent_parse
node_agent_core -->|"scores"| node_agent_eval
node_agent_prompts -->|"uses"| node_seed_baseline
node_agent_prompts -->|"uses"| node_domain_knowledge
node_agent_eval -->|"feeds"| node_knowledge_learner
node_knowledge_learner -->|"updates"| node_domain_knowledge
node_agent_core -->|"submits"| node_environment_orch
node_environment_orch -->|"dispatches"| node_plugin_api
node_plugin_api -->|"requests GPUs"| node_gpu_scheduler
node_gpu_scheduler -->|"allocates"| node_executor_base
node_executor_base -->|"implements"| node_docker_exec
node_executor_base -->|"implements"| node_singularity_exec
node_docker_exec -->|"runs"| node_recsys_pipeline
node_docker_exec -->|"runs"| node_ts_pipeline
node_recsys_plugin -->|"adapts"| node_recsys_pipeline
node_ts_plugin -->|"adapts"| node_ts_pipeline
node_recsys_pipeline -->|"reports"| node_result_table
node_ts_pipeline -->|"reports"| node_result_table
node_result_table -->|"feedback"| node_agent_eval
node_result_table -->|"updates"| node_agent_state

click node_scientist_cli "https://github.com/tsinghuaise/alchemy/blob/main/ai_scientist/__main__.py"
click node_scientist_main "https://github.com/tsinghuaise/alchemy/blob/main/ai_scientist/main.py"
click node_agent_core "https://github.com/tsinghuaise/alchemy/blob/main/ai_scientist/agent/agent.py"
click node_agent_state "https://github.com/tsinghuaise/alchemy/blob/main/ai_scientist/agent/context.py"
click node_agent_prompts "https://github.com/tsinghuaise/alchemy/blob/main/ai_scientist/agent/prompt.py"
click node_agent_parse "https://github.com/tsinghuaise/alchemy/blob/main/ai_scientist/agent/response_parser.py"
click node_agent_eval "https://github.com/tsinghuaise/alchemy/blob/main/ai_scientist/agent/evaluator.py"
click node_llm_client "https://github.com/tsinghuaise/alchemy/blob/main/ai_scientist/utils/llm_client.py"
click node_seed_baseline "https://github.com/tsinghuaise/alchemy/tree/main/ai_scientist/seed_baseline"
click node_domain_knowledge "https://github.com/tsinghuaise/alchemy/tree/main/ai_scientist/domain_knowledge"
click node_knowledge_learner "https://github.com/tsinghuaise/alchemy/blob/main/ai_scientist/tools/knowledge_learner.py"
click node_environment_orch "https://github.com/tsinghuaise/alchemy/blob/main/research_environment/orchestrator.py"
click node_plugin_api "https://github.com/tsinghuaise/alchemy/blob/main/research_environment/plugin.py"
click node_gpu_scheduler "https://github.com/tsinghuaise/alchemy/blob/main/research_environment/scheduler/gpu_scheduler.py"
click node_executor_base "https://github.com/tsinghuaise/alchemy/blob/main/research_environment/executor/base.py"
click node_docker_exec "https://github.com/tsinghuaise/alchemy/blob/main/research_environment/executor/docker.py"
click node_singularity_exec "https://github.com/tsinghuaise/alchemy/blob/main/research_environment/executor/singularity.py"
click node_result_table "https://github.com/tsinghuaise/alchemy/blob/main/research_environment/collector/result_table.py"
click node_recsys_plugin "https://github.com/tsinghuaise/alchemy/blob/main/research_environment/tasks/Recsys/MMRec/plugin.py"
click node_recsys_pipeline "https://github.com/tsinghuaise/alchemy/blob/main/research_environment/tasks/Recsys/MMRec/pipeline/src/main.py"
click node_ts_plugin "https://github.com/tsinghuaise/alchemy/blob/main/research_environment/tasks/TimeSeries/long_term_forecast/plugin.py"
click node_ts_pipeline "https://github.com/tsinghuaise/alchemy/blob/main/research_environment/tasks/TimeSeries/long_term_forecast/pipeline/run.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_scientist_cli,node_scientist_main,node_agent_core,node_agent_state,node_agent_prompts,node_agent_parse,node_agent_eval,node_llm_client,node_seed_baseline,node_domain_knowledge,node_knowledge_learner toneBlue
class node_environment_orch,node_plugin_api,node_gpu_scheduler,node_executor_base,node_docker_exec,node_singularity_exec,node_result_table toneAmber
class node_recsys_plugin,node_recsys_pipeline toneMint
class node_ts_plugin,node_ts_pipeline toneRose
```

## Components

### Scientist layer

LLM-driven hypothesis generation, code synthesis, evaluation, and memory.

- [Scientist CLI](https://github.com/tsinghuaise/alchemy/blob/main/ai_scientist/__main__.py): entrypoint · Package entrypoint that starts the iteration loop. · `ai_scientist/__main__.py`
- [Main loop](https://github.com/tsinghuaise/alchemy/blob/main/ai_scientist/main.py): control loop · Runs seed loading, hypothesis generation, execution, feedback, and iteration. · `ai_scientist/main.py`
- [Agent core](https://github.com/tsinghuaise/alchemy/blob/main/ai_scientist/agent/agent.py): orchestrator · Coordinates the scientist-side reasoning workflow. · `ai_scientist/agent/agent.py`
- [Agent state](https://github.com/tsinghuaise/alchemy/blob/main/ai_scientist/agent/context.py): context store · Holds mutable experiment state across iterations. · `ai_scientist/agent/context.py`
- [Prompt builder](https://github.com/tsinghuaise/alchemy/blob/main/ai_scientist/agent/prompt.py): prompt assembly · Combines templates, baseline context, and knowledge into LLM prompts. · `ai_scientist/agent/prompt.py`
- [Response parser](https://github.com/tsinghuaise/alchemy/blob/main/ai_scientist/agent/response_parser.py): structured parsing · Extracts code, hyperparameters, and decisions from model output. · `ai_scientist/agent/response_parser.py`
- [Evaluator](https://github.com/tsinghuaise/alchemy/blob/main/ai_scientist/agent/evaluator.py): judge · Scores candidate outputs and turns run results into feedback. · `ai_scientist/agent/evaluator.py`
- [LLM client](https://github.com/tsinghuaise/alchemy/blob/main/ai_scientist/utils/llm_client.py): model boundary · External model-provider interface used by the agent. · `ai_scientist/utils/llm_client.py`
- [Seed baseline](https://github.com/tsinghuaise/alchemy/tree/main/ai_scientist/seed_baseline): starter corpus · Reference implementations and hyperparameters used as the starting point. · `ai_scientist/seed_baseline`
- [Domain knowledge](https://github.com/tsinghuaise/alchemy/tree/main/ai_scientist/domain_knowledge): knowledge base · Curated domain notes consumed by prompts and memory. · `ai_scientist/domain_knowledge`
- [Knowledge learner](https://github.com/tsinghuaise/alchemy/blob/main/ai_scientist/tools/knowledge_learner.py): memory writer · Distills outcomes into reusable domain knowledge. · `ai_scientist/tools/knowledge_learner.py`

### Execution layer

GPU scheduling, containerized runs, task plugins, and result collection.

- [Orchestrator](https://github.com/tsinghuaise/alchemy/blob/main/research_environment/orchestrator.py): runtime coordinator · High-level coordinator for execution, plugins, and result flow. · `research_environment/orchestrator.py`
- [Plugin API](https://github.com/tsinghuaise/alchemy/blob/main/research_environment/plugin.py): task extension point · Task-family interface for expanding hyperparameters and parsing outputs. · `research_environment/plugin.py`
- [GPU scheduler](https://github.com/tsinghuaise/alchemy/blob/main/research_environment/scheduler/gpu_scheduler.py): resource manager · Allocates GPUs and controls concurrency. · `research_environment/scheduler/gpu_scheduler.py`
- [Executor base](https://github.com/tsinghuaise/alchemy/blob/main/research_environment/executor/base.py): runtime abstraction · Common execution interface for container backends. · `research_environment/executor/base.py`
- [Docker exec](https://github.com/tsinghuaise/alchemy/blob/main/research_environment/executor/docker.py): container backend · Runs tasks inside Docker containers. · `research_environment/executor/docker.py`
- [Singularity exec](https://github.com/tsinghuaise/alchemy/blob/main/research_environment/executor/singularity.py): container backend · Runs tasks inside Singularity containers. · `research_environment/executor/singularity.py`
- [Result table](https://github.com/tsinghuaise/alchemy/blob/main/research_environment/collector/result_table.py): aggregation · Collects and structures experiment outputs and metrics. · `research_environment/collector/result_table.py`

### Recsys tasks

MMRec benchmark pipeline and plugin wiring.

- [MMRec plugin](https://github.com/tsinghuaise/alchemy/blob/main/research_environment/tasks/Recsys/MMRec/plugin.py): task plugin · Recsys task adapter for config expansion and output parsing. · `research_environment/tasks/Recsys/MMRec/plugin.py`
- [MMRec pipeline](https://github.com/tsinghuaise/alchemy/blob/main/research_environment/tasks/Recsys/MMRec/pipeline/src/main.py): benchmark pipeline · Recommendation benchmark runtime and model zoo. · `research_environment/tasks/Recsys/MMRec/pipeline/src/main.py`

### TimeSeries tasks

Forecasting, classification, imputation, and anomaly pipelines.

- [TimeSeries plugin](https://github.com/tsinghuaise/alchemy/blob/main/research_environment/tasks/TimeSeries/long_term_forecast/plugin.py): task plugin · TimeSeries adapter for the shared orchestration contract. · `research_environment/tasks/TimeSeries/long_term_forecast/plugin.py`
- [TS pipeline](https://github.com/tsinghuaise/alchemy/blob/main/research_environment/tasks/TimeSeries/long_term_forecast/pipeline/run.py): benchmark pipeline · Shared pipeline for forecasting, classification, imputation, and anomaly tasks. · `research_environment/tasks/TimeSeries/long_term_forecast/pipeline/run.py`

## Connections

- Scientist CLI → Main loop: starts · CLI entrypoint launches the main loop.
- Main loop → Agent core: drives · Main program delegates iteration to the agent.
- Agent core → Agent state: reads/writes · Agent updates experiment context over time.
- Agent core → Prompt builder: builds · Agent assembles prompts for hypothesis and code generation.
- Agent core → LLM client: queries · Agent calls the external language model.
- Agent core → Response parser: parses · Structured outputs are extracted from model responses.
- Agent core → Evaluator: scores · Candidate results are judged before the next iteration.
- Prompt builder → Seed baseline: uses · Baseline implementations seed prompt context.
- Prompt builder → Domain knowledge: uses · Curated knowledge is injected into prompts.
- Evaluator → Knowledge learner: feeds · Evaluation outcomes can be distilled into memory.
- Knowledge learner → Domain knowledge: updates · Learner writes reusable domain knowledge.
- Agent core → Orchestrator: submits · Scientist sends candidate code/config for execution.
- Orchestrator → Plugin API: dispatches · Orchestrator routes work through task plugins.
- Plugin API → GPU scheduler: requests GPUs · Plugins rely on shared scheduling for parallel runs.
- GPU scheduler → Executor base: allocates · Scheduler hands work to an execution backend.
- Executor base → Docker exec: implements · Docker is one concrete execution backend.
- Executor base → Singularity exec: implements · Singularity is the other concrete execution backend.
- Docker exec → MMRec pipeline: runs · Container backend executes the Recsys benchmark pipeline.
- Docker exec → TS pipeline: runs · Container backend executes the TimeSeries benchmark pipeline.
- MMRec plugin → MMRec pipeline: adapts · Recsys plugin connects orchestration to the pipeline.
- TimeSeries plugin → TS pipeline: adapts · TimeSeries plugin connects orchestration to the pipeline.
- MMRec pipeline → Result table: reports · Pipeline outputs are summarized into structured results.
- TS pipeline → Result table: reports · Pipeline outputs are summarized into structured results.
- Result table → Evaluator: feedback · Collected results feed back into evaluation.
- Result table → Agent state: updates · Structured outcomes are stored in agent context.
