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.

Logging LLM requests

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

1

Install Baserun SDK

pip install baserun
2

Generate an API key

Create an account at https://app.baserun.ai/sign-up. Then generate an API key for your project in the settings tab. Set it as an environment variable:

export BASERUN_API_KEY="your_api_key_here"

Alternatively set the Baserun API key when initializing the SDK

import baserun

baserun.api_key = "br-..."

baserun.init()
3

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.

import baserun

baserun.init()

Example

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())

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,
Typescript example repo

If you have any questions or feature requests, join our Discord channel or send us an email at hello@baserun.ai