feat: add LiteLLM as AI gateway provider
Summary
- Adds
LiteLLMas a new LLM provider extendingBaseLLM - Gives users access to 100+ LLM providers (OpenAI, Anthropic, Azure, Bedrock, Vertex, Groq, Cohere, Ollama, etc.) through a single unified interface
- Users switch providers by changing the
LITELLM_CHAT_MODELenv var, no code changes needed - Follows the exact same
BaseLLM+ config pattern used by the existing OpenAI, Anthropic, and GoogleAI providers
-
What kind of change does this PR introduce? Feature, adds LiteLLM as a new LLM provider
-
Why was this change needed? Director currently supports 4 LLM providers (OpenAI, Anthropic, GoogleAI, VideoDB Proxy), each with separate integration code. LiteLLM provides a unified interface to 100+
providers through a singlecompletion()call. Users switch providers by changing an env var. -
Other information: See sections below.
What Changed
| File | Change |
|---|---|
backend/director/llm/litellm.py |
New LiteLLM provider extending BaseLLM with LiteLLMConfig |
backend/director/llm/__init__.py |
Registered LiteLLM in get_default_llm() |
backend/director/constants.py |
Added LLMType.LITELLM and EnvPrefix.LITELLM_ |
backend/requirements.txt |
Added litellm>=1.60.0,<2.0.0 |
backend/tests/test_litellm.py |
37 unit tests |
Usage
Quick Start
# Set LiteLLM as the default provider
export DEFAULT_LLM=litellm
# Set the model (any LiteLLM-supported model string)
export LITELLM_CHAT_MODEL=anthropic/claude-3-haiku
# Set the provider's API key (LiteLLM reads standard env vars automatically)
export ANTHROPIC_API_KEY=sk-ant-...
### Switching Providers: Just Change the Model
# OpenAI
export LITELLM_CHAT_MODEL=openai/gpt-4o
export OPENAI_API_KEY=sk-...
# Anthropic
export LITELLM_CHAT_MODEL=anthropic/claude-3-haiku
export ANTHROPIC_API_KEY=sk-ant-...
# AWS Bedrock
export LITELLM_CHAT_MODEL=bedrock/anthropic.claude-v2
export AWS_ACCESS_KEY_ID=...
export AWS_SECRET_ACCESS_KEY=...
# Google Vertex AI
export LITELLM_CHAT_MODEL=vertex_ai/gemini-pro
export VERTEXAI_PROJECT=your-project
# Groq
export LITELLM_CHAT_MODEL=groq/llama3-70b-8192
export GROQ_API_KEY=gsk_...
# Local Ollama
export LITELLM_CHAT_MODEL=ollama/llama3
Any of the https://docs.litellm.ai/docs/providers works.
Programmatic Usage
from director.llm.litellm import LiteLLMConfig, LiteLLM
config = LiteLLMConfig(
chat_model="anthropic/claude-3-haiku",
# api_key is optional, litellm reads ANTHROPIC_API_KEY from env
)
llm = LiteLLM(config=config)
response = llm.chat_completions(
messages=[{"role": "user", "content": "What is Director?"}],
)
print(response.content)
Testing
Unit Tests (37/37 Passing)
tests/test_litellm.py::TestLiteLLMChatCompletions::test_basic_completion_returns_content PASSED
tests/test_litellm.py::TestLiteLLMChatCompletions::test_drop_params_always_true PASSED
tests/test_litellm.py::TestLiteLLMChatCompletions::test_model_forwarded PASSED
tests/test_litellm.py::TestLiteLLMChatCompletions::test_api_key_forwarded_when_set PASSED
tests/test_litellm.py::TestLiteLLMChatCompletions::test_api_key_omitted_when_empty PASSED
tests/test_litellm.py::TestLiteLLMChatCompletions::test_api_base_forwarded_when_set PASSED
tests/test_litellm.py::TestLiteLLMChatCompletions::test_api_base_omitted_when_empty PASSED
tests/test_litellm.py::TestLiteLLMChatCompletions::test_temperature_forwarded PASSED
tests/test_litellm.py::TestLiteLLMChatCompletions::test_max_tokens_forwarded PASSED
tests/test_litellm.py::TestLiteLLMChatCompletions::test_timeout_forwarded PASSED
tests/test_litellm.py::TestLiteLLMChatCompletions::test_stop_forwarded PASSED
tests/test_litellm.py::TestLiteLLMChatCompletions::test_response_format_forwarded PASSED
tests/test_litellm.py::TestLiteLLMChatCompletions::test_top_p_forwarded PASSED
tests/test_litellm.py::TestLiteLLMToolCalling::test_tool_calls_parsed_correctly PASSED
tests/test_litellm.py::TestLiteLLMToolCalling::test_multiple_tool_calls PASSED
tests/test_litellm.py::TestLiteLLMToolCalling::test_no_tool_calls_returns_empty_list PASSED
tests/test_litellm.py::TestLiteLLMToolCalling::test_tools_formatted_with_tool_choice_auto PASSED
tests/test_litellm.py::TestLiteLLMToolCalling::test_no_tools_omits_tool_choice PASSED
tests/test_litellm.py::TestLiteLLMToolCalling::test_empty_tool_arguments_handled PASSED
tests/test_litellm.py::TestLiteLLMToolCalling::test_none_tool_arguments_handled PASSED
tests/test_litellm.py::TestLiteLLMResponseFields::test_none_usage_returns_zero_tokens PASSED
tests/test_litellm.py::TestLiteLLMResponseFields::test_token_counts PASSED
tests/test_litellm.py::TestLiteLLMResponseFields::test_finish_reason PASSED
tests/test_litellm.py::TestLiteLLMResponseFields::test_none_content_becomes_empty_string PASSED
tests/test_litellm.py::TestLiteLLMErrorHandling::test_exception_returns_error_response PASSED
tests/test_litellm.py::TestLiteLLMErrorHandling::test_error_response_has_zero_tokens PASSED
tests/test_litellm.py::TestLiteLLMRegistration::test_llm_type_exists PASSED
tests/test_litellm.py::TestLiteLLMRegistration::test_env_prefix_exists PASSED
tests/test_litellm.py::TestLiteLLMRegistration::test_get_default_llm_returns_litellm PASSED
tests/test_litellm.py::TestLiteLLMRegistration::test_litellm_not_default_when_openai_key_set PASSED
tests/test_litellm.py::TestLiteLLMMessageFormatting::test_regular_messages_pass_through PASSED
tests/test_litellm.py::TestLiteLLMMessageFormatting::test_tool_call_messages_reformatted PASSED
tests/test_litellm.py::TestLiteLLMMessageFormatting::test_tool_result_message_passes_through PASSED
tests/test_litellm.py::TestLiteLLMToolFormatting::test_tools_formatted_to_openai_spec PASSED
tests/test_litellm.py::TestLiteLLMConfig::test_default_config_values PASSED
tests/test_litellm.py::TestLiteLLMConfig::test_config_reads_from_env PASSED
tests/test_litellm.py::TestLiteLLMConfig::test_config_no_api_key_required PASSED
======================= 37 passed in 0.51s ========================
Live E2E (Azure AI Foundry, Claude Sonnet via LiteLLM)
Model: azure_ai/claude-sonnet-4-6
chat_completions() -> Content: "4", Status: SUCCESS, Tokens: send=20, recv=5, total=25
get_default_llm() -> Type: LiteLLM, Response: "OK"
Integration Bug Found and Fixed
Claude (via Azure AI Foundry) rejects requests that include both temperature and top_p simultaneously. Rather than omitting top_p, the LiteLLM provider forwards it and relies on drop_params=True to handle
provider conflicts automatically. Also added defensive handling for None usage responses and empty/None tool-call arguments, which can occur with certain LiteLLM backends.
Risk / Compatibility
- Additive only, no existing provider code changed
- drop_params=True set by default so provider-unsupported kwargs are silently dropped
- LiteLLM lazily imported inside chat_completions(), users who don't use this provider are unaffected
- LiteLLM is selected via DEFAULT_LLM=litellm env var, does not affect existing provider selection
Summary by CodeRabbit
-
New Features
- Added LiteLLM as a selectable LLM backend with configurable model, token limits, API credentials/endpoints, and tool + response-format support.
-
Tests
- Added comprehensive tests for LiteLLM covering parameter forwarding, tool calling, response parsing, token accounting, error handling, and environment-driven configuration.
Adds a new LiteLLM provider: registers enum entries, implements a LiteLLM adapter and config loader, updates default LLM selection to prefer LiteLLM when configured, adds the LiteLLM provider 🎯 4 (Complex) | ⏱️ ~45 minutes 🐇 I hopped through env and code tonight, ✏️ Tip: You can configure your own custom pre-merge checks in the settings.📝 Walkthrough
Walkthrough
litellm dependency, and introduces comprehensive tests for the integration.Changes
Layer / File(s)
Summary
Constants / Data Shape
backend/director/constants.pyAdds
LLMType.LITELLM = "litellm" and EnvPrefix.LITELLM_ = "LITELLM_".
Core Implementation
backend/director/llm/litellm.pyNew
LiteLLMConfig(BaseLLMConfig) loads LITELLM_ env vars (api_key, api_base, chat_model, max_tokens). New LiteLLM(BaseLLM) implements _format_messages, _format_tools, and chat_completions which assemble parameters for litellm.completion, handle api_key/api_base, parse choices, tool_calls, usage into LLMResponse, and return error responses on exceptions.
Wiring / Selection
backend/director/llm/__init__.pyget_default_llm() imports and returns LiteLLM() when DEFAULT_LLM equals LLMType.LITELLM; OpenAI branch changed to elif.
Dependency
backend/requirements.txtAdds
litellm>=1.60.0,<2.0.0.
Tests / Coverage
backend/tests/test_litellm.pyNew comprehensive tests: config defaults and env overrides, instantiation without API key,
_format_messages/_format_tools, parameter forwarding (model, temperature, max_tokens, timeout, stop, response_format, top_p), tool-calling behavior and parsing, usage/token mapping (including usage=None), and error handling when underlying call raises.Sequence Diagram
sequenceDiagram
participant App as Application
participant Init as get_default_llm()
participant LLM as LiteLLM
participant Lib as litellm
App->>Init: request default LLM
Init->>Init: read DEFAULT_LLM env
alt DEFAULT_LLM == "litellm"
Init-->>App: return LiteLLM instance
end
App->>LLM: chat_completions(messages, tools, params)
LLM->>LLM: load LITELLM_* env config
LLM->>LLM: format messages and tools (OpenAI-style)
LLM->>Lib: litellm.completion(model, messages, tools, params...)
Lib-->>LLM: completion response (choices, usage, tool_calls)
LLM->>LLM: parse choices, tool_calls, token usage
LLM-->>App: LLMResponse (content, tool_calls, usage, status)
Estimated code review effort
Poem
Added LiteLLM to shine so bright,
Messages shaped and tools set right,
Completions fetched in cozy light,
A tiny rabbit cheers: all tests in sight ✨🚥 Pre-merge checks | ✅ 4 | ❌ 1
❌ Failed checks (1 warning)
Check name
Status
Explanation
Resolution
Docstring Coverage
⚠️ Warning
Docstring coverage is 15.94% which is insufficient. The required threshold is 80.00%.
Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name
Status
Explanation
Description Check
✅ Passed
Check skipped - CodeRabbit’s high-level summary is enabled.
Title check
✅ Passed
The title 'feat: add LiteLLM as AI gateway provider' is clear, concise, and directly summarizes the main objective of the changeset: integrating LiteLLM as a new supported LLM provider.
Linked Issues check
✅ Passed
Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check
✅ Passed
Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Comment @coderabbitai help to get the list of available commands and usage tips.
cc @ashish-spext and @0xrohitgarg
@0xrohitgarg do you have any update on this PR?