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

# Add Tracing to OpenAI applications

> Trace OpenAI API calls in your applications using the Traceloop SDK and TrueFoundry OtelCollector.

This guide demonstrates how to use TrueFoundry OtelCollector along with the Traceloop SDK to instrument OpenAI API calls.
In this example, we'll show how to instrument a simple Python application that makes calls to OpenAI's API.

<Steps>
  <Step title="Create Tracing Project, API Key and copy tracing code">
    Follow the instructions in [Getting Started](/docs/tracing/tracing-getting-started) to create a tracing project, generate API key and copy the
    tracing code.
  </Step>

  <Step title="Install Dependencies">
    First, you need to install the following

    ```shell lines theme={"dark"}
    pip install openai traceloop-sdk dotenv
    ```
  </Step>

  <Step title="Add Tracing code to OpenAI application">
    For OpenAI applications, we need to add the `Traceloop.init()` call to the application. The Traceloop SDK will automatically trace all OpenAI API calls.

    ```python OpenAI Code {5-6,10-19} lines theme={"dark"}
    from dotenv import load_dotenv
    from openai import OpenAI
    import os

    # importing traceloop sdk
    from traceloop.sdk import Traceloop

    load_dotenv()

    # Add the traceloop init code to your application
    TFY_API_KEY = os.environ.get("TFY_API_KEY")
    Traceloop.init(
        api_endpoint="<enter_your_api_endpoint>",
        app_name="your tracing application name",
        headers = {
            "Authorization": f"Bearer {TFY_API_KEY}",
            "TFY-Tracing-Project": "<enter_your_tracing_project_fqn>",
        },
    )

    # Initialize OpenAI client
    client = OpenAI()

    # Make API call - this will be automatically traced
    stream = client.chat.completions.create(
        messages = [
                {"role": "system", "content": "You are an AI bot."},
                {"role": "user", "content": "Explain the concept of AI in 50 words"},
        ],
        model= "gpt-4o",
        stream=True,
        temperature=0.7,
        max_tokens=256,
        top_p=0.8,
        frequency_penalty=0,
        presence_penalty=0
    )

    for chunk in stream:
        if chunk.choices and len(chunk.choices) > 0 and chunk.choices[0].delta.content is not None:
            print(chunk.choices[0].delta.content, end="")
    ```
  </Step>

  <Step title="Run your application and view logged trace">
    <Frame caption="">
      <img src="https://mintcdn.com/truefoundry/2MMcllD7kMlpnaWX/images/tracing_openai.png?fit=max&auto=format&n=2MMcllD7kMlpnaWX&q=85&s=eabeca2cdd8a4317c63457e47424cce6" width="3024" height="1718" data-path="images/tracing_openai.png" />
    </Frame>
  </Step>
</Steps>
