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

# Log via Python SDK

<Tabs>
  <Tab title="Non-Streaming">
    <Steps>
      <Step title="Install Python SDK">
        ```
        pip install athina-logger
        ```
      </Step>

      <Step title="Set Athina API Key">
        ```python
        from athina_logger.api_key import AthinaApiKey

        AthinaApiKey.set_api_key(os.getenv('ATHINA_API_KEY'))
        ```
      </Step>

      <Step title="Log your inference">
        ```python inference.py
        response = client.chat.completions.create(
              model='gpt-4-1106-preview',
              messages=[{"role": "user", "content": "What is machine learning?"}],
          )

        response = response.model_dump() # For openai > 1 version

        try:
          InferenceLogger.log_inference(
              prompt_slug="sdk_test",
              prompt=messages,
              language_model_id="gpt-4-1106-preview",
              response=response,
              external_reference_id="abc",
              cost=0.0123,
              custom_attributes={
                  "name": "John Doe"
                  # Your custom attributes
              }
          )
        except Exception as e:
          if isinstance(e, CustomException):
              print(e.status_code)
              print(e.message)
          else:
              print(e)
        ```
      </Step>
    </Steps>

    <Tip>
      Tip: Include your logging code in a try/catch block to ensure that your
      application doesn't crash if the logging request fails.
    </Tip>
  </Tab>

  <Tab title="Streaming">
    #### 1. Install the Python SDK

    Run `pip install athina-logger`

    #### 2. Import Athina Logger and openai package

    ```python
    from athina_logger.api_key import AthinaApiKey
    from openai import OpenAI

    client = OpenAI(api_key=os.getenv('OPENAI_API_KEY'))
    ```

    #### 3. Set Athina API key

    #### Initialize the Athina API key somewhere in your code

    ```python
    AthinaApiKey.set_api_key(os.getenv('ATHINA_API_KEY'))
    ```

    #### 4. Call the logging function

    ```python
    from athina_logger.log_stream_inference.openai_chat_completion_stream import LogOpenAiChatCompletionStreamInference

    # Using python openai SDK
    def stream_openai_chat_response(user_query, content):
        response = client.chat.completions.create(
            model='gpt-3.5-turbo',
            messages=[{"role": "user", "content": content}],
            stream=True,
        )
        logger = LogOpenAiChatCompletionStreamInference(
            prompt=[{"role": "user", "content": content}],
            language_model_id="gpt-3.5-turbo",
            prompt_slug="test",
            context=context, # OPTIONAL
            response_time=response_time_ms, # OPTIONAL
            customer_id="stripe", # OPTIONAL
            customer_user_id="shiv@athina.ai", # OPTIONAL
            session_id="c45g-1234-s6g4-43d3", # OPTIONAL
            user_query=user_query, # OPTIONAL
            environment="production", # OPTIONAL
            external_reference_id="5e838eaf-7dd0-4b6f-a32c-26110dd54e58", # OPTIONAL; If passed, should be unique across all inference calls
            custom_attributes={
                "name": "John Doe"
            } # OPTIONAL;
        )
    ```

    #### 5. Collect the streaming responses

    There are 2 ways to collect openai chat streams

    #### Option 1: Collect automatically from response

    ```python
    try:
        logger.collect_stream_inference(response)
        logger.log_stream_inference()
    except Exception as e:
        print(e)
    ```

    #### Option 2: Collect individually by chunk

    ```python
    try:
        for chunk in response:
            logger.collect_stream_inference_by_chunk(chunk)
        logger.log_stream_inference()
    except Exception as e:
        print(e)
    ```

    <Tip>
      Tip: Include your logging code in a try/catch block to ensure that your
      application doesn't crash if the logging request fails.
    </Tip>
  </Tab>
</Tabs>

#### Logging Attributes

You can find the full list of [logging attributes here](/api-reference/logging/logging-attributes).
