# Vercel AI SDK

Turn OrcaRouter tools into AI SDK tools with jsonSchema() and let generateText call them over several steps.

|  |  |
| --- | --- |
| Kind | framework |
| Connects through | functions |
| Docs | <https://ai-sdk.dev/docs/ai-sdk-core/tools-and-tool-calling> |

## Install

1. Install the packages: `npm install ai @ai-sdk/openai`.
2. Set `ORCAROUTER_API_KEY` to an API key from your OrcaRouter console.
3. Build a tool set for a task with the helper below and pass it to `generateText` or `streamText`.

## Code

### Load OrcaRouter tools

```typescript
// orcarouter-tools.ts
import { jsonSchema, tool, type ToolSet } from "ai";

const BASE = "https://api.orcarouter.ai";
const headers = {
  Authorization: `Bearer ${process.env.ORCAROUTER_API_KEY}`,
  "Content-Type": "application/json",
};

export async function orcarouterTools(query: string, limit = 5): Promise<ToolSet> {
  const found = await fetch(`${BASE}/v1/tools/discover`, {
    method: "POST",
    headers,
    body: JSON.stringify({ q: query, limit }),
  }).then((res) => res.json());
  const ids = found.data.results.map((t: { id: string }) => t.id).join(",");
  if (!ids) return {};
  const fns = await fetch(
    `${BASE}/v1/tools/functions?format=openai&ids=${encodeURIComponent(ids)}`,
    { headers },
  ).then((res) => res.json());

  const tools: ToolSet = {};
  for (const { function: fn } of fns.data.tools) {
    tools[fn.name] = tool({
      description: fn.description,
      inputSchema: jsonSchema(fn.parameters),
      execute: async (input) => {
        const res = await fetch(`${BASE}/v1/tools/${fn.name}/run`, {
          method: "POST",
          headers,
          body: JSON.stringify({ input }),
        });
        return res.json();
      },
    });
  }
  return tools;
}
```

### Call them from generateText

```typescript
import { createOpenAI } from "@ai-sdk/openai";
import { generateText, stepCountIs } from "ai";
import { orcarouterTools } from "./orcarouter-tools";

const orcarouter = createOpenAI({
  baseURL: "https://api.orcarouter.ai/v1",
  apiKey: process.env.ORCAROUTER_API_KEY,
});

const { text } = await generateText({
  model: orcarouter.chat("openai/gpt-5-mini"),
  tools: await orcarouterTools("tiktok user profile"),
  stopWhen: stepCountIs(5),
  prompt: "How many followers does @nasa have on TikTok?",
});
console.log(text);
```

## Recipes

- [Track competitor TikTok accounts](https://www.orcarouter.ai/tools/recipes/track-competitor-tiktok-accounts.md): Pull each competitor's TikTok profile, find its latest videos and read what viewers say, in three tool calls an agent can repeat every week.
- [Monitor brand mentions on the web and TikTok](https://www.orcarouter.ai/tools/recipes/monitor-brand-mentions.md): Search the web and TikTok for your brand, then read the comments under the posts that mention it: a daily brand check an agent can run on its own.
- [Enrich a lead list](https://www.orcarouter.ai/tools/recipes/enrich-a-lead-list.md): Turn a list of company domains into firmographics, a contact's work email and a recent news hook, with one API key.

## Related integrations

- [Mastra](https://www.orcarouter.ai/tools/integrations/mastra.md): Connect a Mastra agent to the OrcaRouter MCP server with MCPClient, so it can search the tool catalog and run what it finds.
- [LangChain](https://www.orcarouter.ai/tools/integrations/langchain.md): Load OrcaRouter tools as LangChain tools from the functions export and give them to any LangChain agent; each call runs the tool through OrcaRouter.
- [OpenAI Agents SDK](https://www.orcarouter.ai/tools/integrations/openai-agents-sdk.md): Wrap each OrcaRouter tool in a FunctionTool with its own JSON schema and run it from an Agents SDK agent, with the model served through OrcaRouter too.

---

Markdown version of https://www.orcarouter.ai/tools/integrations/vercel-ai-sdk
