CrewAI Integration
Protect CrewAI tools with agentguard guardrails using GuardedCrewAITool and guard_crewai_tools().
Overview
The agentguard.integrations.crewai_integration module provides drop-in wrappers for CrewAI tools. All agentguard guardrails — validation, hallucination detection, circuit breakers, rate limiting — work seamlessly with CrewAI agents.
Wrapping a Single Tool
python
from agentguard.integrations.crewai_integration import GuardedCrewAITool
from agentguard import GuardConfig
from agentguard.config import CircuitBreakerConfig
config = GuardConfig(
validate_input=True,
detect_hallucination=True,
max_retries=2,
circuit_breaker=CircuitBreakerConfig(failure_threshold=5),
)
# Wrap any CrewAI tool
guarded_search = GuardedCrewAITool(search_web, config=config)
# Use directly
result = guarded_search.run(query="Python tutorials")
# Or convert to a native CrewAI BaseTool for agents
crewai_tool = guarded_search.to_crewai_tool()
Bulk Wrapping with guard_crewai_tools()
python
from agentguard.integrations.crewai_integration import guard_crewai_tools
from agentguard import GuardConfig
config = GuardConfig(
validate_input=True,
detect_hallucination=True,
max_retries=2,
)
# Wrap all tools at once
guarded = guard_crewai_tools([search_web, query_db, send_email], config=config)
# Use in a CrewAI agent — drop-in replacement
from crewai import Agent
analyst = Agent(
role="Research Analyst",
goal="Research topics thoroughly",
tools=guarded, # list of GuardedCrewAITool instances
)
Working with @tool Decorated Functions
GuardedCrewAITool handles all CrewAI tool styles automatically:
python
from crewai.tools import tool
# 1. Functions decorated with @tool
@tool("Search Web")
def search_web(query: str) -> str:
'''Search the web for information.'''
return requests.get(f"https://...?q={{query}}").text
guarded = GuardedCrewAITool(search_web, config=config)
# 2. BaseTool subclass instances
from crewai.tools import BaseTool
class SearchTool(BaseTool):
name: str = "Search Web"
description: str = "Searches the web."
def _run(self, query: str) -> str:
return requests.get(f"https://...?q={{query}}").text
guarded = GuardedCrewAITool(SearchTool(), config=config)
# 3. Plain Python callables
def my_tool(x: str) -> str:
return x.upper()
guarded = GuardedCrewAITool(my_tool, config=config)
GuardedCrewAITool API
| Method / Attribute | Description |
|---|---|
run(*args, **kwargs) | Execute through agentguard protection stack |
arun(*args, **kwargs) | Async execution |
to_crewai_tool() | Convert to native BaseTool for CrewAI agents |
.name | Tool name (forwarded from original) |
.description | Tool description (forwarded from original) |
.guarded_fn | The underlying GuardedTool instance |
CrewAI is optional
The crewai package is an optional dependency. You can install agentguard without it. The module imports gracefully when crewai is not installed — just avoid calling to_crewai_tool() in that case.