Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.stellartools.dev/llms.txt

Use this file to discover all available pages before exploring further.

The @stellartools/langchain-adapter package wraps any LangChain language model with metered billing. It checks credit balance before each call and charges by token usage after.

Install

npm install @stellartools/langchain-adapter @langchain/core langchain

Usage

Pass your config and a base model to createMeteredModel. Then call it with a customerId as the first argument, followed by the usual LangChain arguments.
import { ChatOpenAI } from "@langchain/openai";
import { createMeteredModel } from "@stellartools/langchain-adapter";

const model = createMeteredModel(
  {
    api_key: process.env.STELLAR_TOOLS_API_KEY!,
    product_id: "prod_llm_metered",
  },
  new ChatOpenAI({ model: "gpt-4o" })
);

const result = await model.invoke("cust_xxx", [{ role: "user", content: "Hello" }]);

console.log(result);
Streaming:
for await (const chunk of model.stream("cust_xxx", [{ role: "user", content: "Hello" }])) {
  process.stdout.write(chunk.content as string);
}

Config options

api_key
string
required
Your StellarTools API key.
product_id
string
required
Credit product ID to charge for model usage.

Available methods

MethodDescription
invoke(customerId, input, options?)Single call; charges by token usage.
stream(customerId, input, options?)Streaming; charges after stream ends.
batch(customerId, inputs, options?)Batch calls; charges by total usage.
streamEvents(customerId, input, options?)Stream events; charges on model end.