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

# Logging LLM requests

> Start logging your LLM requests with 2 lines of code changes.

### Introduction

When the Baserun SDK is initialized explicitly or via our testing plugins, all OpenAI and Anthropic requests are automatically logged with no additional code changes.

For an in-depth overview of how our logging data is structured, please see our Logging Overview page.

<Frame>
  <img src="https://mintcdn.com/baserun/sfThaUjl8v9XCswy/images/2.0-python-llm_request.gif?s=ee159935573edc81151504f84ad2ca23" alt="Logging LLM requests" width="1724" height="1080" data-path="images/2.0-python-llm_request.gif" />
</Frame>

### Use cases

Get insight into individual LLM requests, including prompt templates, input, output, duration, token usage cost, and duration.

### Features

* Is model and framework agnostic
* Provides token usage, estimated cost, duration, input, and output
* Supports evaluation
* Supports annotation
* Supports user feedback
* Supports async functions

### Instruction

<Steps>
  <Step title="Install Baserun SDK">
    <CodeGroup>
      ```bash python theme={null}
      pip install baserun
      ```

      ```bash typescript theme={null}
      npm install baserun
      # or
      yarn add baserun
      ```
    </CodeGroup>
  </Step>

  <Step title="Generate an API key">
    Create an account at [https://app.baserun.ai/sign-up](https://app.baserun.ai/sign-up). Then generate an API key for your project in the [settings](https://app.baserun.ai/settings) tab. Set it as an environment variable:

    ```bash theme={null}
    export BASERUN_API_KEY="your_api_key_here"
    ```

    Alternatively set the Baserun API key when initializing the SDK

    <CodeGroup>
      ```python python theme={null}
      import baserun

      baserun.api_key = "br-..."

      baserun.init()
      ```

      ```typescript typescript theme={null}
      import { baserun } from "baserun";

      // init needs to be awaited. If top-level await is not available, wrap it in an async function,
      // but make sure it is called before instantiating OpenAI, Anthropic or Replicate
      await baserun.init({
        apiKey: "br-...",
      });
      ```
    </CodeGroup>
  </Step>

  <Step title="Initialize Baserun">
    At your application's startup, define the environment in which you'd like to run Baserun. You can use Baserun in the development environment while iterating on your features, utilizing it for debugging and analysis, or in the production environment to monitor your application.

    <CodeGroup>
      ```python python theme={null}
      import baserun

      baserun.init()

      ```

      ```typescript typescript theme={null}
      import { baserun } from "baserun";

      // in your main function

      // init needs to be awaited. If top-level await is not available, wrap it in an async function,
      // but make sure it is called before instantiating OpenAI, Anthropic or Replicate
      await baserun.init();
      ```
    </CodeGroup>
  </Step>
</Steps>

## Example

<CodeGroup>
  ```python python theme={null}
  import baserun
  import openai


  def example():
      response = openai.chat.completions.create(
          model="gpt-3.5-turbo",
          temperature=0.7,
          messages=[
              {
                  "role": "user",
                  "content": "What are three activities to do in Paris?"
              }
          ],
      )
      return response.choices[0].message.content


  if __name__ == "__main__":
      baserun.api_key = YOUR_BASERUN_API_KEY_HERE
      openai.api_key = YOUR_OPEANI_API_KEY_HERE

      baserun.init()
      print(example())

  ```

  ```typescript typescript theme={null}
  import { baserun } from "baserun";
  import OpenAI from "openai";

  async function main() {
    await baserun.init();

    const openai = new OpenAI();

    const chatCompletion = await openai.chat.completions.create({
      messages: [
        { role: "user", content: "What are three activities to do in Paris?" },
      ],
      model: "gpt-3.5-turbo",
    });

    // response
    console.log(chatCompletion.choices[0].message.content);
  }

  main();
  ```
</CodeGroup>

Congrats, you are done! Now, you can navigate to the monitoring tab. You should see your request logged in the monitoring tab.

## Demo projects

[Python example repo](https://github.com/baserun-ai/testing-agent/), <br />
[Typescript example repo](https://github.com/baserun-ai/testing-agent-js)

If you have any questions or feature requests, join our [Discord channel](https://discord.com/invite/xEPFsvSmkb) or send us an email at [hello@baserun.ai](mailto:hello@baserun.ai)
