OpenRouter

Install

npm install @better-agent/openrouter

Agent model

import { defineAgent } from "@better-agent/core";
import { createOpenRouter } from "@better-agent/openrouter";

const openrouter = createOpenRouter({
  apiKey: process.env.OPENROUTER_API_KEY,
});

export const supportAgent = defineAgent({
  name: "support",
  model: openrouter("openai/gpt-5.5"),
  instruction: "You help customers.",
});

Configure

Use createOpenRouter for API keys, headers, or a custom OpenRouter-compatible base URL.

const openrouter = createOpenRouter({
  apiKey: process.env.OPENROUTER_API_KEY,
  headers: {
    "HTTP-Referer": "https://example.com",
  },
});

Hosted tools

OpenRouter does not expose a hosted tools helper on this provider wrapper. Use another provider wrapper when the agent needs tool calls.

Direct generation

Use generation models when a tool needs a focused model call without running an agent.

const text = openrouter.text("openai/gpt-5.5");

const draftReply = defineTool({
  name: "draft_reply",
  description: "Draft a customer reply.",
  inputSchema: z.object({
    issue: z.string(),
  }),
  execute: async ({ issue }) => {
    const result = await text.generate({
      input: `Draft a concise customer support reply for:\n\n${issue}`,
    });

    return { reply: result.text };
  },
});

Other generation helpers:

const embedding = openrouter.embedding("openai/text-embedding-3-small");
const image = openrouter.image("openai/gpt-image-1");

Model types

openrouter("openai/gpt-5.5") is an agent model for defineAgent. Agent models need text for messages and streaming. They can support more than text depending on the selected model.

openrouter.text(...), openrouter.embedding(...), and the other helpers are generation models for direct calls from app code or tools. They do not run the agent loop.

Provider options

Pass OpenRouter options at run time with the openrouter provider key.

await app.agent("support").run({
  messages,
  providerOptions: {
    openrouter: {
      reasoningEffort: "medium",
    },
  },
});

Capabilities

FeatureSupport
Agent modelYes
Text generationYes
StreamingYes
Structured outputNo
Tool callsNo
Hosted toolsNo wrapper helper
EmbeddingsYes
ImagesYes
AudioNo

Source: built on @ai-sdk/openai-compatible.