LangChain and LiteLLM Integration Guide

Target audience: Developers Last updated: 2026-08-06

LangChain is suitable for calling models directly in your application code. LiteLLM is suitable for selecting an OpenAI Compatible Provider at a unified interface layer. Both examples use the platform Base URL.

Use the endpoint address shown on the "API Key" page in the console.

LangChain

Installation:

python -m pip install langchain-openai

Minimal call:

import os
from langchain_openai import ChatOpenAI
 
llm = ChatOpenAI(
    model="deepseek-v4-flash",
    api_key=os.environ["CUSTOM_API_KEY"],
    base_url="https://<your-api-endpoint>",
    timeout=60,
    max_retries=2,
)
 
result = llm.invoke("Please reply only: connection successful")
print(result.content)

Set CUSTOM_API_KEY before running. You can replace the model name with any other call name available in the console.

LiteLLM

Installation:

python -m pip install litellm
import os
import litellm
 
response = litellm.completion(
    model="openai/deepseek-v4-flash",
    api_key=os.environ["CUSTOM_API_KEY"],
    base_url="https://<your-api-endpoint>",
    messages=[{"role": "user", "content": "Please reply only: connection successful"}],
    timeout=60,
)
 
print(response.choices[0].message.content)

The openai/ prefix tells LiteLLM to use the OpenAI Compatible adapter. It is not sent as part of the model name to the platform.

Before Adding to Production Code

First confirm the connection with a minimal example, then add streaming output, tool calls, and complex retry logic. For 429 responses, follow the wait information in the response first. For 502 responses, apply a limited number of backoff retries. Request logs must not record the full Key or prompts containing sensitive business content.