> ## Documentation Index
> Fetch the complete documentation index at: https://docs.stophy.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Vercel AI SDK

> Give any AI SDK model live web search with Stophy: define a tool with zod, call the API in execute, and return the results as JSON.

## Setup

```bash theme={null}
npm install ai @ai-sdk/openai zod
```

Set `OPENAI_API_KEY` in your environment. `STOPHY_API_KEY` is optional for this example.

## The Stophy tool

This function calls Google search and returns the results as JSON, limited to 5 so the model's context stays small. It reads your key from `STOPHY_API_KEY`, and works without one.

```ts stophy.ts theme={null}
export async function stophySearch(query: string): Promise<string> {
  const headers: Record<string, string> = { "content-type": "application/json" };
  if (process.env.STOPHY_API_KEY) headers.authorization = `Bearer ${process.env.STOPHY_API_KEY}`;
  const response = await fetch("https://api.stophy.dev/v1/google/search", {
    method: "POST",
    headers,
    body: JSON.stringify({ query }),
  });
  const body = await response.json();
  if (!body.success) throw new Error(body.error.message);
  return JSON.stringify(body.data.results);
}
```

## Use it as a tool

Wrap the search in `tool()`. `stopWhen` lets the model search, read the results, and answer, in up to five steps.

```ts theme={null}
import { openai } from "@ai-sdk/openai";
import { generateText, stepCountIs, tool } from "ai";
import { z } from "zod";
import { stophySearch } from "./stophy";

const { text } = await generateText({
  model: openai("gpt-5-mini"),
  prompt: "What changed in the latest Bun release?",
  tools: {
    webSearch: tool({
      description: "Search the web. Returns the top results as JSON.",
      inputSchema: z.object({ query: z.string().describe("What to search for") }),
      execute: async ({ query }) => stophySearch(query),
    }),
  },
  stopWhen: stepCountIs(5),
});

console.log(text);
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.