from tealtiger import TealTiger, PolicyMode
from crewai import Agent, Task, Crew, Process
from crewai_tools import SerperDevTool, FileWriterTool
# 1. Initialize TealTiger with agent-specific policies
teal = TealTiger({
"policies": {
# Agent-specific tool access
"agents": {
"researcher": {
"tools": {
"web_search": {"allowed": True},
"file_read": {"allowed": True},
"file_write": {"allowed": False}
},
"budget": {
"maxCostPerTask": 5.00
}
},
"writer": {
"tools": {
"file_write": {"allowed": True},
"file_read": {"allowed": True},
"web_search": {"allowed": False}
},
"budget": {
"maxCostPerTask": 3.00
}
},
"editor": {
"tools": {
"file_write": {"allowed": True},
"file_read": {"allowed": True}
},
"budget": {
"maxCostPerTask": 2.00
}
}
},
# Crew-level policies
"crew": {
"maxCostPerRun": 50.00,
"maxDuration": 300, # 5 minutes
"requireApprovalFor": ["file_delete", "api_call"]
},
# Security policies
"security": {
"detectPII": True,
"redactPII": True,
"blockPromptInjection": True
}
},
# Audit configuration
"audit": {
"enabled": True,
"outputs": ["console", "file"],
"trackAgentActions": True,
"correlateCrewRuns": True
},
# Start in MONITOR mode
"mode": {
"defaultMode": PolicyMode.MONITOR
}
})
# 2. Create tools
search_tool = SerperDevTool()
file_tool = FileWriterTool()
# 3. Create agents with governance
researcher = teal.wrap_agent(Agent(
role='Senior Research Analyst',
goal='Research and analyze AI trends',
backstory='Expert at finding and analyzing information',
tools=[search_tool],
verbose=True
), agent_id='researcher')
writer = teal.wrap_agent(Agent(
role='Content Writer',
goal='Write engaging blog posts',
backstory='Skilled writer with SEO expertise',
tools=[file_tool],
verbose=True
), agent_id='writer')
editor = teal.wrap_agent(Agent(
role='Editor',
goal='Review and improve content',
backstory='Detail-oriented editor',
tools=[file_tool],
verbose=True
), agent_id='editor')
# 4. Create tasks
research_task = Task(
description='Research the latest AI trends in 2026',
agent=researcher,
expected_output='Detailed research report'
)
writing_task = Task(
description='Write a blog post based on the research',
agent=writer,
expected_output='Blog post draft'
)
editing_task = Task(
description='Edit and finalize the blog post',
agent=editor,
expected_output='Final blog post'
)
# 5. Create crew with governance
crew = teal.wrap_crew(Crew(
agents=[researcher, writer, editor],
tasks=[research_task, writing_task, editing_task],
process=Process.sequential,
verbose=True
))
# 6. Run the crew
try:
result = crew.kickoff()
print("Crew completed successfully!")
print(result)
# Get cost metrics per agent
metrics = teal.get_crew_metrics()
print(f"\nCost breakdown:")
print(f" Researcher: ${metrics['agents']['researcher']['cost']:.2f}")
print(f" Writer: ${metrics['agents']['writer']['cost']:.2f}")
print(f" Editor: ${metrics['agents']['editor']['cost']:.2f}")
print(f" Total: ${metrics['total_cost']:.2f}")
except Exception as error:
print(f"Crew failed: {str(error)}")