Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

796 B
Raw Blame
import OpenAI, { type ClientOptions } from "openai";
import { getGatewayBaseUrl } from "@/components/networking";
import { getAuthHeaderName } from "@/lib/http/runtime";

type GatewayClientOptions = Pick<ClientOptions, "baseURL" | "defaultHeaders" | "fetch" | "maxRetries" | "timeout"> & {
  accessToken: string;
};

export function createGatewayClient({ accessToken, ...options }: GatewayClientOptions): OpenAI {
  const authHeader = getAuthHeaderName();
  const config: ClientOptions = {
    ...options,
    apiKey: accessToken,
    baseURL: options.baseURL || getGatewayBaseUrl(),
    dangerouslyAllowBrowser: true,
    defaultHeaders: {
      Authorization: null,
      [authHeader]: `Bearer ${accessToken}`,
      ...options.defaultHeaders,
    },
  };
  return new OpenAI.OpenAI(config);
}