Service Layer Architecture
The service layer sits below the tool registry and handles direct API communication:Existing Service Pattern
All services follow a consistent pattern. Let’s examine the Gmail service:services/gmail.py
- Constructor accepts authenticated API client
- Methods return structured dicts with
success,message, and data - Comprehensive error handling with user-friendly messages
- Type hints for all parameters
Adding a New Service
Create Service Class
Create a new file in
services/ following the naming convention:services/outlook.py
Register Service in Main
Initialize your service in
main.py or wherever services are created:main.py
Service Implementation Examples
Here are complete examples from the codebase:Calendar Service
services/calendar.py
Tasks Service
services/tasks.py
Service Best Practices
Consistent Returns
Always return dicts with
success, message, and data keys. Never raise exceptions to the caller.Timeout Handling
Set reasonable timeouts on API calls (typically 10-30 seconds) to prevent hangs.
Rate Limiting
Implement retry logic with exponential backoff for rate-limited APIs.
Credential Management
Store credentials securely and refresh tokens before expiry. Never hardcode secrets.
Error Handling Pattern
All service methods follow this error handling structure:Testing Services
Test services independently from the agent:See Also
Custom Tools
Create tools that use your new service
Testing
Learn how to write comprehensive tests