> ## Documentation Index
> Fetch the complete documentation index at: https://agent-observability-docs.splunk.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Log Using the OpenAI Wrapper

> Learn how to integrate and use OpenAI's API with Splunk Agent Observability's wrapper client

## Overview

When working with OpenAI's API, it's important to set up your environment and client correctly to ensure secure and efficient API calls. This guide shows you how to create a basic integration using Splunk Agent Observability's OpenAI client wrapper.

In this guide you will:

1. [Set up a project with Splunk Agent Observability](#install-dependencies)

1) [Create a chat client using the Splunk Agent Observability OpenAI wrapper](#create-a-chat-client-using-the-splunk-agent-observability-openai-wrapper)

<Note>
  The Splunk Agent Observability OpenAI wrapper currently only supports the synchronous chat completions API.
</Note>

## Before you start

To complete this how-to, you will need:

* An [OpenAI API key](https://openai.com/api/)
* A [Splunk Agent Observability project](/concepts/projects) configured
* Your Splunk Agent Observability API key, which you can get from the **API keys** page in the UI

## Install dependencies

To use Splunk Agent Observability, you need to install some package dependencies, and configure environment variables.

<Steps>
  <Step title="Install Required Dependencies">
    Install the required dependencies for your app. If you are using Python, create a virtual environment using your preferred method, then install dependencies inside that environment:

    <CodeGroup>
      ```bash Python theme={null}
      pip install "splunk-ao[openai]" python-dotenv
      ```
    </CodeGroup>
  </Step>

  <Step title="Create a .env file, and add the following values">
    <CodeGroup>
      <GalileoOpenAIEnvVars />
    </CodeGroup>
  </Step>
</Steps>

## Create a chat client using the Splunk Agent Observability OpenAI wrapper

<Steps>
  <Step title="Create a file for your application called app.py or app.ts." />

  <Step title="Add code to call OpenAI">
    Add the following code to your application file:

    <CodeGroup>
      ```python Python theme={null}
      from splunk_ao.openai import openai
      from dotenv import load_dotenv

      # Load the Splunk Agent Observability and OpenAI environment variables
      load_dotenv()

      # Create the Splunk Agent Observability wrapped OpenAI client
      client = openai.OpenAI()

      # Define a prompt
      prompt = "Explain the following topic succinctly: Newton's First Law"

      # Get a response from OpenAI
      response = client.chat.completions.create(
          model="gpt-4",
          messages=[{"role": "user", "content": prompt}],
      )

      # Print the response
      print(response.choices[0].message.content.strip())
      ```
    </CodeGroup>
  </Step>

  <Step title="Run the app">
    <CodeGroup>
      ```bash Python theme={null}
      python app.py
      ```
    </CodeGroup>

    When the app runs, the span will be logged automatically, with the input as the `prompt`, the output as the returned `response`. The duration and number of tokens will also be logged.
  </Step>

  <Step title="View the logged trace">
    From your Splunk Agent Observability homepage, open the Agent Stream for your project. You will see a trace with a single span containing the logged function call.
  </Step>
</Steps>

Your logging is now set up! You are ready to configure evaluators for your project.

## See also

* [Configure evaluators](/concepts/evaluators/overview)
* [Agent Streams](/sdk-api/logging/logging-basics)
