Overview

Gentrace automatically collects spans from compliant automatic instrumentations, for example from the AI SDK or Pydantic AI SDK. Where automatic instrumentations are not available, you can manually construct spans in compliance with the OpenTelemetry specification. For full details of the attributes and events supported by Gentrace, see our OpenTelemetry API reference.

Example

import { trace } from "@opentelemetry/api";
import OpenAI from "openai";

const tracer = trace.getTracer("my-app-tracer");
const openai = new OpenAI();

const span = tracer.startSpan(`chat o4-mini`, {
  attributes: {
    'gen_ai.operation.name': 'chat',
    'gen_ai.provider.name': 'openai',
    'gen_ai.request.model': 'o4-mini',
    'gen_ai.request.max_tokens': 1000,
  }
});

const messages = [{ role: 'user', content: 'Hello, world!' }];

span.addEvent('gen_ai.user.message', {
  'content': messages[0].content,
});

const response = await openai.chat.completions.create({
  model: 'o4-mini',
  messages,
});

const choice = response.choices[0];
const completion = choice.message.content;

span.addEvent('gen_ai.choice', {
  'index': choice.index,
  'finish_reason': choice.finish_reason,
  'message.content': completion,
  'message.role': choice.message.role,
});

span.setAttributes({
  'gen_ai.usage.input_tokens': response.usage.prompt_tokens,
  'gen_ai.usage.output_tokens': response.usage.completion_tokens,
  'gen_ai.response.finish_reasons': ['stop'],
});

span.end();

Next steps