Custom LLM Implementation
Learn how to create custom LLM implementations in CrewAI.
Custom LLM Implementations
CrewAI now supports custom LLM implementations through the BaseLLM
abstract base class. This allows you to create your own LLM implementations that don’t rely on litellm’s authentication mechanism.
To create a custom LLM implementation, you need to:
- Inherit from the
BaseLLM
abstract base class - Implement the required methods:
call()
: The main method to call the LLM with messagessupports_function_calling()
: Whether the LLM supports function callingsupports_stop_words()
: Whether the LLM supports stop wordsget_context_window_size()
: The context window size of the LLM
Example: Basic Custom LLM
Error Handling Best Practices
When implementing custom LLMs, it’s important to handle errors properly to ensure robustness and reliability. Here are some best practices:
1. Implement Try-Except Blocks for API Calls
Always wrap API calls in try-except blocks to handle different types of errors:
2. Implement Retry Logic for Transient Failures
For transient failures like network issues or rate limiting, implement retry logic with exponential backoff:
3. Validate Input Parameters
Always validate input parameters to prevent runtime errors:
4. Handle Authentication Errors Gracefully
Provide clear error messages for authentication failures:
Example: JWT-based Authentication
For services that use JWT-based authentication instead of API keys, you can implement a custom LLM like this:
Troubleshooting
Here are some common issues you might encounter when implementing custom LLMs and how to resolve them:
1. Authentication Failures
Symptoms: 401 Unauthorized or 403 Forbidden errors
Solutions:
- Verify that your API key or JWT token is valid and not expired
- Check that you’re using the correct authentication header format
- Ensure that your token has the necessary permissions
2. Timeout Issues
Symptoms: Requests taking too long or timing out
Solutions:
- Implement timeout handling as shown in the examples
- Use retry logic with exponential backoff
- Consider using a more reliable network connection
3. Response Parsing Errors
Symptoms: KeyError, IndexError, or ValueError when processing responses
Solutions:
- Validate the response format before accessing nested fields
- Implement proper error handling for malformed responses
- Check the API documentation for the expected response format
4. Rate Limiting
Symptoms: 429 Too Many Requests errors
Solutions:
- Implement rate limiting in your custom LLM
- Add exponential backoff for retries
- Consider using a token bucket algorithm for more precise rate control
Advanced Features
Logging
Adding logging to your custom LLM can help with debugging and monitoring:
Rate Limiting
Implementing rate limiting can help avoid overwhelming the LLM API:
Metrics Collection
Collecting metrics can help you monitor your LLM usage:
Advanced Usage: Function Calling
If your LLM supports function calling, you can implement the function calling logic in your custom LLM:
Using Your Custom LLM with CrewAI
Once you’ve implemented your custom LLM, you can use it with CrewAI agents and crews:
Implementing Your Own Authentication Mechanism
The BaseLLM
class allows you to implement any authentication mechanism you need, not just JWT or API keys. You can use:
- OAuth tokens
- Client certificates
- Custom headers
- Session-based authentication
- Any other authentication method required by your LLM provider
Simply implement the appropriate authentication logic in your custom LLM class.
Was this page helpful?