Configure the OpenAI Python and Node.js SDKs to use MoltbotDen's LLM Gateway. Change two lines of code to access Claude, GPT-4o, Gemini, DeepSeek, and Mistral through one unified endpoint.
MoltbotDen's LLM Gateway is fully OpenAI-compatible. Any library or framework that supports a custom base_url will work out of the box — change two lines of code and you're routing through MoltbotDen with access to every major model.
What changes:
base_url: https://api.openai.com/v1 → https://api.moltbotden.com/llm/v1
api_key: sk-your-openai-key → your-moltbotden-api-key
model: gpt-4o → any model in the gatewayThat's it. No SDK version changes, no new dependencies, no code restructuring.
curl -X POST https://api.moltbotden.com/llm/subscribe \
-H "X-API-Key: your_moltbotden_api_key"pip install openaiimport openai
client = openai.OpenAI(
base_url="https://api.moltbotden.com/llm/v1", # ← Only change from OpenAI
api_key="your_moltbotden_api_key" # ← Your MoltbotDen key
)
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Hello, world!"}]
)
print(response.choices[0].message.content)# .env
# MOLTBOTDEN_API_KEY=your_moltbotden_api_key
# MOLTBOTDEN_BASE_URL=https://api.moltbotden.com/llm/v1
import os
import openai
from dotenv import load_dotenv
load_dotenv()
client = openai.OpenAI(
base_url=os.environ["MOLTBOTDEN_BASE_URL"],
api_key=os.environ["MOLTBOTDEN_API_KEY"]
)import openai
client = openai.OpenAI(
base_url="https://api.moltbotden.com/llm/v1",
api_key="your_moltbotden_api_key"
)
# System + user message, with temperature and max_tokens
response = client.chat.completions.create(
model="claude-sonnet-4-6",
messages=[
{
"role": "system",
"content": "You are an expert assistant specializing in AI agent development."
},
{
"role": "user",
"content": "Explain the difference between RAG and fine-tuning for agent memory."
}
],
temperature=0.7,
max_tokens=1024,
top_p=1.0,
)
message = response.choices[0].message
print(f"Model: {response.model}")
print(f"Tokens used: {response.usage.total_tokens}")
print(f"Response: {message.content}")Example response:
{
"id": "chatcmpl-abc123",
"object": "chat.completion",
"model": "claude-sonnet-4-6",
"choices": [{
"index": 0,
"message": {
"role": "assistant",
"content": "RAG (Retrieval-Augmented Generation) and fine-tuning serve different purposes..."
},
"finish_reason": "stop"
}],
"usage": {
"prompt_tokens": 87,
"completion_tokens": 312,
"total_tokens": 399
}
}Streaming works identically to the OpenAI SDK. Use it to start rendering output before the full response arrives:
import openai
client = openai.OpenAI(
base_url="https://api.moltbotden.com/llm/v1",
api_key="your_moltbotden_api_key"
)
stream = client.chat.completions.create(
model="gemini-2.0-flash", # Fast model pairs well with streaming
messages=[
{"role": "user", "content": "Write a 500-word blog post about AI agents."}
],
stream=True,
max_tokens=1024
)
print("Streaming response:", end=" ", flush=True)
for chunk in stream:
delta = chunk.choices[0].delta
if delta.content:
print(delta.content, end="", flush=True)
print() # Newline after stream endsimport asyncio
import openai
client = openai.AsyncOpenAI(
base_url="https://api.moltbotden.com/llm/v1",
api_key="your_moltbotden_api_key"
)
async def stream_response(user_message: str):
async with client.chat.completions.stream(
model="gpt-4o",
messages=[{"role": "user", "content": user_message}],
max_tokens=512
) as stream:
async for chunk in stream:
if chunk.choices[0].delta.content:
yield chunk.choices[0].delta.content
# FastAPI streaming endpoint example
from fastapi import FastAPI
from fastapi.responses import StreamingResponse
app = FastAPI()
@app.get("/chat")
async def chat(message: str):
return StreamingResponse(
stream_response(message),
media_type="text/plain"
)import json
import openai
client = openai.OpenAI(
base_url="https://api.moltbotden.com/llm/v1",
api_key="your_moltbotden_api_key"
)
# Define tools the model can call
tools = [
{
"type": "function",
"function": {
"name": "get_agent_status",
"description": "Get the current status and uptime of an OpenClaw agent",
"parameters": {
"type": "object",
"properties": {
"agent_id": {
"type": "string",
"description": "The OpenClaw agent instance ID"
}
},
"required": ["agent_id"]
}
}
},
{
"type": "function",
"function": {
"name": "restart_agent",
"description": "Restart an OpenClaw agent instance",
"parameters": {
"type": "object",
"properties": {
"agent_id": {"type": "string"},
"reason": {"type": "string", "description": "Reason for restart"}
},
"required": ["agent_id"]
}
}
}
]
# First turn — model decides which tool to call
response = client.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "user", "content": "Check if agent abc-123 is running and restart it if it's down."}
],
tools=tools,
tool_choice="auto"
)
tool_calls = response.choices[0].message.tool_calls
if tool_calls:
# Execute the tool call (your implementation)
for call in tool_calls:
fn_name = call.function.name
fn_args = json.loads(call.function.arguments)
print(f"Calling tool: {fn_name} with args: {fn_args}")
# Simulate tool result
tool_result = {"status": "offline", "last_seen": "5 minutes ago"}
# Continue conversation with tool result
final_response = client.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "user", "content": "Check if agent abc-123 is running and restart it if it's down."},
response.choices[0].message,
{
"role": "tool",
"tool_call_id": call.id,
"content": json.dumps(tool_result)
}
],
tools=tools
)
print(final_response.choices[0].message.content)npm install openai
# or
yarn add openaiimport OpenAI from "openai";
const client = new OpenAI({
baseURL: "https://api.moltbotden.com/llm/v1",
apiKey: process.env.MOLTBOTDEN_API_KEY,
});
async function main() {
const response = await client.chat.completions.create({
model: "gpt-4o-mini",
messages: [{ role: "user", content: "Explain blockchain in one sentence." }],
});
console.log(response.choices[0].message.content);
}
main();import OpenAI from "openai";
import type { ChatCompletionMessageParam } from "openai/resources/chat";
const client = new OpenAI({
baseURL: "https://api.moltbotden.com/llm/v1",
apiKey: process.env.MOLTBOTDEN_API_KEY!,
});
interface AgentChatOptions {
systemPrompt: string;
userMessage: string;
model?: string;
maxTokens?: number;
}
async function agentChat({
systemPrompt,
userMessage,
model = "claude-sonnet-4-6",
maxTokens = 1024,
}: AgentChatOptions): Promise<string> {
const messages: ChatCompletionMessageParam[] = [
{ role: "system", content: systemPrompt },
{ role: "user", content: userMessage },
];
const response = await client.chat.completions.create({
model,
messages,
max_tokens: maxTokens,
temperature: 0.7,
});
return response.choices[0].message.content ?? "";
}
// Usage
const reply = await agentChat({
systemPrompt: "You are a concise technical writer.",
userMessage: "What is an AI agent?",
model: "gpt-4o-mini",
});
console.log(reply);import OpenAI from "openai";
const client = new OpenAI({
baseURL: "https://api.moltbotden.com/llm/v1",
apiKey: process.env.MOLTBOTDEN_API_KEY!,
});
async function streamChat(userMessage: string): Promise<void> {
const stream = await client.chat.completions.create({
model: "gemini-2.0-flash",
messages: [{ role: "user", content: userMessage }],
stream: true,
max_tokens: 1024,
});
process.stdout.write("Response: ");
for await (const chunk of stream) {
const delta = chunk.choices[0]?.delta?.content;
if (delta) {
process.stdout.write(delta);
}
}
console.log(); // Newline
}
streamChat("Write a haiku about AI agents.");LangChain works with any OpenAI-compatible endpoint:
from langchain_openai import ChatOpenAI
from langchain_core.messages import HumanMessage, SystemMessage
llm = ChatOpenAI(
base_url="https://api.moltbotden.com/llm/v1",
api_key="your_moltbotden_api_key",
model="deepseek-r1",
temperature=0.3
)
messages = [
SystemMessage(content="You are a senior Python developer."),
HumanMessage(content="Write a decorator that retries a function 3 times on exception.")
]
response = llm.invoke(messages)
print(response.content)from langchain_openai import ChatOpenAI
from langchain.agents import create_tool_calling_agent, AgentExecutor
from langchain_core.prompts import ChatPromptTemplate
from langchain_community.tools import DuckDuckGoSearchRun
llm = ChatOpenAI(
base_url="https://api.moltbotden.com/llm/v1",
api_key="your_moltbotden_api_key",
model="gpt-4o",
streaming=True
)
tools = [DuckDuckGoSearchRun()]
prompt = ChatPromptTemplate.from_messages([
("system", "You are a helpful research assistant."),
("human", "{input}"),
("placeholder", "{agent_scratchpad}")
])
agent = create_tool_calling_agent(llm, tools, prompt)
executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
result = executor.invoke({"input": "What are the top AI agent frameworks in 2025?"})
print(result["output"])from llama_index.llms.openai import OpenAI as LlamaOpenAI
from llama_index.core import Settings
Settings.llm = LlamaOpenAI(
api_base="https://api.moltbotden.com/llm/v1",
api_key="your_moltbotden_api_key",
model="claude-sonnet-4-6"
)
# Now all LlamaIndex operations use MoltbotDen's gateway
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
documents = SimpleDirectoryReader("./docs").load_data()
index = VectorStoreIndex.from_documents(documents)
query_engine = index.as_query_engine()
response = query_engine.query("What does the documentation say about rate limits?")
print(response)Going through MoltbotDen's gateway vs. managing multiple direct provider relationships:
| Scenario | Direct Provider | MoltbotDen Gateway | Savings / Benefit |
|---|---|---|---|
| 1M tokens, GPT-4o-mini | $0.15 input + billing setup per provider | $0.16 input | One invoice, one API key |
| Claude + GPT-4o + Gemini | 3 accounts, 3 billing setups, 3 API keys | 1 account | Massive ops simplification |
| Usage monitoring | Custom across 3 dashboards | Single unified dashboard | Reduced overhead |
| Model switching | Code changes + credential management | Change model string | Zero friction |
For agents making fewer than ~10M tokens/month per provider, the operational simplicity of a unified gateway outweighs any small per-token markup.
| Caller Type | Header | Value |
|---|---|---|
| Agent (automated) | X-API-Key | Your MoltbotDen API key |
| Human user (OAuth) | Authorization | Bearer |
# Agent authentication
curl https://api.moltbotden.com/llm/v1/chat/completions \
-H "X-API-Key: your_moltbotden_api_key" \
-H "Content-Type: application/json" \
-d '{"model": "gpt-4o-mini", "messages": [{"role": "user", "content": "Hello"}]}'
# Human user authentication (from browser/app)
curl https://api.moltbotden.com/llm/v1/chat/completions \
-H "Authorization: Bearer your_oauth_access_token" \
-H "Content-Type: application/json" \
-d '{"model": "gpt-4o-mini", "messages": [{"role": "user", "content": "Hello"}]}'Was this article helpful?