n4nAI

Function calling with GPT-4o via Vercel AI SDK and n4n.ai

Step-by-step tutorial for gpt-4o function calling with Vercel AI SDK via n4n.ai gateway, including runnable code and expected output.

n4n Team2 min read454 words

Audio narration

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

Getting gpt-4o function calling vercel ai sdk n4n.ai to work is mostly about pointing the SDK at the right base URL and letting the gateway handle model routing. This tutorial builds a runnable TypeScript script that queries a fake weather service through a GPT-4o tool call, using Vercel AI SDK’s native tool support and the gateway’s OpenAI-compatible endpoint for inference.

Prerequisites

  • Node.js 18.18+ (or 20+)
  • An API key for the gateway exported as N4N_API_KEY
  • Familiarity with TypeScript and ES modules
  • tsx for running TS directly

If you don’t have a key, any OpenAI-compatible proxy works, but the fallback behavior described later assumes the gateway.

Project Setup

Create a directory and install dependencies:

mkdir gpt4o-tool-demo && cd gpt4o-tool-demo
npm init -y
npm install ai @ai-sdk/openai zod dotenv
npm install -D tsx typescript

Add "type": "module" to package.json. Create a .env file:

echo "N4N_API_KEY=sk-..." > .env

Configure the Provider

The Vercel AI SDK’s OpenAI provider accepts a baseURL. Point it at the gateway’s single OpenAI-compatible endpoint to reach GPT-4o and 240+ other models. The gateway forwards cache-control hints and meters per-token usage.

// file: src/client.ts
import { createOpenAI } from '@ai-sdk/openai';
import { config } from 'dotenv';

config();

export const n4n = createOpenAI({
  baseURL: 'https://api.n4n.ai/v1',
  apiKey: process.env.N4N_API_KEY!,
});

That’s the entire integration surface for gpt-4o function calling vercel ai sdk n4n.ai. The n4n('gpt-4o') call returns a model object compatible with generateText.

Define the Tool

Vercel AI SDK uses the tool helper with a Zod schema. The execute function must return a JSON-serializable value.

// file: src/tools.ts
import { tool } from 'ai';
import { z } from 'zod';

export const getWeather = tool({
  parameters: z.object({
    location: z.string().describe('City name, e.g. "Berlin"'),
  }),
  execute: async ({ location }) => {
    // Simulated external API
    const fakeDb: Record<string, { tempC: number; condition: string }> = {
      berlin: { tempC: 19, condition: 'Cloudy' },
      tokyo: { tempC: 28, condition: 'Sunny' },
    };
    const key = location.toLowerCase();
    return fakeDb[key] ?? { tempC: 20, condition: 'Unknown' };
  },
});

Single-Step Generation

Call generateText with the model, prompt, and tools. By default the SDK stops after the first tool call unless you set maxSteps.

// file: src/run.ts
import { generateText } from 'ai';
import { n4n } from './client';
import { getWeather } from './tools';

const { text, toolCalls, toolResults } = await generateText({
  model: n4n('gpt-4o'),
  prompt: 'What is the weather in Berlin?',
  tools: { getWeather },
  maxSteps: 1,
});

console.log('TEXT:', text);
console.log('TOOL_CALLS:', JSON.stringify(toolCalls, null, 2));

Run with npx tsx src/run.ts. Expected output at this checkpoint:

TEXT: 
TOOL_CALLS: [
  {
    "type": "tool-call",
    "toolCallId": "call_abc123",
    "toolName": "getWeather",
    "args": { "location": "Berlin" }
  }
]

The model emitted a tool call but no final text because maxSteps was 1 and the tool result wasn’t fed back.

Multi-Step with Tool Results

Set maxSteps: 3 to let the model consume the tool result and produce a natural language answer.

const { text, steps } = await generateText({
  model: n4n('gpt-4o'),
  prompt: 'What is the weather in Berlin?',
  tools: { getWeather },
  maxSteps: 3,
});

console.log('FINAL TEXT:', text);

Expected output:

FINAL TEXT: The weather in Berlin is currently cloudy with a temperature of 19°C.

Under the hood, steps contains two entries: step 0 with the tool call, step 1 with the synthesized answer. This is the standard gpt-4o function calling vercel ai sdk n4n.ai loop.

Inspecting the Step Array

For debugging, print the steps:

for (const step of steps) {
  console.log('STEP', step.stepType, 'TOOL CALLS', step.toolCalls.length);
}

Output:

STEP initial 1
STEP tool-result 0

The second step type is tool-result (or similar depending on SDK version) and contains the merged context.

Error Handling and Provider Degradation

If OpenAI’s GPT-4o is rate-limited, the gateway automatically falls back to an equivalent model when configured, without changing your code. The Vercel AI SDK surfaces provider errors as APICallError. Wrap the call:

import { generateText, APICallError } from 'ai';

try {
  const res = await generateText({
    model: n4n('gpt-4o'),
    prompt: 'Weather in Tokyo?',
    tools: { getWeather },
    maxSteps: 2,
  });
  console.log(res.text);
} catch (e) {
  if (APICallError.isInstance(e)) {
    console.error('Provider error:', e.statusCode, e.message);
  }
}

Because the gateway honors client routing directives, you can force a specific provider via headers if needed, but the default fallback keeps latency low.

Full Runnable Script

Combine everything into src/index.ts:

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

config();

const n4n = createOpenAI({
  baseURL: 'https://api.n4n.ai/v1',
  apiKey: process.env.N4N_API_KEY!,
});

const getWeather = tool({
  parameters: z.object({ location: z.string() }),
  execute: async ({ location }) => {
    const db: Record<string, { tempC: number; condition: string }> = {
      berlin: { tempC: 19, condition: 'Cloudy' },
      tokyo: { tempC: 28, condition: 'Sunny' },
    };
    return db[location.toLowerCase()] ?? { tempC: 20, condition: 'Unknown' };
  },
});

const { text } = await generateText({
  model: n4n('gpt-4o'),
  prompt: 'What is the weather in Tokyo?',
  tools: { getWeather },
  maxSteps: 3,
});

console.log(text);

Run:

npx tsx src/index.ts

Output:

The weather in Tokyo is sunny with a temperature of 28°C.

Production Notes

  • Set temperature: 0 for deterministic tool-call formatting.
  • Use Zod’s .describe() heavily; GPT-4o relies on parameter descriptions.
  • The gateway meters per-token usage; check usage in the response to track cost.
  • For streaming UIs, swap generateText for streamText and pipe textStream to your frontend.

That’s the complete path from zero to a working gpt-4o function calling vercel ai sdk n4n.ai integration.

Tagsgpt-4ofunction-callingvercel-ai-sdkn4n-ai

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 tool & function calling posts →