forked from crewAIInc/crewAI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path.cursorrules
More file actions
1429 lines (1207 loc) · 50.7 KB
/
.cursorrules
File metadata and controls
1429 lines (1207 loc) · 50.7 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# CrewAI Development Rules
# Comprehensive best practices for developing with the CrewAI library, covering code organization, performance, security, testing, and common patterns. Based on actual CrewAI codebase analysis for accuracy.
## General Best Practices:
- Leverage structured responses from LLM calls using Pydantic BaseModel for output validation.
- Use the @CrewBase decorator pattern with @agent, @task, and @crew decorators for proper organization.
- Regularly validate outputs from agents and tasks using built-in guardrails or custom validation.
- Use UV for dependency management (CrewAI's standard) with pyproject.toml configuration.
- Python version requirements: 3.10 to 3.14 (as per CrewAI's pyproject.toml).
- Prefer declarative YAML configuration for agents and tasks over hardcoded definitions.
## Code Organization and Structure:
- **Standard CrewAI Project Structure** (from CLI templates):
- `project_name/` (Root directory)
- `.env` (Environment variables - never commit API keys)
- `pyproject.toml` (UV-based dependency management)
- `knowledge/` (Knowledge base files)
- `src/project_name/`
- `__init__.py`
- `main.py` (Entry point)
- `crew.py` (Crew orchestration with @CrewBase decorator)
- `config/`
- `agents.yaml` (Agent definitions)
- `tasks.yaml` (Task definitions)
- `tools/`
- `custom_tool.py` (Custom agent tools)
- `__init__.py`
- **File Naming Conventions**:
- Use descriptive, lowercase names with underscores (e.g., `research_agent.py`).
- Pydantic models: singular names (e.g., `article_summary.py` with class `ArticleSummary`).
- Tests: mirror source file name with `_test` suffix (e.g., `crew_test.py`).
- **CrewAI Class Architecture**:
- Use @CrewBase decorator for main crew class.
- Define agents with @agent decorator returning Agent instances.
- Define tasks with @task decorator returning Task instances.
- Define crew orchestration with @crew decorator returning Crew instance.
- Access configuration via `self.agents_config` and `self.tasks_config`.
## Memory System Patterns:
- **Memory Types** (all supported by CrewAI):
- Short-term memory: ChromaDB with RAG for current context
- Long-term memory: SQLite for task results across sessions
- Entity memory: RAG to track entities (people, places, concepts)
- External memory: Mem0 integration for advanced memory features
- **Memory Configuration**:
- Enable basic memory: `Crew(..., memory=True)`
- Custom storage location: Set `CREWAI_STORAGE_DIR` environment variable
- Memory is stored in platform-specific directories via `appdirs` by default
- **Memory Usage**:
- Memory is automatically managed by agents during task execution
- Access via agent's memory attribute for custom implementations
- Use metadata for categorizing and filtering memory entries
## Pydantic Integration Patterns:
- **Structured Outputs**:
- Use `output_pydantic` in Task definitions for structured results
- Use `output_json` for JSON dictionary outputs
- Cannot use both output_pydantic and output_json simultaneously
- **Task Output Handling**:
- TaskOutput contains raw, pydantic, and json_dict attributes
- CrewOutput aggregates all task outputs with token usage metrics
- Use model_validate_json for Pydantic model validation
- **Custom Models**:
- Inherit from BaseModel for all data structures
- Use Field descriptions for LLM understanding
- Implement model_validator for custom validation logic
## YAML Configuration Best Practices:
- **agents.yaml Structure**:
```yaml
agent_name:
role: "Clear, specific role description"
goal: "Specific goal statement"
backstory: "Detailed background for context"
# Optional: tools, llm, memory, etc.
```
- **tasks.yaml Structure**:
```yaml
task_name:
description: "Detailed task description with context"
expected_output: "Clear output format specification"
agent: agent_name # Reference to agent in agents.yaml
# Optional: context, tools, output_file, etc.
```
- **Configuration Access**:
- Use `self.agents_config['agent_name']` in @agent methods
- Use `self.tasks_config['task_name']` in @task methods
- Support for dynamic configuration via placeholders like {topic}
## Tools and Integration Patterns:
- **Custom Tools**:
- Inherit from BaseTool for custom tool implementation
- Use @tool decorator for simple tool definitions
- Implement proper error handling and input validation
- **Tool Integration**:
- Add tools to agents via tools parameter in Agent constructor
- Tools are automatically inherited by tasks from their assigned agents
- Use structured tool outputs for better LLM understanding
## Performance Considerations:
- **LLM Optimization**:
- Use task context to pass information between sequential tasks
- Implement output caching to avoid redundant LLM calls
- Configure appropriate LLM models per agent for cost/performance balance
- **Memory Management**:
- Be mindful of memory storage growth in long-running applications
- Use score_threshold in memory search to filter relevant results
- Implement periodic memory cleanup if needed
- **Async Operations**:
- Use execute_sync for synchronous task execution
- Consider async patterns for I/O-bound operations in custom tools
## Security Best Practices:
- **API Key Management**:
- Always use .env files for API keys and sensitive configuration
- Never commit API keys to version control
- Use environment variables in production deployments
- **Input Validation**:
- Validate all inputs using Pydantic models where possible
- Implement guardrails for task output validation
- Use field_validator for custom validation logic
- **Tool Security**:
- Implement proper access controls in custom tools
- Validate tool inputs and outputs
- Follow principle of least privilege for tool permissions
## Testing Approaches:
- **Unit Testing**:
- Test individual agents, tasks, and tools in isolation
- Use mocking for external dependencies (LLMs, APIs)
- Test configuration loading and validation
- **Integration Testing**:
- Test crew execution end-to-end with realistic scenarios
- Verify memory persistence across crew runs
- Test tool integration and data flow between tasks
- **Test Organization**:
- Follow CrewAI's test structure: separate test files for each component
- Use pytest fixtures for common test setup
- Mock LLM responses for consistent, fast tests
## Common CrewAI Patterns and Anti-patterns:
- **Recommended Patterns**:
- Use sequential Process for dependent tasks, hierarchical for manager delegation
- Implement task context for data flow between tasks
- Use output_file for persistent task results
- Leverage crew callbacks with @before_kickoff and @after_kickoff decorators
- **Anti-patterns to Avoid**:
- Don't hardcode agent configurations in Python code (use YAML)
- Don't create circular task dependencies
- Don't ignore task execution failures without proper error handling
- Don't overload single agents with too many diverse tools
- **Error Handling**:
- Implement task-level guardrails for output validation
- Use try-catch blocks in custom tools
- Set appropriate max_retries for tasks prone to failures
- Log errors with sufficient context for debugging
## Development Workflow:
- **UV Commands**:
- `crewai create crew <name>` - Create new crew project
- `crewai install` - Install dependencies via UV
- `crewai run` - Execute the crew
- `uv sync` - Sync dependencies
- `uv add <package>` - Add new dependencies
- **Project Setup**:
- Use CrewAI CLI for project scaffolding
- Follow the standard directory structure
- Configure agents and tasks in YAML before implementing crew logic
- **Development Tools**:
- Use UV for dependency management (CrewAI standard)
- Configure pre-commit hooks for code quality
- Use pytest for testing with CrewAI's testing patterns
## Deployment and Production:
- **Environment Configuration**:
- Set CREWAI_STORAGE_DIR for controlled memory storage location
- Use proper logging configuration for production monitoring
- Configure appropriate LLM providers and rate limits
- **Containerization**:
- Include knowledge and config directories in Docker images
- Mount memory storage as persistent volumes if needed
- Set proper environment variables for API keys and configuration
- **Monitoring**:
- Monitor token usage via CrewOutput.token_usage
- Track task execution times and success rates
- Implement health checks for long-running crew services
## CrewAI Flow Patterns and Best Practices
### Flow Architecture and Structure
- **Use Flow class** for complex multi-step workflows that go beyond simple crew orchestration
- **Combine Flows with Crews** to create sophisticated AI automation pipelines
- **Leverage state management** to share data between flow methods
- **Event-driven design** allows for dynamic and responsive workflow execution
### Flow Decorators and Control Flow
- **@start()**: Mark entry points for flow execution (can have multiple start methods)
- **@listen()**: Create method dependencies and execution chains
- **@router()**: Implement conditional branching based on method outputs
- **or_()** and **and_()**: Combine multiple trigger conditions for complex workflows
### Flow State Management Patterns
```python
# Structured state with Pydantic (recommended for complex workflows)
class WorkflowState(BaseModel):
task_results: List[str] = []
current_step: str = "initialize"
user_preferences: dict = {}
completion_status: bool = False
class MyFlow(Flow[WorkflowState]):
@start()
def initialize(self):
self.state.current_step = "processing"
# State automatically gets unique UUID in self.state.id
# Unstructured state (good for simple workflows)
class SimpleFlow(Flow):
@start()
def begin(self):
self.state["counter"] = 0
self.state["results"] = []
# Auto-generated ID available in self.state["id"]
```
### Flow Method Patterns
```python
# Basic sequential flow
@start()
def step_one(self):
return "data from step one"
@listen(step_one)
def step_two(self, data_from_step_one):
return f"processed: {data_from_step_one}"
# Parallel execution with convergence
@start()
def task_a(self):
return "result_a"
@start()
def task_b(self):
return "result_b"
@listen(and_(task_a, task_b))
def combine_results(self):
# Waits for both task_a AND task_b to complete
return f"combined: {self.state}"
# Conditional routing
@router(step_one)
def decision_point(self):
if some_condition:
return "success_path"
return "failure_path"
@listen("success_path")
def handle_success(self):
# Handle success case
pass
@listen("failure_path")
def handle_failure(self):
# Handle failure case
pass
# OR condition listening
@listen(or_(task_a, task_b))
def process_any_result(self, result):
# Triggers when EITHER task_a OR task_b completes
return f"got result: {result}"
```
### Flow Persistence Patterns
```python
# Class-level persistence (all methods persisted)
@persist(verbose=True)
class PersistentFlow(Flow[MyState]):
@start()
def initialize(self):
self.state.counter += 1
# Method-level persistence (selective)
class SelectiveFlow(Flow):
@persist
@start()
def critical_step(self):
# Only this method's state is persisted
self.state["important_data"] = "value"
@start()
def temporary_step(self):
# This method's state is not persisted
pass
```
### Flow Execution Patterns
```python
# Synchronous execution
flow = MyFlow()
result = flow.kickoff()
final_state = flow.state
# Asynchronous execution
async def run_async_flow():
flow = MyFlow()
result = await flow.kickoff_async()
return result
# Flow with input parameters
flow = MyFlow()
result = flow.kickoff(inputs={"user_id": "123", "task": "research"})
# Flow plotting and visualization
flow.plot("workflow_diagram") # Generates HTML visualization
```
### Advanced Flow Patterns
```python
# Cyclic/Loop patterns
class CyclicFlow(Flow):
max_iterations = 5
current_iteration = 0
@start("loop")
def process_iteration(self):
if self.current_iteration >= self.max_iterations:
return
# Process current iteration
self.current_iteration += 1
@router(process_iteration)
def check_continue(self):
if self.current_iteration < self.max_iterations:
return "loop" # Continue cycling
return "complete"
@listen("complete")
def finalize(self):
# Final processing
pass
# Complex multi-router pattern
@router(analyze_data)
def primary_router(self):
# Returns multiple possible paths based on analysis
if self.state.confidence > 0.8:
return "high_confidence"
elif self.state.errors_found:
return "error_handling"
return "manual_review"
@router("high_confidence")
def secondary_router(self):
# Further routing based on high confidence results
return "automated_processing"
# Exception handling in flows
@start()
def risky_operation(self):
try:
# Some operation that might fail
result = dangerous_function()
self.state["success"] = True
return result
except Exception as e:
self.state["error"] = str(e)
self.state["success"] = False
return None
@listen(risky_operation)
def handle_result(self, result):
if self.state.get("success", False):
# Handle success case
pass
else:
# Handle error case
error = self.state.get("error")
# Implement error recovery logic
```
### Flow Integration with Crews
```python
# Combining Flows with Crews for complex workflows
class CrewOrchestrationFlow(Flow[WorkflowState]):
@start()
def research_phase(self):
research_crew = ResearchCrew()
result = research_crew.crew().kickoff(inputs={"topic": self.state.research_topic})
self.state.research_results = result.raw
return result
@listen(research_phase)
def analysis_phase(self, research_results):
analysis_crew = AnalysisCrew()
result = analysis_crew.crew().kickoff(inputs={
"data": self.state.research_results,
"focus": self.state.analysis_focus
})
self.state.analysis_results = result.raw
return result
@router(analysis_phase)
def decide_next_action(self):
if self.state.analysis_results.confidence > 0.7:
return "generate_report"
return "additional_research"
@listen("generate_report")
def final_report(self):
reporting_crew = ReportingCrew()
return reporting_crew.crew().kickoff(inputs={
"research": self.state.research_results,
"analysis": self.state.analysis_results
})
```
### Flow Best Practices
- **State Management**: Use structured state (Pydantic) for complex workflows, unstructured for simple ones
- **Method Design**: Keep flow methods focused and single-purpose
- **Error Handling**: Implement proper exception handling and error recovery paths
- **State Persistence**: Use @persist for critical workflows that need recovery capability
- **Flow Visualization**: Use flow.plot() to understand and debug complex workflow structures
- **Async Support**: Leverage async methods for I/O-bound operations within flows
- **Resource Management**: Be mindful of state size and memory usage in long-running flows
- **Testing Flows**: Test individual methods and overall flow execution patterns
- **Event Monitoring**: Use CrewAI event system to monitor flow execution and performance
### Flow Anti-patterns to Avoid
- **Don't create overly complex flows** with too many branches and conditions
- **Don't store large objects** in state that could cause memory issues
- **Don't ignore error handling** in flow methods
- **Don't create circular dependencies** between flow methods
- **Don't mix synchronous and asynchronous** patterns inconsistently
- **Don't overuse routers** when simple linear flow would suffice
- **Don't forget to handle edge cases** in router logic
## CrewAI Version Compatibility:
- Stay updated with CrewAI releases for new features and bug fixes
- Test crew functionality when upgrading CrewAI versions
- Use version constraints in pyproject.toml (e.g., "crewai[tools]>=0.140.0,<1.0.0")
- Monitor deprecation warnings for future compatibility
## Code Examples and Implementation Patterns
### Complete Crew Implementation Example:
```python
from crewai import Agent, Crew, Process, Task
from crewai.project import CrewBase, agent, crew, task, before_kickoff, after_kickoff
from crewai_tools import SerperDevTool, FileReadTool
from crewai.agents.agent_builder.base_agent import BaseAgent
from typing import List
from pydantic import BaseModel, Field
class ResearchOutput(BaseModel):
title: str = Field(description="Research topic title")
summary: str = Field(description="Executive summary")
key_findings: List[str] = Field(description="Key research findings")
recommendations: List[str] = Field(description="Actionable recommendations")
sources: List[str] = Field(description="Source URLs and references")
confidence_score: float = Field(description="Confidence in findings (0-1)")
@CrewBase
class ResearchCrew():
"""Advanced research crew with structured outputs and validation"""
agents: List[BaseAgent]
tasks: List[Task]
@before_kickoff
def setup_environment(self):
"""Initialize environment before crew execution"""
print("🚀 Setting up research environment...")
# Validate API keys, create directories, etc.
@after_kickoff
def cleanup_and_report(self, output):
"""Handle post-execution tasks"""
print(f"✅ Research completed. Generated {len(output.tasks_output)} task outputs")
print(f"📊 Token usage: {output.token_usage}")
@agent
def researcher(self) -> Agent:
return Agent(
config=self.agents_config['researcher'],
tools=[SerperDevTool()],
verbose=True,
memory=True,
max_iter=15,
max_execution_time=1800
)
@agent
def analyst(self) -> Agent:
return Agent(
config=self.agents_config['analyst'],
tools=[FileReadTool()],
verbose=True,
memory=True
)
@task
def research_task(self) -> Task:
return Task(
config=self.tasks_config['research_task'],
agent=self.researcher(),
output_pydantic=ResearchOutput
)
@task
def validation_task(self) -> Task:
return Task(
config=self.tasks_config['validation_task'],
agent=self.analyst(),
context=[self.research_task()],
guardrail=self.validate_research_quality,
max_retries=3
)
def validate_research_quality(self, output) -> tuple[bool, str]:
"""Custom guardrail to ensure research quality"""
content = output.raw
if len(content) < 500:
return False, "Research output too brief. Need more detailed analysis."
if not any(keyword in content.lower() for keyword in ['conclusion', 'finding', 'result']):
return False, "Missing key analytical elements."
return True, content
@crew
def crew(self) -> Crew:
return Crew(
agents=self.agents,
tasks=self.tasks,
process=Process.sequential,
memory=True,
verbose=True,
max_rpm=100
)
```
### Custom Tool Implementation with Error Handling:
```python
from crewai.tools import BaseTool
from typing import Type, Optional, Any
from pydantic import BaseModel, Field
import requests
import time
from tenacity import retry, stop_after_attempt, wait_exponential
class SearchInput(BaseModel):
query: str = Field(description="Search query")
max_results: int = Field(default=10, description="Maximum results to return")
timeout: int = Field(default=30, description="Request timeout in seconds")
class RobustSearchTool(BaseTool):
name: str = "robust_search"
description: str = "Perform web search with retry logic and error handling"
args_schema: Type[BaseModel] = SearchInput
def __init__(self, api_key: Optional[str] = None, **kwargs):
super().__init__(**kwargs)
self.api_key = api_key or os.getenv("SEARCH_API_KEY")
self.rate_limit_delay = 1.0
self.last_request_time = 0
@retry(
stop=stop_after_attempt(3),
wait=wait_exponential(multiplier=1, min=4, max=10)
)
def _run(self, query: str, max_results: int = 10, timeout: int = 30) -> str:
"""Execute search with retry logic"""
try:
# Rate limiting
time_since_last = time.time() - self.last_request_time
if time_since_last < self.rate_limit_delay:
time.sleep(self.rate_limit_delay - time_since_last)
# Input validation
if not query or len(query.strip()) == 0:
return "Error: Empty search query provided"
if len(query) > 500:
return "Error: Search query too long (max 500 characters)"
# Perform search
results = self._perform_search(query, max_results, timeout)
self.last_request_time = time.time()
return self._format_results(results)
except requests.exceptions.Timeout:
return f"Search timed out after {timeout} seconds"
except requests.exceptions.RequestException as e:
return f"Search failed due to network error: {str(e)}"
except Exception as e:
return f"Unexpected error during search: {str(e)}"
def _perform_search(self, query: str, max_results: int, timeout: int) -> List[dict]:
"""Implement actual search logic here"""
# Your search API implementation
pass
def _format_results(self, results: List[dict]) -> str:
"""Format search results for LLM consumption"""
if not results:
return "No results found for the given query."
formatted = "Search Results:\n\n"
for i, result in enumerate(results[:10], 1):
formatted += f"{i}. {result.get('title', 'No title')}\n"
formatted += f" URL: {result.get('url', 'No URL')}\n"
formatted += f" Summary: {result.get('snippet', 'No summary')}\n\n"
return formatted
```
### Advanced Memory Management:
```python
import os
from crewai.memory import ExternalMemory, ShortTermMemory, LongTermMemory
from crewai.memory.storage.mem0_storage import Mem0Storage
class AdvancedMemoryManager:
"""Enhanced memory management for CrewAI applications"""
def __init__(self, crew, config: dict = None):
self.crew = crew
self.config = config or {}
self.setup_memory_systems()
def setup_memory_systems(self):
"""Configure multiple memory systems"""
# Short-term memory for current session
self.short_term = ShortTermMemory()
# Long-term memory for cross-session persistence
self.long_term = LongTermMemory()
# External memory with Mem0 (if configured)
if self.config.get('use_external_memory'):
self.external = ExternalMemory.create_storage(
crew=self.crew,
embedder_config={
"provider": "mem0",
"config": {
"api_key": os.getenv("MEM0_API_KEY"),
"user_id": self.config.get('user_id', 'default')
}
}
)
def save_with_context(self, content: str, memory_type: str = "short_term",
metadata: dict = None, agent: str = None):
"""Save content with enhanced metadata"""
enhanced_metadata = {
"timestamp": time.time(),
"session_id": self.config.get('session_id'),
"crew_type": self.crew.__class__.__name__,
**(metadata or {})
}
if memory_type == "short_term":
self.short_term.save(content, enhanced_metadata, agent)
elif memory_type == "long_term":
self.long_term.save(content, enhanced_metadata, agent)
elif memory_type == "external" and hasattr(self, 'external'):
self.external.save(content, enhanced_metadata, agent)
def search_across_memories(self, query: str, limit: int = 5) -> dict:
"""Search across all memory systems"""
results = {
"short_term": [],
"long_term": [],
"external": []
}
# Search short-term memory
results["short_term"] = self.short_term.search(query, limit=limit)
# Search long-term memory
results["long_term"] = self.long_term.search(query, limit=limit)
# Search external memory (if available)
if hasattr(self, 'external'):
results["external"] = self.external.search(query, limit=limit)
return results
def cleanup_old_memories(self, days_threshold: int = 30):
"""Clean up old memories based on age"""
cutoff_time = time.time() - (days_threshold * 24 * 60 * 60)
# Implement cleanup logic based on timestamps in metadata
# This would vary based on your specific storage implementation
pass
```
### Production Monitoring and Metrics:
```python
import time
import logging
import json
from datetime import datetime
from typing import Dict, Any, List
from dataclasses import dataclass, asdict
@dataclass
class TaskMetrics:
task_name: str
agent_name: str
start_time: float
end_time: float
duration: float
tokens_used: int
success: bool
error_message: Optional[str] = None
memory_usage_mb: Optional[float] = None
class CrewMonitor:
"""Comprehensive monitoring for CrewAI applications"""
def __init__(self, crew_name: str, log_level: str = "INFO"):
self.crew_name = crew_name
self.metrics: List[TaskMetrics] = []
self.session_start = time.time()
# Setup logging
logging.basicConfig(
level=getattr(logging, log_level),
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler(f'crew_{crew_name}_{datetime.now().strftime("%Y%m%d")}.log'),
logging.StreamHandler()
]
)
self.logger = logging.getLogger(f"CrewAI.{crew_name}")
def start_task_monitoring(self, task_name: str, agent_name: str) -> dict:
"""Start monitoring a task execution"""
context = {
"task_name": task_name,
"agent_name": agent_name,
"start_time": time.time()
}
self.logger.info(f"Task started: {task_name} by {agent_name}")
return context
def end_task_monitoring(self, context: dict, success: bool = True,
tokens_used: int = 0, error: str = None):
"""End monitoring and record metrics"""
end_time = time.time()
duration = end_time - context["start_time"]
# Get memory usage (if psutil is available)
memory_usage = None
try:
import psutil
process = psutil.Process()
memory_usage = process.memory_info().rss / 1024 / 1024 # MB
except ImportError:
pass
metrics = TaskMetrics(
task_name=context["task_name"],
agent_name=context["agent_name"],
start_time=context["start_time"],
end_time=end_time,
duration=duration,
tokens_used=tokens_used,
success=success,
error_message=error,
memory_usage_mb=memory_usage
)
self.metrics.append(metrics)
# Log the completion
status = "SUCCESS" if success else "FAILED"
self.logger.info(f"Task {status}: {context['task_name']} "
f"(Duration: {duration:.2f}s, Tokens: {tokens_used})")
if error:
self.logger.error(f"Task error: {error}")
def get_performance_summary(self) -> Dict[str, Any]:
"""Generate comprehensive performance summary"""
if not self.metrics:
return {"message": "No metrics recorded yet"}
successful_tasks = [m for m in self.metrics if m.success]
failed_tasks = [m for m in self.metrics if not m.success]
total_duration = sum(m.duration for m in self.metrics)
total_tokens = sum(m.tokens_used for m in self.metrics)
avg_duration = total_duration / len(self.metrics)
return {
"crew_name": self.crew_name,
"session_duration": time.time() - self.session_start,
"total_tasks": len(self.metrics),
"successful_tasks": len(successful_tasks),
"failed_tasks": len(failed_tasks),
"success_rate": len(successful_tasks) / len(self.metrics),
"total_duration": total_duration,
"average_task_duration": avg_duration,
"total_tokens_used": total_tokens,
"average_tokens_per_task": total_tokens / len(self.metrics) if self.metrics else 0,
"slowest_task": max(self.metrics, key=lambda x: x.duration).task_name if self.metrics else None,
"most_token_intensive": max(self.metrics, key=lambda x: x.tokens_used).task_name if self.metrics else None,
"common_errors": self._get_common_errors()
}
def _get_common_errors(self) -> Dict[str, int]:
"""Get frequency of common errors"""
error_counts = {}
for metric in self.metrics:
if metric.error_message:
error_counts[metric.error_message] = error_counts.get(metric.error_message, 0) + 1
return dict(sorted(error_counts.items(), key=lambda x: x[1], reverse=True))
def export_metrics(self, filename: str = None) -> str:
"""Export metrics to JSON file"""
if not filename:
filename = f"crew_metrics_{self.crew_name}_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json"
export_data = {
"summary": self.get_performance_summary(),
"detailed_metrics": [asdict(m) for m in self.metrics]
}
with open(filename, 'w') as f:
json.dump(export_data, f, indent=2, default=str)
self.logger.info(f"Metrics exported to {filename}")
return filename
# Usage in crew implementation
monitor = CrewMonitor("research_crew")
@task
def monitored_research_task(self) -> Task:
def task_callback(task_output):
# This would be called after task completion
context = getattr(task_output, '_monitor_context', {})
if context:
tokens = getattr(task_output, 'token_usage', {}).get('total', 0)
monitor.end_task_monitoring(context, success=True, tokens_used=tokens)
# Start monitoring would be called before task execution
# This is a simplified example - in practice you'd integrate this into the task execution flow
return Task(
config=self.tasks_config['research_task'],
agent=self.researcher(),
callback=task_callback
)
```
### Error Handling and Recovery Patterns:
```python
from enum import Enum
from typing import Optional, Callable, Any
import traceback
class ErrorSeverity(Enum):
LOW = "low"
MEDIUM = "medium"
HIGH = "high"
CRITICAL = "critical"
class CrewError(Exception):
"""Base exception for CrewAI applications"""
def __init__(self, message: str, severity: ErrorSeverity = ErrorSeverity.MEDIUM,
context: dict = None):
super().__init__(message)
self.severity = severity
self.context = context or {}
self.timestamp = time.time()
class TaskExecutionError(CrewError):
"""Raised when task execution fails"""
pass
class ValidationError(CrewError):
"""Raised when validation fails"""
pass
class ConfigurationError(CrewError):
"""Raised when configuration is invalid"""
pass
class ErrorHandler:
"""Centralized error handling for CrewAI applications"""
def __init__(self, crew_name: str):
self.crew_name = crew_name
self.error_log: List[CrewError] = []
self.recovery_strategies: Dict[type, Callable] = {}
def register_recovery_strategy(self, error_type: type, strategy: Callable):
"""Register a recovery strategy for specific error types"""
self.recovery_strategies[error_type] = strategy
def handle_error(self, error: Exception, context: dict = None) -> Any:
"""Handle errors with appropriate recovery strategies"""
# Convert to CrewError if needed
if not isinstance(error, CrewError):
crew_error = CrewError(
message=str(error),
severity=ErrorSeverity.MEDIUM,
context=context or {}
)
else:
crew_error = error
# Log the error
self.error_log.append(crew_error)
self._log_error(crew_error)
# Apply recovery strategy if available
error_type = type(error)
if error_type in self.recovery_strategies:
try:
return self.recovery_strategies[error_type](error, context)
except Exception as recovery_error:
self._log_error(CrewError(
f"Recovery strategy failed: {str(recovery_error)}",
ErrorSeverity.HIGH,
{"original_error": str(error), "recovery_error": str(recovery_error)}
))
# If critical, re-raise
if crew_error.severity == ErrorSeverity.CRITICAL:
raise crew_error
return None
def _log_error(self, error: CrewError):
"""Log error with appropriate level based on severity"""
logger = logging.getLogger(f"CrewAI.{self.crew_name}.ErrorHandler")
error_msg = f"[{error.severity.value.upper()}] {error}"
if error.context:
error_msg += f" | Context: {error.context}"
if error.severity in [ErrorSeverity.HIGH, ErrorSeverity.CRITICAL]:
logger.error(error_msg)
logger.error(f"Stack trace: {traceback.format_exc()}")
elif error.severity == ErrorSeverity.MEDIUM:
logger.warning(error_msg)
else:
logger.info(error_msg)
def get_error_summary(self) -> Dict[str, Any]:
"""Get summary of errors encountered"""
if not self.error_log:
return {"total_errors": 0}
severity_counts = {}
for error in self.error_log:
severity_counts[error.severity.value] = severity_counts.get(error.severity.value, 0) + 1
return {
"total_errors": len(self.error_log),
"severity_breakdown": severity_counts,
"recent_errors": [str(e) for e in self.error_log[-5:]], # Last 5 errors
"most_recent_error": str(self.error_log[-1]) if self.error_log else None
}
# Example usage in crew
error_handler = ErrorHandler("research_crew")
# Register recovery strategies
def retry_with_simpler_model(error, context):
"""Recovery strategy: retry with a simpler model"""
if "rate limit" in str(error).lower():
time.sleep(60) # Wait and retry
return "RETRY"
elif "model overloaded" in str(error).lower():
# Switch to simpler model and retry
return "RETRY_WITH_SIMPLE_MODEL"
return None
error_handler.register_recovery_strategy(TaskExecutionError, retry_with_simpler_model)
@task
def robust_task(self) -> Task:
def execute_with_error_handling(task_func):
def wrapper(*args, **kwargs):
try:
return task_func(*args, **kwargs)
except Exception as e:
result = error_handler.handle_error(e, {"task": "research_task"})
if result == "RETRY":
# Implement retry logic
pass
elif result == "RETRY_WITH_SIMPLE_MODEL":