
Security News
Package Maintainers Call for Improvements to GitHub’s New npm Security Plan
Maintainers back GitHub’s npm security overhaul but raise concerns about CI/CD workflows, enterprise support, and token management.
ai-gateway-provider
Advanced tools
This library provides an AI Gateway Provider for the Vercel AI SDK, enabling you to seamlessly integrate multiple AI models from different providers behind a unified interface. It leverages Cloudflare's AI Gateway to manage and optimize your AI model usage.
npm install ai-gateway-provider
import {createOpenAI} from '@ai-sdk/openai';
import {createAiGateway} from 'ai-gateway-provider';
import {generateText} from "ai";
import {createAnthropic} from "@ai-sdk/anthropic";
const aigateway = createAiGateway({
accountId: "my-cloudflare-account-id",
gateway: 'my-gateway-name',
apiKey: 'optionally my cloudflare api key',
options: { // Optional per-request override
skipCache: true
}
});
const openai = createOpenAI({apiKey: 'openai api key'});
const anthropic = createAnthropic({apiKey: 'anthropic api key'});
const model = aigateway([
anthropic('claude-3-5-haiku-20241022'), // Primary choice
openai("gpt-4o-mini"), // Fallback if first fails
]);
const {text} = await generateText({
model: model,
prompt: 'Write a vegetarian lasagna recipe for 4 people.',
});
Binding Benefits:
const aigateway = createAiGateway({
binding: env.AI.gateway('my-gateway'),
options: { // Optional per-request override
skipCache: true
}
});
const openai = createOpenAI({apiKey: 'openai api key'});
const anthropic = createAnthropic({apiKey: 'anthropic api key'});
const model = aigateway([
anthropic('claude-3-5-haiku-20241022'), // Primary choice
openai("gpt-4o-mini"), // Fallback if first fails
]);
const {text} = await generateText({
model: model,
prompt: 'Write a vegetarian lasagna recipe for 4 people.',
});
You can now customize AI Gateway settings for each request:
const aigateway = createAiGateway({
// ... other configs
options: { // all fields are optional!
cacheKey: 'my-custom-cache-key',
cacheTtl: 3600, // Cache for 1 hour
skipCache: false,
metadata: {
userId: 'user123',
requestType: 'recipe'
},
retries: {
maxAttempts: 3,
retryDelayMs: 1000,
backoff: 'exponential'
}
},
});
createAiGateway(options: AiGatewaySettings)
accountId
: Your Cloudflare account IDgateway
: The name of your AI GatewayapiKey
(Optional): Your Cloudflare API keybinding
: Cloudflare AI Gateway bindingoptions
(Optional): Request-level AI Gateway settingscacheKey
: Custom cache key for the requestcacheTtl
: Cache time-to-live in secondsskipCache
: Bypass caching for the requestmetadata
: Custom metadata for the requestcollectLog
: Enable/disable log collectioneventId
: Custom event identifierrequestTimeoutMs
: Request timeout in millisecondsretries
: Retry configuration
maxAttempts
: Number of retry attempts (1-5)retryDelayMs
: Delay between retriesbackoff
: Retry backoff strategy ('constant', 'linear', 'exponential')// Define multiple provider options with fallback priority
const model = aigateway([
anthropic('claude-3-5-haiku-20241022'), // Primary choice
openai("gpt-4o-mini"), // First fallback
mistral("mistral-large-latest"), // Second fallback
]);
// The system will automatically try the next model if previous ones fail
const {text} = await generateText({
model: model,
prompt: 'Suggest three names for my tech startup.',
});
Currently, the following methods are supported:
generateText()
from the Vercel AI SDKgenerateText()
with message-based promptsMore can be added, please open an issue in the GitHub repository!
The library throws the following custom errors:
AiGatewayUnauthorizedError
: Your AI Gateway has authentication enabled, but a valid API key was not provided.AiGatewayDoesNotExist
: Specified AI Gateway does not existThis project is licensed under the MIT License - see the LICENSE file for details.
FAQs
AI Gateway Provider for AI-SDK
The npm package ai-gateway-provider receives a total of 1,925 weekly downloads. As such, ai-gateway-provider popularity was classified as popular.
We found that ai-gateway-provider demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Maintainers back GitHub’s npm security overhaul but raise concerns about CI/CD workflows, enterprise support, and token management.
Product
Socket Firewall is a free tool that blocks malicious packages at install time, giving developers proactive protection against rising supply chain attacks.
Research
Socket uncovers malicious Rust crates impersonating fast_log to steal Solana and Ethereum wallet keys from source code.