LangChain

Moth DocsTutorials

LangChain's OpenAI and Anthropic chat models both reach Moth once they're given its base URL.

Python

shell
pip install langchain-openai
moth_chat.py
import os
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(
    model="gemini-3.8-flash",
    base_url="https://api.runmoth.si/v1",
    api_key=os.environ["MOTH_API_KEY"],
    use_responses_api=False,
)
print(llm.invoke("Say hi in three words").content)

use_responses_api=False matters. LangChain picks between chat completions and OpenAI's Responses API partly by the model's name, whatever the base URL, and Moth serves only chat completions.

JavaScript

shell
npm install @langchain/openai
moth-chat.mts
import { ChatOpenAI } from "@langchain/openai";

const llm = new ChatOpenAI({
  model: "gemini-3.8-flash",
  apiKey: process.env.MOTH_API_KEY,
  configuration: { baseURL: "https://api.runmoth.si/v1" },
  useResponsesApi: false,
});
console.log((await llm.invoke("Say hi in three words")).content);

In JavaScript the base URL goes inside configuration, which LangChain passes to the OpenAI client.

The Anthropic format

ChatAnthropic from langchain-anthropic works too, with Moth's host as base_url and no /v1. Every chat model answers in this format, not only Claude.

python
from langchain_anthropic import ChatAnthropic

llm = ChatAnthropic(model="claude-sonnet-4.6", base_url="https://api.runmoth.si", api_key=os.environ["MOTH_API_KEY"])

Tools work through bind_tools on both, and streaming through stream. Embeddings aren't served, so pair Moth with an embedding model from elsewhere for retrieval.