# Vercel AI SDK

> Give an AI SDK model scrape, map and batch tools, capped at a price you choose.

**TL;DR**: [install](#install) `@megalania/ai-sdk`, then pass
`megalaniaTools()` as `tools` to `generateText` or `streamText`. The key comes
from `MEG_API_KEY`. Blocked pages and errors are free.

## Install

Node 22.18 or newer, AI SDK 5 or newer.

```sh
npm install @megalania/ai-sdk megalania ai
```

## Use it

```js
import { generateText, stepCountIs } from "ai";
import { megalaniaTools } from "@megalania/ai-sdk";

const { text } = await generateText({
  model: "openai/gpt-5",
  tools: megalaniaTools(),
  stopWhen: stepCountIs(5),
  prompt: "What can the API at https://docs.megalania.io/ do?",
});
console.log(text);
```

The five tools are `scrape`, `extract`, `map_site`, `create_batch` and
`get_batch`. `extract` reads fields by CSS rules or by a JSON Schema. Their
inputs use the same names as the API. You can run one yourself, with no model:

```js
import { megalaniaTools } from "@megalania/ai-sdk";

const page = await megalaniaTools().scrape.execute({ url: "https://docs.megalania.io/" }, { toolCallId: "1", messages: [] });
console.log(page.credits_charged, page.text.slice(0, 200));
```

`scrape` returns `{ text, credits_charged, request_id }`. `get_batch` returns
the batch with the content of its finished pages.

## What a call may cost

**Each page costs at most 128 credits** by default. That covers every route, and
a residential Chrome page up to about 1.5 MiB. A bigger page comes back cut,
with a note that says so. Pass `megalaniaTools({ maxCredits })` (up to 32767)
to allow more. `maxCredits` is per page, so a batch can cost up to
`maxCredits` times its pages: **one `create_batch` call takes at most 100
pages** by default. More URLs are refused, and a crawl stops at 100 pages.
Pass `megalaniaTools({ maxPages })` to allow more. The model can raise
neither. `maxChars` (default 100,000) cuts what a tool returns, so one page
cannot fill the context.

The code is MIT licensed at https://github.com/megalania/megalania-typescript.
