Deployment and Event Listeners
Deployment and Event Listeners
Section titled “Deployment and Event Listeners”Congratulations! You have built a sophisticated Crew. Now it is time to move it from your local machine to a production environment. In this final chapter, we cover the lifecycle hooks (Event Listeners), security best practices, and how to containerize your application using Docker.
Event Listeners: Hooking into the Lifecycle
Section titled “Event Listeners: Hooking into the Lifecycle”In production, you often need to perform actions “outside” the main logic of the AI. For example, you might want to log data to a database every time a task finishes, or send a Slack notification when a Crew starts. CrewAI provides callback mechanisms that act as event listeners.
You can assign callbacks at the Task level or the Crew level using task_callback, step_callback, or lifecycle hooks like before_kickoff and after_kickoff.
def on_task_completed(output): # Send output to a logging service print(f"[Log] Task Finished: {output.raw[:50]}...")
research_task = Task( description="Research AI trends", expected_output="A summary report", agent=researcher, callback=on_task_completed # Triggered when this task finishes)Securing API Keys
Section titled “Securing API Keys”Never hardcode your API keys (e.g., sk-proj-...) directly into your Python scripts. This is a major security risk, especially if you push code to GitHub.
Standard practice is to use a .env file. CrewAI is designed to look for these environment variables automatically.
- Create a file named
.envin your root folder. - Add your keys:
OPENAI_API_KEY=sk-... - Add
.envto your.gitignorefile so it is never uploaded. - CrewAI will automatically load these if you rely on standard model configuration.
Dockerizing CrewAI
Section titled “Dockerizing CrewAI”To ensure your crew runs the same way on a server as it does on your laptop, you should use Docker. Docker creates a self-contained environment (container) for your code.
Below is a standard Dockerfile for a Python-based CrewAI project:
# Use an official Python runtime as a parent imageFROM python:3.10-slim
# Set the working directory in the containerWORKDIR /app
# Copy the current directory contents into the container at /appCOPY . /app
# Install any needed packages specified in requirements.txt# If using poetry, you would install poetry and run 'poetry install' insteadRUN pip install --no-cache-dir crewai crewai-toolsRUN pip install -r requirements.txt
# Run the crew script when the container launchesCMD ["python", "src/main.py"]Optimization Strategies
Section titled “Optimization Strategies”Running LLM agents can be expensive. Here are two key strategies to optimize costs:
- Caching: CrewAI enables caching by default. If an agent receives the exact same input with the exact same tool output, it can pull the result from the cache rather than calling the expensive LLM again. Ensure
cache=Trueis set in your Crew config. - Model Selection: Not every agent needs GPT-4. Assign smaller, faster, and cheaper models (like GPT-4o-mini or Haiku) to agents performing simple tasks (like summarization), and reserve the “smartest” models for complex reasoning tasks.
With these deployment strategies, your CrewAI application is robust, secure, and ready for the real world. Happy coding!