CrewAI

CrewAI is a framework for orchestrating role-playing autonomous AI agents. It allows you to define agents with specific roles, goals, and tools, then coordinate them to accomplish complex tasks through collaborative workflows. Respan traces CrewAI runs with respan-instrumentation-crewai, which activates the OpenInference CrewAI instrumentor and exports crew, agent, task, tool, and LLM spans through the Respan tracing pipeline.

Create an account at platform.respan.ai and grab an API key. For gateway, also add credits or a provider key.

Run npx @respan/cli setup to set up with your coding agent.

Setup

1

Install packages

$pip install respan-ai respan-instrumentation-crewai crewai
2

Set environment variables

$export OPENAI_API_KEY="YOUR_OPENAI_API_KEY"
$export RESPAN_API_KEY="YOUR_RESPAN_API_KEY"

OPENAI_API_KEY is used for LLM requests. RESPAN_API_KEY is used to export traces to Respan.

3

Initialize and run

Initialize Respan before importing CrewAI so the instrumentor can patch CrewAI early.

1from respan import Respan
2from respan_instrumentation_crewai import CrewAIInstrumentor
3
4respan = Respan(instrumentations=[CrewAIInstrumentor()])
5
6from crewai import Agent, Crew, Task
7
8researcher = Agent(
9 role="Researcher",
10 goal="Research and summarize the latest AI trends",
11 backstory="You are a senior AI researcher with years of experience.",
12)
13
14writer = Agent(
15 role="Writer",
16 goal="Write a concise report based on the research",
17 backstory="You are a technical writer who excels at clear communication.",
18)
19
20research_task = Task(
21 description="Research the latest trends in AI agent frameworks.",
22 expected_output="A summary of key trends and developments.",
23 agent=researcher,
24)
25
26write_task = Task(
27 description="Write a brief report based on the research findings.",
28 expected_output="A well-structured report in markdown format.",
29 agent=writer,
30)
31
32crew = Crew(
33 agents=[researcher, writer],
34 tasks=[research_task, write_task],
35)
36
37result = crew.kickoff()
38print(result)
39respan.flush()
4

View your trace

Open the Traces page to see your CrewAI workflow with agent spans, task execution, tool usage, and LLM calls.

Configuration

ParameterTypeDefaultDescription
api_keystr | NoneNoneFalls back to RESPAN_API_KEY env var.
base_urlstr | NoneNoneFalls back to RESPAN_BASE_URL env var.
instrumentationslist[]Plugin instrumentations to activate, such as CrewAIInstrumentor().
customer_identifierstr | NoneNoneDefault customer identifier for all spans.
metadatadict | NoneNoneDefault metadata attached to all spans.
environmentstr | NoneNoneEnvironment tag (e.g. "production").

CrewAIInstrumentor(use_event_listener=True, create_llm_spans=True, **kwargs) passes keyword arguments through to the underlying OpenInference CrewAI instrumentor.

Attributes

In Respan()

Set defaults at initialization. These apply to all spans.

1from respan import Respan
2from respan_instrumentation_crewai import CrewAIInstrumentor
3
4respan = Respan(
5 instrumentations=[CrewAIInstrumentor()],
6 customer_identifier="user_123",
7 metadata={"service": "crew-api", "version": "1.0.0"},
8)

With propagate_attributes

Override per-request using a context scope.

1from respan import Respan, propagate_attributes
2from respan_instrumentation_crewai import CrewAIInstrumentor
3
4respan = Respan(instrumentations=[CrewAIInstrumentor()])
5
6from crewai import Agent, Crew, Task
7
8def handle_request(user_id: str, topic: str):
9 with propagate_attributes(
10 customer_identifier=user_id,
11 thread_identifier="conv_abc_123",
12 metadata={"plan": "pro"},
13 ):
14 researcher = Agent(
15 role="Researcher",
16 goal=f"Research {topic}",
17 backstory="Expert researcher.",
18 )
19 task = Task(
20 description=f"Research {topic}",
21 expected_output="Summary",
22 agent=researcher,
23 )
24 crew = Crew(agents=[researcher], tasks=[task])
25 print(crew.kickoff())
AttributeTypeDescription
customer_identifierstrIdentifies the end user in Respan analytics.
thread_identifierstrGroups related messages into a conversation.
metadatadictCustom key-value pairs. Merged with default metadata.

Decorators (optional)

Decorators are not required. All CrewAI agent steps, tasks, and LLM calls are auto-traced by the instrumentor. Use @workflow and @task to add structure when you want to group related crews into a named workflow with nested tasks.

1from respan import Respan, workflow, task
2from respan_instrumentation_crewai import CrewAIInstrumentor
3
4respan = Respan(instrumentations=[CrewAIInstrumentor()])
5
6from crewai import Agent, Crew, Task
7
8@task(name="run_research_crew")
9def run_research_crew(topic: str) -> str:
10 researcher = Agent(
11 role="Researcher",
12 goal=f"Research {topic}",
13 backstory="Expert.",
14 )
15 research_task = Task(
16 description=f"Research {topic}",
17 expected_output="Findings.",
18 agent=researcher,
19 )
20 crew = Crew(agents=[researcher], tasks=[research_task])
21 return str(crew.kickoff())
22
23@workflow(name="content_pipeline")
24def pipeline(topic: str):
25 findings = run_research_crew(topic)
26 print(findings)
27
28pipeline("AI agent frameworks")
29respan.flush()

Examples

Tool calls

Tool calls are automatically captured as spans with inputs, outputs, and timing.

1from respan import Respan
2from respan_instrumentation_crewai import CrewAIInstrumentor
3
4respan = Respan(instrumentations=[CrewAIInstrumentor()])
5
6from crewai import Agent, Crew, Task
7from crewai.tools import tool
8
9@tool
10def get_weather(city: str) -> str:
11 """Get the current weather for a city."""
12 return f"Sunny, 22C in {city}"
13
14researcher = Agent(
15 role="City Researcher",
16 goal="Gather weather data for a city",
17 backstory="You collect city data using available tools.",
18 tools=[get_weather],
19)
20
21task = Task(
22 description="Research the weather in Paris.",
23 expected_output="Weather data for Paris.",
24 agent=researcher,
25)
26
27crew = Crew(agents=[researcher], tasks=[task])
28print(crew.kickoff())
29respan.flush()