home / skills / blockmatic-icebox / basilic-old / ai-sdk-core-v6

ai-sdk-core-v6 skill

/.cursor/skills/ai-sdk-core-v6

This skill helps you build backend AI using Vercel AI SDK v5/v6, handling agents, tools, and structured outputs efficiently.

npx playbooks add skill blockmatic-icebox/basilic-old --skill ai-sdk-core-v6

Review the files below or copy the command above to add this skill to your agents.

Files (6)
SKILL.md
3.5 KB
---
name: AI SDK v6 Core
description: |
  Build backend AI with Vercel AI SDK v5/v6. Agent abstraction, tool approval, error solutions.
  
  Use when: implementing AI SDK v5/v6 or troubleshooting AI errors.
---

# Skill: ai-sdk-core

## Scope

- Applies to: Backend AI features with Vercel AI SDK v5/v6, server-side text generation, structured outputs, AI agents with tools, multi-provider integration
- Does NOT cover: React chat UIs (see [ai-sdk-ui](@cursor/skills/ai-sdk-ui-v6/SKILL.md)), frontend hooks, embeddings, image generation

## Assumptions

- AI SDK v5.0.98+ (stable) or v6.0.0-beta.107+ (beta)
- Node.js 18+ or Cloudflare Workers
- Zod v3.23.8+ or v4.x for schema validation
- TypeScript v5+ with strict mode

## Principles

- Use `generateText` for simple text generation
- Use `generateObject` for structured outputs with Zod schemas
- Use `streamText` for streaming responses
- Use `ToolLoopAgent` (v6) for agent workflows with tool calling
- Use tool execution approval for human-in-the-loop patterns
- Use `callOptions` for dynamic runtime configuration
- Handle errors with specific error types (`AI_APICallError`, `AI_NoObjectGeneratedError`, etc.)
- Use provider abstraction (`openai`, `anthropic`, `google`) for multi-provider support

## Constraints

### MUST

- Handle `response.error` for typed error responses
- Use Zod schemas for structured output validation
- Use `onError` callback in `streamText` for error handling
- Validate API keys at startup

### SHOULD

- Use `ToolLoopAgent` (v6) for agent workflows instead of manual tool orchestration
- Implement retry logic with exponential backoff for rate limits
- Use `mode: 'json'` when available for structured outputs
- Prefer GPT-4+ for complex structured output

### AVOID

- Top-level imports in Cloudflare Workers (causes startup overhead)
- Skipping error handling for API calls
- Using `any` types (use proper error types)
- Mixing v5 and v6 APIs without migration

## Interactions

- Complements [ai-sdk-ui](@cursor/skills/ai-sdk-ui-v6/SKILL.md) for React chat interfaces
- Works with [nextjs](@cursor/skills/nextjs-v16/SKILL.md) Server Components and API routes
- Uses Zod for schema validation (see [typescript](@cursor/skills/typescript-v5/SKILL.md))

## Patterns

### Basic Text Generation

```typescript
import { generateText } from 'ai'
import { openai } from '@ai-sdk/openai'

const result = await generateText({
  model: openai('gpt-4'),
  prompt: 'Hello, world!',
})
```

### Structured Output

```typescript
import { generateObject } from 'ai'
import { z } from 'zod'

const result = await generateObject({
  model: openai('gpt-4'),
  schema: z.object({
    name: z.string(),
    age: z.number(),
  }),
  prompt: 'Generate a person',
})
```

### Agent with Tools (v6)

```typescript
import { ToolLoopAgent } from 'ai'
import { openai } from '@ai-sdk/openai'

const agent = new ToolLoopAgent({
  model: openai('gpt-4'),
  tools: { /* tool definitions */ },
})

const result = await agent.run({ prompt: 'Task' })
```

See [Production Patterns](references/production-patterns.md) and [Templates](templates/) for detailed examples.

## References

- [Production Patterns](references/production-patterns.md) - Deployment and optimization patterns
- [Top Errors](references/top-errors.md) - Common error solutions and troubleshooting

## Resources

- [AI SDK Docs](https://ai-sdk.dev/docs/ai-sdk-core/overview)
- [AI SDK 6 Beta](https://ai-sdk.dev/docs/announcing-ai-sdk-6-beta)
- [Error Reference](https://ai-sdk.dev/docs/reference/ai-sdk-errors)

Overview

This skill shows how to build and troubleshoot backend AI using the Vercel AI SDK v5/v6. It focuses on server-side text generation, structured outputs with Zod, and agent workflows with tools and multi-provider support. Practical patterns, error handling, and runtime configuration guidance are included for reliable production use.

How this skill works

The skill explains core APIs: generateText for plain text, generateObject for Zod-validated structured outputs, and streamText for streaming responses. It covers ToolLoopAgent (v6) for agent-driven tool calling and tool approval flows, provider abstraction (openai, anthropic, google), and error types to inspect for robust error handling. Recommendations include runtime callOptions, schema validation, and retry/backoff patterns.

When to use it

  • Implement server-side AI features with Vercel AI SDK v5 or v6
  • Produce structured JSON outputs validated by Zod
  • Build agent workflows that call external tools or require human approval
  • Stream model responses for low-latency UX or server-sent events
  • Troubleshoot common API and schema validation errors in production

Best practices

  • Use generateText for simple prompts and generateObject with Zod for structured responses
  • Prefer ToolLoopAgent (v6) for agent orchestration rather than custom loops
  • Validate API keys at startup and handle response.error for typed errors
  • Implement exponential backoff and retries for rate limits and transient failures
  • Use callOptions to adjust model, temperature, and provider per request
  • Keep Cloudflare Worker entry modules free of top-level imports to avoid startup overhead

Example use cases

  • Backend endpoint that returns typed JSON user summaries using generateObject and a Zod schema
  • Agent that queries a database, calls a search tool, and composes an answer with ToolLoopAgent and tool approval
  • Streaming chat assistant that uses streamText with onError to gracefully recover and emit partial data
  • Multi-provider fallback where requests try openai then anthropic or google based on latency/cost
  • Error handling middleware that maps AI_APICallError and AI_NoObjectGeneratedError to actionable responses

FAQ

Which API should I use for structured outputs?

Use generateObject with a Zod schema and mode:'json' when available; validate the result and handle AI_NoObjectGeneratedError.

How do I implement human approval for tool calls?

Use ToolLoopAgent with tool execution approval hooks so tool actions pause for a human decision before execution.

What error handling is required at minimum?

Always inspect response.error, catch provider-specific error types (e.g., AI_APICallError), and use onError in streamText to handle streaming failures.