CrewAI

CrewAI orchestrates role-based agents executing structured tasks. Trefur ships a dedicated patch_crewai adapter that wraps the Crew + Agent + Task lifecycle, so you see one step per task with explicit agent role attribution.

Install

pip install "trefur-observe[crewai]"

Setup

import os
from crewai import Agent, Task, Crew, Process
from trefur_observe import TrefurObserve, patch_crewai

# Initialise observe once at startup.
TrefurObserve.init(
    api_key=os.environ["TREFUR_API_KEY"],
    agent={"name": "research-crew", "framework": "crewai"},
)
patch_crewai(TrefurObserve.get_instance())

researcher = Agent(
    role="Research analyst",
    goal="Find authoritative sources on a topic",
    backstory="You verify claims and only cite primary sources.",
    allow_delegation=False,
)
writer = Agent(
    role="Writer",
    goal="Turn research into a 200-word brief",
    backstory="You write for executives.",
    allow_delegation=False,
)

research_task = Task(
    description="Research the top three pricing tiers for AI observability tools.",
    expected_output="Bullet list of 5 dated price points with source URLs.",
    agent=researcher,
)
write_task = Task(
    description="Write a 200-word executive brief based on the research.",
    expected_output="A 200-word memo.",
    agent=writer,
    context=[research_task],
)

crew = Crew(
    agents=[researcher, writer],
    tasks=[research_task, write_task],
    process=Process.sequential,
)
result = crew.kickoff()

What gets traced

  • Crew kickoff as the root run.
  • Each task as a step with name, agent role, status, latency.
  • Tool calls — every CrewAI @tool registers as a tool step.
  • LLM completions on whichever provider the Agent is configured with (OpenAI, Anthropic, Bedrock, Ollama).
  • Delegation events when one agent delegates to another (recorded as a child run).
  • Sequential vs hierarchical process — the orchestration mode is tagged on the root run.

Common use case — research-then-write pipelines

The canonical CrewAI pattern is a research agent + writer + critic running sequentially. Trefur shows the full chain with timing for each handoff. Build drift alerts on Task latency or on tool-call count exceeding a threshold to catch regressions.

Common pitfalls

  • Hierarchical process + manager_llm. The manager agent makes its own LLM calls to pick the next worker. These are captured as separate steps tagged role="crewai_manager"; filter them out when computing per-worker latency.
  • Async kickoff. crew.kickoff_async()requires you to await the result before the process exits. If the event loop closes early, the buffer is dropped — call TrefurObserve.get_instance().flush() after the await.

See the Python SDK reference for the full adapter surface.