Blog Post

Microsoft Foundry Blog
6 MIN READ

Extending the Vercel AI SDK to Microsoft Foundry (TypeScript)

jyravi's avatar
jyravi
Icon for Microsoft rankMicrosoft
May 28, 2026

The Vercel AI SDK gives developers one elegant, provider‑agnostic surface (generateText, streamText, tools, structured output) for building agentic apps. The official AI​-sdk/azure provider, however, only targets the Azure OpenAI chat surface and hence the rest of Microsoft Foundry's catalog (Llama, DeepSeek, Mistral, Phi, and now Anthropic Claude) is not supported out of the box.

Technical Rationale

If you have shipped agents with the Vercel AI SDK, you already know the appeal: one clean surface which generateText, streamText, and tool that lets you swap models and providers without touching your orchestration logic. That portability is the whole point.

The catch is that when you move those agents onto Azure, the official AI​-sdk/azure provider is scoped to Azure OpenAI’s chat API. It builds URLs like `https://{resourceName}.openai.azure.com/openai/v1{path}` and authenticates specifically for Azure OpenAI. Hence, if you want to use Foundry catalog models like Llama, DeepSeek, Mistral, Phi, Anthropic Claude, and the long tail beyond, a native “drop‑in” provider out of the box is not available.

Microsoft Foundry is designed to act as a single multi‑model backplane with one endpoint and one set of credentials that can front models from multiple providers. Which means the hard part is not “how do I reach the models from Azure?”, but it is “how do I plug Foundry cleanly into the AI SDK so my existing agent code stays exactly the same?”

High‑level architecture

The extension introduces two provider adapters that sit between the AI SDK's model‑agnostic surface and Foundry's two parallel routes based on the underlying fact that

  1. Foundry exposes models from many providers through one endpoint + one credential and accepts OpenAI‑style Chat Completions for OpenAI and non‑OpenAI deployments when you target /openai/v1/.
  2. Claude is on a parallel /anthropic/v1/ surface speaking the Anthropic Messages API.

Adapter

Foundry route

Wire protocol

AI SDK provider

OpenAI‑compatible adapter

/openai/v1/*

OpenAI Chat Completions / Responses

AI​-sdk/openai-compatible

Anthropic adapter

/anthropic/v1/*

Anthropic Messages API

AI​-sdk/anthropic (with custom baseURL + headers)

Both adapters share the same auth choices (Entra ID default, API key fallback) and the same model‑routing pattern (deployment name as modelId). Application‑level code (agents, planners, routers, RAG pipelines) remains unchanged.

OpenAI‑family and OSS models on Foundry (/openai/v1)

Azure OpenAI model families (GPT‑4.1, GPT‑5.x, the o‑series, etc.), plus Llama, DeepSeek, Mistral, Phi, and partner models, are accessible via Foundry’s OpenAI‑compatible path (/openai/v1).

 

// providers/foundry-openai.ts
import { createOpenAICompatible } from '@ai-sdk/openai-compatible';
import { DefaultAzureCredential } from '@azure/identity';
const credential = new DefaultAzureCredential();

// Scope for OpenAI-compatible surface on Foundry
const FOUNDRY_AI_SCOPE = 'https://cognitiveservices.azure.com/.default';

// Provider configuration — endpoint targeting and auth selection. Pass either `resource` (shortcut) or `baseURL` (full override). Auth defaults to Entra ID; set `apiKey`.
export interface FoundryOpenAIOptions {
    resource?: string; // Foundry resource name. Used to build the default base URL. 
  baseURL?: string; // Full base URL override,
 // "https://<resource>.services.ai.azure.com/openai/v1". 
    tenantId?: string; // Optional Entra tenant ID where the Foundry resource lives.
   apiKey?: string;// Optional Foundry resource API key for key based authentication
}
export function createFoundry(options: FoundryOpenAIOptions = {}) {
  const baseURL =
    options.baseURL ??
    (options.resource
      ? `https://${options.resource}.services.ai.azure.com/openai/v1`
      : process.env.FOUNDRY_RESOURCE
        ? `https://${process.env.FOUNDRY_RESOURCE}.services.ai.azure.com/openai/v1`
        : undefined);
  if (!baseURL) {
    throw new Error(
      'createFoundry: provide `resource`, `baseURL`, or set FOUNDRY_RESOURCE env var.',
    );
  }
  
  // Auth mode A (default) — Entra ID via DefaultAzureCredential.
  
  const credential = new DefaultAzureCredential(
    options.tenantId ? { tenantId: options.tenantId } : undefined,
  );
  return createOpenAICompatible({
    name: 'microsoft-foundry',
    baseURL,
    apiKey: 'placeholder-not-used',
    fetch: async (input, init) => {
      const token = (await credential.getToken(FOUNDRY_AI_SCOPE))?.token;
      if (!token) throw new Error('Failed to acquire Entra ID token for Foundry');
      const headers = new Headers(init?.headers);
      headers.set('Authorization', `Bearer ${token}`);
      return fetch(input, { ...init, headers });
    },
  });
} 

Example code is provided below to test the providers/foundry-openai.ts on three different models from Azure OpenAI, DeepSeek and Moonshot AI.

import { generateText, streamText } from 'ai';
import { createFoundry } from './providers/foundry-openai';

// Pass the Foundry resource name (or a full baseURL) here.
const foundry = createFoundry({
  resource: {Your Foundry Resource name},
  // baseURL: 'https://{Your Foundry Resource name}.services.ai.azure.com/openai/v1', // alternative
});

// One AI SDK surface, three different Foundry models, zero transport changes.
const gpt   = foundry.chatModel('gpt-4.1-mini');
const ds    = foundry.chatModel('DeepSeek-V3.1');
const kimi = foundry.chatModel('Kimi-K2.6');
//const llama = foundry.chatModel('Llama-3.3-70B-Instruct');

// Simple generation
const { text } = await generateText({
  model: gpt,
  prompt: 'Say hello in one short sentence.',
});
console.log('[gpt-4.1-mini]', text);
const { text: dsText } = await generateText({
  model: ds,
  prompt: 'What is 2 + 2?',
});
console.log('[DeepSeek-V3.1]', dsText);

// Streaming
process.stdout.write('[kimi] ');
const stream = streamText({ model: kimi, prompt: 'Draft a one-line customer reply.' });
for await (const chunk of stream.textStream) process.stdout.write(chunk);
process.stdout.write('\n');

If you would like to use API-key (only for dev or non-prod environments, you may use the following code

  // Auth mode B — API key (used when `apiKey` is set or AZURE_FOUNDRY_API_KEY is in the environment). Foundry accepts the resource key via the
  // `api-key` header on the OpenAI-compatible route.
  
  const apiKey = options.apiKey ?? process.env.AZURE_FOUNDRY_API_KEY;
  if (apiKey) {
    return createOpenAICompatible({
      name: 'microsoft-foundry',
      baseURL,
      apiKey: 'placeholder-not-used',
      fetch: async (input, init) => {
        const headers = new Headers(init?.headers);
        headers.delete('Authorization');
        headers.set('api-key', apiKey);
        return fetch(input, { ...init, headers });
      },
    });
  }

Anthropic Claude on Foundry (/anthropic/v1)

Claude on Foundry does not live behind /openai/v1. It is exposed at https://{resource}.services.ai.azure.com/anthropic/v1/* and speaks the native Anthropic Messages API, not OpenAI Chat Completions. Therefore, @ai-sdk/openai-compatible cannot be used but use @ai-sdk/anthropic` instead.

import { createAnthropic } from "@ai-sdk/anthropic";
import { DefaultAzureCredential } from "@azure/identity";

const SCOPE = "https://cognitiveservices.azure.com/.default";

export interface AnthropicFoundryOptions {
  /** Foundry resource hostname or full URL, e.g. "https://<resource>.services.ai.azure.com" */
  endpoint: string;
  /** Optional Entra tenant ID where the Foundry resource lives */
  tenantId?: string;
  // OPTIONAL: Key-based authentication (uncomment to use API key instead of Entra ID). If `apiKey` is provided, it will be used directly and  //DefaultAzureCredential will be skipped.

  // apiKey?: string;

export function createAnthropicFoundry(options: AnthropicFoundryOptions) {
  const url = new URL(options.endpoint);
  const baseURL = `${url.protocol}//${url.host}/anthropic/v1`;
  const credential = new DefaultAzureCredential(
    options.tenantId ? { tenantId: options.tenantId } : undefined,
  );
  return createAnthropic({
    baseURL,
    // apiKey is required by the provider but unused; overriding the Authorization header per-request via custom fetch.
    apiKey: "placeholder-not-used",
    fetch: async (input, init) => {
      // Entra ID (default) — acquire a bearer token via DefaultAzureCredential.
      const token = (await credential.getToken(SCOPE))?.token;
      if (!token) throw new Error("Failed to acquire Entra ID token");

      // OPTION B: Key-based authentication (uncomment to use API key). Foundry accepts the resource key via the `api-key` header.
      // Comment out the Entra block above when enabling this.
      // const apiKey = options.apiKey ?? process.env.AZURE_FOUNDRY_API_KEY;
      // if (!apiKey) throw new Error("Missing Foundry API key");
      const headers = new Headers(init?.headers);
      // Strip Anthropic's default API key header and replace with bearer token
      headers.delete("x-api-key");
      headers.set("Authorization", `Bearer ${token}`);
      // For key-based auth, replace the line above with:
      // headers.set("api-key", apiKey);
      return fetch(input, { ...init, headers });
    },
  });
}
}

Example code for testing the Claude model is provided below:

// Test using AI-sdk/anthropic against a Microsoft Foundry endpoint with Entra ID.
import { generateText } from "ai";
import { createAnthropicFoundry } from "./providers/anthropic-foundry";

const anthropic = createAnthropicFoundry({
  endpoint: "https://<your-foundry-resource>.services.ai.azure.com",
  tenantId: process.env.AZURE_TENANT_ID, // optional (only needed if multi-tenant)

  // OPTIONAL: Key-based authentication. Uncomment to use an API key instead of Entra ID (DefaultAzureCredential). Requires enabling the
  // matching `apiKey` branch in providers/anthropic-foundry.ts.
  // apiKey: process.env.AZURE_FOUNDRY_API_KEY,
});
const { text } = await generateText({
  model: anthropic("claude-opus-4-7"), // replace with your Foundry model deployment name 
  prompt: "Hello!",
});
console.log(text);

This approach enables seamless connections across systems while ensuring flexibility, scalability, and security.

Summary

Model Category via MS FoundryEndpoint Type UsedVercel AI SDK Integration PatternAuth Options Highlighted

OpenAI-compatible models 

(OpenAI, DeepSeek, Moonshot AI, Mistral AI, Meta etc.)

OpenAI-compatible /openai/v1 endpointNative SDK usage (works out-of-the-box with baseURL override)• API Key
• Microsoft Entra ID (token-based) 
Anthropic ClaudeAnthropic Messages API endpointUses provider pattern (not OpenAI-compatible)• API Key
• Microsoft Entra ID (token-based) 

Next Steps

 
Ready to start building? Here are your pathways:
Learn More 
- Microsoft Foundry Models- Foundry models Overview
- Microsoft Foundry Model endpoints - Endpoints for Foundry models and how to access them
- Vercel AI SDK docs - core concepts and advanced patterns
- Anthropic on Foundry guide - Deploy and use Claude models in MS Foundry
Published May 25, 2026
Version 1.0
No CommentsBe the first to comment