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

# AI

> Multi-model AI gateway and agents with the Vercel AI SDK.

`@repo/ai` connects your app to multiple AI providers through the [Vercel AI SDK](https://ai-sdk.dev/docs/introduction) and [AI Gateway](https://vercel.com/docs/ai-gateway). It includes model routing, agent creation, prompt management, and cost tracking.

## Usage

Create a chat agent with tools:

```ts title="packages/ai/agent/chat.ts" theme={null}
import { createChatAgent, tool, z } from "@repo/ai/agent";

const agent = createChatAgent({ token }, {
  weather: tool({
    description: "Get the weather for a location",
    parameters: z.object({ city: z.string() }),
    execute: async ({ city }) => `72°F in ${city}`,
  }),
});
```

## Gateway

The gateway routes requests to Anthropic, OpenAI, Google, and other providers through a single API:

```ts title="packages/ai/gateway/index.ts" theme={null}
import { gateway } from "@repo/ai/gateway";
import { generateText } from "ai";

const { text } = await generateText({
  model: gateway("anthropic/claude-sonnet-4.6"),
  prompt: "Explain monorepos in one sentence.",
});
```

## Models

Pre-configured model constants and provider options:

```ts title="packages/ai/models/index.ts" theme={null}
import { CLAUDE_SONNET, GEMINI_FLASH, CHAT_MODELS } from "@repo/ai/models";
```

Available models include Claude Sonnet 4.6, Claude Haiku 4.5, GPT-4o, GPT-4o Mini, Gemini 3 Flash, and Gemini 3 Pro. Each model has thinking/reasoning options pre-configured for its provider.

## Prompts

Shared system prompts for chat and title generation:

```ts title="packages/backend/convex/chat/api/title.ts" theme={null}
import { SYSTEM_PROMPT } from "@repo/ai/prompts";
import { generateChatTitle } from "@repo/ai/prompts/title";

const title = await generateChatTitle("How do I deploy to Vercel?");
```

## Web Search

Add Perplexity-powered web search as a tool (requires `PERPLEXITY_API_KEY`):

```ts title="packages/ai/tools/search.ts" theme={null}
import { getSearchTool } from "@repo/ai/tools/search";

const search = getSearchTool();
```

## Environment Variables

| Variable             | Description                                                      |
| -------------------- | ---------------------------------------------------------------- |
| `AI_GATEWAY_API_KEY` | Vercel AI Gateway API key                                        |
| `AI_GATEWAY_URL`     | Gateway endpoint (defaults to `https://ai-gateway.vercel.sh/v3`) |
| `PERPLEXITY_API_KEY` | Perplexity API key for web search (optional)                     |

## Learn More

* [Vercel AI SDK documentation](https://ai-sdk.dev/docs/)
* [AI Gateway documentation](https://vercel.com/docs/ai-gateway)
