Skip to main content
@repo/ai connects your app to multiple AI providers through the Vercel AI SDK and AI Gateway. It includes model routing, agent creation, prompt management, RAG pipelines, and cost tracking.

Usage

Create a chat agent with tools:
packages/backend/convex/chat/streaming.ts
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:
apps/app/api/chat/route.ts
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:
packages/backend/convex/chat/streaming.ts
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, title generation, and RAG:
packages/backend/convex/chat/streaming.ts
import { SYSTEM_PROMPT } from "@repo/ai/prompts";
import { generateChatTitle } from "@repo/ai/prompts/title";

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

RAG

Build retrieval-augmented generation pipelines with query preprocessing, retrieval, and reranking:
packages/backend/convex/rag/ragAsTools.ts
import { createPipeline } from "@repo/ai/rag";

const pipeline = createPipeline({
  stages: ["rewrite", "stepback"],
  retriever: myRetriever,
});

const results = await pipeline.run("How does auth work?");
Add Perplexity-powered web search as a tool (requires PERPLEXITY_API_KEY):
packages/backend/convex/agents/weather.ts
import { getSearchTool } from "@repo/ai/tools/search";

const search = getSearchTool();

Environment Variables

VariableDescription
AI_GATEWAY_API_KEYVercel AI Gateway API key
AI_GATEWAY_URLGateway endpoint (defaults to https://ai-gateway.vercel.sh/v3)
COHERE_API_KEYCohere API key for reranking (optional)
PERPLEXITY_API_KEYPerplexity API key for web search (optional)

Learn More