CrewAI (tracing)

CrewAI is a framework for orchestrating role-playing autonomous AI agents. Respan’s first-party respan-instrumentation-crewai listener subscribes to CrewAI’s official lifecycle events and emits the canonical workflow, task, agent, tool, and chat span hierarchy directly. Chat spans include the model, provider, prompt, completion, and provider-reported token usage.

Complete first-party tracing requires: respan-instrumentation-crewai 0.2.0 or later and crewai 1.10.1 or later. Earlier 0.1.x instrumentation releases can export workflow structure without a chat child span or token usage.

Create an account at platform.respan.ai and grab an API key.

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

See CrewAI gateway setup to route this integration through the Respan gateway.

Setup

1

Install packages

pip install respan-ai "respan-instrumentation-crewai>=0.2.0" "crewai>=1.10.1"
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 listener is active before CrewAI emits lifecycle events.

from respan import Respan
from respan_instrumentation_crewai import CrewAIInstrumentor
respan = Respan(instrumentations=[CrewAIInstrumentor()])
from crewai import Agent, Crew, Task
researcher = Agent(
role="Researcher",
goal="Research and summarize the latest AI trends",
backstory="You are a senior AI researcher with years of experience.",
)
writer = Agent(
role="Writer",
goal="Write a concise report based on the research",
backstory="You are a technical writer who excels at clear communication.",
)
research_task = Task(
description="Research the latest trends in AI agent frameworks.",
expected_output="A summary of key trends and developments.",
agent=researcher,
)
write_task = Task(
description="Write a brief report based on the research findings.",
expected_output="A well-structured report in markdown format.",
agent=writer,
)
crew = Crew(
agents=[researcher, writer],
tasks=[research_task, write_task],
)
result = crew.kickoff()
print(result)
4

View your trace

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

The coordinated backend release includes CrewAI LLM spans classified as chat in llm_call_count. Historical materialized aggregate rows are not backfilled, so traces aggregated before that release can retain their previous summary count; the LLM child span and its model, prompt, completion, and token fields remain the source of truth.

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() takes no configuration. Activation and deactivation are idempotent, and the listener uses CrewAI’s official event bus to emit canonical spans directly.

LLM usage and cost

On the supported baseline, CrewAI tracing records the model, prompt, completion, and provider-reported prompt, completion, and total token counts. The instrumentation does not emit a direct cost. Respan can derive cost when the model is in its pricing catalog; custom or unknown models can show zero cost until custom pricing is configured.

Attributes

In Respan()

Set defaults at initialization. These apply to all spans.

from respan import Respan
from respan_instrumentation_crewai import CrewAIInstrumentor
respan = Respan(
instrumentations=[CrewAIInstrumentor()],
customer_identifier="user_123",
metadata={"service": "crew-api", "version": "1.0.0"},
)

With propagate_attributes

Override per-request using a context scope.

from respan import Respan, propagate_attributes
from respan_instrumentation_crewai import CrewAIInstrumentor
respan = Respan(instrumentations=[CrewAIInstrumentor()])
from crewai import Agent, Crew, Task
def handle_request(user_id: str, topic: str):
with propagate_attributes(
customer_identifier=user_id,
thread_identifier="conv_abc_123",
metadata={"plan": "pro"},
):
researcher = Agent(
role="Researcher",
goal=f"Research {topic}",
backstory="Expert researcher.",
)
task = Task(
description=f"Research {topic}",
expected_output="Summary",
agent=researcher,
)
crew = Crew(agents=[researcher], tasks=[task])
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. On the supported baseline, each CrewAI kickoff automatically emits a workflow with task, agent, tool, and chat descendants. Use @workflow and @task to add structure when you want to group related crews into a named workflow with nested tasks.

from respan import Respan, workflow, task
from respan_instrumentation_crewai import CrewAIInstrumentor
respan = Respan(instrumentations=[CrewAIInstrumentor()])
from crewai import Agent, Crew, Task
@task(name="run_research_crew")
def run_research_crew(topic: str) -> str:
researcher = Agent(
role="Researcher",
goal=f"Research {topic}",
backstory="Expert.",
)
research_task = Task(
description=f"Research {topic}",
expected_output="Findings.",
agent=researcher,
)
crew = Crew(agents=[researcher], tasks=[research_task])
return str(crew.kickoff())
@workflow(name="content_pipeline")
def pipeline(topic: str):
findings = run_research_crew(topic)
print(findings)
pipeline("AI agent frameworks")

Examples

Tool calls

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

from respan import Respan
from respan_instrumentation_crewai import CrewAIInstrumentor
respan = Respan(instrumentations=[CrewAIInstrumentor()])
from crewai import Agent, Crew, Task
from crewai.tools import tool
@tool
def get_weather(city: str) -> str:
"""Get the current weather for a city."""
return f"Sunny, 22C in {city}"
researcher = Agent(
role="City Researcher",
goal="Gather weather data for a city",
backstory="You collect city data using available tools.",
tools=[get_weather],
)
task = Task(
description="Research the weather in Paris.",
expected_output="Weather data for Paris.",
agent=researcher,
)
crew = Crew(agents=[researcher], tasks=[task])
print(crew.kickoff())