Skip to main content
The OpenAI wrapper is the simplest way to integrate Splunk Agent Observability logging into your application. By using Splunk Agent Observability’s OpenAI wrapper instead of importing the OpenAI library directly, you can automatically log all prompts, responses, and statistics without any additional code changes in an LLM span for every call.
The Python SDK supports both the Chat Completions API and the Responses API.
The wrapper supports both the OpenAI and Azure OpenAI APIs, so will work out of the box with Azure deployments, as well as any LLM that supports the OpenAI SDK.

Python Splunk Agent Observability OpenAI SDK reference

The Python Splunk Agent Observability OpenAI SDK reference.

Installation

First, make sure you have the Splunk Agent Observability SDK installed. If you are using Python, ensure you install the OpenAI optional dependency.

Basic usage

If you are using Python, import the splunk_ao.openai module, instead of the OpenAI openai module and use that to create your client.
This example will automatically produce a single-span trace in your Agent Stream. The wrapper handles all the logging for you, capturing:
  • The input prompt
  • The model used
  • The response
  • Timing information
  • Token usage
  • Other relevant metadata

Responses API (Python only)

The Python SDK also supports OpenAI’s Responses API, which provides a simplified interface for single-turn interactions and additional features like built-in tools.

Responses API basic example

Python

Tool calls

The Responses API supports function calling. Here’s an example that demonstrates tool definitions, executing tool calls, and providing results back to the model:
Python
The Responses API also supports advanced features like:
  • Reasoning items with the reasoning parameter for chain-of-thought outputs
  • Built-in tools including web search, code interpreter, and file search
  • Streaming with stream=True
See the OpenAI Responses API documentation for more details.

Sessions and traces

If you use the OpenAI wrapper by itself, it will automatically create a session and start a new trace for you, adding the call as an LLM span. Subsequent calls will be added as an LLM span to a new trace in the same session. The session will have an autogenerated name based off the content.
A single session with 2 traces in the Splunk Agent Observability UI If you manually start a session before using the OpenAI wrapper, all calls to the wrapper will be added as new traces to that session.
A single session called Chat about the Roman Empire with 2 traces in Splunk Agent Observability If you create a new trace before using the OpenAI wrapper, all calls will be added as LLM spans to that trace.
A single session called Chat about the Roman Empire with 1 trace in Splunk Agent Observability

Streaming support

The OpenAI wrapper also supports streaming responses. When streaming, the wrapper will log the response as it streams in:

Combining with the log decorator

You can combine the OpenAI wrapper with the log decorator to create more complex traces:

Benefits of using the OpenAI integration

  • Zero-config logging: No need to add logging code throughout your application
  • Complete visibility: All prompts and responses are automatically captured
  • Minimal code changes: Change your import statement in Python. No other code changes are required.
  • Automatic tracing: Creates spans and traces without manual setup
  • Streaming support: Works with both regular and streaming responses

Asynchronous OpenAI calls with Splunk Agent Observability

The Splunk Agent Observability OpenAI wrapper currently supports only synchronous calls for both the Chat Completions API and the Responses API. It does not include built-in support for the AsyncOpenAI class from the official OpenAI Python library. As a result, asynchronous calls made via the splunk_ao.openai wrapper won’t automatically generate LLM spans or upload telemetry to Splunk Agent Observability.You can still track async interactions by manually using the low-level SplunkAOLogger API. This requires importing and awaiting the OpenAI AsyncOpenAI client, wrapping each call with a call to add an LLM span, and flushing the logger to send your traces.

Next steps

Splunk Agent Observability logger

Log with full control over sessions, traces, and spans using the Splunk Agent Observability logger.

Log decorator

Quickly add logging to your code with the log decorator and wrapper.

Splunk Agent Observability context

Manage logging using the Splunk Agent Observability context manager.