Skip to main content

Crate oxtel_genai

Crate oxtel_genai 

Source
Expand description

Spans and metrics for calls to generative AI models, as the OpenTelemetry GenAI semantic conventions describe them: a span for each call, how long it took, and the tokens it used. Part of oxtel.

It works with any model client: say what the call is when it starts, make the call inside its span, and say how it went when it ends.

let call = oxtel_genai::chat("ollama", "llama3.2")
    .server("localhost", 11434)
    .temperature(0.2)
    .start();
// Make the call inside the span: in async code, `.instrument(call.span().clone())`.
match call.span().in_scope(ollama_chat) {
    Ok(reply) => call
        .response_model(&reply.model)
        .input_tokens(reply.prompt_eval_count)
        .output_tokens(reply.eval_count)
        .finish(),
    Err(_) => call.fail("timeout"),
}

That records:

  • A client span named chat llama3.2, with gen_ai.operation.name, gen_ai.provider.name, gen_ai.request.model, server.address and server.port, the request’s settings, and the response’s model, id and token counts. A failed call is an error, with error.type.
  • gen_ai.client.inference.duration, in seconds.
  • Token usage: gen_ai.client.inference.usage.input_tokens and .output_tokens, counters by gen_ai.token.modality for totals and cost, and gen_ai.client.inference.operation.input_tokens and .output_tokens, histograms of the tokens each call used.
  • For a streamed response, with Call::chunk as each chunk arrives, gen_ai.client.inference.time_to_first_chunk and gen_ai.client.inference.time_per_output_chunk.

An embeddings call isn’t inference, so it records gen_ai.client.operation.duration instead, and its input tokens on its span only.

No prompt or completion content is recorded: messages, system instructions and tool definitions can hold anything, and they’re the conventions’ opt-in attributes.

Spans are recorded with tracing and metrics with metrics, so they go wherever the program’s subscriber and recorder send them: with oxtel::init(), to OTLP or Prometheus, with the buckets the conventions advise. Arrays can’t be tracing fields, so gen_ai.response.finish_reasons and gen_ai.request.stop_sequences aren’t recorded. The GenAI conventions are still in development, so the names may change in a later release, as they do upstream.

Structs§

Call
A model call under way. Say how it went with finish or fail; a call dropped without either, as when its future is cancelled, ends its span without a status and records no metrics.
CallBuilder
A model call about to be made. Each setting is recorded on the span, as the conventions name it; leave out what the request doesn’t set.

Enums§

Modality
What kind of input or output tokens are: gen_ai.token.modality.
Operation
What a call asks of the model: gen_ai.operation.name.

Functions§

chat
A chat completion call to model from provider, such as openai, anthropic, gcp.gemini or ollama: see gen_ai.provider.name for the names the conventions know.
describe_metrics
Gives the current metrics recorder the units and descriptions of the GenAI client metrics, so durations are in seconds with the conventions’ buckets. CallBuilder::start does this each time, so there’s no need to call it yourself.
embeddings
An embeddings call.
generate_content
A multimodal content generation call.
text_completion
A text completion call.