> ## 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 markdown.

## 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 web search and returns the results as markdown, which is short and easy for a model to read. It reads your key from `STOPHY_API_KEY`.

```ts stophy.ts theme={null}
export async function stophySearch(query: string): Promise<string> {
  const headers: Record<string, string> = {
    "content-type": "application/json",
    accept: "text/markdown",
  };
  headers.authorization = `Bearer ${process.env.STOPHY_API_KEY}`;
  const response = await fetch("https://api.stophy.dev/v1/web/search", {
    method: "POST",
    headers,
    body: JSON.stringify({ query, limit: 5 }),
  });
  return response.text();
}
```

## 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 markdown.",
      inputSchema: z.object({ query: z.string().describe("What to search for") }),
      execute: async ({ query }) => stophySearch(query),
    }),
  },
  stopWhen: stepCountIs(5),
});

console.log(text);
```
