# baidu-baige/loongflow architecture

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

- Interactive diagram: https://gitdiagram.com/baidu-baige/loongflow
- Repository: https://github.com/baidu-baige/loongflow

## Overview

#### Purpose and entry points
LoongFlow is a Python 3.12+ framework for expert-grade AI agents that iterate through Plan–Execute–Summary (PES), retain experience, and improve candidate solutions. Its primary programmatic surfaces are `PESAgent` and `ReActAgent`; runnable domain agents are launched through `run_general.sh`, `run_math.sh`, `run_ml.sh`, and `run_mlebench.sh`. Package configuration is in `pyproject.toml`.

#### Core framework: PES orchestration
The PES framework is the central long-horizon control loop: it registers domain-specific planner, executor, and summary workers, invokes them iteratively, and finalizes results. It also owns checkpoint/workspace context and the evolving solution database boundary.
- `src/loongflow/framework/pes/pes_agent.py`
- `src/loongflow/framework/pes/base_runner.py`
- `src/loongflow/framework/pes/database/database.py`

#### Planning, execution, evaluation, and summarization stages
PES separates deliberate planning from side-effecting execution and post-run reflection. Planner workers produce candidate plans; executors run experiments/code; evaluators score outputs; summaries extract reusable findings for the next iteration. This separation lets domain agents replace stage implementations without replacing the loop.
- `src/loongflow/framework/pes/planner/planner.py`
- `src/loongflow/framework/pes/executor/executor.py`
- `src/loongflow/framework/pes/summary/summary.py`

#### Agent SDK boundaries
The SDK abstracts messages, model invocation, tools, logging, and token accounting from agent logic. Models use a LiteLLM-oriented adapter and formatter, allowing OpenAI-compatible, Anthropic-compatible, local vLLM, and sglang endpoints to sit behind one request/response boundary. Tools provide controlled filesystem, shell, code-execution, function, and sub-agent capabilities.
- `src/loongflow/agentsdk/models/litellm_model.py`
- `src/loongflow/agentsdk/tools/toolkit.py`
- `src/loongflow/agentsdk/message/message.py`

#### Memory and learning
Two memory families support accumulated experience. Evolution memory offers in-memory, Redis-backed, and Boltzmann-based selection mechanisms; grade memory persists/compresses structured context using file or in-memory storage. These are the main persistence and retrieval boundary between PES iterations and runs.
- `src/loongflow/agentsdk/memory/evolution/memory_factory.py`
- `src/loongflow/agentsdk/memory/evolution/boltzmann.py`
- `src/loongflow/agentsdk/memory/grade/memory.py`

#### ReAct path for interactive tool use
Alongside PES, ReAct provides a shorter reasoning/action loop with default reasoner, actor, observer, and finalizer components. It is appropriate where continuous tool-mediated interaction is more important than evolutionary planning; it shares the SDK model/message/tool abstractions.
- `src/loongflow/framework/react/react_agent.py`
- `src/loongflow/framework/react/components/default_reasoner.py`
- `src/loongflow/framework/react/components/default_actor.py`

#### Domain agent implementations
Three domain packages bind framework stages, prompts, evaluation, and execution policies to concrete workloads. General Agent targets skill-driven coding tasks; Math Agent evolves mathematical algorithms; ML Agent performs autonomous ML experimentation, including MLE-bench workflows. Their task configuration and evaluation programs form the boundary to user-provided problems, datasets, and scoring.
- `agents/general_agent/general_evolve_agent.py`
- `agents/math_agent/math_evolve_agent.py`
- `agents/ml_agent/ml_evolve_agent.py`

#### Runtime, infrastructure, and observability
Runs are local Python processes, optionally backgrounded by launcher scripts, with task artifacts/checkpoints written to output directories. Redis is an optional external memory backend; LLM APIs are required for agent reasoning. Lightweight visualization servers expose evolution trees, performance, diffs, and island distributions from checkpoints.
- `src/loongflow/agentsdk/memory/evolution/redis_memory.py`
- `agents/math_agent/visualizer/visualizer.py`
- `agents/general_agent/visualizer/visualizer.py`

Main entry points: [`src/loongflow/framework/pes/pes_agent.py`](https://github.com/baidu-baige/loongflow/blob/main/src/loongflow/framework/pes/pes_agent.py) (PES Agent), [`src/loongflow/framework/pes/planner/planner.py`](https://github.com/baidu-baige/loongflow/blob/main/src/loongflow/framework/pes/planner/planner.py) (Planner), [`run_general.sh`](https://github.com/baidu-baige/loongflow/blob/main/run_general.sh) (Run scripts), [`src/loongflow/framework/pes/summary/summary.py`](https://github.com/baidu-baige/loongflow/blob/main/src/loongflow/framework/pes/summary/summary.py) (Summary)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_pes["PES Framework"]
  node_pes_agent{{"PES Agent<br/>orchestrator<br/>[pes_agent.py]"}}
  node_base_runner["Base Runner<br/>run context<br/>[base_runner.py]"]
  node_planner["Planner<br/>PES worker<br/>[planner.py]"]
  node_executor["Executor<br/>PES worker<br/>[executor.py]"]
  node_summary["Summary<br/>PES worker<br/>[summary.py]"]
  node_solution_db[("Solution Database<br/>solution state<br/>[database.py]")]
end

subgraph group_sdk["Agent SDK &amp; Memory"]
  node_model_adapter["LiteLLM Model Adapter<br/>model gateway<br/>[litellm_model.py]"]
  node_messages["Messages<br/>message abstraction<br/>[message.py]"]
  node_toolkit["Toolkit<br/>controlled tools<br/>[toolkit.py]"]
  node_memory[("Learning Memory<br/>memory boundary<br/>[memory_factory.py]")]
  node_grade_memory[("Grade Memory<br/>structured context store<br/>[memory.py]")]
  node_redis_memory[("Redis Memory<br/>optional backend<br/>[redis_memory.py]")]
end

subgraph group_react["ReAct Framework"]
  node_react_agent{{"ReAct Agent<br/>interactive orchestrator<br/>[react_agent.py]"}}
  node_react_reasoner["Default Reasoner<br/>ReAct component"]
  node_react_actor["Default Actor<br/>ReAct component<br/>[default_actor.py]"]
end

subgraph group_domains["Domain Agents"]
  node_domain_tasks["Tasks, datasets &amp; scoring<br/>user workload boundary"]
  node_general_agent["General Agent<br/>domain PES agent"]
  node_math_agent["Math Agent<br/>domain PES agent"]
  node_ml_agent["ML Agent<br/>domain PES agent<br/>[ml_evolve_agent.py]"]
end

node_launchers["Run scripts<br/>shell launchers<br/>[run_general.sh]"]
node_llm_apis(("LLM APIs<br/>external inference"))
node_visualizers["Checkpoint Visualizers<br/>local visualization<br/>[visualizer.py]"]

node_launchers -->|"launches"| node_general_agent
node_launchers -->|"launches"| node_math_agent
node_launchers -->|"launches"| node_ml_agent
node_general_agent -->|"binds stages"| node_pes_agent
node_math_agent -->|"binds stages"| node_pes_agent
node_ml_agent -->|"binds stages"| node_pes_agent
node_domain_tasks -->|"coding tasks"| node_general_agent
node_domain_tasks -->|"problems and scoring"| node_math_agent
node_domain_tasks -->|"datasets and scoring"| node_ml_agent
node_pes_agent -->|"uses run context"| node_base_runner
node_pes_agent -->|"plan"| node_planner
node_planner -->|"candidate plans"| node_executor
node_executor -->|"outputs and scores"| node_summary
node_summary -->|"findings for next iteration"| node_planner
node_pes_agent -->|"evolves solutions"| node_solution_db
node_summary -->|"stores experience"| node_memory
node_memory -.->|"retrieved experience"| node_planner
node_memory -.->|"optional backend"| node_redis_memory
node_grade_memory -.->|"structured context"| node_pes_agent
node_pes_agent -->|"agent inference"| node_model_adapter
node_model_adapter -->|"requests"| node_llm_apis
node_pes_agent -->|"controlled execution"| node_toolkit
node_react_agent -->|"reasons"| node_react_reasoner
node_react_reasoner -->|"actions"| node_react_actor
node_react_actor -->|"uses tools"| node_toolkit
node_react_agent -->|"shared inference"| node_model_adapter
node_visualizers -.->|"reads checkpoints"| node_base_runner

click node_launchers "https://github.com/baidu-baige/loongflow/blob/main/run_general.sh"
click node_general_agent "https://github.com/baidu-baige/loongflow/blob/main/agents/general_agent/general_evolve_agent.py"
click node_math_agent "https://github.com/baidu-baige/loongflow/blob/main/agents/math_agent/math_evolve_agent.py"
click node_ml_agent "https://github.com/baidu-baige/loongflow/blob/main/agents/ml_agent/ml_evolve_agent.py"
click node_pes_agent "https://github.com/baidu-baige/loongflow/blob/main/src/loongflow/framework/pes/pes_agent.py"
click node_base_runner "https://github.com/baidu-baige/loongflow/blob/main/src/loongflow/framework/pes/base_runner.py"
click node_planner "https://github.com/baidu-baige/loongflow/blob/main/src/loongflow/framework/pes/planner/planner.py"
click node_executor "https://github.com/baidu-baige/loongflow/blob/main/src/loongflow/framework/pes/executor/executor.py"
click node_summary "https://github.com/baidu-baige/loongflow/blob/main/src/loongflow/framework/pes/summary/summary.py"
click node_solution_db "https://github.com/baidu-baige/loongflow/blob/main/src/loongflow/framework/pes/database/database.py"
click node_model_adapter "https://github.com/baidu-baige/loongflow/blob/main/src/loongflow/agentsdk/models/litellm_model.py"
click node_messages "https://github.com/baidu-baige/loongflow/blob/main/src/loongflow/agentsdk/message/message.py"
click node_toolkit "https://github.com/baidu-baige/loongflow/blob/main/src/loongflow/agentsdk/tools/toolkit.py"
click node_memory "https://github.com/baidu-baige/loongflow/blob/main/src/loongflow/agentsdk/memory/evolution/memory_factory.py"
click node_grade_memory "https://github.com/baidu-baige/loongflow/blob/main/src/loongflow/agentsdk/memory/grade/memory.py"
click node_redis_memory "https://github.com/baidu-baige/loongflow/blob/main/src/loongflow/agentsdk/memory/evolution/redis_memory.py"
click node_react_agent "https://github.com/baidu-baige/loongflow/blob/main/src/loongflow/framework/react/react_agent.py"
click node_react_reasoner "https://github.com/baidu-baige/loongflow/blob/main/src/loongflow/framework/react/components/default_reasoner.py"
click node_react_actor "https://github.com/baidu-baige/loongflow/blob/main/src/loongflow/framework/react/components/default_actor.py"
click node_visualizers "https://github.com/baidu-baige/loongflow/blob/main/agents/math_agent/visualizer/visualizer.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_pes_agent,node_base_runner,node_planner,node_executor,node_summary,node_solution_db toneBlue
class node_model_adapter,node_messages,node_toolkit,node_memory,node_grade_memory,node_redis_memory toneAmber
class node_react_agent,node_react_reasoner,node_react_actor toneMint
class node_domain_tasks,node_general_agent,node_math_agent,node_ml_agent toneRose
class node_launchers,node_llm_apis,node_visualizers toneNeutral
```

## Components

### PES Framework

Long-horizon Plan–Execute–Summary orchestration.

- [PES Agent](https://github.com/baidu-baige/loongflow/blob/main/src/loongflow/framework/pes/pes_agent.py): orchestrator · Registers workers and drives iterative PES runs. · `src/loongflow/framework/pes/pes_agent.py`
- [Base Runner](https://github.com/baidu-baige/loongflow/blob/main/src/loongflow/framework/pes/base_runner.py): run context · Owns workspace and checkpoint context. · `src/loongflow/framework/pes/base_runner.py`
- [Planner](https://github.com/baidu-baige/loongflow/blob/main/src/loongflow/framework/pes/planner/planner.py): PES worker · Produces candidate plans. · `src/loongflow/framework/pes/planner/planner.py`
- [Executor](https://github.com/baidu-baige/loongflow/blob/main/src/loongflow/framework/pes/executor/executor.py): PES worker · Runs code and experiments. · `src/loongflow/framework/pes/executor/executor.py`
- [Summary](https://github.com/baidu-baige/loongflow/blob/main/src/loongflow/framework/pes/summary/summary.py): PES worker · Extracts reusable findings and final results. · `src/loongflow/framework/pes/summary/summary.py`
- [Solution Database](https://github.com/baidu-baige/loongflow/blob/main/src/loongflow/framework/pes/database/database.py): solution state · Evolving candidate-solution boundary. · `src/loongflow/framework/pes/database/database.py`

### Agent SDK & Memory

Shared model, tool, message, and learning boundaries.

- [LiteLLM Model Adapter](https://github.com/baidu-baige/loongflow/blob/main/src/loongflow/agentsdk/models/litellm_model.py): model gateway · Unifies hosted and local LLM endpoints. · `src/loongflow/agentsdk/models/litellm_model.py`
- [Messages](https://github.com/baidu-baige/loongflow/blob/main/src/loongflow/agentsdk/message/message.py): message abstraction · `src/loongflow/agentsdk/message/message.py`
- [Toolkit](https://github.com/baidu-baige/loongflow/blob/main/src/loongflow/agentsdk/tools/toolkit.py): controlled tools · Provides filesystem, shell, code, function, and sub-agent tools. · `src/loongflow/agentsdk/tools/toolkit.py`
- [Learning Memory](https://github.com/baidu-baige/loongflow/blob/main/src/loongflow/agentsdk/memory/evolution/memory_factory.py): memory boundary · Selects and persists accumulated experience. · `src/loongflow/agentsdk/memory/evolution/memory_factory.py`
- [Grade Memory](https://github.com/baidu-baige/loongflow/blob/main/src/loongflow/agentsdk/memory/grade/memory.py): structured context store · Persists and compresses context in file or memory storage. · `src/loongflow/agentsdk/memory/grade/memory.py`
- [Redis Memory](https://github.com/baidu-baige/loongflow/blob/main/src/loongflow/agentsdk/memory/evolution/redis_memory.py): optional backend · `src/loongflow/agentsdk/memory/evolution/redis_memory.py`

### ReAct Framework

Interactive reasoning and tool-use path.

- [ReAct Agent](https://github.com/baidu-baige/loongflow/blob/main/src/loongflow/framework/react/react_agent.py): interactive orchestrator · Runs a short reasoning/action loop. · `src/loongflow/framework/react/react_agent.py`
- [Default Reasoner](https://github.com/baidu-baige/loongflow/blob/main/src/loongflow/framework/react/components/default_reasoner.py): ReAct component · `src/loongflow/framework/react/components/default_reasoner.py`
- [Default Actor](https://github.com/baidu-baige/loongflow/blob/main/src/loongflow/framework/react/components/default_actor.py): ReAct component · `src/loongflow/framework/react/components/default_actor.py`

### Domain Agents

Workload-specific PES bindings.

- **Tasks, datasets & scoring**: user workload boundary · Supplies problems, data, and evaluation signals.
- [General Agent](https://github.com/baidu-baige/loongflow/blob/main/agents/general_agent/general_evolve_agent.py): domain PES agent · Skill-driven coding workload binding. · `agents/general_agent/general_evolve_agent.py`
- [Math Agent](https://github.com/baidu-baige/loongflow/blob/main/agents/math_agent/math_evolve_agent.py): domain PES agent · Mathematical algorithm evolution binding. · `agents/math_agent/math_evolve_agent.py`
- [ML Agent](https://github.com/baidu-baige/loongflow/blob/main/agents/ml_agent/ml_evolve_agent.py): domain PES agent · Autonomous ML experimentation and MLE-bench binding. · `agents/ml_agent/ml_evolve_agent.py`

### Other components

- [Run scripts](https://github.com/baidu-baige/loongflow/blob/main/run_general.sh): shell launchers · Launch domain-agent runs. · `run_general.sh`
- **LLM APIs**: external inference · Required reasoning provider boundary.
- [Checkpoint Visualizers](https://github.com/baidu-baige/loongflow/blob/main/agents/math_agent/visualizer/visualizer.py): local visualization · Expose evolution and performance views from checkpoints. · `agents/math_agent/visualizer/visualizer.py`

## Connections

- Run scripts → General Agent: launches
- Run scripts → Math Agent: launches
- Run scripts → ML Agent: launches
- General Agent → PES Agent: binds stages
- Math Agent → PES Agent: binds stages
- ML Agent → PES Agent: binds stages
- Tasks, datasets & scoring → General Agent: coding tasks
- Tasks, datasets & scoring → Math Agent: problems and scoring
- Tasks, datasets & scoring → ML Agent: datasets and scoring
- PES Agent → Base Runner: uses run context
- PES Agent → Planner: plan
- Planner → Executor: candidate plans
- Executor → Summary: outputs and scores
- Summary → Planner: findings for next iteration
- PES Agent → Solution Database: evolves solutions
- Summary → Learning Memory: stores experience
- Learning Memory → Planner: retrieved experience
- Learning Memory → Redis Memory: optional backend
- Grade Memory → PES Agent: structured context
- PES Agent → LiteLLM Model Adapter: agent inference
- LiteLLM Model Adapter → LLM APIs: requests
- PES Agent → Toolkit: controlled execution
- ReAct Agent → Default Reasoner: reasons
- Default Reasoner → Default Actor: actions
- Default Actor → Toolkit: uses tools
- ReAct Agent → LiteLLM Model Adapter: shared inference
- Checkpoint Visualizers → Base Runner: reads checkpoints
