Skip to content

CrewAI

Give a CrewAI agent its own calendar. CrewAI custom tools are plain Python functions, so each tool is a few lines that call the chronary Python SDK.

Terminal window
pip install chronary crewai

CrewAI supports Python 3.10 to 3.13.

Create an API key in the console and set it as CHRONARY_API_KEY. An Organization key (chr_sk_) or an Agent key (chr_ak_) both work. See Authentication. CrewAI’s default model provider also needs OPENAI_API_KEY.

Decorate a function with @tool. CrewAI builds the input schema from the type hints and shows the docstring to the model as the description. Return a string. Returning an error as text, instead of raising, lets the agent read it and try again:

import json
import os
from chronary import APIError, Chronary
from crewai.tools import tool
chronary = Chronary(api_key=os.environ["CHRONARY_API_KEY"])
@tool("create_event")
def create_event(calendar_id: str, title: str, start_time: str, end_time: str) -> str:
"""Book an event on a calendar. Times are ISO 8601 with offset. calendar_id starts with cal_."""
try:
event = chronary.events.create(
calendar_id, title=title, start_time=start_time, end_time=end_time
)
return event.model_dump_json()
except APIError as err:
return json.dumps({"error": type(err).__name__, "message": err.message})

The agent books on the first agent and its first calendar, so the account needs an agent with at least one calendar. A new account has none: create them in the console first.

import json
import os
from datetime import datetime, timezone
from chronary import APIError, Chronary
from crewai import Agent, Crew, Process, Task
from crewai.tools import tool
chronary = Chronary(api_key=os.environ["CHRONARY_API_KEY"])
def run(call) -> str:
try:
return json.dumps(call(), default=str)
except APIError as err:
return json.dumps({"error": type(err).__name__, "message": err.message})
@tool("list_agents")
def list_agents() -> str:
"""List the agents in this Chronary organization (id, name)."""
return run(lambda: [{"id": a.id, "name": a.name} for a in chronary.agents.list().data])
@tool("list_calendars")
def list_calendars(agent_id: str) -> str:
"""List the calendars of one agent (id, name, timezone). agent_id starts with agt_."""
return run(
lambda: [
{"id": c.id, "name": c.name, "timezone": c.timezone}
for c in chronary.agents.calendars.list(agent_id).data
]
)
@tool("get_availability")
def get_availability(agent_id: str, start: str, end: str, duration: str = "30m") -> str:
"""Find free slots for an agent between start and end (ISO 8601). duration: 15m, 30m, 45m, 1h or 2h."""
return run(
lambda: chronary.availability.get(
agent_id, start=start, end=end, duration=duration
).model_dump(mode="json")
)
@tool("create_event")
def create_event(calendar_id: str, title: str, start_time: str, end_time: str) -> str:
"""Book an event on a calendar. Times are ISO 8601 with offset. calendar_id starts with cal_."""
return run(
lambda: chronary.events.create(
calendar_id, title=title, start_time=start_time, end_time=end_time
).model_dump(mode="json")
)
@tool("list_events")
def list_events(calendar_id: str, start_after: str, start_before: str) -> str:
"""List the events on a calendar that start between start_after and start_before (ISO 8601)."""
return run(
lambda: [
{"id": e.id, "title": e.title, "start_time": e.start_time, "end_time": e.end_time}
for e in chronary.events.list(
calendar_id, start_after=start_after, start_before=start_before
).data
]
)
# The model has no clock: tell it today's date so "tomorrow" is right.
today = datetime.now(timezone.utc).date().isoformat()
scheduler = Agent(
role="Calendar scheduling assistant",
goal="Read and write the user's Chronary calendar exactly as asked.",
backstory=f"You manage calendars through the Chronary tools. Today is {today} (UTC). Use the first agent and its first calendar.",
tools=[list_agents, list_calendars, get_availability, create_event, list_events],
llm="openai/gpt-4o",
)
task = Task(
description="{request}",
expected_output="A short summary of what you booked and tomorrow's events.",
agent=scheduler,
)
crew = Crew(agents=[scheduler], tasks=[task], process=Process.sequential)
result = crew.kickoff(
inputs={
"request": 'Find a free 30-minute slot tomorrow afternoon (UTC), book "Project sync" in it, then list tomorrow\'s events.'
}
)
print(result.raw)

crewai-scheduler is this crew as a complete project: it checks availability, books a meeting and lists tomorrow’s events.