-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathcrewai_harness.py
More file actions
73 lines (60 loc) · 2.23 KB
/
crewai_harness.py
File metadata and controls
73 lines (60 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
"""
CrewAI + cascadeflow harness integration example.
Run:
pip install "cascadeflow[crewai,openai]"
export OPENAI_API_KEY="your-key"
python examples/integrations/crewai_harness.py
"""
from __future__ import annotations
def main() -> None:
try:
from crewai import Agent, Crew, Process, Task
except ImportError as exc:
raise SystemExit(
"CrewAI is not installed. " 'Install with: pip install "cascadeflow[crewai,openai]"'
) from exc
from cascadeflow import init, run
from cascadeflow.integrations.crewai import CrewAIHarnessConfig, enable
# 1) Initialize harness globally.
init(mode="observe", budget=1.0, max_tool_calls=6)
# 2) Explicitly enable CrewAI integration hooks (opt-in).
enabled = enable(
config=CrewAIHarnessConfig(
fail_open=True,
enable_budget_gate=True,
)
)
if not enabled:
raise SystemExit(
"CrewAI hooks are unavailable in this environment. " "Ensure crewai>=1.5 is installed."
)
agent = Agent(
role="Routing Analyst",
goal="Explain model routing impact on cost and latency in plain language.",
backstory="You are concise and practical.",
allow_delegation=False,
llm="openai/gpt-4o-mini",
verbose=False,
)
task = Task(
description="Explain why inside-the-loop routing helps agent workloads.",
expected_output="One short paragraph and three bullet points.",
agent=agent,
)
with run(budget=0.5, max_tool_calls=4) as session:
crew = Crew(agents=[agent], tasks=[task], process=Process.sequential, verbose=False)
result = crew.kickoff()
print("=== Result ===")
print(result)
print("\n=== Harness Metrics ===")
print(f"Cost: ${session.cost:.6f}")
print(f"Remaining budget: {session.budget_remaining}")
print(f"Steps: {session.step_count}")
print(f"Tool calls: {session.tool_calls}")
print(f"Latency: {session.latency_used_ms:.0f}ms")
print(f"Energy: {session.energy_used:.1f}")
print("\n=== Decision Trace ===")
for event in session.trace():
print(event)
if __name__ == "__main__":
main()