Your own tool
Production patternsChapter 13 of 147 min read
Offload calendar arithmetic and contract deadline calculations from probabilistic language models to deterministic, unit-tested Python functions wrapped as custom tools.
Write the tests first#
Define unit tests in tests/test_tools.py covering month additions, notice offsets, leap years, and rollover conditions:
Key test cases:
- Month-end snapping: January 31 plus one month resolves to February 28 (or 29 in leap years).
- Notice period offset: A 12-month term starting March 1, 2026 ends February 28, 2027; a 60-day notice cutoff falls on December 30, 2026.
- Term rollover: A missed deadline rolls over to the subsequent 12-month term.
Execute pytest to confirm test failure before implementing the logic:
Write the code#
Implement the calendar arithmetic functions in contract_crew/tools.py:
Passing today explicitly as a parameter enables deterministic unit testing across past, present, and leap-year dates.
Run pytest to verify the implementation:
Turn it into a tool#
Wrap the logic in a CrewAI BaseTool subclass with an explicit Pydantic argument schema in contract_crew/tools.py:
Test the tool directly in an interactive Python session:
Give the tool to the agents#
Provision NoticeDeadlineTool to both risk_analyst and fact_checker in contract_crew/crew.py:
Update contract_crew/config/tasks.yaml to instruct the agents to invoke the tool for all renewal calculations:
Run the crew:
The agent execution log records tool calls with exact arguments:
Review the verified memo and automated corrections:
The fact-checker verifies dates against the calculator, correcting erroneous preliminary estimates in the finalized risk memo.
Review the final project architecture: