Working with CrewAI

Add end-to-end tracing to Agno agent workflows with LangDB—monitor model calls, tool usage, and step flows using a single init() call.

LangDB makes it effortless to trace CrewAI workflows end-to-end. With a single init() call, all agent interactions, task executions, and LLM calls are captured.

Installation

Install the LangDB client with LangChain feature flag:

pip install 'pylangdb[crewai]'

Quick Start

Export Environment Variables

Set your LangDB credentials:

export LANGDB_API_KEY="<your_langdb_api_key>"
export LANGDB_PROJECT_ID="<your_langdb_project_id>"

Initialize Tracing

Import and run the initialize before configuring your CrewAI Code:

from pylangdb.crewai import init
# Initialise LangDB
init()

Configure your CrewAI code

import os
from dotenv import load_dotenv
from crewai import Agent, Task, Crew, LLM

# Configure LLM with LangDB headers
llm = LLM(
    model="openai/gpt-4o",  # Use LiteLLM Like Model Names
    api_key=os.getenv("LANGDB_API_KEY"),
    base_url=os.getenv("LANGDB_API_BASE_URL"),
    extra_headers={"x-project-id": os.getenv("LANGDB_PROJECT_ID")}
)

# Define agents and tasks as usual
researcher = Agent(
    role="researcher",
    goal="Research topic thoroughly",
    backstory="You are an expert researcher",
    llm=llm,
    verbose=True
)
task = Task(description="Research the given topic", agent=researcher)
crew = Crew(agents=[researcher], tasks=[task])

# Kick off the workflow
result = crew.kickoff()
print(result)

All CrewAI calls—agent initialization, task execution, and model responses—are automatically linked.

Complete CrewAI example

Here is a full example based on CrewAI report writing agent.

Setup Evironment

pip install crewai 'pylangdb[crewai]' crewai_tools setuptools python-dotenv

Export Environment Variables

You also need to get API Key from Serper.dev

export LANGDB_API_KEY="<your_langdb_api_key>"
export LANGDB_PROJECT_ID="<your_langdb_project_id>"
export LANGDB_API_BASE_URL='https://api.us-east-1.langdb.ai'

main.py

#!/usr/bin/env python3

import os
import sys
from pylangdb.crewai import init
init()
from dotenv import load_dotenv
from crewai import Agent, Task, Crew, Process, LLM
from crewai_tools import SerperDevTool

load_dotenv()

def create_llm(model):
    return LLM(
        model=model,
        api_key=os.environ.get("LANGDB_API_KEY"),
        base_url=os.environ.get("LANGDB_API_BASE_URL"),
        extra_headers={"x-project-id": os.environ.get("LANGDB_PROJECT_ID")}
    )

class ResearchPlanningCrew:
    def researcher(self) -> Agent:
        return Agent(
            role="Research Specialist",
            goal="Research topics thoroughly",
            backstory="Expert researcher with skills in finding information",
            tools=[SerperDevTool()],
            llm=create_llm("openai/gpt-4o"),
            verbose=True
        )
    
    def planner(self) -> Agent:
        return Agent(
            role="Strategic Planner",
            goal="Create actionable plans based on research",
            backstory="Strategic planner who breaks down complex challenges",
            reasoning=True,
            max_reasoning_attempts=3,
            llm=create_llm("openai/anthropic/claude-3.7-sonnet"),
            verbose=True
        )
    
    def research_task(self) -> Task:
        return Task(
            description="Research the topic thoroughly and compile information",
            agent=self.researcher(),
            expected_output="Comprehensive research report"
        )
    
    def planning_task(self) -> Task:
        return Task(
            description="Create a strategic plan based on research",
            agent=self.planner(),
            expected_output="Strategic execution plan with phases and goals",
            context=[self.research_task()]
        )
    
    def crew(self) -> Crew:
        return Crew(
            agents=[self.researcher(), self.planner()],
            tasks=[self.research_task(), self.planning_task()],
            verbose=True,
            process=Process.sequential
        )

def main():
    topic = sys.argv[1] if len(sys.argv) > 1 else "Artificial Intelligence in Healthcare"
    
    crew_instance = ResearchPlanningCrew()
    
    # Update task descriptions with topic
    crew_instance.research_task().description = f"Research {topic} thoroughly and compile information"
    crew_instance.planning_task().description = f"Create a strategic plan for {topic} based on research"
    
    result = crew_instance.crew().kickoff()
    print(result)

if __name__ == "__main__":
    main()

Running your Agent

Navigate to the parent directory of your agent project and use one of the following commands:

python main.py

Traces on LangDB:

When you run queries against your agent, LangDB automatically captures detailed traces of all agent interactions:

Simple CrewAI Example Trace

Next Steps: Advanced CrewAI Integration

This guide covered the basics of integrating LangDB with CrewAI using a Research and Planning agent example. For more complex scenarios and advanced use cases, check out our comprehensive resources in Guides Section.

Last updated

Was this helpful?