Building a Robust Multi-Agent Pipeline with CAMEL
Explore a sophisticated workflow using the CAMEL framework for multi-agent collaboration.
Overview
In this tutorial, we build an advanced, end-to-end multi-agent research workflow using the CAMEL framework. We design a coordinated society of agents—Planner, Researcher, Writer, Critic, and Finalizer—that collaboratively transform a high-level topic into a polished, evidence-grounded research brief.
Integrating OpenAI API
We securely integrate the OpenAI API, orchestrate agent interactions programmatically, and add lightweight persistent memory to retain knowledge across runs. By structuring the system around clear roles, JSON-based contracts, and iterative refinement, we demonstrate how CAMEL can be used to construct reliable, controllable, and scalable agentic pipelines.
Setting Up the Environment
We set up the execution environment and securely load the OpenAI API key using Colab secrets or a hidden prompt. We ensure the runtime is ready by installing dependencies and configuring authentication securely.
!pip -q install "camel-ai[all]" "python-dotenv" "rich"
import os
import json
import time
from typing import Dict, Any
from rich import print as rprint
def load_openai_key() -> str:
key = None
try:
from google.colab import userdata
key = userdata.get("OPENAI_API_KEY")
except Exception:
key = None
if not key:
import getpass
key = getpass.getpass("Enter OPENAI_API_KEY (hidden): ").strip()
if not key:
raise ValueError("OPENAI_API_KEY is required.")
return key
os.environ["OPENAI_API_KEY"] = load_openai_key()Initializing the CAMEL Model
We initialize the CAMEL model configuration and create a shared language model instance using the ModelFactory abstraction. We standardize model behavior across all agents to ensure consistent, reproducible reasoning throughout the multi-agent pipeline.
from camel.models import ModelFactory
from camel.types import ModelPlatformType, ModelType
from camel.agents import ChatAgent
from camel.toolkits import SearchToolkit
MODEL_CFG = {"temperature": 0.2}
model = ModelFactory.create(
model_platform=ModelPlatformType.OPENAI,
model_type=ModelType.GPT_4O,
model_config_dict=MODEL_CFG,
)Implementing Persistent Memory
We implement a lightweight persistent memory layer backed by a JSON file, which stores artifacts from each run and retrieves summaries of previous executions, allowing us to introduce continuity across sessions.
MEM_PATH = "camel_memory.json"
def mem_load() -> Dict[str, Any]:
if not os.path.exists(MEM_PATH):
return {"runs": []}
with open(MEM_PATH, "r", encoding="utf-8") as f:
return json.load(f)
def mem_save(mem: Dict[str, Any]) -> None:
with open(MEM_PATH, "w", encoding="utf-8") as f:
json.dump(mem, f, ensure_ascii=False, indent=2)
def mem_add_run(topic: str, artifacts: Dict[str, str]) -> None:
mem = mem_load()
mem["runs"].append({"ts": int(time.time()), "topic": topic, "artifacts": artifacts})
mem_save(mem)Defining Agent Roles
We define the core agent roles and responsibilities within the workflow. Each agent is specialized with clear goals and output contracts, enhancing productivity and collaboration among them.
def make_agent(role: str, goal: str, extra_rules: str = "") -> ChatAgent:
system = (
f"You are {role}.
"
f"Goal: {goal}
"
f"{extra_rules}
"
"Output must be crisp, structured, and directly usable by the next agent."
)
return ChatAgent(model=model, system_message=system)Orchestrating the Workflow
The entire multi-agent workflow is orchestrated from planning to finalization, passing artifacts between agents to produce a finalized research brief ready for use.
def run_workflow(topic: str) -> Dict[str, str]:
rprint(mem_last_summaries(3))
plan = step_json(
planner,
f"Topic: {topic}\nCreate a tight plan and research questions."
)
research = step_json(
researcher,
f"Research the topic using web search.\n{json.dumps(plan)}"
)
draft = step_text(
writer,
f"Write a research brief using:\n{json.dumps(research)}"
)
critique = step_json(
critic,
f"Critique the draft:\n{draft}"
)
final = step_text(
finalizer,
f"Rewrite using critique:\n{json.dumps(critique)}\nDraft:\n{draft}"
)
artifacts = {
"plan_json": json.dumps(plan, indent=2),
"research_json": json.dumps(research, indent=2),
"draft_md": draft,
"critique_json": json.dumps(critique, indent=2),
"final_md": final,
}
mem_add_run(topic, artifacts)
return artifactsConclusion
We implemented a practical CAMEL-based multi-agent system that mirrors real-world research and review workflows. Clearly defined agent roles and critique-driven refinement lead to higher-quality outputs while reducing hallucinations and structural weaknesses.
Full Codes
Check out the FULL CODES here.
Сменить язык
Читать эту статью на русском