Skip to main content
Valar exposes an OpenAI-compatible API, so you can use it with the Vercel AI SDK through the @ai-sdk/openai-compatible provider. Point the provider at Valar’s base URL and pass any Valar model id to generateText, streamText, and the rest of the SDK.

Set up the provider

1

Install the packages

Add the AI SDK core package and the OpenAI-compatible provider.
npm install ai @ai-sdk/openai-compatible
2

Set your API key

Store your key in the environment so it stays out of source control.
export VALAR_API_KEY="your-api-key"
3

Create the provider

Create a provider instance pointed at Valar’s base URL.
import { createOpenAICompatible } from '@ai-sdk/openai-compatible';

const valar = createOpenAICompatible({
  name: 'valar',
  baseURL: 'https://api.valarhq.ai/v1',
  apiKey: process.env.VALAR_API_KEY,
});
4

Generate text

Call generateText with a Valar model id. See Models for the full list.
import { generateText } from 'ai';

const { text } = await generateText({
  model: valar('moonshotai/Kimi-K2.6'),
  prompt: 'Explain the key ideas behind transformers.',
});

console.log(text);

Stream a response

Use streamText to receive tokens as they are produced.
import { streamText } from 'ai';

const { textStream } = streamText({
  model: valar('moonshotai/Kimi-K2.6'),
  prompt: 'Write a short note on retrieval-augmented generation.',
});

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

Call tools

The AI SDK’s tools option works against Valar through the same provider. Define each tool with a schema and a handler, and the model can call it during generation.
import { generateText, tool } from 'ai';
import { z } from 'zod';

const { text } = await generateText({
  model: valar('moonshotai/Kimi-K2.6'),
  prompt: 'What is the weather in Amsterdam?',
  tools: {
    getWeather: tool({
      description: 'Get the current weather for a city',
      parameters: z.object({
        city: z.string(),
      }),
      execute: async ({ city }) => {
        return { city, temperature: 18, conditions: 'cloudy' };
      },
    }),
  },
});

Choose a completion window

Valar offers two completion windows: Now (asap) for interactive turns, and Standard (standard) for cost-optimized work. Set one with the OpenAI-compatible provider’s request metadata, for example providerOptions: { 'valar': { completion_window: 'standard' } }. See Completion windows and Inference modes for details.