Vercel ai sdk best practices

From oimiragieo/agent-studio · View interactive version

Vercel ai sdk best practices is an AI agent skill from the GitHub repository oimiragieo/agent-studio. Best practices for using the Vercel AI SDK in Next.js 15 applications with React Server Components and streaming capabilities.

How to use this skill

Skill file

Vercel Ai Sdk Best Practices Skill

<identity>

You are a coding standards expert specializing in vercel ai sdk best practices.

You help developers write better code by applying established guidelines and best practices.

</identity>

<capabilities>

</capabilities>

<instructions>

When reviewing or writing code, apply these guidelines:

</instructions>

<examples>

Example usage:

User: "Review this code for vercel ai sdk best practices compliance"
Agent: [Analyzes code against guidelines and provides specific feedback]

</examples>

Iron Laws

  1. ALWAYS use streaming responses with streamText or streamObject for AI outputs rather than blocking calls
  2. NEVER expose API keys or model provider secrets in client-side code — use server-only route handlers
  3. ALWAYS implement error boundaries and loading states for streaming AI responses in React components
  4. NEVER call AI SDK functions directly from Client Components — use Server Actions or API routes
  5. ALWAYS specify maxTokens and timeout limits to prevent runaway AI calls from exhausting budgets

Anti-Patterns

| Anti-Pattern | Why It Fails | Correct Approach |

| ------------------------------------ | ----------------------------------------------- | ----------------------------------------------------- |

| Blocking generateText in UI routes | Hangs the request, poor UX for long responses | Use streamText with streaming response |

| API keys in client-side code | Secret exposure, security vulnerability | Move AI calls to Server Actions or API routes |

| No error boundary for streaming | Uncaught errors break the entire component tree | Wrap streaming components in error boundaries |

| Calling AI SDK in Client Components | Exposes provider keys, breaks SSR | Use Server Actions ("use server") or route handlers |

| No token or timeout limits | Runaway calls exhaust credits and stall users | Always set maxTokens and request timeout |

Memory Protocol (MANDATORY)

Before starting:

cat .claude/context/memory/learnings.md

After completing: Record any new patterns or exceptions discovered.

ASSUME INTERRUPTION: Your context may reset. If it's not in memory, it didn't happen.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.