n4nAI

Your first generateText call with Vercel AI SDK on n4n.ai

Step-by-step vercel ai sdk generatetext n4n.ai tutorial: set up Vercel AI SDK, point it at an OpenAI-compatible gateway, and run your first call.

n4n Team3 min read619 words

Audio narration

Coming soon — every post will get a voice note here.

This vercel ai sdk generatetext n4n.ai tutorial gets you from an empty directory to a working text generation call in under ten minutes. We’ll use the Vercel AI SDK’s generateText function against an OpenAI-compatible gateway that routes across 240+ models with automatic fallback when a provider is degraded.

Prerequisites

  • Node.js 18.18 or newer (the SDK relies on global fetch and stable async primitives).
  • A package manager (npm works; pnpm is fine).
  • An API key for an OpenAI-compatible inference gateway. The endpoint referenced in this vercel ai sdk generatetext n4n.ai tutorial is https://api.n4n.ai/v1, but any compliant base URL behaves identically with this code.

You should be comfortable with ES modules and reading stack traces. No prior LLM gateway experience required.

Project setup

Create a scratch directory and install the only two runtime dependencies you need:

mkdir n4n-ai-sdk-demo && cd n4n-ai-sdk-demo
npm init -y
npm install ai @ai-sdk/openai dotenv

The ai package ships generateText and the core abstraction. @ai-sdk/openai provides the OpenAI-compatible provider factory, which is the correct integration point for any gateway that speaks the OpenAI chat protocol. dotenv keeps keys out of source.

Set "type": "module" in package.json so .js files parse as ESM, or just use the .mjs extension as shown below.

Configure the client

Write a .env file at the project root. Never hard-code credentials in source.

# .env
N4N_API_KEY=sk-your-key-here
BASE_URL=https://api.n4n.ai/v1

The BASE_URL is the only n4n.ai-specific line. Because the gateway is OpenAI-compatible, the Vercel provider does not need a custom adapter—you just override baseURL.

Make your first generateText call

Create index.mjs:

import 'dotenv/config';
import { generateText } from 'ai';
import { createOpenAI } from '@ai-sdk/openai';

const gateway = createOpenAI({
  baseURL: process.env.BASE_URL,
  apiKey: process.env.N4N_API_KEY,
});

const { text, usage } = await generateText({
  model: gateway('gpt-4o-mini'),
  prompt: 'Write a concise definition of idempotency for REST APIs.',
});

console.log('OUTPUT:', text);
console.log('USAGE:', usage);

Run it:

node index.mjs

Expected output

The text will vary slightly by model, but the shape is stable:

OUTPUT: Idempotency in REST APIs means that making the same request multiple times produces the same result as making it once, with no additional side effects.
USAGE: { promptTokens: 14, completionTokens: 27, totalTokens: 41 }

If you see the output, the call succeeded. The usage object comes straight from the gateway’s response and is already normalized by the SDK.

Why this wiring works

The Vercel AI SDK decouples model selection from transport. createOpenAI returns a function that, given a model id, produces a model object. When the base URL points at a gateway instead of OpenAI’s own servers, the gateway interprets the model string and routes accordingly.

In the example above, gpt-4o-mini is forwarded as-is. Gateways that aggregate many providers often expect a prefixed id like openai/gpt-4o-mini or anthropic/claude-3-haiku. Check your gateway’s model list; the call signature does not change.

Because the gateway honors client routing directives and forwards provider cache-control hints, you can pass standard OpenAI chat parameters and they propagate. For example, to leverage prompt caching on supported backends:

const { text } = await generateText({
  model: gateway('anthropic/claude-3-haiku'),
  prompt: 'System: you are a terse API docs writer.\nUser: explain 429 responses',
  // provider-specific headers forwarded by the gateway
  headers: { 'cache-control': 'max-age=300' },
});

Error handling and degraded providers

Network calls fail. The SDK throws typed errors; inspect statusCode to distinguish 4xx from 5xx.

try {
  const { text } = await generateText({
    model: gateway('gpt-4o-mini'),
    prompt: 'Summarize this log: ' + hugeString,
  });
  console.log(text);
} catch (err) {
  if (err.statusCode === 429) {
    console.error('Rate limited. Back off and retry.');
  } else if (err.statusCode >= 500) {
    console.error('Gateway or provider upstream error.');
  } else {
    console.error('Unexpected:', err.message);
  }
}

When you point at a gateway with automatic fallback, a 429 from one provider can trigger a silent reroute to another. Your code does not need to implement retry logic for provider-specific outages—but you should still handle the case where the gateway itself returns an error after exhausting fallbacks.

Inspecting token usage and metering

Per-token metering is exposed on every successful generateText response via the usage field. This is the number you bill against or use for cost guardrails.

const { usage } = await generateText({
  model: gateway('gpt-4o-mini'),
  prompt: 'List three HTTP methods that are idempotent.',
});

if (usage.totalTokens > 1000) {
  console.warn('Prompt+completion exceeded 1k tokens:', usage);
}

The gateway returns token counts using the provider’s tokenizer. Cross-provider totals are not directly comparable, but they are accurate for the model that served the request. If you switch models behind the same gateway call, the usage reflects whatever backend actually executed.

Streaming is one line away

Once generateText works, streamText is the same shape with an async iterable. You do not need to change the client configuration.

import { streamText } from 'ai';

const { textStream } = await streamText({
  model: gateway('gpt-4o-mini'),
  prompt: 'Explain exponential backoff in bullet points.',
});

for await (const delta of textStream) {
  process.stdout.write(delta);
}

Use streaming when latency matters more than a single atomic response. For batch jobs, generateText is simpler and gives you the full usage object without reassembly.

Wrapping up

By the end of this vercel ai sdk generatetext n4n.ai tutorial, you have a runnable script that talks to an OpenAI-compatible gateway through the Vercel AI SDK. The pattern—override baseURL, pick a model id, call generateText—scales to 240+ models without rewriting application code. Swap the model string, read the usage field, and let the gateway handle routing and fallback.

Tagsvercel-ai-sdkgeneratetextn4n-aitutorial

Written by

n4n Team

The team building n4n — a single OpenAI-compatible API in front of 240+ models, with automatic fallback, load balancing and pay-per-token metering.

More from n4n Team →

All vercel ai sdk getting started with n4n.ai posts →