mintlyr-sdk
v0.1.4
Published
The official JavaScript/TypeScript SDK for Mintlyr. Govern, optimize, and prove the ROI of your AI portfolio.
Maintainers
Readme
mintlyr-sdk
Add one function around your AI calls to automatically track cost, tokens, latency, and usage across OpenAI, Anthropic, Gemini, Groq, Azure OpenAI, and more—without logging prompts or responses.
Why Mintlyr?
Mintlyr SDK captures AI usage and sends telemetry to the Mintlyr platform, where you can monitor costs, usage trends, budgets, and ROI across your AI applications.
Supported Providers
| Provider | Supported | | ------------ | --------- | | OpenAI | ✅ | | Anthropic | ✅ | | Gemini | ✅ | | Azure OpenAI | ✅ | | Groq | ✅ | | OpenRouter | ✅ | | Mistral | ✅ | (Even if some are auto-detected by the SDK!)
Requirements
- Node.js >= 18
- Modern Browser
- Mintlyr API Key
Installation
npm install mintlyr-sdk
# or
yarn add mintlyr-sdk
# or
pnpm add mintlyr-sdkQuick Start
Initialize the SDK once at the entry point of your application (e.g., index.ts, app.tsx, or server.ts):
import { init } from 'mintlyr-sdk';
init({
apiKey: "<YOUR_API_KEY>",
// localMode: process.env.NODE_ENV !== 'production' // Set to true to disable telemetry during dev
});Environment Variables
If you are using environment variables for your keys:
Next.js
NEXT_PUBLIC_MINTLYR_API_KEY=...Node.js / Express
MINTLYR_API_KEY=...Minimal Example
import { init, trackAI } from "mintlyr-sdk";
init({
apiKey: "<YOUR_API_KEY>"
});
await trackAI({
feature: "chat",
call: async () => {
return await myAiCall();
}
});Full Example with OpenAI
Then, wrap your AI calls with the trackAI wrapper:
import { trackAI } from 'mintlyr-sdk';
import OpenAI from 'openai';
const openai = new OpenAI();
async function generateReport(userId: string, prompt: string) {
// The SDK automatically extracts tokens, measures latency, and calculates costs.
const response = await trackAI({
feature: 'generate_report',
userId: userId, // Automatically hashed for privacy
provider: 'openai',
call: () => openai.chat.completions.create({
model: 'gpt-4o',
messages: [{ role: 'user', content: prompt }]
})
});
return response.choices[0].message.content;
}Features
- Zero Overheads: Operates completely asynchronously.
trackAIwill never block your main thread or crash your application. - Privacy First: We never log your prompts or response bodies. End-user IDs are securely hashed on the client before being sent.
- Multi-Provider Support: Automatically extracts token usage from OpenAI, Anthropic, Gemini, Mistral, and more.
- Tree-shakeable & Isomorphic: Works flawlessly in Node.js, Next.js (App & Pages router), Express, and browser environments. Zero external dependencies.
Powered by the Mintlyr Platform
The SDK is the client-side component of the Mintlyr platform. Once telemetry is sent, Mintlyr enables:
- AI cost analytics
- Token usage monitoring
- Provider-level insights
- Team and feature attribution
- ROI reporting
- Budget management
- Historical trends
- Future enterprise governance capabilities
API Reference
init(config: MintlyrConfig)
Configures the global Mintlyr client.
| Property | Type | Description |
|---|---|---|
| apiKey | string | Required. Your Mintlyr Project API Key. |
| apiUrl | string | Optional. Override the ingestion endpoint. Defaults to https://api.mintlyr.com. |
| localMode | boolean | Optional. If true, tracking events will not be sent to the backend. |
trackAI(options: TrackOptions)
Wraps an asynchronous AI call to capture telemetry.
| Property | Type | Description |
|---|---|---|
| feature | string | Required. The name of the feature being executed (e.g., chat_bot, summarize). |
| call | () => Promise<T> | Required. An async function containing your AI logic. |
| userId | string | Optional. A distinct identifier for the end user. Will be hashed. |
| sessionId | string | Optional. A distinct identifier for the session/thread. |
| provider | Provider | Optional. The AI provider used. Defaults to auto. |
| model | string | Optional. The model used. Overrides auto-detection. |
| fallback | () => T | Optional. Function to execute if the budget is exhausted. |
Privacy & Data Collection
Mintlyr is built for enterprise observability with privacy as the absolute highest priority.
What we collect:
- Token counts:
input_tokensandoutput_tokens. - Performance metrics:
response_time_ms. - Metadata:
provider,model,featurename, andsession_id. - End-User ID: Passed through a secure one-way cryptographic hash on the client side before leaving your infrastructure.
What we NEVER collect:
- Prompts: The text of your users' queries is never tracked.
- Responses: The content generated by the AI is never tracked.
Platform Support
The SDK is highly isomorphic, shipping with both CommonJS (require) and ESModule (import) builds, fully typed. It runs seamlessly in:
- Node.js (>= v18)
- Next.js (App Router & Pages Router)
- React / Vite / Create React App
- Express.js / NestJS / Fastify
- Vanilla Browser Environments
License
MIT
