Skip to main content
LiteLLM is a library that simplifies calling Anthropic, Azure, Huggingface, Replicate, etc. This notebook covers how to get started with using LangChain + the LiteLLM I/O library. This integration contains two main classes:
  • ChatLiteLLM: The main LangChain wrapper for basic usage of LiteLLM (docs).
  • ChatLiteLLMRouter: A ChatLiteLLM wrapper that leverages LiteLLM’s Router (docs).

Overview

Integration details

Model features

Setup

To access ChatLiteLLM and ChatLiteLLMRouter models, you’ll need to install the langchain-litellm package and create an OpenAI, Anthropic, Azure, Replicate, OpenRouter, Hugging Face, Together AI, or Cohere account. Then, you have to get an API key and export it as an environment variable.

Credentials

You have to choose the LLM provider you want and sign up with them to get their API key.

Example - Anthropic

Head to the Claude console to sign up and generate a Claude API key. Once you’ve done this set the ANTHROPIC_API_KEY environment variable:

Example - OpenAI

Head to platform.openai.com/api-keys to sign up for OpenAI and generate an API key. Once you’ve done this, set the OPENAI_API_KEY environment variable.

Installation

The LangChain LiteLLM integration is available in the langchain-litellm package:

Instantiation

ChatLiteLLM

You can instantiate a ChatLiteLLM model by providing a model name supported by LiteLLM.

ChatLiteLLMRouter

You can also leverage LiteLLM’s routing capabilities by defining your model list as specified in the LiteLLM routing documentation.

Invocation

Whether you’ve instantiated a ChatLiteLLM or a ChatLiteLLMRouter, you can now use the ChatModel through LangChain’s API.

Async and streaming functionality

ChatLiteLLM and ChatLiteLLMRouter also support async and streaming functionality:

API reference

For detailed documentation of all ChatLiteLLM and ChatLiteLLMRouter features and configurations, head to the API reference.