Skip to main content

About Vercel AI SDK with Tools

Vercel AI SDK is an open-source toolkit from the team behind Vercel and Next.js. It provides a unified developer experience for building AI-powered applications with tool calling capabilities.

Key Features for Tool Integration

  • Easy integration with multiple model providers (OpenAI, Anthropic, etc.)
  • Support for streaming responses for real-time chat UIs
  • Type-safe APIs with excellent TypeScript support
  • Built-in support for agents, tools, and structured outputs
  • Ready-to-use React/Next.js hooks for managing conversation state
By using the AI SDK with Alchemyst tools, you can avoid handling low-level APIs directly and focus on creating seamless AI-driven experiences in your app.

How It Works

The AI SDK is split into modular packages:
  • Core API: A unified way to call LLMs and handle outputs
  • Provider Adapters: Packages like @ai-sdk/openai let you plug in specific providers
  • UI Utilities: Hooks such as useChat make it easy to build interactive experiences
  • Tooling / Agents: Support for calling external APIs or chaining workflows
This modular design means you can start small and scale up to more complex AI flows as your application grows.

Alchemyst Tools Integration

Alchemyst provides specialized tools that you can integrate with the AI SDK:
  • Context Management Tools: Add, search, and retrieve context dynamically
  • Search Tools: Find relevant information in your stored context
  • Delete Tools: Remove outdated or unnecessary context
  • Custom Tools: Extend with your own business logic

When to Use Vercel AI SDK with Tools

You should consider using the SDK with Alchemyst tools if:
  • You want to build chatbots, assistants, or agent-like applications quickly
  • You need real-time streaming responses from your models
  • You want to give your AI access to dynamic context and knowledge bases
  • You need cross-provider flexibility without rewriting core logic
  • You prefer type safety and well-structured APIs
For more details, check out the official Vercel AI SDK documentation

As a Tool in AI-SDK

Alchemyst can be added to your AI SDK codebase as a regular tool, which is the recommended way for developer control.The snippet below shows how to set up a tool using Vercel’s AI SDK, using OpenAI GPT-4o-mini. This tool uses Alchemyst AI SDK under the hood, and exposes a nifty set of tools, letting you control if you want to use memory, context or both (default).This assumes that you have an OpenAI Key and Alchemyst API Key. If you don’t have the Alchemyst API Key, you can get them in the Alchemyst Settings page

Basic Setup

aiSdkToolSetup.ts
Hierarchical groupName Strategy: Use 2-3 layers for optimal organization:
  • Layer 1: Organization/Domain (“engineering”, “marketing”, “sales”)
  • Layer 2: Category/Time (“q4_2024”, “backend”, “campaign”)
  • Layer 3: Specifics (“auth”, “api”, “billing”)
Example: [“engineering”, “backend”, “auth”] instead of flat [“engineering_backend_auth_login_jwt”]

Complete Streaming Example

This example shows how to handle streaming responses and tool calls:
streamingExample.ts

Context Search Example

This example demonstrates how the AI can search your stored context:
contextSearchExample.ts

Adding Context Example

This example shows how to add context that the AI can later search:
addContextExample.ts
Document Size Best Practice: Keep documents between 500-2000 words for optimal retrieval:
  • Too small (less than 100 words): Loses context, requires many docs
  • Too large (more than 10,000 words): Retrieves too much irrelevant content, wastes tokens
  • Just right (500-2000 words): Single cohesive topic with enough context

Updating Context (Delete-Then-Add Pattern)

When you need to update existing context, use the delete-then-add pattern to avoid 409 conflicts:
updateContextExample.ts
Update Strategy: Alchemyst uses metadata.fileName as the deduplication key. Same fileName = update attempt, which requires delete first to avoid 409 Conflict errors. This is by design to prevent accidental duplicates.

Chat Assistant with Context and Memory

A complete chat assistant that uses both context and memory:
chatAssistant.ts

Tool Configuration Options

Control which tools are available:
toolConfiguration.ts

Advanced Context Management

Working with dynamic context in real-time with proper metadata structure:
advancedContext.ts
The 5-Field Metadata Rule: Start with maximum 5 metadata fields. Only add more when you have a specific query pattern that requires it.Store in metadata if you:
  • Filter or sort by it (category, price, status)
  • Need exact matching (id, sku)
  • Use for access control (department, classification)
Store in content if it’s:
  • Descriptive text (description, features)
  • Rarely filtered (dimensions, weight)
  • Only needed when retrieved (warranty, specifications)

Bulk Context Operations

For adding large amounts of context efficiently:
bulkContextOperations.ts
Performance Guidelines:
  • 100 docs: ~0.5s (single call)
  • 1,000 docs: ~3s (single call)
  • 10,000 docs: ~35s (10 batches of 1000)
  • Always use batches of 1000 for optimal performance
  • Include retry logic with exponential backoff
If you don’t have an Alchemyst API Key, you can get one in the Alchemyst Settings page