Cookbook

Vercel AI SDK — streamText

Stream a Krawl deep-research report through Vercel AI SDK 6 streamText — wire Krawl in via the OpenAI-compatible provider and pipe the cited markdown into a server component or CLI.

The Vercel AI SDK's @ai-sdk/openai provider speaks the same OpenAI-shape Krawl exposes at /v1/chat/completions. Point the provider at Krawl and streamText becomes a streaming deep-research call.

Working file: docs/cookbook/vercel-ai-sdk-streamtext.ts.

Install

npm i ai @ai-sdk/openai
export KRAWL_API_KEY=krawl_demo_xxx

curl

The raw streaming surface, no SDK:

curl -N -X POST https://api.krawl.sh/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "X-API-Key: $KRAWL_API_KEY" \
  -d '{
    "model": "krawl-deep",
    "messages": [{"role":"user","content":"What is EIP-4844?"}],
    "stream": true
  }'

Node

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

const krawl = createOpenAI({
  apiKey: process.env.KRAWL_API_KEY!,
  baseURL: "https://api.krawl.sh/v1",
  headers: { "X-API-Key": process.env.KRAWL_API_KEY! },
});

const result = await streamText({
  model: krawl("krawl-deep"),
  system: "You are a deep-research analyst. Cite every claim.",
  prompt: process.argv.slice(2).join(" ") || "What is EIP-4844?",
});

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

const usage = await result.usage;
console.error(`prompt=${usage.promptTokens} completion=${usage.completionTokens}`);

Run:

npx tsx cookbook/vercel-ai-sdk-streamtext.ts "Compare Optimism and Arbitrum"

Python

import os, httpx

with httpx.stream(
    "POST",
    "https://api.krawl.sh/v1/chat/completions",
    headers={"X-API-Key": os.environ["KRAWL_API_KEY"], "Content-Type": "application/json"},
    json={
        "model": "krawl-deep",
        "messages": [{"role": "user", "content": "What is EIP-4844?"}],
        "stream": True,
    },
    timeout=600,
) as resp:
    for line in resp.iter_lines():
        if line.startswith("data: "):
            print(line.removeprefix("data: "))

Notes

  • Use model: "krawl-deep" for the full recursive pipeline or "krawl-quick" for the flat agent loop (~10s).
  • Inside a Next.js Server Component, result.toTextStreamResponse() is the idiomatic way to forward Krawl's tokens to the browser.
  • For per-source SSE events (sources, learnings, citation_verified) call POST /research directly — the OpenAI-compatible surface only emits chat-shaped chunks.

On this page