11import logging
22import uuid
3+ import webbrowser
4+ from pathlib import Path
35
46from rich .console import Console
57from rich .panel import Panel
1416logger = logging .getLogger (__name__ )
1517
1618
19+ def _update_or_create_env_file ():
20+ """Update or create .env file with CREWAI_TRACING_ENABLED=true."""
21+ env_path = Path (".env" )
22+ env_content = ""
23+ variable_name = "CREWAI_TRACING_ENABLED"
24+ variable_value = "true"
25+
26+ # Read existing content if file exists
27+ if env_path .exists ():
28+ with open (env_path , "r" ) as f :
29+ env_content = f .read ()
30+
31+ # Check if CREWAI_TRACING_ENABLED is already set
32+ lines = env_content .splitlines ()
33+ variable_exists = False
34+ updated_lines = []
35+
36+ for line in lines :
37+ if line .strip ().startswith (f"{ variable_name } =" ):
38+ # Update existing variable
39+ updated_lines .append (f"{ variable_name } ={ variable_value } " )
40+ variable_exists = True
41+ else :
42+ updated_lines .append (line )
43+
44+ # Add variable if it doesn't exist
45+ if not variable_exists :
46+ if updated_lines and not updated_lines [- 1 ].strip ():
47+ # If last line is empty, replace it
48+ updated_lines [- 1 ] = f"{ variable_name } ={ variable_value } "
49+ else :
50+ # Add new line and then the variable
51+ updated_lines .append (f"{ variable_name } ={ variable_value } " )
52+
53+ # Write updated content
54+ with open (env_path , "w" ) as f :
55+ f .write ("\n " .join (updated_lines ))
56+ if updated_lines : # Add final newline if there's content
57+ f .write ("\n " )
58+
59+
1760class FirstTimeTraceHandler :
1861 """Handles the first-time user trace collection and display flow."""
1962
@@ -48,6 +91,12 @@ def handle_execution_completion(self):
4891 if user_wants_traces :
4992 self ._initialize_backend_and_send_events ()
5093
94+ # Enable tracing for future runs by updating .env file
95+ try :
96+ _update_or_create_env_file ()
97+ except Exception : # noqa: S110
98+ pass
99+
51100 if self .ephemeral_url :
52101 self ._display_ephemeral_trace_link ()
53102
@@ -108,9 +157,14 @@ def _initialize_backend_and_send_events(self):
108157 self ._gracefully_fail (f"Backend initialization failed: { e } " )
109158
110159 def _display_ephemeral_trace_link (self ):
111- """Display the ephemeral trace link to the user."""
160+ """Display the ephemeral trace link to the user and automatically open browser ."""
112161 console = Console ()
113162
163+ try :
164+ webbrowser .open (self .ephemeral_url )
165+ except Exception : # noqa: S110
166+ pass
167+
114168 panel_content = f"""
115169🎉 Your First CrewAI Execution Trace is Ready!
116170
@@ -123,7 +177,8 @@ def _display_ephemeral_trace_link(self):
123177• Tool usage and results
124178• LLM calls and responses
125179
126- To use traces add tracing=True to your Crew(tracing=True) / Flow(tracing=True)
180+ ✅ Tracing has been enabled for future runs! (CREWAI_TRACING_ENABLED=true added to .env)
181+ You can also add tracing=True to your Crew(tracing=True) / Flow(tracing=True) for more control.
127182
128183📝 Note: This link will expire in 24 hours.
129184 """ .strip ()
@@ -158,8 +213,8 @@ def _show_local_trace_message(self):
158213• Execution duration: { self .batch_manager .calculate_duration ("execution" )} ms
159214• Batch ID: { self .batch_manager .trace_batch_id }
160215
216+ Tracing has been enabled for future runs! (CREWAI_TRACING_ENABLED=true added to .env)
161217The traces include agent decisions, task execution, and tool usage.
162- Try running with CREWAI_TRACING_ENABLED=true next time for persistent traces.
163218 """ .strip ()
164219
165220 panel = Panel (
0 commit comments