Home › LLM apps & agents › Vercel AI SDK rules

Vercel AI SDK rules

Cursor rules for the Vercel AI SDK 5 in TypeScript: streamText, structured output, tools, and useChat UIs.

CursorLLM apps & agents

How to use

Save this as .cursor/rules/ai-sdk.mdc in your repository root, commit it, and your agent will pick it up automatically. Edit the commands to match your project.

.cursor/rules/ai-sdk.mdcDownload  
---
description: Vercel AI SDK conventions
globs: ["app/**/*.{ts,tsx}","src/**/*.{ts,tsx}","lib/**/*.ts"]
alwaysApply: false
---

# Vercel AI SDK (v5)

- Server calls use `ai` core functions: `streamText` for chat, `generateText` for one-shot, `generateObject`/`streamObject` with a zod schema for structured data.
- Models come from provider packages (`@ai-sdk/openai`, `@ai-sdk/anthropic`, ...) or the project's gateway config. Keep model ids in one config module.
- Route handlers return `result.toUIMessageStreamResponse()` for `useChat`. Convert incoming UI messages with `convertToModelMessages(messages)`.
- Client: `useChat` from `@ai-sdk/react` with a transport; render `message.parts` (text, tool calls, reasoning) rather than a single `content` string. Manage the input with your own `useState`.
- Tools: `tool({ description, inputSchema: z.object(...), execute })`. Bound multi-step loops with `stopWhen: stepCountIs(n)`.
- Tools that change data or cost money need user confirmation (no `execute` on the server; handle approval in the UI).
- Set `maxOutputTokens`, handle `onError`, and pass `abortSignal` so cancelled requests stop billing.
- Log usage from `onFinish` (`usage`, model) for cost tracking.
- Keys only on the server (`OPENAI_API_KEY` etc.); never call providers from client components.

## Don't
- Don't use v4 names (`maxTokens`, `parameters` in tools, `toDataStreamResponse`, `message.content`) in new code.

## Done when
Typecheck passes and tests use `MockLanguageModelV2` from `ai/test` instead of real API calls.

More: all LLM apps & agents rules · all Cursor rules

Related rules

Claude Agent SDK CLAUDE.md

CLAUDE.md for building agents with the Claude Agent SDK (Python/TypeScript): options, tools, permissions, and hooks.

Claude Code (CLAUDE.md)LLM apps & agents

CrewAI multi-agent AGENTS.md

AGENTS.md for CrewAI 1.x projects: YAML agents and tasks, crews and flows, tools, and cost-aware runs.

AGENTS.mdLLM apps & agents

LangGraph agent rules

Cursor rules for building agents with LangGraph in Python: typed state, nodes, tools, checkpointers, and tests.

CursorLLM apps & agents

LLM app (Python) AGENTS.md

AGENTS.md for Python apps that call LLM APIs (OpenAI, Anthropic, Gemini): prompts, structured output, evals, and cost.

AGENTS.mdLLM apps & agents

OpenAI Agents SDK rules

Cursor rules for the OpenAI Agents SDK (Python 0.2x): agents, function tools, handoffs, guardrails, sessions, and tracing.

CursorLLM apps & agents

Writing LLM prompts in code rules

Cursor rules for writing and maintaining prompts inside applications: structure, variables, structured output, and evals.

CursorLLM apps & agents

Get new rules weekly

One email a week: fresh rule files, agent tips, and what's changing in Cursor, Claude Code and Copilot. Free, unsubscribe anytime.

Opens the Rulestack Newsletter on beehiiv in a new tab. Or subscribe directly on beehiiv.