Opik Python SDK
Using the Opik Python SDK
This guide shows you how to directly instrument your Python applications with the Opik SDK to send trace data to Opik.
Use this approach when you are tracing your own code rather than a supported framework or provider. If you are using one of the supported integrations — LangChain, OpenAI, LiteLLM, and many others — prefer that integration instead: it captures inputs, outputs, token usage and cost for you with a single line of setup.
Installation
First, install the Opik package:
Configuration
Configure the SDK with your credentials, either by running opik configure once on the machine:
Or by setting environment variables:
Opik Cloud
Self-hosted
You can find your API key and workspace name in the Opik dashboard.
Set OPIK_PROJECT_NAME to control which project traces are logged to; it defaults to Default Project. See
SDK configuration for the full list of options and precedence rules.
Full Example
Here’s a complete example that demonstrates how to instrument a chatbot application with the Opik SDK:
The @opik.track decorator creates a span for every decorated function it wraps. The outermost decorated call also
creates the trace, and nested calls are attached to it automatically — so llm_completion appears as a child span of
chatbot_conversation without any manual ID passing. Inputs and outputs are captured from the function’s arguments
and return value by default.
Using thread_id allows you to group related traces into a single conversational thread.
Created threads can be used to evaluate multi-turn conversations as described in the Multi-turn conversations guide.
Span types
Set the type argument to tell Opik what kind of work a span represents. The supported values are general (the
default), llm, tool and guardrail:
Marking a span as llm is what makes it eligible for token and cost accounting, so use it for any function that
calls a model provider.
Tracking cost
Opik computes cost from the model, provider and usage fields on an llm span. Set them with
update_current_span as in the example above, using the standard OpenAI token keys:
For models Opik doesn’t price automatically, you can set total_cost directly. See
Cost tracking for the details.
Tracing code you can’t decorate
When the code you want to trace isn’t a function you can decorate — a block inside a longer function, or a third-party
call — use the start_as_current_span context manager instead. It creates the parent trace if one isn’t already
active:
Flushing before exit
The SDK batches and sends data in the background, so a short-lived script can exit before everything is delivered.
Call opik.flush_tracker() before the process ends, as in the example above, or pass flush=True to @opik.track on
your entrypoint function:
Long-running services don’t need this — the background sender keeps up on its own.
Next steps
- Log traces — the low-level
Opikclient, for cases where the decorator and context manager don’t fit - SDK configuration — all configuration options and their precedence
- Log distributed traces — tracing a request across multiple services
- Python SDK reference — the full API reference