Integrate Your Existing OpenAI App

5-Min Integration Guide

Quickly integrate your existing OpenAI features by using Klu's AI Gateway as the Base URL for your OpenAI projects. The AI Gateway, which runs on Cloudflare Workers with end-to-end SSL encryption, enables the fastest generation times possible and connects to Klu for app insights and data management.

To integrate Klu, you will need to:

  1. Get your Klu API Key
  2. Create an Action
  3. Update Your Code

Let's get started.


Step 01 → Your Klu API Key

You can grab your API Key during onboarding or by visiting the settings section in your Workspace. Copy your default key or create a specific one for your application.

Klu Platform API Key


Step 02 → Create an Action

Keep your prompts and individual metrics neatly organized using Klu Actions. Input your prompt and save your Action. This will help you keep track of versions, filter data, and run evaluations laters. After saving, copy the Action GUID, you'll need this later. Repeat this step for each prompt you want to optimize.


Step 03 → Update Your Code

Finally, you need to modify the Base URL used by the OpenAI SDK and include two new default headers X-Klu-Api-Key and X-Action-Guid. We recommend adding your OpenAI and Klu API keys to your env secrets, and add the associated Action GUIDs for your prompt(s).

Developers with many Actions opt to define the GUID locally in the code instead of including it as a secret.

OPENAI_API_KEY="sk-AbC123XyZ456DeF789GhI012JkL345MnO678PqR901"
KLU_API_KEY="TwHmpvWXG6+EyxPgCjuwIXfB7nR103Dbb3xpWmKehSQ="
PROMPT01="065ca948-c752-41e7-8fca-1167c5c38c84"

Now, you simply need to update your code to the new Base URL and extra Klu headers for the request. Here are two examples for the Node.js and Python OpenAI SDKs.

const openai = new OpenAI({
    apiKey: process.env.OPENAI_API_KEY, 
    baseURL: "https://gateway.optimizedata.app/v1",  
    defaultHeaders: { 
        "x-klu-api-key": process.env.KLU_API_KEY, 
        "x-action-duid": process.env.PROMPT01, 
    },
});

Congratulations! You have successfully integrated your app into Klu. Now, you can utilize all the observability and optimization features provided by the Klu platform.