Cost tracking

Describes how to track and monitor costs for your LLM applications using Opik

Opik has been designed to track and monitor costs for your LLM applications by measuring token usage across all traces. Using the Opik dashboard, you can analyze spending patterns and quickly identify cost anomalies. All costs across Opik are estimated and displayed in USD.

Monitoring Costs in the Dashboard

You can use the Opik dashboard to review costs at three levels: spans, traces, and projects. Each level provides different insights into your application’s cost structure.

Span-Level Costs

Individual spans show the computed costs (in USD) for each LLM spans of your traces:

Trace-Level Costs

Opik automatically aggregates costs from all spans within a trace to compute total trace costs:

Project-Level Analytics

Track your overall project costs in:

  1. The main project view, through the Estimated Cost column:

  2. The project Metrics tab, which shows cost trends over time:

Retrieving Costs Programmatically

You can retrieve the estimated cost programmatically for both spans and traces. Note that the cost will be None if the span or trace used an unsupported model. See Exporting Traces and Spans for more ways of exporting traces and spans.

Retrieving Span Costs

1import opik
2
3client = opik.Opik()
4
5span = client.get_span_content("<SPAN_ID>")
6# Returns estimated cost in USD, or None for unsupported models
7print(span.total_estimated_cost)

Retrieving Trace Costs

1import opik
2
3client = opik.Opik()
4
5trace = client.get_trace_content("<TRACE_ID>")
6# Returns estimated cost in USD, or None for unsupported models
7print(trace.total_estimated_cost)

Manually Setting Span Costs

For cases where you need to set a custom cost or when using an unsupported model, you can manually set the cost of a span using update_current_span. Note that manually setting a cost will override any automatically computed cost by Opik:

1from opik.opik_context import update_current_span
2
3# Inside a span context
4update_current_span(total_cost=0.05) # Cost in USD will override any automatic cost calculation

This is particularly useful when:

  • Using models or providers not yet supported by automatic cost tracking
  • You have a custom pricing agreement with your provider
  • You want to track additional costs beyond model usage

Supported Models and Integrations

Opik currently calculates costs automatically for:

We are actively expanding our cost tracking support. Need support for additional models or providers? Please open a feature request to help us prioritize development.

Built with