Integrations

Vercel AI SDK

Wrap Thunderbit as a TypeScript tool for streamText / generateText agents

The Vercel AI SDK's tool() helper turns any TypeScript function into something an LLM can call. Wrap /distill and the model can read live web pages mid-conversation.

Install

npm install ai @ai-sdk/openai zod

Define the tool

import { tool } from 'ai';
import { z } from 'zod';

const API = 'https://openapi.thunderbit.com/openapi/v1';
const H = { Authorization: `Bearer ${process.env.THUNDERBIT_API_KEY}` };

export const readUrl = tool({
  description:
    'Fetch a URL and return clean Markdown. Use for any web research task: docs, articles, product pages.',
  parameters: z.object({
    url: z.string().url(),
    renderMode: z.enum(['basic', 'advanced']).default('basic'),
  }),
  execute: async ({ url, renderMode }) => {
    const res = await fetch(`${API}/distill`, {
      method: 'POST',
      headers: { ...H, 'Content-Type': 'application/json' },
      body: JSON.stringify({ url, renderMode }),
    });
    const json = await res.json();
    return json.data.markdown;
  },
});

Use with streamText

import { openai } from '@ai-sdk/openai';
import { streamText } from 'ai';

const result = streamText({
  model: openai('gpt-4o'),
  tools: { readUrl },
  maxSteps: 5,
  prompt: 'Summarize the latest post on https://vercel.com/blog',
});

for await (const chunk of result.textStream) process.stdout.write(chunk);

Tips

  • Cap returned Markdown to ~8k tokens before returning from execute — avoids context bloat
  • For multi-URL fan-out, expose a second readUrls tool backed by /batch/distill
  • renderMode: 'advanced' only when the page is JS-heavy — basic is 3-5× cheaper