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, withgen_ai.operation.name,gen_ai.provider.name,gen_ai.request.model,server.addressandserver.port, the request’s settings, and the response’s model, id and token counts. A failed call is an error, witherror.type. gen_ai.client.inference.duration, in seconds.- Token usage:
gen_ai.client.inference.usage.input_tokensand.output_tokens, counters bygen_ai.token.modalityfor totals and cost, andgen_ai.client.inference.operation.input_tokensand.output_tokens, histograms of the tokens each call used. - For a streamed response, with
Call::chunkas each chunk arrives,gen_ai.client.inference.time_to_first_chunkandgen_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
finishorfail; a call dropped without either, as when its future is cancelled, ends its span without a status and records no metrics. - Call
Builder - 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
modelfromprovider, such asopenai,anthropic,gcp.geminiorollama: seegen_ai.provider.namefor the names the conventions know. - describe_
metrics - Gives the current
metricsrecorder the units and descriptions of the GenAI client metrics, so durations are in seconds with the conventions’ buckets.CallBuilder::startdoes 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.