DS DevShelfHub Projects · AI tools
Tutorials / AI Agents / Debugging & Observability
Build with CrewAI Advanced · 13 min read Page 13 of 29

CrewAI Debugging: Trace Stuck Crews and Tool Failures

By DevShelfHub

Verbose logging, lifecycle callbacks, token/cost tracking, Langfuse tracing, and a field guide to the symptoms you'll actually see.

Series progress13 / 29
CrewAI debugging tutorial — CrewAI Debugging: Trace Stuck Crews and Tool Failures

Verbose Mode is Your Best Friend

Set verbose=True on agents and the crew. You'll see every reasoning step and tool call.

Verbose everywhere

PYTHON
researcher = Agent(role="...", goal="...", backstory="...", verbose=True)
crew = Crew(agents=[researcher], tasks=[t], verbose=True)

💡 Always: Capture verbose logs to a file in production — python run.py 2>&1 | tee run.log. They are gold when something breaks.

Callbacks for Tracing

Hook into agent and task lifecycle for custom logging, metrics, or alerts.

Step + task callbacks

PYTHON
def log_step(step):
    print(f"[STEP] {step.agent.role}: {step.thought[:80]}...")

def log_task(output):
    print(f"[TASK DONE] {output.description[:60]} → {len(str(output))} chars")

researcher = Agent(role="...", goal="...", backstory="...", step_callback=log_step)
research_task = Task(description="...", agent=researcher, callback=log_task, expected_output="...")

Tracking Token Usage & Cost

Token usage from result

PYTHON
result = crew.kickoff(inputs={"topic": "RAG"})

usage = result.token_usage
print(f"Total tokens: {usage.total_tokens}")
print(f"Prompt:       {usage.prompt_tokens}")
print(f"Completion:   {usage.completion_tokens}")

# rough cost estimate (gpt-4o-mini, 2025 prices)
cost = (usage.prompt_tokens * 0.15 + usage.completion_tokens * 0.60) / 1_000_000
print(f"≈ ${cost:.4f}")

Langfuse / OpenTelemetry Tracing

For visualizing multi-agent flows, plug into Langfuse or any OTel-compatible backend.

Langfuse setup

PYTHON
import os
os.environ["LANGFUSE_PUBLIC_KEY"] = "pk-..."
os.environ["LANGFUSE_SECRET_KEY"] = "sk-..."

from langfuse.callback import CallbackHandler
handler = CallbackHandler()

# Pass to the underlying LLM
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(model="gpt-4o", callbacks=[handler])

agent = Agent(role="...", goal="...", backstory="...", llm=llm)

Common Symptoms & Causes

Crew loops forever

Cap with max_iter on the agent and max_execution_time on the task. Often caused by an unattainable expected_output.

Tool gets called with wrong args

Tool description / docstring isn't precise enough. Show example args in the docstring.

Output ignores expected_output format

Strengthen expected_output. Add an example. Switch to output_pydantic for hard guarantees.

Silent failure / empty output

Tool raised an exception that aborted the agent. Wrap tools in try/except and return an error string.

Recovering failed kickoffs

When upstream tasks succeeded but a late task failed, Crew.replay() resumes from a recorded task_id instead of paying for the entire chain again. See the train, test, and replay tutorial for CLI parity and guardrails.

Notes

Log the inputs, not just the outputs

When a crew drifts, the fastest signal is what each agent actually saw after templating and tool results. Redact secrets, but keep structured input snapshots so you can replay a single step without re-running the whole chain.

Verbose mode is loud for a reason

Turn verbose on in local dev, then pair it with log sampling or tracing in shared environments. Full prompt dumps in centralized logs can violate policy and balloon storage costs.

Separate model issues from orchestration issues

If tools succeed but answers are wrong, fix prompts and contracts. If tools fail intermittently, look at timeouts, retries, and network partitions before you tune agent personalities.

Stuck loops often mean unclear stop conditions

Tighten max iterations, add guardrails that validate partial results, and require explicit done signals in expected_output so agents cannot negotiate another pass without new evidence.

CrewAI debugging FAQ

Why is my CrewAI crew stuck in a loop?

Common causes are underspecified tasks, conflicting goals between agents, or tools that return unusable data. Turn on verbose logging, cap iterations, and simplify to two agents to isolate the failure.

How do I trace CrewAI runs in production?

Prefer structured callbacks or tracing integrations over printing raw prompts. Correlate each kickoff with an id, capture latency and tokens per step, and redact secrets automatically.

What CrewAI verbose mode is for?

Verbose mode prints intermediate reasoning during local development. Disable it for customer-facing paths and replace it with sampled tracing so you still retain observability.

How do I debug CrewAI tool failures?

Log tool inputs and normalized errors, add retries only for idempotent tools, and write unit tests around tool parsers so malformed responses surface early.

When should I add Langfuse or similar tracing?

Add external tracing when multiple teams depend on the same crews or when you need dashboards for latency, cost, and error budgets across environments.

See also: DevShelfHub's CrewAI tool review for a product-level comparison, pricing notes, and links back into this tutorial series.

Quick jump: API Reference