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?
- Up to 50% Cost Reduction: Automatic token compression on every request
- Real-Time Savings: See exactly how many tokens and dollars you’ve saved
- 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 and costs
- Observability: Request tracing and logging across all providers
Installation
Install the OpenAI SDK for your preferred language:
Configuration
Configure the OpenAI SDK to use Edgee’s API endpoint:
import OpenAI from "openai";
const openai = new OpenAI({
baseURL: "https://api.edgee.ai/v1",
apiKey: process.env.EDGEE_API_KEY, // Your Edgee API key
});
async function main() {
const completion = await openai.chat.completions.create({
model: "gpt-4o",
messages: [
{ role: "user", content: "What is the capital of France?" }
],
});
console.log(completion.choices[0].message.content);
}
main();
Cost Tracking & Compression
Every response includes token compression and cost metrics through the standard OpenAI usage field:
import OpenAI from "openai";
const openai = new OpenAI({
baseURL: "https://api.edgee.ai/v1",
apiKey: process.env.EDGEE_API_KEY,
});
const completion = await openai.chat.completions.create({
model: "gpt-4o",
messages: [
{ role: "user", content: "Summarize this long document..." }
],
});
console.log(completion.choices[0].message.content);
// Access compression metrics (if compression was applied)
if (completion.compression) {
console.log(`Tokens saved: ${completion.compression.saved_tokens}`);
console.log(`Compression rate: ${(completion.compression.rate * 100).toFixed(1)}%`);
}
console.log(`Total tokens: ${completion.usage.total_tokens}`);
Edgee extends the OpenAI API response with a compression field containing compression metrics (input_tokens, saved_tokens, rate). All standard OpenAI fields remain unchanged.
Advanced Usage
Edgee fully supports OpenAI’s function calling interface:
import OpenAI from "openai";
const openai = new OpenAI({
baseURL: "https://api.edgee.ai/v1",
apiKey: process.env.EDGEE_API_KEY,
});
async function main() {
const completion = await openai.chat.completions.create({
model: "gpt-4o",
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();
You can add tags to your requests for analytics and filtering using the x-edgee-tags header:
import OpenAI from "openai";
const openai = new OpenAI({
baseURL: "https://api.edgee.ai/v1",
apiKey: process.env.EDGEE_API_KEY,
defaultHeaders: {
"x-edgee-tags": "production,user-123,summarization",
},
});
// All requests will include these tags
const completion = await openai.chat.completions.create({
model: "gpt-4o",
messages: [
{ role: "user", content: "What is the capital of France?" }
],
});
Tags are comma-separated strings in the header. They help you categorize and filter requests in Edgee’s analytics dashboard.
Streaming Responses
Edgee supports streaming responses for real-time token delivery:
import OpenAI from "openai";
const openai = new OpenAI({
baseURL: "https://api.edgee.ai/v1",
apiKey: process.env.EDGEE_API_KEY,
});
async function main() {
const stream = await openai.chat.completions.create({
model: "gpt-4o",
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:
- Change the base URL: Update
baseURL from https://api.openai.com/v1 to https://api.edgee.ai/v1
- Update API key: Use your Edgee API key instead of your OpenAI key
- That’s it! Your existing code will work without any other changes
All OpenAI SDK features are supported, including streaming, function calling, and response formatting. Edgee maintains full compatibility with the OpenAI API specification.
What’s Next?