@aethermind/agent
v0.1.6
Published
Lightweight SDK for monitoring AI API costs with Aethermind
Maintainers
Readme
@aethermind/agent
Lightweight SDK for real-time AI API cost monitoring. Works with any setup -- official SDKs, raw fetch(), or any HTTP client that calls OpenAI or Anthropic APIs.
Installation
npm install @aethermind/agent
# or
pnpm add @aethermind/agent
# or
yarn add @aethermind/agentNo peer dependencies required. If you use the official OpenAI or Anthropic Node.js SDKs, the agent will patch those too for deeper telemetry -- but it is entirely optional.
Quick Start
import { initAethermind } from "@aethermind/agent";
// Initialize once at app startup
initAethermind({
apiKey: process.env.AETHERMIND_API_KEY!,
});That's it. Every call to api.openai.com or api.anthropic.com is now tracked automatically -- regardless of how you make the request.
Using raw fetch()
// No SDK needed -- fetch() calls are intercepted automatically
const response = await fetch("https://api.openai.com/v1/chat/completions", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${process.env.OPENAI_API_KEY}`,
},
body: JSON.stringify({
model: "gpt-4o",
messages: [{ role: "user", content: "Hello!" }],
}),
});
// Costs are tracked in the background
const data = await response.json();
console.log(data.choices[0].message.content);Using the OpenAI SDK
import OpenAI from "openai";
const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
// Automatically tracked via SDK patching
const completion = await openai.chat.completions.create({
model: "gpt-4o",
messages: [{ role: "user", content: "Hello!" }],
});Using the Anthropic SDK
import Anthropic from "@anthropic-ai/sdk";
const anthropic = new Anthropic({ apiKey: process.env.ANTHROPIC_API_KEY });
// Automatically tracked via SDK patching
const message = await anthropic.messages.create({
model: "claude-3-5-sonnet-20241022",
max_tokens: 1024,
messages: [{ role: "user", content: "Hello, Claude!" }],
});How It Works
The SDK uses three layers to capture telemetry with zero code changes:
Fetch interceptor -- Patches
globalThis.fetchto detect calls to OpenAI/Anthropic API hostnames. Extracts model, tokens, cost, and latency from the response. Works with any HTTP client that usesfetch()under the hood.SDK patching -- Monkey-patches
OpenAI.chat.completions.createandAnthropic.messages.createprototypes for SDK-specific telemetry (covers streaming and other SDK features). Only activates if the SDKs are installed.Batch transport -- Captured events are queued locally and flushed to the Aethermind API in batches (default: every 5 seconds or 50 events). Failed events are stored in a dead-letter queue for retry.
Non-LLM fetch calls (e.g. your own API routes) pass through untouched with zero overhead. All telemetry extraction is wrapped in try/catch -- the SDK never throws errors into your application.
Configuration
import { initAethermind } from "@aethermind/agent";
initAethermind({
apiKey: "your-aethermind-api-key", // Required
endpoint: "https://aethermind-agentos-production.up.railway.app", // Optional
flushInterval: 5000, // Optional: ms between flushes (default: 5000)
batchSize: 50, // Optional: events per batch (default: 50)
enabled: true, // Optional: enable/disable (default: true)
});| Option | Type | Default | Description |
| --------------- | --------- | --------------------------- | ---------------------------------- |
| apiKey | string | required | Your Aethermind API key |
| endpoint | string | https://aethermind-agentos-production.up.railway.app | API endpoint URL |
| flushInterval | number | 5000 | Milliseconds between batch flushes |
| batchSize | number | 50 | Maximum events per batch |
| enabled | boolean | true | Enable/disable monitoring |
Disable Monitoring in Development
initAethermind({
apiKey: process.env.AETHERMIND_API_KEY!,
enabled: process.env.NODE_ENV === "production",
});CommonJS Usage
const { initAethermind } = require("@aethermind/agent");
initAethermind({
apiKey: process.env.AETHERMIND_API_KEY,
});Troubleshooting
Events not appearing in dashboard?
- Wait 5 seconds (default batch interval)
- Verify API key is correct:
process.env.AETHERMIND_API_KEY - Check endpoint URL is correct
- Ensure
enabled: true(default)
TypeScript errors?
Make sure you have @types/node installed:
npm install -D @types/nodeWant to verify initialization?
The SDK logs to console when initialized:
[Aethermind] SDK initialized successfullyIf disabled:
[Aethermind] SDK initialized but disabledDocumentation
Support
License
MIT - Aethermind Team
