Skip to content
LLM Router
Integrations

OpenAI SDK

Using Edgee with OpenAI SDK

Edgee provides an OpenAI-compatible API, which means you can use the official OpenAI SDKs for TypeScript and Python with Edgee. This allows you to leverage Edgee's routing, observability, and cost tracking features without changing your existing code.

Why Use OpenAI SDK with Edgee?

  • 15-20% Cost Reduction: Automatic token compression when enabled via headers or console (50% on our SWE-bench Lite benchmark)
  • No Code Changes: Use your existing OpenAI SDK code as-is
  • Multi-Provider Access: Route to OpenAI, Anthropic, Google, and more through one API
  • Automatic Failover: Built-in reliability with fallback providers
  • Cost Tracking: Real-time visibility into token usage in the Edgee dashboard
  • Observability: Request tracing, compression metrics, and logging across all providers

Installation

Install the OpenAI SDK for your preferred language:

npm install openai

Configuration

Configure the OpenAI SDK to use Edgee's API endpoint:

TypeScript
import OpenAI from "openai";

const openai = new OpenAI({
  baseURL: "https://edgee.io/v1",
  apiKey: process.env.EDGEE_API_KEY, // Your Edgee API key
});

async function main() {
  const completion = await openai.chat.completions.create({
    model: "gpt-5.2",
    messages: [
      { role: "user", content: "What is the capital of France?" }
    ],
  });

  console.log(completion.choices[0].message.content);
}

main();

Token Usage Tracking

Access standard OpenAI token usage metrics in every response:

TypeScript
import OpenAI from "openai";

const openai = new OpenAI({
  baseURL: "https://edgee.io/v1",
  apiKey: process.env.EDGEE_API_KEY,
});

const completion = await openai.chat.completions.create({
  model: "gpt-5.2",
  messages: [
    { role: "user", content: "Summarize this long document..." }
  ],
});

console.log(completion.choices[0].message.content);
console.log(`Prompt tokens: ${completion.usage?.prompt_tokens}`);
console.log(`Completion tokens: ${completion.usage?.completion_tokens}`);
console.log(`Total tokens: ${completion.usage?.total_tokens}`);

Compression & Tags via Headers

When using the OpenAI SDK with Edgee, you can control token compression and add tags using HTTP headers:

Enabling Compression

TypeScript
import OpenAI from "openai";

const openai = new OpenAI({
  baseURL: "https://edgee.io/v1",
  apiKey: process.env.EDGEE_API_KEY,
  defaultHeaders: {
    "x-edgee-compression-tool-result-trimming": "true",
    "x-edgee-tags": "production,openai-sdk,user-123",
  },
});

Available Headers:

HeaderTypeDescription
x-edgee-compression-tool-result-trimmingtrue / false (also 1/0, on/off)Turn tool-result trimming on or off for this request
x-edgee-compression-tool-surface-reductiontrue / false (also 1/0, on/off)Turn MCP tool surface reduction on or off for this request. The threshold still comes from the API key
x-edgee-compression-brevitytrue / false (also 1/0, on/off)Turn output brevity on or off for this request
x-edgee-tagsstringComma-separated tags for analytics and filtering

Advanced Usage

Function Calling (Tools)

Edgee fully supports OpenAI's function calling interface:

TypeScript
import OpenAI from "openai";

const openai = new OpenAI({
  baseURL: "https://edgee.io/v1",
  apiKey: process.env.EDGEE_API_KEY,
});

async function main() {
  const completion = await openai.chat.completions.create({
    model: "gpt-5.2",
    messages: [
      { role: "user", content: "What's the weather in Paris?" }
    ],
    tools: [
      {
        type: "function",
        function: {
          name: "get_weather",
          description: "Get the current weather for a location",
          parameters: {
            type: "object",
            properties: {
              location: {
                type: "string",
                description: "City name",
              },
            },
            required: ["location"],
          },
        },
      },
    ],
    tool_choice: "auto",
  });

  if (completion.choices[0].message.tool_calls) {
    console.log("Tool calls:", completion.choices[0].message.tool_calls);
  } else {
    console.log("Response:", completion.choices[0].message.content);
  }
}

main();

Streaming Responses

Edgee supports streaming responses for real-time token delivery:

TypeScript
import OpenAI from "openai";

const openai = new OpenAI({
  baseURL: "https://edgee.io/v1",
  apiKey: process.env.EDGEE_API_KEY,
});

async function main() {
  const stream = await openai.chat.completions.create({
    model: "gpt-5.2",
    messages: [
      { role: "user", content: "Tell me a short story" }
    ],
    stream: true,
  });

  for await (const chunk of stream) {
    const content = chunk.choices[0]?.delta?.content || "";
    process.stdout.write(content);
  }
}

main();

Migration from OpenAI

If you're already using the OpenAI SDK, migrating to Edgee is straightforward:

  1. Change the base URL: Update baseURL from https://api.openai.com/v1 to https://edgee.io/v1
  2. Update API key: Use your Edgee API key instead of your OpenAI key
  3. That's it! Your existing code will work without any other changes

Verify the connection

Send one request from the configured client, then open Logs in the same Edgee organization. Check the served model, provider, and key. For a CLI-launched coding agent, also inspect its session report.

If the request is missing, check that you launched through Edgee or saved the client’s gateway URL and Gateway API key. If it appears with an error, inspect the error before changing settings. See Troubleshooting and Gateway errors.