Open History to see the last commit
1import { getApiKeyFromEnv } from '@tanstack/ai-utils'
2import type { ClientOptions } from 'openai'
3
4export interface LLMGatewayClientConfig extends Omit<ClientOptions, 'apiKey'> {
5 apiKey: string
6}
7
8/**
9 * Gets the LLM Gateway API key from environment variables
10 * @throws Error if LLM_GATEWAY_API_KEY is not found
11 */
12export function getLLMGatewayApiKeyFromEnv(): string {
13 try {
14 return getApiKeyFromEnv('LLM_GATEWAY_API_KEY')
15 } catch (cause) {
16 throw new Error(
17 'LLM_GATEWAY_API_KEY is required. Please set it in your environment variables or use the factory function with an explicit API key.',
18 { cause },
19 )
20 }
21}
22
23/**
24 * Returns an LLM Gateway client config with the gateway's OpenAI-compatible
25 * base URL applied when not already set. LLM Gateway accepts the OpenAI SDK
26 * verbatim, so the adapter drives it via the OpenAI SDK with this baseURL.
27 * Point `baseURL` at your own deployment when self-hosting.
28 */
29export function withLLMGatewayDefaults(
30 config: LLMGatewayClientConfig,
31): LLMGatewayClientConfig {
32 return {
33 ...config,
34 baseURL: config.baseURL || 'https://api.llmgateway.io/v1',
35 }
36}