Mot AI agent don le nhanh chong dat den gioi han. Chung hay bi ao giac, mat ngu canh va kho xu ly cac quy trinh phuc tap doi hoi nhieu loai chuyen mon khac nhau. He thong multi-agent giai quyet van de nay bang cach cho phep cac agent chuyen biet phoi hop voi nhau — moi agent tap trung vao the manh rieng.

CrewAI

CrewAI la mot framework Python giup viec xay dung cac he thong nay tro nen don gian. Thay vi phai tu xu ly cac lop dieu phoi phuc tap, ban chi can dinh nghia agent voi vai tro cu the, gan nhiem vu va de framework lo phan phoi hop.

Trong bai huong dan nay, ban se xay dung mot crew nghien cuu va viet bai: mot agent nghien cuu chu de, agent con lai viet noi dung dua tren ket qua nghien cuu do. Ket thuc bai, ban se co mot he thong hoan chinh co the mo rong cho cac nhu cau rieng.

Yeu Cau Truoc Khi Bat Dau

Dam bao ban da co:

  • Python 3.10 tro len
  • API key cua OpenAI (hoac bat ky nha cung cap LLM nao duoc CrewAI ho tro)
  • Kien thuc co ban ve class va function trong Python

Buoc 1: Cai Dat CrewAI va Thiet Lap Du An

Tao thu muc du an moi va cai dat framework:

mkdir my-crew && cd my-crew
python -m venv venv
source venv/bin/activate  # Tren Windows: venv\Scripts\activate
pip install crewai crewai-tools

Dat API key lam bien moi truong:

export OPENAI_API_KEY="your-key-here"

Hoac tao file .env trong thu muc goc cua du an:

OPENAI_API_KEY=your-key-here

Buoc 2: Dinh Nghia Cac Agent

Agent trong CrewAI co ba thuoc tinh chinh: role (vai tro), goal (muc tieu), va backstory (boi canh dinh huong hanh vi).

Tao file crew.py:

from crewai import Agent, Task, Crew, Process
 
researcher = Agent(
    role="Senior Research Analyst",
    goal="Find comprehensive, accurate information on the given topic",
    backstory=(
        "You are an experienced research analyst who excels at "
        "finding reliable sources, identifying key facts, and "
        "synthesizing complex information into clear summaries."
    ),
    verbose=True,
    allow_delegation=False,
)
 
writer = Agent(
    role="Content Writer",
    goal="Write engaging, well-structured content based on research findings",
    backstory=(
        "You are a skilled writer who transforms research data "
        "into compelling articles. You focus on clarity, accuracy, "
        "and reader engagement."
    ),
    verbose=True,
    allow_delegation=False,
)

Flag verbose=True cho phep ban thay qua trinh suy luan cua tung agent trong terminal — rat huu ich khi debug.

Buoc 3: Tao Cac Task

Task dinh nghia nhung gi moi agent can hoan thanh. Moi task co mo ta, dinh dang output mong muon va agent duoc chi dinh:

research_task = Task(
    description=(
        "Research the topic: {topic}. "
        "Find key facts, recent developments, and expert opinions. "
        "Provide at least 5 distinct points with supporting evidence."
    ),
    expected_output=(
        "A detailed research brief with numbered points, "
        "each containing a fact and its source or context."
    ),
    agent=researcher,
)
 
writing_task = Task(
    description=(
        "Using the research provided, write a 500-word article on {topic}. "
        "Structure it with an introduction, main points, and conclusion. "
        "Make it informative yet accessible to a general audience."
    ),
    expected_output="A polished article of approximately 500 words in markdown format.",
    agent=writer,
)

Luu y placeholder {topic} — ban se truyen gia tri nay khi chay crew.

Buoc 4: Ghep Noi va Chay Crew

Ket noi cac agent va task thanh mot crew:

crew = Crew(
    agents=[researcher, writer],
    tasks=[research_task, writing_task],
    process=Process.sequential,
    verbose=True,
)
 
result = crew.kickoff(inputs={"topic": "the impact of AI agents on software development"})
print(result)

Thiet lap Process.sequential nghia la cac task chay tuan tu — writer doi researcher hoan thanh truoc khi bat dau. CrewAI cung ho tro Process.hierarchical trong do mot manager agent phan cong cong viec dong.

Buoc 5: Chay Crew

Thuc thi script:

python crew.py

Ban se thay output hien thi qua trinh suy nghi cua tung agent, cac tool chung su dung va ket qua cuoi cung. Bai viet cua writer se tich hop cac phat hien tu researcher — do chinh la su phoi hop multi-agent dang hoat dong.

Buoc 6: Them Tool De Mo Rong Kha Nang

Agent tro nen huu ich hon nhieu khi co tool. Hay trang bi cho researcher kha nang tim kiem web:

from crewai_tools import SerperDevTool
 
search_tool = SerperDevTool()
 
researcher = Agent(
    role="Senior Research Analyst",
    goal="Find comprehensive, accurate information on the given topic",
    backstory=(
        "You are an experienced research analyst who excels at "
        "finding reliable sources and synthesizing complex information."
    ),
    tools=[search_tool],
    verbose=True,
    allow_delegation=False,
)

Ban can Serper API key (bien moi truong SERPER_API_KEY). Cac tool tich hop khac bao gom ScrapeWebsiteTool, FileReadTool va DirectoryReadTool.

Buoc 7: Them Memory De Giu Ngu Canh

Bat memory de agent nho cac tuong tac truoc do trong phien lam viec:

crew = Crew(
    agents=[researcher, writer],
    tasks=[research_task, writing_task],
    process=Process.sequential,
    memory=True,
    verbose=True,
)

Khi memory duoc bat, writer nhan duoc ngu canh phong phu hon tu ket qua nghien cuu cua researcher, va cac lan chay lap lai tren chu de tuong tu se cai thien ve chat luong.

Meo Thuc Te

Bat dau don gian, sau do them do phuc tap. Khoi dau voi hai agent va xu ly tuan tu. Chi dung hierarchical process hoac them nhieu agent khi thuc su can thiet.

Viet backstory cu the. Backstory mo ho tao ra output chung chung. Cang cho agent nhieu boi canh ve chuyen mon, quyet dinh cua no cang tot.

Dung allow_delegation=False ban dau. Delegation cho phep agent chuyen task cho nhau, manh me nhung kho debug hon. Bat len khi luong co ban da hoat dong on.

Theo doi luong token. Moi lan goi agent tieu thu token LLM. Voi verbose mode bat, quan sat so buoc suy luan cua moi agent. Viet mo ta task chat hon neu agent di lan man.

Test voi model re truoc. Dung gpt-4o-mini trong luc phat trien, sau do chuyen sang model manh hon cho production:

researcher = Agent(
    role="Senior Research Analyst",
    goal="Find comprehensive information on the given topic",
    backstory="...",
    llm="gpt-4o-mini",  # Re hon de test
)

Huong Mo Rong Tiep Theo

Khi da co nen tang hoat dong, ban co the thu:

  • Them agent kiem tra su kien de xac minh cac tuyen bo cua researcher
  • Tao crew sinh code voi architect, developer va reviewer
  • Xay dung he thong ho tro khach hang voi agent phan loai chuyen den chuyen gia
  • Ket noi voi API ben ngoai bang custom tool cho cac workflow chuyen biet

Dieu quan trong nhat voi he thong multi-agent la moi agent nen co trach nhiem tap trung va ro rang. Neu ban thay minh viet backstory dai liet ke nhieu ky nang, do la dau hieu can tach thanh nhieu agent.