> ## Documentation Index
> Fetch the complete documentation index at: https://docs.coreweave.com/llms.txt
> Use this file to discover all available pages before exploring further.

# CrewAI

> CrewAI를 Weave와 통합하여 멀티 에이전트 애플리케이션을 모니터링하고 트레이싱하세요

<a target="_blank" href="https://github.com/wandb/examples/blob/master/weave/docs/quickstart_crewai.ipynb">
  <img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Colab에서 열기" />
</a>

이 가이드에서는 Weave를 사용하여 [Crews](https://docs.crewai.com/guides/crews/first-crew)와 [플로우](https://docs.crewai.com/guides/flows/first-flow)를 포함한 [CrewAI](https://docs.crewai.com/introduction) 멀티 에이전트 애플리케이션을 모니터링하고 트레이싱하는 방법을 설명합니다.

CrewAI는 자율형 AI 에이전트를 구축하기 위한 Python 프레임워크입니다. LangChain 등 다른 에이전트 프레임워크에 의존하지 않으며, 상위 수준의 [Crews](https://docs.crewai.com/guides/crews/first-crew)와 하위 수준의 [플로우](https://docs.crewai.com/guides/flows/first-flow)라는 두 가지 추상화를 지원합니다.

CrewAI 애플리케이션은 여러 에이전트가 함께 동작하는 경우가 많으므로, 에이전트들이 어떻게 협업하고 소통하는지 파악하는 것이 중요합니다. Weave는 CrewAI 애플리케이션의 트레이스를 자동으로 캡처하므로 에이전트의 성능과 상호작용을 모니터링하고 분석할 수 있습니다.

다음 섹션에서는 Crew 트레이싱, 도구 사용 추적, 플로우 트레이싱, 가드레일 함수를 Weave Op으로 래핑하는 방법을 차례로 살펴봅니다.

<h2 id="get-started-with-crew">
  Crew 시작하기
</h2>

이 예제를 실행하려면 CrewAI와 Weave를 설치하세요. CrewAI 설치에 대한 자세한 내용은 [CrewAI 설치 가이드](https://docs.crewai.com/installation)를 참조하세요.

```bash theme={"system"}
pip install crewai weave
```

다음 예시에서는 CrewAI Crew를 생성하고 Weave로 실행을 트레이스합니다. 트레이싱을 활성화하려면 스크립트 시작 부분에서 `weave.init()`을 호출하세요. `weave.init()`에 전달하는 인수는 Weave가 트레이스를 로깅할 프로젝트 이름입니다.

```python lines {5} theme={"system"}
import weave
from crewai import Agent, Task, Crew, LLM, Process

# 프로젝트 이름으로 Weave 초기화
weave.init(project_name="crewai_demo")

# 결정적 출력을 얻기 위해 temperature를 0으로 설정한 LLM 생성
llm = LLM(model="gpt-4o-mini", temperature=0)

# 에이전트 생성
researcher = Agent(
    role='Research Analyst',
    goal='Find and analyze the best investment opportunities',
    backstory='Expert in financial analysis and market research',
    llm=llm,
    verbose=True,
    allow_delegation=False,
)

writer = Agent(
    role='Report Writer',
    goal='Write clear and concise investment reports',
    backstory='Experienced in creating detailed financial reports',
    llm=llm,
    verbose=True,
    allow_delegation=False,
)

# 태스크 생성
research_task = Task(
    description='Deep research on the {topic}',
    expected_output='Comprehensive market data including key players, market size, and growth trends.',
    agent=researcher
)

writing_task = Task(
    description='Write a detailed report based on the research',
    expected_output='The report should be easy to read and understand. Use bullet points where applicable.',
    agent=writer
)

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

# Crew 실행
result = crew.kickoff(inputs={"topic": "AI in material science"})
print(result)
```

Weave는 CrewAI 라이브러리를 통해 이루어지는 모든 Call(에이전트 상호작용, 태스크 실행, LLM Call 포함)을 추적하고 로깅합니다. 트레이스는 Weave 웹 인터페이스에서 확인할 수 있습니다.

[<img src="https://mintcdn.com/coreweave-dbfa0e8d/3Dv_sw2eg8feUJlx/products/wandb/weave/_media/crew.png?fit=max&auto=format&n=3Dv_sw2eg8feUJlx&q=85&s=554c4c7b788d65db6a5090e9ae24b332" alt="crew_trace.png" width="3344" height="1880" data-path="products/wandb/weave/_media/crew.png" />](https://forge.coreweave.com/wandb/ayut/crewai_demo/weave/traces?filter=%7B%22opVersionRefs%22%3A%5B%22weave%3A%2F%2F%2Fayut%2Fcrewai_demo%2Fop%2Fcrewai.Crew.kickoff%3A*%22%5D%7D\&peekPath=%2Fayut%2Fcrewai_demo%2Fcalls%2F0195c7ac-bd52-7390-95a7-309370e9e058%3FhideTraceTree%3D0\&cols=%7B%22wb_run_id%22%3Afalse%2C%22attributes.weave.client_version%22%3Afalse%2C%22attributes.weave.os_name%22%3Afalse%2C%22attributes.weave.os_release%22%3Afalse%2C%22attributes.weave.os_version%22%3Afalse%2C%22attributes.weave.source%22%3Afalse%2C%22attributes.weave.sys_version%22%3Afalse%7D)

<Note>
  CrewAI는 킥오프 프로세스를 더 세밀하게 제어할 수 있도록 `kickoff()`, `kickoff_for_each()`, `kickoff_async()`, `kickoff_for_each_async()` 등 여러 메서드를 제공합니다. 이 인테그레이션은 이 모든 메서드의 트레이스 로깅을 지원합니다.
</Note>

<h2 id="track-tools">
  도구 추적
</h2>

CrewAI 도구를 사용하면 에이전트가 웹 검색, 데이터 분석, 협업, 동료 간 태스크 위임 등의 기능을 활용할 수 있습니다. 인테그레이션은 이러한 도구도 트레이스합니다.

다음 예시에서는 인터넷을 검색해 가장 관련성 높은 결과를 반환하는 도구에 대한 액세스 권한을 에이전트에게 부여하여, 이전 예시에서 생성한 보고서의 품질을 높입니다.

먼저 추가 의존성을 설치하세요:

```bash theme={"system"}
pip install 'crewai[tools]'
```

이 예제에서는 `SerperDevTool`을 사용해 'Research Analyst' 에이전트가 인터넷에서 관련 정보를 검색할 수 있게 합니다. 이 도구와 API 요구 사항에 대한 자세한 내용은 [SerperDevTool 문서](https://docs.crewai.com/tools/serperdevtool)를 참조하세요.

```python lines {12} theme={"system"}
# .... 기존 임포트 ....
from crewai_tools import SerperDevTool

# 에이전트에 도구를 제공합니다.
researcher = Agent(
    role='Research Analyst',
    goal='Find and analyze the best investment opportunities',
    backstory='Expert in financial analysis and market research',
    llm=llm,
    verbose=True,
    allow_delegation=False,
    tools=[SerperDevTool()],
)

# .... 기존 코드 ....
```

인터넷에 액세스할 수 있는 에이전트로 이 Crew를 실행하면 더 적절한 결과를 얻을 수 있습니다. 다음 이미지와 같이 Weave는 도구 사용을 자동으로 트레이스합니다.

[<img src="https://mintcdn.com/coreweave-dbfa0e8d/3Dv_sw2eg8feUJlx/products/wandb/weave/_media/crew_with_tool.png?fit=max&auto=format&n=3Dv_sw2eg8feUJlx&q=85&s=a0f5a311ab892762bb8f33b821a39f1c" alt="crew_with_tool_trace.png" width="3344" height="1880" data-path="products/wandb/weave/_media/crew_with_tool.png" />](https://forge.coreweave.com/wandb/ayut/crewai_demo/weave/traces?filter=%7B%22opVersionRefs%22%3A%5B%22weave%3A%2F%2F%2Fayut%2Fcrewai_demo%2Fop%2Fcrewai.Crew.kickoff%3A*%22%5D%7D\&peekPath=%2Fayut%2Fcrewai_demo%2Fcalls%2F0195c7c7-0213-7f42-b130-caa93a79316c%3FdescendentCallId%3D0195c7c7-0a16-7f11-8cfd-9dedf1d03b3b\&cols=%7B%22wb_run_id%22%3Afalse%2C%22attributes.weave.client_version%22%3Afalse%2C%22attributes.weave.os_name%22%3Afalse%2C%22attributes.weave.os_release%22%3Afalse%2C%22attributes.weave.os_version%22%3Afalse%2C%22attributes.weave.source%22%3Afalse%2C%22attributes.weave.sys_version%22%3Afalse%7D)

<Note>
  이 인테그레이션은 [`crewAI-tools`](https://github.com/crewAIInc/crewAI-tools) 저장소에서 제공하는 모든 도구를 자동으로 패치합니다.
</Note>

<h2 id="get-started-with-flow">
  플로우 시작하기
</h2>

다음 예시에서는 CrewAI 플로우를 정의하고 Weave로 트레이스합니다. Crew와 마찬가지로 플로우를 정의하기 전에 `weave.init()`을 호출하세요. 그러면 Weave가 `Flow.kickoff` 진입점과 `@start`, `@listen`, `@router`, `@or_`, `@and_` 데코레이터를 자동으로 캡처합니다.

```python lines {3} theme={"system"}
import weave
# 프로젝트 이름으로 Weave를 초기화합니다
weave.init("crewai_demo")

from crewai.flow.flow import Flow, listen, router, start
from litellm import completion


class CustomerFeedbackFlow(Flow):
    model = "gpt-4o-mini"

    @start()
    def fetch_feedback(self):
        print("Fetching customer feedback")
        # 실제 환경에서는 이 부분을 API 호출로 대체할 수 있습니다.
        # 이 예제에서는 고객 피드백을 시뮬레이션합니다.
        feedback = (
            "I had a terrible experience with the product. "
            "It broke after one use and customer service was unhelpful."
        )
        self.state["feedback"] = feedback
        return feedback

    @router(fetch_feedback)
    def analyze_feedback(self, feedback):
        # 언어 모델로 감성을 분석합니다
        prompt = (
            f"Analyze the sentiment of this customer feedback and "
            "return only 'positive' or 'negative':\n\n"
            f"Feedback: {feedback}"
        )
        response = completion(
            model=self.model,
            messages=[{"role": "user", "content": prompt}],
        )
        sentiment = response["choices"][0]["message"]["content"].strip().lower()
        # 응답이 모호하면 기본값인 negative로 처리합니다
        if sentiment not in ["positive", "negative"]:
            sentiment = "negative"
        return sentiment

    @listen("positive")
    def handle_positive_feedback(self):
        # 긍정적인 피드백에 대한 감사 메시지를 생성합니다
        prompt = "Generate a thank you message for a customer who provided positive feedback."
        response = completion(
            model=self.model,
            messages=[{"role": "user", "content": prompt}],
        )
        thank_you_message = response["choices"][0]["message"]["content"].strip()
        self.state["response"] = thank_you_message
        return thank_you_message

    @listen("negative")
    def handle_negative_feedback(self):
        # 부정적인 피드백에 대해 서비스 개선을 약속하는 사과 메시지를 생성합니다
        prompt = (
            "Generate an apology message to a customer who provided negative feedback and offer assistance or a solution."
        )
        response = completion(
            model=self.model,
            messages=[{"role": "user", "content": prompt}],
        )
        apology_message = response["choices"][0]["message"]["content"].strip()
        self.state["response"] = apology_message
        return apology_message

# 플로우를 인스턴스화하고 실행합니다
flow = CustomerFeedbackFlow()
result = flow.kickoff()
print(result)
```

[<img src="https://mintcdn.com/coreweave-dbfa0e8d/3Dv_sw2eg8feUJlx/products/wandb/weave/_media/flow.png?fit=max&auto=format&n=3Dv_sw2eg8feUJlx&q=85&s=23ea552bad2c0f2dbece5ce40fd46da1" alt="flow.png" width="3344" height="1880" data-path="products/wandb/weave/_media/flow.png" />](https://forge.coreweave.com/wandb/ayut/crewai_demo/weave/traces?filter=%7B%22opVersionRefs%22%3A%5B%22weave%3A%2F%2F%2Fayut%2Fcrewai_demo%2Fop%2Fcrewai.Flow.kickoff%3A*%22%5D%7D\&peekPath=%2Fayut%2Fcrewai_demo%2Fcalls%2F0195c7e3-7a63-7283-bef4-9e0eb2f0eab1\&cols=%7B%22wb_run_id%22%3Afalse%2C%22attributes.weave.client_version%22%3Afalse%2C%22attributes.weave.os_name%22%3Afalse%2C%22attributes.weave.os_release%22%3Afalse%2C%22attributes.weave.os_version%22%3Afalse%2C%22attributes.weave.source%22%3Afalse%2C%22attributes.weave.sys_version%22%3Afalse%7D)

<Note>
  이 인테그레이션은 `Flow.kickoff` 진입점과 사용 가능한 모든 데코레이터(`@start`, `@listen`, `@router`, `@or_`, `@and_`)를 자동으로 패치합니다.
</Note>

<h2 id="crew-guardrail-track-your-own-ops">
  Crew 가드레일: 직접 만든 Op 추적하기
</h2>

태스크 가드레일을 사용하면 CrewAI가 태스크 출력을 다음 태스크로 전달하기 전에 검증하고 변환할 수 있습니다. Python 함수로 에이전트의 실행 결과를 즉석에서 검증할 수 있습니다.

가드레일 함수를 `@weave.op`로 래핑하면 함수의 입력, 출력, 앱 로직이 캡처되므로 에이전트를 거치면서 데이터가 어떻게 검증되는지 디버그할 수 있습니다. 또한 실험하는 동안 코드 버전이 자동으로 관리되므로 git에 커밋하지 않은 임시 세부 사항까지 캡처됩니다.

다음 예시에서는 생성된 보고서의 길이를 검증하는 가드레일을 추가하여 리서치 분석가 및 작가 Crew를 확장합니다.

```python lines {4,36} theme={"system"}
# .... 기존 임포트 및 weave 초기화 코드 ....

# 가드레일 함수에 `@weave.op()` 데코레이터를 적용하세요
@weave.op(name="guardrail-validate_blog_content")
def validate_blog_content(result: TaskOutput) -> Tuple[bool, Any]:
    # 원시 문자열 결과 조회
    result = result.raw

    """Validate blog content meets requirements."""
    try:
        # 단어 수 확인
        word_count = len(result.split())

        if word_count > 200:
            return (False, {
                "error": "Blog content exceeds 200 words",
                "code": "WORD_COUNT_ERROR",
                "context": {"word_count": word_count}
            })

        # 여기에 추가 검증 로직 작성
        return (True, result.strip())
    except Exception as e:
        return (False, {
            "error": "Unexpected error during validation",
            "code": "SYSTEM_ERROR"
        })


# .... 기존 에이전트 및 리서치 분석가 태스크 ....

writing_task = Task(
    description='Write a detailed report based on the research under 200 words',
    expected_output='The report should be easy to read and understand. Use bullet points where applicable.',
    agent=writer,
    guardrail=validate_blog_content,
)

# .... crew를 실행하는 기존 코드 ....
```

가드레일 함수에 `@weave.op` 데코레이터를 적용하면 이 함수의 입력과 출력은 물론 실행 시간, 토큰 정보(함수가 LLM을 사용하는 경우), 코드 버전 등을 추적할 수 있습니다.

[<img src="https://mintcdn.com/coreweave-dbfa0e8d/3Dv_sw2eg8feUJlx/products/wandb/weave/_media/crew_with_guardrail.png?fit=max&auto=format&n=3Dv_sw2eg8feUJlx&q=85&s=fbe828b47e37cd3490b3660a2e002ddf" alt="guardrail.png" width="3344" height="1880" data-path="products/wandb/weave/_media/crew_with_guardrail.png" />](https://forge.coreweave.com/wandb/ayut/crewai_demo/weave/traces?filter=%7B%22opVersionRefs%22%3A%5B%22weave%3A%2F%2F%2Fayut%2Fcrewai_demo%2Fop%2Fcrewai.Crew.kickoff%3A*%22%5D%7D\&peekPath=%2Fayut%2Fcrewai_demo%2Fcalls%2F0195c838-38cb-71a2-8a15-651ecddf9d89%3FdescendentCallId%3D0195c838-8632-7173-846d-f230e7272c20\&cols=%7B%22wb_run_id%22%3Afalse%2C%22attributes.weave.client_version%22%3Afalse%2C%22attributes.weave.os_name%22%3Afalse%2C%22attributes.weave.os_release%22%3Afalse%2C%22attributes.weave.os_version%22%3Afalse%2C%22attributes.weave.source%22%3Afalse%2C%22attributes.weave.sys_version%22%3Afalse%7D)

<h2 id="conclusion">
  결론
</h2>

이제 에이전트 상호 작용, 도구 사용, 플로우 실행, 가드레일 검증을 캡처하는 Weave 트레이스 기반 CrewAI 애플리케이션이 완성되었습니다. 이 인테그레이션에 대한 개선 사항을 제안하거나 문제를 보고하려면 [GitHub에서 이슈를 등록하세요](https://github.com/wandb/weave/issues/new/choose).

CrewAI로 멀티 에이전트 시스템을 구축하는 방법을 자세히 알아보려면 CrewAI [예시](https://github.com/crewAIInc/crewAI-examples)와 [문서](https://docs.crewai.com/introduction)를 참조하세요.
