How baidu-baige/loongflow works
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: pes_agent.py PES Agent · planner.py Planner · run_general.sh Run scripts · summary.py Summary
Components 22
PES Framework
Long-horizon Plan–Execute–Summary orchestration.
- PES Agent · Registers workers and drives iterative PES runs.
src/loongflow/framework/pes/pes_agent.py - Base Runner · Owns workspace and checkpoint context.
src/loongflow/framework/pes/base_runner.py - Planner · Produces candidate plans.
src/loongflow/framework/pes/planner/planner.py - Executor · Runs code and experiments.
src/loongflow/framework/pes/executor/executor.py - Summary · Extracts reusable findings and final results.
src/loongflow/framework/pes/summary/summary.py - Solution Database · Evolving candidate-solution boundary.
src/loongflow/framework/pes/database/database.py
Agent SDK & Memory
Shared model, tool, message, and learning boundaries.
- LiteLLM Model Adapter · Unifies hosted and local LLM endpoints.
src/loongflow/agentsdk/models/litellm_model.py - Messages
src/loongflow/agentsdk/message/message.py - Toolkit · Provides filesystem, shell, code, function, and sub-agent tools.
src/loongflow/agentsdk/tools/toolkit.py - Learning Memory · Selects and persists accumulated experience.
src/loongflow/agentsdk/memory/evolution/memory_factory.py - Grade Memory · Persists and compresses context in file or memory storage.
src/loongflow/agentsdk/memory/grade/memory.py - Redis Memory
src/loongflow/agentsdk/memory/evolution/redis_memory.py
ReAct Framework
Interactive reasoning and tool-use path.
- ReAct Agent · Runs a short reasoning/action loop.
src/loongflow/framework/react/react_agent.py - Default Reasoner
src/loongflow/framework/react/components/default_reasoner.py - Default Actor
src/loongflow/framework/react/components/default_actor.py
Domain Agents
Workload-specific PES bindings.
- Tasks, datasets & scoring · Supplies problems, data, and evaluation signals.
- General Agent · Skill-driven coding workload binding.
agents/general_agent/general_evolve_agent.py - Math Agent · Mathematical algorithm evolution binding.
agents/math_agent/math_evolve_agent.py - ML Agent · Autonomous ML experimentation and MLE-bench binding.
agents/ml_agent/ml_evolve_agent.py
Other components
- Run scripts · Launch domain-agent runs.
run_general.sh - LLM APIs · Required reasoning provider boundary.
- Checkpoint Visualizers · Expose evolution and performance views from checkpoints.
agents/math_agent/visualizer/visualizer.py
Connections 27
- 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