Use TokenRex with LangChain
Use TokenRex as a custom OpenAI-compatible provider in LangChain.
Setup checked against the linked documentation. Screenshots are reference examples from the tool's publisher, not proof of a TokenRex connection. Live execution inside this third-party app has not been tested; verify a short request before running larger tasks.
Before you start
- A TokenRex API key
- A version of the app that accepts a custom OpenAI-compatible base URL
Setup methods
Python — ChatOpenAI
- 1
Install langchain-openai and save TOKENREX_API_KEY in your private environment.
- 2
Create ChatOpenAI with the versioned base URL and exact enabled model ID. This example uses Chat Completions.
- 3
Run a self-contained prompt first; add repository context only when you actually supply its files.
import os
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(
base_url="https://tokenrex.ai/api/public/v1",
api_key=os.environ["TOKENREX_API_KEY"],
model="gpt-6-astra",
use_responses_api=False,
)
print(llm.invoke("Reply with a short greeting.").content)JavaScript / TypeScript
- 1
Install @langchain/openai and set TOKENREX_API_KEY in your private environment.
- 2
Pass the TokenRex URL through configuration.baseURL, then invoke a short prompt.
- 3
ChatOpenAI targets OpenAI schemas and may not preserve provider-specific reasoning fields. Verify tool calls and streaming separately.
import { ChatOpenAI } from "@langchain/openai";
const llm = new ChatOpenAI({
model: "gpt-6-astra",
apiKey: process.env.TOKENREX_API_KEY,
configuration: { baseURL: "https://tokenrex.ai/api/public/v1" },
useResponsesApi: false,
});
console.log((await llm.invoke("Reply with a short greeting.")).content);Troubleshooting
- Authentication fails
- Check that the saved TokenRex key is active and belongs in the tool's API-key field, not its subscription sign-in. Remove accidental spaces, check your wallet balance, and never share the key in screenshots or exported settings.
- The model is missing or the request URL returns 404
- Copy an exact enabled model ID from Models & Pricing. Follow this method's URL field: some tools expect the versioned base, others the root or full completion address. Check the documented API protocol before changing the path.
- Chat works but streaming or tools fail
- Test a short plain-text conversation first. Confirm the selected model and installed tool version support the required protocol and capabilities. Enable agent actions only after separately checking streaming and tool calls.
Verify the connection
Send a short message with the selected model, read its response, then confirm the exact model appears in your TokenRex usage. Test streaming and tool use separately before larger agent tasks.