Skip to content

LLM tracking (OpenAI, Anthropic, …)

Track AI provider calls with tokens, latency, streaming metrics, and cost.

Node.js — OpenAI

typescript
import { AmlexiaClient, trackOpenAiCompletion } from '@amlexiahq/node';

const client = AmlexiaClient.fromEnv();
const start = Date.now();
const completion = await openai.chat.completions.create({ ... });

await trackOpenAiCompletion(client, {
  model: completion.model,
  statusCode: 200,
  latencyMs: Date.now() - start,
  usage: completion.usage,
  environment: process.env.AMLEXIA_ENVIRONMENT,
});

Node.js — Anthropic

typescript
import { trackAnthropicMessage } from '@amlexiahq/node';

await trackAnthropicMessage(client, {
  model: message.model,
  statusCode: 200,
  latencyMs,
  usage: { input_tokens, output_tokens },
});

Python

python
from amlexia.openai_helper import track_openai_completion
from amlexia.anthropic_helper import track_anthropic_message

track_openai_completion(client, model=..., status_code=200, latency_ms=..., usage=response.usage)

Go / Ruby

See sdks/go and sdks/rubyTrackOpenAICompletion / Amlexia::OpenAI.track_openai_completion.

Manual track()

typescript
await client.track({
  endpoint: 'POST /v1/chat/completions',
  method: 'POST',
  statusCode: 200,
  latencyMs: 320,
  provider: 'openai',
  modelName: 'gpt-4o-mini',
  tokensInput: 100,
  tokensOutput: 40,
  firstTokenLatencyMs: 120,   // TTFT for streaming
  streamingLatencyMs: 800,    // full stream duration
  cacheHit: false,
});

Omit costUsd → ingest sets estimated cost from the pricing table.

Streaming

For streaming completions, record:

FieldMeaning
firstTokenLatencyMsTime to first token (TTFT)
streamingLatencyMsTotal stream duration

Other providers

Set provider explicitly for Gemini, Groq, Together, Replicate:

typescript
provider: 'gemini',
modelName: 'gemini-1.5-pro',

List of detected providers

Dashboard

  • Providers page — per-vendor cards and model table
  • Overview → Cost — reported vs estimated
  • Operations — spend forecast