SDKs & libraries

Using the AI SDK

Generate text, stream responses, and call tools with the Vercel AI SDK and Halfbill

Halfbill works with the Vercel AI SDK through the provider package @llmgateway/ai-sdk-provider. One provider instance and one API key reach every Claude and GPT model in the catalog.

Install

pnpm add ai @llmgateway/ai-sdk-provider

Set your API key (create one from the dashboard):

export HALFBILL_API_KEY=llmgtwy_your_key_here

Generate text

import { createLLMGateway } from "@llmgateway/ai-sdk-provider";
import { generateText } from "ai";

const llmgateway = createLLMGateway({
	apiKey: process.env.HALFBILL_API_KEY,
});

const { text } = await generateText({
	model: llmgateway("gpt-5.5"),
	prompt: "Hello!",
});

Switching models is a one-line change — the same provider serves every model:

const { text } = await generateText({
	model: llmgateway("claude-sonnet-5"),
	prompt: "Hello!",
});

Model IDs

Use the canonical model IDs from the models page — for example gpt-5.5, claude-sonnet-5, or claude-opus-5.

Stream responses

import { createLLMGateway } from "@llmgateway/ai-sdk-provider";
import { streamText } from "ai";

const llmgateway = createLLMGateway({
	apiKey: process.env.HALFBILL_API_KEY,
});

const { textStream } = await streamText({
	model: llmgateway("claude-sonnet-5"),
	prompt: "Write a poem about coding",
});

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

Next.js route handler

// app/api/chat/route.ts
import { createLLMGateway } from "@llmgateway/ai-sdk-provider";
import { streamText } from "ai";

const llmgateway = createLLMGateway({
	apiKey: process.env.HALFBILL_API_KEY,
});

export async function POST(req: Request) {
	const { messages } = await req.json();

	const result = await streamText({
		model: llmgateway("gpt-5.5"),
		messages,
	});

	return result.toDataStreamResponse();
}

Tool calling

import { createLLMGateway } from "@llmgateway/ai-sdk-provider";
import { generateText, tool } from "ai";
import { z } from "zod";

const llmgateway = createLLMGateway({
	apiKey: process.env.HALFBILL_API_KEY,
});

const { text, toolResults } = await generateText({
	model: llmgateway("gpt-5.5"),
	tools: {
		weather: tool({
			description: "Get the weather for a location",
			parameters: z.object({
				location: z.string(),
			}),
			execute: async ({ location }) => {
				return { temperature: 72, condition: "sunny" };
			},
		}),
	},
	prompt: "What's the weather in San Francisco?",
});

Without the provider package

If you prefer not to add a dependency, point @ai-sdk/openai at Halfbill with a custom base URL:

import { createOpenAI } from "@ai-sdk/openai";
import { generateText } from "ai";

const llmgateway = createOpenAI({
	baseURL: "https://api.halfbill.uk/v1",
	apiKey: process.env.HALFBILL_API_KEY,
});

const { text } = await generateText({
	model: llmgateway("gpt-5.5"),
	prompt: "Hello!",
});

Every request made through the AI SDK shows up in your Activity and Usage & Metrics dashboards like any other API request — with per-request cost, tokens, and latency.

Next steps

How is this guide?

On this page