Use the SDK for chat, streaming, function calling, and embeddings — with both OpenAI and Anthropic protocols.

OpenAI Chat Completions

rb.openai() gives you a fully typed OpenAI SDK instance. Use it exactly as you would the official OpenAI client — all requests go through the gateway.

Basic chat

import { RouterBrain } from '@router-brain/sdk';

const rb = new RouterBrain('sk-xxx');

const res = await rb.openai().chat.completions.create({
  model: 'gpt-4o',
  messages: [
    { role: 'system', content: 'You are a helpful assistant' },
    { role: 'user', content: 'What is the weather like in Tokyo?' },
  ],
});
console.log(res.choices[0].message.content);

Streaming

const stream = await rb.openai().chat.completions.create({
  model: 'gpt-4o',
  messages: [{ role: 'user', content: 'Tell me a story' }],
  stream: true,
});
for await (const chunk of stream) {
  process.stdout.write(chunk.choices[0]?.delta?.content || '');
}

Function calling

const res = await rb.openai().chat.completions.create({
  model: 'gpt-4o',
  messages: [{ role: 'user', content: 'What is the weather in Beijing?' }],
  tools: [{
    type: 'function',
    function: {
      name: 'get_weather',
      parameters: {
        type: 'object',
        properties: { city: { type: 'string' } },
      },
    },
  }],
});

Embeddings

const res = await rb.openai().embeddings.create({
  model: 'text-embedding-3-small',
  input: 'Text to embed',
});
console.log(res.data[0].embedding); // number[]

Responses API

const res = await rb.openai().responses.create({
  model: 'gpt-4o',
  input: 'Write a poem about autumn',
});
console.log(res.output_text);

rb.openai() returns the full OpenAI SDK instance. Every OpenAI method is available.

Anthropic Messages

rb.anthropic() gives you a fully typed Anthropic SDK instance. All requests go through the gateway.

Basic message

const res = await rb.anthropic().messages.create({
  model: 'claude-sonnet-4-20250514',
  max_tokens: 1024,
  messages: [{ role: 'user', content: 'Explain quantum computing in simple terms' }],
});
console.log(res.content[0].text);

Streaming

const stream = rb.anthropic().messages.stream({
  model: 'claude-sonnet-4-20250514',
  max_tokens: 1024,
  messages: [{ role: 'user', content: 'Tell me a long sci-fi story' }],
}).on('text', (text) => {
  process.stdout.write(text);
});

await stream.finalMessage();

With a system prompt

const res = await rb.anthropic().messages.create({
  model: 'claude-sonnet-4-20250514',
  max_tokens: 1024,
  system: 'You are a senior Python engineer. Output code only.',
  messages: [{ role: 'user', content: 'Write a quicksort implementation' }],
});

rb.anthropic() returns the full Anthropic SDK instance. Every Anthropic method is available.

See also

Streaming responses · Error responses · Error handling