Use Flux with LangChain

Point LangChain's OpenAI chat model at FluxRouter with a custom base URL and your Flux key.

LangChain talks to FluxRouter through its OpenAI chat model. Because Flux is OpenAI-compatible, you use ChatOpenAI and set the base URL to https://api.fluxrouter.ai/v1, your Flux key, and the model to flux-auto. No custom integration is needed.

Set up ChatOpenAI to use Flux

Install the LangChain OpenAI package (pip install langchain-openai), then point the chat model at Flux:

python
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(
    model="flux-auto",                            # let Flux route, or pin a flux-* id
    api_key="sk-...",                             # your Flux key
    base_url="https://api.fluxrouter.ai/v1",      # the one line that points at Flux
)

response = llm.invoke("Say hello from FluxRouter.")
print(response.content)

base_url is the documented ChatOpenAI parameter for a custom OpenAI-compatible endpoint. If you are on an older version of langchain-openai that uses the legacy name, pass openai_api_base="https://api.fluxrouter.ai/v1" instead.

Read the key from the environment

To avoid hard-coding the key, read it from an environment variable:

python
import os
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(
    model="flux-auto",
    api_key=os.environ["FLUX_API_KEY"],
    base_url="https://api.fluxrouter.ai/v1",
)

Use it in a chain

Once the model is pointed at Flux, the rest of LangChain works unchanged. Chains, prompts, and output parsers don't need to know they're talking to Flux:

python
from langchain_core.prompts import ChatPromptTemplate

prompt = ChatPromptTemplate.from_messages([
    ("system", "You are a concise assistant."),
    ("user", "{question}"),
])

chain = prompt | llm
print(chain.invoke({"question": "What is an LLM gateway?"}).content)

Pick a model

flux-auto routes each request for you. To pin a model, swap the model value for a tier alias (flux-fast, flux-standard, flux-reasoning) or a flux-pinned-* id. See Models.