@calcis/langchain
v1.0.4
Published
Calcis cost estimation callback for LangChain
Downloads
510
Maintainers
Readme
@calcis/langchain
Calcis cost-estimation callback for LangChain.
Live pricing for 25+ models, side-by-side comparisons, and a web estimator: https://calcis.dev
- Full price index: https://calcis.dev/models
- Compare models: https://calcis.dev/compare
- API reference: https://calcis.dev/api-docs
Attach the handler to any LangChain runnable and get a one-line cost estimate for every LLM call, plus a rolling session total.
Install
npm install @calcis/langchain @langchain/coreYou also need a Calcis API key (Pro tier or above). Get one at calcis.dev/dashboard.
Usage
import { ChatAnthropic } from "@langchain/anthropic";
import { CalcisCallbackHandler } from "@calcis/langchain";
const calcis = new CalcisCallbackHandler({
apiKey: process.env.CALCIS_API_KEY!,
});
const model = new ChatAnthropic({
model: "claude-sonnet-4-6",
callbacks: [calcis],
});
const response = await model.invoke("Explain quantum computing");
console.log(`Session total: $${calcis.getSessionTotal().toFixed(4)}`);Every LLM call now emits:
[calcis] claude-sonnet-4-6 · 243 in · 650 out · $0.0105 · session: $0.0105 (1 calls)Config
new CalcisCallbackHandler({
apiKey: string, // required: calc_… key
verbose?: boolean, // default true: logs per-call summary
onEstimate?: (e) => void, // optional structured sink
});The estimate object passed to onEstimate:
{
model: string,
inputTokens: number,
outputTokens: number,
inputCost: number,
outputCost: number,
totalCost: number,
confidence: "high" | "medium" | "low"
}Session helpers
calcis.getSessionTotal(); // cumulative cost across calls
calcis.getCallCount(); // number of LLM calls so far
calcis.resetSession(); // zero the counters (e.g. per request)Failure mode
This handler never throws. If the Calcis API is unreachable, returns an error, or the response is malformed, the handler silently skips the estimate for that call: the underlying LangChain chain keeps running. Cost estimation is a nice-to-have; your product is not.
Model name mapping
LangChain's provider integrations don't always name models the way Calcis does. The handler ships a small translation map:
| LangChain | Calcis | | ----------------- | ----------------- | | claude-3-5-sonnet | claude-sonnet-4-6 | | claude-3-opus | claude-opus-4-6 | | gpt-5.4 | gpt-5-4 |
Unmapped names are passed through unchanged. Use calcis models (or
visit calcis.dev/models) for the
full list of recognised IDs.
Links
License
MIT
