# Vercel AI SDK integration

Use AI SDK tool calling for bounded platform retrieval while retaining the Monocrawl response envelope and source evidence.

## Prepare a server-side project

Use Node.js 22+ in a separate integration project. Create a dedicated Monocrawl key with an explicit cumulative credit cap. Model-provider usage is billed separately; set that provider’s limits too. Keep both credentials in server environment variables.

**Install dependencies**

```
npm install ai@7 @ai-sdk/anthropic@4 zod@4
```

- [Download shared bounded MCP client](https://www.monocrawl.com/examples/agent-tools.mjs)

- [Download Vercel AI SDK example](https://www.monocrawl.com/examples/vercel-ai-agent.mjs)

Save both files in the same directory. Set MONOCRAWL_API_KEY, ANTHROPIC_API_KEY, MODEL_ID (a model available to your account), and MONOCRAWL_BUDGET (an integer credit ceiling). The example permits only TikTok and Instagram profile lookups; expand the application-owned allowlist only for an authorized task.

**Run after reviewing the budget**

```
node vercel-ai-agent.mjs
```

## Discovery, retrieval and a grounded answer

- The example gives the model separate discovery, inspection and retrieval tools. It cannot submit an arbitrary REST URL or call account-changing tools.

- Inspection reads current parameters, MCP availability and credit_cost. Retrieval requires that inspection and validates declared and required parameters.

- The client serializes requests, passes the remaining budget as max_credits and counts each logical result once. Repeated identical inputs reuse the saved response within that process.

- Receipts, warnings and source fields reach the model with the data. The prompt asks it to preserve unknown values and cite returned source URLs.

## Handle failure and restart deliberately

The shared client does not retry transport failures automatically. It halts further paid retrieval if the result is ambiguous, has pending accounting or exceeds its local budget. Capture onRequest events in your secure job journal before sending; the example journal stores keys and arguments in memory, so it is not automatic crash recovery. Never log the API credential.

Use the original idempotency key and exact parameters for recovery, following the production guide. Each new process has a new local budget; a dedicated server-side key cap bounds cumulative spend across restarts. Model step limits bound the loop, while the shared client also enforces a maximum number of MCP requests.

- [Production recovery](https://www.monocrawl.com/docs/production-checklist)

- [Integration testing](https://www.monocrawl.com/docs/testing)

- [Official Vercel AI SDK reference](https://ai-sdk.dev/docs/ai-sdk-core/tools-and-tool-calling)
