Skip to content

Feat/add litellm provider - #93

Open
RheagalFire wants to merge 2 commits into
cosmicstack-labs:mainfrom
RheagalFire:feat/add-litellm-provider
Open

Feat/add litellm provider#93
RheagalFire wants to merge 2 commits into
cosmicstack-labs:mainfrom
RheagalFire:feat/add-litellm-provider

Conversation

@RheagalFire

Copy link
Copy Markdown

Summary

Adds LiteLLM as a first-class provider, enabling access to 100+ LLM providers (OpenAI, Anthropic, Google Gemini, AWS Bedrock, Azure OpenAI, Mistral, Cohere, and more) through Mercury's existing provider system via a LiteLLM proxy.

Changes

  • src/utils/config.ts - Added litellm to ProviderName union, config defaults (LITELLM_API_KEY, LITELLM_BASE_URL, LITELLM_MODEL, LITELLM_ENABLED), keyless proxy support in isProviderConfigured()
  • src/providers/registry.ts - Registered litellm as OpenAICompatProvider
  • src/providers/openai-compat.ts - Fixed isAvailable() for keyless proxies (checks baseUrl instead of apiKey for litellm/ollama), added formatApiError() with specific messages for 401/404/429
  • src/utils/provider-models.ts - LiteLLM-specific error message for model fetch failures, added litellm to model ID filter
  • src/web/api/providers.ts - Fixed /api/providers/:name/test endpoint for keyless proxy testing

Tests

Tests (66/67 passing):
66 pass. 1 pre-existing failure in tokens.test.ts (locale-dependent number formatting, unrelated to this change).

Error handling:

  • 401: [litellm] Authentication failed - check your API key or proxy credentials.
  • 404: [litellm] Model not found - verify the model name and base URL.
  • 429: [litellm] Rate limited - wait a moment before retrying.
  • Keyless proxy: isAvailable() now checks baseUrl instead of apiKey
  • Model fetch failure: Mercury could not fetch models from the LiteLLM proxy. Please check the base URL and ensure the proxy is running.

Risk / Compatibility

  • Additive only. Reuses existing OpenAICompatProvider class.
  • Opt-in via LITELLM_ENABLED=true env var.
  • Keyless proxy fix also benefits Ollama local provider.

Example usage

1. Start a LiteLLM proxy (manages models + API keys server-side):

pip install litellm
litellm --model openai/gpt-4o --model anthropic/claude-sonnet-4-20250514
# Proxy running at http://localhost:4000

2. Configure Mercury via env vars:

export LITELLM_ENABLED=true
export LITELLM_BASE_URL=http://localhost:4000/v1
export LITELLM_API_KEY=sk-your-litellm-master-key  # optional for keyless proxies
export LITELLM_MODEL=openai/gpt-4o

3. Use in code - Mercury routes through the LiteLLM provider:

import { createProvider } from './providers/registry.js';
import { getConfig } from './utils/config.js';

const config = getConfig();
const provider = createProvider('litellm', config.providers.litellm);

// Non-streaming
const response = await provider.generateText(
  "What is the capital of France?",
  "You are a helpful assistant."
);
console.log(response.text);     // "The capital of France is Paris."
console.log(response.provider); // "litellm"
console.log(response.model);    // "openai/gpt-4o"
console.log(response.totalTokens); // 42

// Streaming
for await (const chunk of provider.streamText(
  "Tell me a joke",
  "You are a funny assistant."
)) {
  if (!chunk.done) {
    process.stdout.write(chunk.text);
  }
}

4. Under the hood - Mercury calls the LiteLLM proxy via Vercel AI SDK:

// OpenAICompatProvider creates an @ai-sdk/openai client pointing at the proxy
const client = createOpenAI({
  apiKey: 'sk-your-litellm-key',
  baseURL: 'http://localhost:4000/v1',
});

// generateText/streamText call the proxy's /v1/chat/completions endpoint
// The proxy handles routing to the right provider based on the model name
const result = await generateText({
  model: client('openai/gpt-4o'),
  system: "You are a helpful assistant.",
  prompt: "What is 2+2?",
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant