megflow-observability
v0.16.0
Published
AI API observability SDK — track tokens, cost, latency and errors across OpenAI, Anthropic and more
Readme
megflow-observability
JavaScript/TypeScript SDK for Megflow AI Observability — track tokens, cost, latency and errors across OpenAI, Anthropic and more.
Install
npm install megflow-observabilityQuick start
import { ObserveClient } from 'megflow-observability';
const observe = new ObserveClient({ apiKey: 'obs_your_key_here' });
observe.track({
provider: 'openai',
model: 'gpt-4o',
input_tokens: 100,
output_tokens: 50,
total_tokens: 150,
cost_usd: 0.00075,
latency_ms: 320,
status_code: 200,
error: '',
timestamp: new Date().toISOString(),
request_id: 'req_abc123',
});Auto-instrument OpenAI
One line wraps all calls — including streaming.
import OpenAI from 'openai';
import { ObserveClient, wrapOpenAI } from 'megflow-observability';
const client = wrapOpenAI(
new OpenAI({ apiKey: process.env.OPENAI_API_KEY }),
new ObserveClient({ apiKey: 'obs_your_key_here' }),
);
// All calls tracked automatically
const response = await client.chat.completions.create({
model: 'gpt-4o',
messages: [{ role: 'user', content: 'Hello' }],
});Works with Azure OpenAI, Groq, xAI, Mistral, Together AI, Fireworks, Ollama and any OpenAI-compatible provider:
const client = wrapOpenAI(
new OpenAI({ baseURL: 'https://api.groq.com/openai/v1', apiKey: process.env.GROQ_API_KEY }),
new ObserveClient({ apiKey: 'obs_your_key_here' }),
);Auto-instrument Anthropic
import Anthropic from '@anthropic-ai/sdk';
import { ObserveClient, wrapAnthropic } from 'megflow-observability';
const client = wrapAnthropic(
new Anthropic({ apiKey: process.env.ANTHROPIC_API_KEY }),
new ObserveClient({ apiKey: 'obs_your_key_here' }),
);
const response = await client.messages.create({
model: 'claude-sonnet-4-6',
max_tokens: 1024,
messages: [{ role: 'user', content: 'Hello' }],
});Streaming
Both wrappers handle streaming automatically — no extra code needed.
const stream = await client.chat.completions.create({
model: 'gpt-4o',
messages: [{ role: 'user', content: 'Hello' }],
stream: true,
});
for await (const chunk of stream) {
process.stdout.write(chunk.choices[0]?.delta?.content ?? '');
}
// tracked automatically when stream endsOptional fields
observe.track({
// ... required fields above
cached_tokens: 50,
finish_reason: 'stop',
environment: 'production',
user_id: 'user_123',
rate_limit_remaining: 4999,
retry_count: 0,
is_streaming: false,
stream_chunks: 0,
stream_duration_ms: 0,
stream_completed: true,
prompt_size: 0,
prompt_hash: '',
prompt_version: '',
metadata: { session_id: 'sess_abc', feature: 'chat' },
});Prompt observability
Every wrap*() function auto-computes a prompt_size (character count) and prompt_hash (SHA-256, first 16 chars) from the prompt-relevant input before sending anything — the raw prompt text itself is never transmitted. This lets the dashboard's Prompts page detect and group repeated prompts.
prompt_version has no natural source in an API call, so tag it explicitly per wrapped client:
const client = wrapOpenAI(new OpenAI(), observe, { promptVersion: 'v3' });Get your API key
Dashboard → observability.megflow.com → Projects → Show keys
Auto-instrument Gemini
import { GoogleGenerativeAI } from '@google/generative-ai';
import { ObserveClient, wrapGemini } from 'megflow-observability';
const genAI = new GoogleGenerativeAI(process.env.GOOGLE_API_KEY!);
const observe = new ObserveClient({ apiKey: 'obs_your_key_here' });
const model = wrapGemini(
genAI.getGenerativeModel({ model: 'gemini-2.0-flash' }),
observe,
);
const result = await model.generateContent('Hello');Streaming is also tracked automatically:
const result = await model.generateContentStream('Hello');
for await (const chunk of result.stream) {
process.stdout.write(chunk.text());
}
// tracked when result.response resolves
await result.response;Auto-instrument AWS Bedrock
Wraps InvokeModelCommand and InvokeModelWithResponseStreamCommand — works with Claude, Llama, Mistral, Titan and Cohere on Bedrock, streaming included.
import { BedrockRuntimeClient, InvokeModelCommand } from '@aws-sdk/client-bedrock-runtime';
import { ObserveClient, wrapBedrock } from 'megflow-observability';
const observe = new ObserveClient({ apiKey: 'obs_your_key_here' });
const client = wrapBedrock(new BedrockRuntimeClient({ region: 'us-east-1' }), observe);
const response = await client.send(new InvokeModelCommand({
modelId: 'anthropic.claude-3-5-sonnet-20241022-v2:0',
contentType: 'application/json',
body: JSON.stringify({
anthropic_version: 'bedrock-2023-05-31',
messages: [{ role: 'user', content: 'Hello' }],
max_tokens: 1024,
}),
}));Auto-instrument Cohere
Works with both CohereClient (v1) and CohereClientV2 (v2), including chatStream().
import { CohereClientV2 } from 'cohere-ai';
import { ObserveClient, wrapCohere } from 'megflow-observability';
const observe = new ObserveClient({ apiKey: 'obs_your_key_here' });
const co = wrapCohere(new CohereClientV2({ token: process.env.COHERE_API_KEY }), observe);
const response = await co.chat({
model: 'command-r-plus',
messages: [{ role: 'user', content: 'Hello' }],
});Links
- Dashboard: observability.megflow.com
- Python SDK: pypi.org/project/megflow-observability
