metrix-ai
v0.1.0
Published
LLM Cost Intelligence SDK — track, optimize, and control your AI spend
Maintainers
Readme
metrix.ai Node.js SDK
LLM Cost Intelligence SDK — track, optimize, and control your AI spend across OpenAI, Anthropic, and Gemini.
Install
npm install metrix-aiQuickstart
import { MetrixClient } from "metrix-ai";
const client = new MetrixClient({
apiKey: "your-metrix-token",
providerApiKey: "your-anthropic-key",
project: "my-project",
debug: true,
});
const response = await client.chat({
provider: "anthropic",
model: "claude-sonnet-4-6",
messages: [{ role: "user", content: "Hello from metrix.ai!" }],
});
console.log(response.content);
console.log(`Tokens used: ${response.totalTokens}`);
console.log(`Latency: ${response.latencyMs}ms`);Environment Variables
export METRIX_API_KEY=your-metrix-token
export METRIX_PROVIDER_API_KEY=your-anthropic-key
export METRIX_PROJECT=my-project
export METRIX_AUTO_ROUTE=true
export METRIX_DEBUG=trueThen just:
import { MetrixClient } from "metrix-ai";
const client = new MetrixClient();Smart Routing
const client = new MetrixClient({
apiKey: "...",
providerApiKey: "...",
autoRoute: true,
});
const response = await client.chat({
provider: "anthropic",
model: "claude-opus-4-6",
messages: [{ role: "user", content: "What is 2+2?" }],
});
if (response.routed) {
console.log(`Routed from ${response.originalModel} → ${response.model}`);
}Project Attribution
const response = await client.chat({
provider: "anthropic",
model: "claude-sonnet-4-6",
messages: [{ role: "user", content: "Summarize this..." }],
project: "customer-support",
tags: { env: "prod", feature: "summarization" },
});Supported Providers
| Provider | Models | |-----------|--------| | anthropic | claude-opus-4-6, claude-sonnet-4-6, claude-haiku-4-5 | | openai | gpt-4o, gpt-4o-mini, gpt-4-turbo | | gemini | gemini-1.5-pro, gemini-1.5-flash |
Error Handling
import {
MetrixClient,
MetrixAuthError,
ProviderRateLimitError,
ProviderTimeoutError,
MetrixConnectionError,
UnsupportedProviderError,
} from "metrix-ai";
try {
const response = await client.chat({ ... });
} catch (err) {
if (err instanceof MetrixAuthError) console.log("Invalid token");
if (err instanceof ProviderRateLimitError) console.log("Rate limited");
if (err instanceof ProviderTimeoutError) console.log("Timed out");
if (err instanceof MetrixConnectionError) console.log("Proxy unreachable");
if (err instanceof UnsupportedProviderError) console.log(err.message);
}Configuration
| Option | Type | Default | Description | |--------|------|---------|-------------| | apiKey | string | env | metrix.ai JWT token | | providerApiKey | string | env | LLM provider API key | | project | string | undefined | Project slug | | autoRoute | boolean | false | Enable smart routing | | tags | object | undefined | Attribution tags | | timeout | number | 60000 | Timeout in ms | | maxRetries | number | 2 | Retry attempts | | debug | boolean | false | Colored debug output | | baseUrl | string | localhost | Proxy base URL |
License
MIT — Ahmad Sharique
