bitsrun

Vercel AI SDK custom OpenAI endpoint for your model

Use a Vercel AI SDK custom OpenAI endpoint with createOpenAICompatible to call your own open model with generateText and streamText, billed per second.

Updated

On this page

This page shows how to use a custom OpenAI endpoint in the Vercel AI SDK, so your TypeScript app calls an open model you deployed on BitsRun. At the end you will generate text, stream it and create embeddings with the SDK's usual functions. It takes about 10 minutes once you have an endpoint.

Checked against the AI SDK docs on ai-sdk.dev on 2026-11-14.

What you get

Package@ai-sdk/openai-compatible with createOpenAICompatible
FunctionsgenerateText, streamText, embed
API usedChat Completions (/v1/chat/completions) and /v1/embeddings
Tool callsSupported, with a model trained for tools
Cost when idleNothing. The endpoint sleeps and wakes on the next request

Which provider should I use: openai-compatible or createOpenAI?

Use @ai-sdk/openai-compatible. The AI SDK's own OpenAI provider calls OpenAI's Responses API by default, and BitsRun answers the Chat Completions API. The OpenAI provider docs say that if your custom base URL only supports Chat Completions, you should create models with openai.chat('model-id') or use the OpenAI-compatible provider. The OpenAI-compatible provider is the simpler choice and is what this page uses.

Before you start

How to set up the Vercel AI SDK with a custom endpoint

  1. Install the packages:

    bash
    npm install ai @ai-sdk/openai-compatible
  2. Save your key in an environment variable (or your host's secret settings):

    bash
    export BITSRUN_API_KEY="brk_your_key"
  3. Create generate.mjs. This is the generateText example from the AI SDK docs, with our name, key, base URL and model:

    javascript
    import { createOpenAICompatible } from '@ai-sdk/openai-compatible';
    import { generateText } from 'ai';
    
    const provider = createOpenAICompatible({
      name: 'bitsrun',
      apiKey: process.env.BITSRUN_API_KEY,
      baseURL: 'https://<endpoint-id>.endpoint.bitsrun.com/v1',
    });
    
    const { text } = await generateText({
      model: provider('qwen3:8b'),
      prompt: 'Write a vegetarian lasagna recipe for 4 people.',
    });
    
    console.log(text);
  4. Run node generate.mjs. You should see the recipe printed after a few seconds (longer if the endpoint was asleep).

apiKey is sent as Authorization: Bearer <key>, which is what BitsRun expects. The model name is the one you deployed; an endpoint serves exactly one model and always uses it.

How to stream text with streamText

Streaming shows words as the model writes them, so people see a reply right away:

javascript
import { createOpenAICompatible } from '@ai-sdk/openai-compatible';
import { streamText } from 'ai';

const provider = createOpenAICompatible({
  name: 'bitsrun',
  apiKey: process.env.BITSRUN_API_KEY,
  baseURL: 'https://<endpoint-id>.endpoint.bitsrun.com/v1',
  includeUsage: true, // Include usage information in streaming responses
});

const { textStream } = streamText({
  model: provider('qwen3:8b'),
  prompt: 'Invent a new holiday and describe its traditions.',
});

for await (const textPart of textStream) {
  process.stdout.write(textPart);
}

How to create embeddings

Point a second provider at an embedding endpoint and use embeddingModel:

javascript
import { createOpenAICompatible } from '@ai-sdk/openai-compatible';
import { embed } from 'ai';

const provider = createOpenAICompatible({
  name: 'bitsrun',
  apiKey: process.env.BITSRUN_API_KEY,
  baseURL: 'https://<embedding-endpoint-id>.endpoint.bitsrun.com/v1',
});

const { embedding } = await embed({
  model: provider.embeddingModel('snowflake-arctic-embed-l-v2.0'),
  value: 'The quick brown fox jumps over the lazy dog',
});

See Self-hosted embeddings API for choosing and deploying an embedding model.

What happens on the first request after the endpoint sleeps

A sleeping endpoint wakes on the next request, and the wait is never billed.

Model kindWarm GPU (ran recently)New GPU machine
GGUF models on llama.cppA few seconds to about 40 secondsAbout 2 to 3 minutes
Embedding models on Text Embeddings InferenceA few seconds to about 40 secondsAbout 2 to 3 minutes
Safetensors chat models on vLLMAbout 40 secondsAbout 3 to 4 minutes

BitsRun keeps the request open for up to 8 minutes and sends keep-alive data while it waits. If you use the timeout option, give it at least 10 minutes (timeout: 600000), and do not set short timeout.firstChunkMs or timeout.chunkMs values on streamText: keep-alive comments are not content, so a short value would abort during a cold start. Your hosting platform's function time limit matters too. If it is shorter than a cold start, wake the endpoint first with Start on the endpoint card. More on Cold starts.

If something goes wrong

Message or symptomWhat to do
401 unauthorizedBITSRUN_API_KEY is missing, mistyped or revoked. Check the variable in the environment that runs the code.
404 not_foundCheck baseURL ends in /v1. Chat calls on an embedding endpoint, or a key limited to another endpoint, also get 404.
402 payment_requiredYour balance is $0. Add credit in the dashboard.
429 rate_limitedToo many requests open at once for your plan. Retry with a short, growing pause.
Error body with model_startingThe model took longer than 8 minutes to wake. Send the request again.
Request aborted during a cold startYour timeout or the platform's function limit is too short. Raise it or start the endpoint first.

Every code is on Error codes.

Frequently asked questions

Does the Vercel AI SDK work with self-hosted models?

Yes. Any server that speaks the OpenAI Chat Completions API works with @ai-sdk/openai-compatible. Deploy the model on BitsRun and pass its base URL and brk_ key to createOpenAICompatible.

Do tool calls work with the AI SDK and BitsRun?

Yes, with a model trained for tools. BitsRun passes tools, tool_choice and parallel_tool_calls; check your model with bitsrun size <model> --json and look for "tools": true.

Can I use createOpenAI with a baseURL instead?

Yes, but call openai.chat('model-id') so it uses Chat Completions. The default openai('model-id') uses the Responses API, which BitsRun does not answer.

Can I send images to my model?

Not yet. BitsRun serves text-only models today, so send text prompts only.

Next step

Estimate what your app will cost with the cost calculator, or bring your own weights with Host a GGUF model as an OpenAI-compatible API. Then create your account.

Try it on your own model

Sign up with your email, add $10 of credit and deploy from an Ollama name or a Hugging Face repo. An L4 costs $0.95 an hour, billed per second only while your model is awake. Waking up is never billed, and credit you buy does not expire.