Raw OpenAI-Compatible Fetch
If you already have an OpenAI-compatible HTTP client or a custom fetch wrapper, @vel4ai/ai-sdk is not required. Use the URL and headers returned by prepared execution to call /chat/completions.
Non-Streaming
Section titled “Non-Streaming”import { createOpenAiCompatibleUsageReportInput } from "@vel4ai/sdk";
declare const prepared: import("@vel4ai/sdk").PreparedExecutionAllowed;declare const messages: Array<{ role: "user"; content: string }>;
export async function callRelay() { const response = await fetch(prepared.openAiCompatibleChatCompletionsUrl, { method: "POST", headers: prepared.openAiCompatibleRequestHeaders, body: JSON.stringify({ model: prepared.openAiCompatibleModel, messages, }), }); const body = (await response.json()) as { usage?: unknown };
if (!response.ok) { await prepared.reportFailed({ errorMessage: JSON.stringify(body) }); return body; }
await prepared.reportCompletedWithUsage( createOpenAiCompatibleUsageReportInput(body.usage), );
return body;}Streaming
Section titled “Streaming”For streaming, pass usage to the completion report only when it can be collected while returning the provider response. Even when usage cannot be collected, still send reportCompleted().
declare const prepared: import("@vel4ai/sdk").PreparedExecutionAllowed;
export async function proxyRelayStream(messages: unknown[]) { const reporter = prepared.createResultReporter(); const response = await fetch(prepared.openAiCompatibleChatCompletionsUrl, { method: "POST", headers: prepared.openAiCompatibleRequestHeaders, body: JSON.stringify({ model: prepared.openAiCompatibleModel, messages, stream: true, }), });
if (!response.ok) { await reporter.tryReportFailed({ errorMessage: `relay status ${response.status}` }); return response; }
await reporter.tryReportCompleted(); return response;}