Cost tracking

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

If you are using one of Opik’s integrations, we 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 the provider and model name

If you are not using one of Opik’s integration, Opik can still compute the cost. For you will need to ensure the span type is llm and you will need to pass:

  1. provider: The name of the provider, typically openai, anthropic or google_ai for example
  2. model: The name of the model
  3. usage: The input, output and total tokens for this LLM call.

You can then update your code to log traces and spans:

If you are using function decorators, you will need to use the update_current_span method:

1from opik import track, opik_context
2
3@track(type="llm") # Note - Specifying the type is this is important
4def llm_call(input):
5 opik_context.update_current_span(
6 provider="openai",
7 model="gpt-3.5-turbo",
8 usage={
9 "prompt_tokens": 4,
10 "completion_tokens": 6,
11 "total_tokens": 10
12 }
13 )
14 return "Hello, world!"
15
16llm_call("Hello world!")

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 import track, opik_context
2
3@track
4def llm_call(input):
5 opik_context.update_current_span(
6 total_cost=0.05,
7 )
8 return "Hello, world!"
9
10llm_call("Hello world!")

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.