Python SDK for AI agent monitoring, LLM cost tracking, benchmarking, and more. Integrates with most LLMs and agent frameworks including CrewAI, Agno, OpenAI Agents SDK, Langchain, Autogen, AG2, and CamelAI
OpenTelemetry Collector Builder
This package transforms token cost data from the tokencost package into YAML configurations for the OpenTelemetry collector, enabling real-time cost calculation for LLM application traces. It also provides tools for generating and managing OpenTelemetry collector configuration files.
Goals
- Extract token cost data from the
tokencostpackage's JSON model price database - Transform this data into YAML configuration for OpenTelemetry collector-contrib processors
- Generate complete configurations for the OpenTelemetry collector
- Apply OTTL (OpenTelemetry Transformation Language) transforms to spans with
gen_ai.usage.completion_tokensand/orgen_ai.usage.prompt_tokensattributes - Match costs to the appropriate model using the
gen_ai.request.modelspan attribute - Enable real-time cost attribution on LLM API spans based on model and token usage
How It Works
- The builder loads token pricing data from the
tokencostpackage'smodel_prices.jsonfile - This data is parsed and converted to appropriate decimal precision formats
- Template files in the config directory are processed, injecting the model costs
- The resulting YAML configurations for the OpenTelemetry collector are generated
- These configurations include OTTL transformations that identify spans with the
gen_ai.request.modelattribute and apply the corresponding cost data
Usage
# Generate configurations with default output directory
python -m builder build_configs
# Generate configurations with custom output directory
python -m builder build_configs -o /path/to/output/directory
The generated configuration will conditionally apply cost transformations to spans with token usage attributes, allowing for automatic cost attribution in your telemetry pipeline. This is also used in the Docker build process to generate configurations at build time.
Configuration Format
The exported YAML configuration creates a transform processor that:
- Populates a span cache with model-specific cost per token values during the build process
- Inspects spans for the
gen_ai.request.modelattribute to identify the LLM model in use - Uses OTTL transforms with
whereclauses to calculate and attribute costs based ongen_ai.usage.prompt_tokensandgen_ai.usage.completion_tokens - Applies the relevant model-specific costs only when both model and token usage attributes are present
This allows the OpenTelemetry collector to automatically calculate and attribute costs for LLM API calls as they pass through the telemetry pipeline. The configuration uses conditional logic to match the model name from span attributes to the appropriate pricing data from the tokencost package.
OTTL Configuration Example
Below is an example of how the OTTL transformations are implemented in the OpenTelemetry collector configuration YAML:
processors:
transform:
trace_statements:
# We are using root-level trace_statements so that the cache is shared across
# all transforms in the pipeline (as opposed to `- scope: span` which creates
# a new cache for each transform)
# Cost data gets populated dynamically on container build
# For each model in the tokencost database, we add input and output costs
# Example:
# - set(span.cache["_input_costs"]["gpt-4"], 0.00001)
# - set(span.cache["_output_costs"]["gpt-4"], 0.00003)
# Set prompt cost on spans that have prompt tokens and a known model
- set(span.attributes["gen_ai.usage.prompt_cost"],
Double(span.attributes["gen_ai.usage.prompt_tokens"]) *
span.cache["_input_costs"][span.attributes["gen_ai.request.model"]])
where (
span.attributes["gen_ai.usage.prompt_tokens"] != nil and
span.attributes["gen_ai.request.model"] != nil and
span.cache["_input_costs"][span.attributes["gen_ai.request.model"]] != nil)
# Set completion cost on spans that have completion tokens and a known model
- set(span.attributes["gen_ai.usage.completion_cost"],
Double(span.attributes["gen_ai.usage.completion_tokens"]) *
span.cache["_output_costs"][span.attributes["gen_ai.request.model"]])
where (
span.attributes["gen_ai.usage.completion_tokens"] != nil and
span.attributes["gen_ai.request.model"] != nil and
span.cache["_output_costs"][span.attributes["gen_ai.request.model"]] != nil)
This example demonstrates how we:
- Store model costs in a span cache using the "_input_costs" and "_output_costs" dictionaries to avoid polluting attributes
- Use root-level trace_statements to ensure the cache is shared across all transformations in the pipeline
- Dynamically populate the cost data during container build time from the tokencost package
- Use conditional "where" clauses to only apply cost calculations when:
- The span has the required attributes (gen_ai.usage.prompt_tokens/completion_tokens)
- The span specifies a model (gen_ai.request.model)
- The model has known costs in our cache
- Apply targeted conditions to minimize unnecessary processing and only calculate costs when all required data is present
The key advantage of this implementation is that cost data is:
- Dynamically generated at build time from the latest tokencost data
- Stored in memory only (not in actual span attributes until calculations are done)
- Used only when spans have the appropriate attributes
- Applied uniformly across all spans in the pipeline
The actual processor configuration includes model cost data for all models available in the tokencost package database, populated through a Jinja2 template at build time.
Implementation References
The implementation uses these OpenTelemetry Transformation Language (OTTL) features:
- OTTL Where Clauses - Used for conditional application of transformations
- Span Cache - Storing model cost data without polluting span attributes
- Double Function - Converting token counts to floating point for calculations
- Nil Checks - Ensuring attributes exist before processing
- Set Function - Creating new span attributes with calculated costs
- Dictionary Access - Accessing model costs by model name
This implementation follows the OpenTelemetry Semantic Conventions for AI/ML and leverages Jinja2 Templates for configuration generation at build time.