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

# OpenAI

> AgentOps provides first class support for OpenAI's GPT family of models

[OpenAI](https://www.openai.com) is a leading provider of AI tools and services.
Explore the [OpenAI API](https://platform.openai.com/) for more information.

<iframe width="560" height="315" src="https://www.youtube.com/embed/qlotcZSh5_0" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen />

<Steps>
  <Step title="Install the AgentOps SDK">
    <CodeGroup>
      ```bash pip  theme={null}
      pip install agentops
      ```

      ```bash poetry theme={null}
      poetry add agentops
      ```
    </CodeGroup>
  </Step>

  <Step title="Install the OpenAI SDK">
    <Note>
      `openai<1.0.0` has limited support while `openai>=1.0.0` is continuously supported.
    </Note>

    <CodeGroup>
      ```bash pip theme={null}
      pip install openai
      ```

      ```bash poetry theme={null}
      poetry add openai
      ```
    </CodeGroup>

    To install `openai<1.0.0`, use the following:

    <CodeGroup>
      ```bash pip theme={null}
      pip install "openai<1.0.0"
      ```

      ```bash poetry theme={null}
      poetry add "openai<1.0.0"
      ```
    </CodeGroup>
  </Step>

  <Step title="Add 3 lines of code">
    <Note>
      Make sure to call `agentops.init` before calling any `openai`, `cohere`, `crew`, etc models.
    </Note>

    <CodeGroup>
      ```python python theme={null}
      import agentops
      from openai import OpenAI

      agentops.init(<INSERT YOUR API KEY HERE>)
      client = OpenAI()
      ...
      # End of program (e.g. main.py)
      agentops.end_session("Success")
      ```
    </CodeGroup>

    <Tip>
      Set your API key as an `.env` variable for easy access.
    </Tip>

    <CodeGroup>
      ```python .env theme={null}
      AGENTOPS_API_KEY=<YOUR API KEY>
      OPENAI_API_KEY=<YOUR OPENAI API KEY>
      ```
    </CodeGroup>

    Read more about environment variables in [Advanced Configuration](/v1/usage/advanced-configuration)
  </Step>

  <Step title="Run your Agent">
    Execute your program and visit [app.agentops.ai/drilldown](https://app.agentops.ai/drilldown) to observe your Agent! 🕵️

    <Tip>
      After your run, AgentOps prints a clickable url to console linking directly to your session in the Dashboard
    </Tip>

    <div />

    <Frame type="glass" caption="Clickable link to session">
      <img height="200" src="https://raw.githubusercontent.com/AgentOps-AI/agentops/refs/heads/main/docs/images/external/app_screenshots/session-replay.png?raw=true" />
    </Frame>
  </Step>
</Steps>

## Full Examples

<CodeGroup>
  ```python sync theme={null}
  from openai import OpenAI
  import agentops

  agentops.init(<INSERT YOUR API KEY HERE>)
  client = OpenAI()

  response = client.chat.completions.create(
      model="gpt-4o-mini",
      messages=[{
          "role": "user",
          "content": "Write a haiku about AI and humans working together"
      }]
  )

  print(response.choices[0].message.content)
  agentops.end_session('Success')
  ```

  ```python async theme={null}
  from openai import AsyncOpenAI
  import agentops
  import asyncio

  async def main():
      agentops.init(<INSERT YOUR API KEY HERE>)
      client = AsyncOpenAI()

      response = await client.chat.completions.create(
          model="gpt-4o-mini",
          messages=[{
              "role": "user",
              "content": "Write a haiku about AI and humans working together"
          }]
      )

      print(response.choices[0].message.content)
      agentops.end_session('Success')

  asyncio.run(main())
  ```
</CodeGroup>

### Streaming examples

<CodeGroup>
  ```python sync theme={null}
  from openai import OpenAI
  import agentops

  agentops.init(<INSERT YOUR API KEY HERE>)
  client = OpenAI()

  stream = client.chat.completions.create(
      model="gpt-4o-mini",
      stream=True,
      messages=[{
            "role": "user",
            "content": "Write a haiku about AI and humans working together"
      }],
  )

  for chunk in stream:
    print(chunk.choices[0].delta.content or "", end="")

  agentops.end_session('Success')
  ```

  ```python async theme={null}
  from openai import AsyncOpenAI
  import agentops
  import asyncio

  async def main():
    agentops.init(<INSERT YOUR API KEY HERE>)
    client = AsyncOpenAI()

    stream = await client.chat.completions.create(
        model="gpt-4o-mini",
        stream=True,
        messages=[{
            "role": "user",
            "content": "Write a haiku about AI and humans working together"
        }],
    )

    async for chunk in stream:
        print(chunk.choices[0].delta.content or "", end="")
    
    agentops.end_session('Success')

  asyncio.run(main())
  ```

  ### Assistants example

  You can find the example in the [Assistants](/v1/examples/openai_assistants) section.
</CodeGroup>

<script type="module" src="/scripts/github_stars.js" />

<script type="module" src="/scripts/scroll-img-fadein-animation.js" />

<script type="module" src="/scripts/button_heartbeat_animation.js" />

<script type="css" src="/styles/styles.css" />

<script type="module" src="/scripts/adjust_api_dynamically.js" />
