Skip to content

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
)

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 .env in your root folder.
  • Add your keys: OPENAI_API_KEY=sk-...
  • Add .env to your .gitignore file so it is never uploaded.
  • CrewAI will automatically load these if you rely on standard model configuration.

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 image
FROM python:3.10-slim
# Set the working directory in the container
WORKDIR /app
# Copy the current directory contents into the container at /app
COPY . /app
# Install any needed packages specified in requirements.txt
# If using poetry, you would install poetry and run 'poetry install' instead
RUN pip install --no-cache-dir crewai crewai-tools
RUN pip install -r requirements.txt
# Run the crew script when the container launches
CMD ["python", "src/main.py"]

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=True is 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!