Package Exports
- priorai
Readme
Priorai
Unified LLM gateway SDK with priority-based fallback and weighted load balancing for TypeScript/Node.js
Built on the shoulders of Portkey AI Gateway β Priorai extracts and refines Portkey's battle-tested provider routing core into a lightweight, embeddable SDK.
Status: π’ All Portkey 2.0 sync complete β 250 tests passing, 0 TS errors, 71 providers via registry
Features
- Priority-based Fallback: Automatic failover across multiple LLM providers
- Weighted Load Balancing: Distribute traffic across providers based on weights
- Provider Registry: 71 providers via Portkey-aligned ProviderConfig architecture
- Multi-API Support: Chat completions, embeddings, image generation, audio transcription, speech synthesis, translation, 3D generation
- Streaming: SSE-based streaming with provider-specific transform pipelines
- Config Validation: Validates targets, provider, timeout, retry at construction
- Request Timeout: Configurable timeout for all fetch paths via
config.timeout - Minimal Dependencies: Only AWS SDK for Bedrock signing, everything else is pure fetch
- Firebase Compatible: Works in Firebase Cloud Functions (Node.js 18+)
- TypeScript First: Full type safety, strict mode, 0 TS errors
Installation
npm install prioraiQuick Start
import { Priorai } from 'priorai';
const priorai = new Priorai({
strategy: 'fallback',
targets: [
{
provider: 'openai',
apiKey: process.env.OPENAI_API_KEY,
overrideParams: { model: 'gpt-4o' }
},
{
provider: 'anthropic',
apiKey: process.env.ANTHROPIC_API_KEY,
overrideParams: { model: 'claude-sonnet-4-5-20250514' }
}
]
});
const response = await priorai.chat.completions.create({
model: 'gpt-4o',
messages: [{ role: 'user', content: 'Hello!' }]
});Supported Providers (71)
All providers use the Portkey-aligned ProviderConfig registry (src/providers/). Each provider directory contains its own api.ts (URL/headers), chatComplete.ts (param mapping + response transform), and optional endpoint configs (embed, image, audio, etc.).
Chat Completion Providers
| Provider | Streaming | Notes |
|---|---|---|
| OpenAI | β | Direct passthrough |
| Anthropic | β | Messages API, output_config, strict tools, thinking support |
| Google AI | β | Gemini REST API, thinking (2.5/3.0+), thought signature |
| Google Vertex AI | β | REST API, thinking (2.5/3.0+), thought signature |
| Azure OpenAI | β | Azure URL format |
| Azure AI Inference | β | Foundry URL |
| GitHub Models | β | /inference/chat |
| AWS Bedrock | β | SigV4 signing via @smithy, Converse API |
| Cohere | β | Dedicated stream transform |
| Groq | β | OpenAI-compatible |
| DeepSeek | β | OpenAI-compatible, thinking support |
| Mistral AI | β | OpenAI-compatible |
| Together AI | β | OpenAI-compatible |
| OpenRouter | β | OpenAI-compatible |
| Perplexity AI | β | OpenAI-compatible |
| Fireworks AI | β | OpenAI-compatible |
| Hugging Face | β | OpenAI-compatible |
| Databricks | β | OpenAI-compatible, workspace-based URL |
| Latitude | β | OpenAI-compatible, developerβsystem role mapping |
And 52 more OpenAI-compatible providers including: DashScope, Zhipu, LingYi, Moonshot, x-ai, Lambda, SageMaker, Oracle, OVHcloud, Anyscale, Workers AI, DeepInfra, Predibase, SambaNova, Cerebras, Nebius, Hyperbolic, Modal, Replicate, SiliconFlow, LemonFox AI, DeepBricks, Featherless AI, Lepton, Novita AI, NCompass, 302.AI, AI21, Bytez, CometAPI, Inference Net, IOIntelligence, Kluster AI, Matter AI, NextBit, Stability AI, Triton, Upstage, AI Badgr, Cortex, Krutrim, Ollama, Palm, Reka AI, Z-AI, MonsterAPI, Nomic, Jina, Voyage, Meshy, Tripo3D, Segmind.
Embeddings Providers (29)
| Provider | API |
|---|---|
| OpenAI | api.openai.com/v1/embeddings |
| Cohere | api.cohere.ai/v2/embed |
| Google AI | generativelanguage.googleapis.com |
| Google Vertex AI | aiplatform.googleapis.com |
| Azure OpenAI | Azure Embeddings |
| Azure AI Inference | Foundry Embeddings |
| AWS Bedrock | Bedrock Runtime |
| Mistral AI | api.mistral.ai/v1/embeddings |
| Together AI | api.together.ai/v1/embeddings |
| Fireworks AI | api.fireworks.ai/v1/embeddings |
| Workers AI | Cloudflare Workers AI |
| SiliconFlow | api.siliconflow.cn/v1/embeddings |
| AI21 | api.ai21.com/v1/embeddings |
| Anyscale | api.endpoints.anyscale.com/v1/embeddings |
| Nomic | api.nomic.ai |
| Jina | api.jina.ai |
| Voyage | api.voyageai.com |
| Databricks | Workspace embeddings |
And more: DashScope, Zhipu, Nebius, Ollama, Palm, Cortex, CometAPI, IOIntelligence, Kluster AI, Upstage, x-ai.
Image Generation Providers (14)
| Provider | API |
|---|---|
| OpenAI DALL-E | api.openai.com/v1/images/generations |
| Azure OpenAI | Azure Image Generation |
| Azure AI Inference | Foundry Image Generation |
| AWS Bedrock | Bedrock Runtime |
| Google Vertex AI | aiplatform.googleapis.com |
| Segmind | api.segmind.com |
| Recraft AI | api.recraft.ai |
| Stability AI | api.stability.ai |
| Workers AI | Cloudflare Workers AI |
| SiliconFlow | api.siliconflow.cn |
| LemonFox AI | api.lemonfox.ai |
| DeepBricks | api.deepbricks.io |
| Hyperbolic | api.hyperbolic.ai |
| Fireworks AI | api.fireworks.ai |
Audio / Speech / Translation / 3D
| Capability | Providers |
|---|---|
| Audio Transcription | OpenAI Whisper, Groq, LemonFox AI, Lepton, Azure OpenAI |
| Speech Synthesis | OpenAI TTS, Azure OpenAI |
| Translation | OpenAI, LemonFox AI, Azure OpenAI |
| 3D Generation | Meshy, Tripo 3D |
Strategies
Fallback (Priority-based)
Tries targets in order, automatically fails over to next on error (429/5xx).
const priorai = new Priorai({
strategy: 'fallback',
targets: [
{ provider: 'openai', apiKey: 'primary-key' },
{ provider: 'anthropic', apiKey: 'fallback-key' }
]
});Load Balance (Weighted)
Distributes requests based on weight values.
const priorai = new Priorai({
strategy: 'loadbalance',
targets: [
{ provider: 'openai', apiKey: 'key1', weight: 0.7 },
{ provider: 'openai', apiKey: 'key2', weight: 0.3 }
]
});Single
Uses a single provider without fallback.
const priorai = new Priorai({
strategy: 'single',
targets: [{ provider: 'openai', apiKey: 'my-key' }]
});Per-Target Configuration
Each target is independently configured β apiKey, customHost, and all provider-specific options are per-target, not global. This means you can mix providers, keys, and base URLs freely within a single strategy:
const priorai = new Priorai({
strategy: 'fallback',
targets: [
{
provider: 'openai',
apiKey: 'sk-primary-key',
customHost: 'https://my-proxy.com/v1/chat/completions',
overrideParams: { model: 'gpt-4o' }
},
{
provider: 'openai',
apiKey: 'sk-backup-key',
// no customHost β uses default api.openai.com
overrideParams: { model: 'gpt-4o-mini' }
},
{
provider: 'anthropic',
apiKey: 'ant-fallback-key',
overrideParams: { model: 'claude-sonnet-4-5-20250514' }
}
]
});All fields from TargetConfig are passed through to the provider's getBaseURL() and headers() functions, so provider-specific options like awsRegion, vertexProjectId, databricksWorkspace, azureResourceName, etc. all work at the target level.
Provider Configuration Examples
AWS Bedrock
const priorai = new Priorai({
strategy: 'single',
targets: [{
provider: 'bedrock',
awsAccessKeyId: 'AKIA...',
awsSecretAccessKey: '...',
awsSessionToken: '...', // optional
awsRegion: 'us-east-1',
overrideParams: { model: 'us.anthropic.claude-sonnet-4-5-20250514-v1:0' }
}]
});Azure OpenAI
const priorai = new Priorai({
strategy: 'single',
targets: [{
provider: 'azure-openai',
apiKey: 'your-azure-key',
azureResourceName: 'your-resource',
azureDeploymentId: 'gpt-4o',
azureApiVersion: '2024-06-01'
}]
});Google Vertex AI
const priorai = new Priorai({
strategy: 'single',
targets: [{
provider: 'vertex-ai',
vertexProjectId: 'your-project',
vertexRegion: 'us-central1',
apiKey: 'your-access-token',
overrideParams: { model: 'gemini-2.5-pro' }
}]
});Databricks
const priorai = new Priorai({
strategy: 'single',
targets: [{
provider: 'databricks',
apiKey: 'your-databricks-token',
databricksWorkspace: 'your-workspace',
overrideParams: { model: 'databricks-meta-llama-3-1-70b-instruct' }
}]
});Custom Host
const priorai = new Priorai({
strategy: 'single',
targets: [{
provider: 'openai',
customHost: 'https://your-proxy.com/v1/chat/completions',
apiKey: 'your-api-key'
}]
});Architecture
src/
βββ index.ts # SDK entry point
βββ globals.ts # Provider constants
βββ types/
β βββ requestBody.ts # Type definitions (Params, Options)
βββ providers/ # 71 provider directories (Portkey-aligned)
β βββ index.ts # Static provider registry
β βββ types.ts # ProviderConfig / ProviderAPIConfig types
β βββ utils.ts # Provider utilities
β βββ open-ai-base/ # Shared OpenAI-compatible base
β βββ anthropic-base/ # Shared Anthropic base
β βββ openai/ # api.ts + chatComplete.ts + embed.ts + ...
β βββ anthropic/
β βββ bedrock/ # Includes AWS SigV4 signing
β βββ google-vertex-ai/
β βββ ... (71 total)
βββ core/
βββ Router.ts # Main Priorai class
βββ types.ts # Core types
βββ providerRequest.ts # buildProviderRequest + transformProviderResponse
βββ RetryHandler.ts # Exponential backoff retry
βββ streamUtils.ts # SSE split patterns
βββ sseParser.ts # SSE parsing
βββ transform*Stream.ts # 6 streaming transforms
βββ strategies/
βββ FallbackStrategy.ts
βββ LoadBalanceStrategy.ts
βββ SingleStrategy.tsRequest Flow
Priorai.chat.completions.create(params)
β Strategy.execute(targets, params)
β buildProviderRequest(params, provider, target, endpoint)
β Providers[provider].api.getBaseURL() // URL
β Providers[provider].api.getEndpoint() // endpoint path
β Providers[provider].api.headers() // headers
β transformUsingProviderConfig(config, params) // body
β fetch(url, { headers, body })
β transformProviderResponse(response, provider, endpoint)
β Providers[provider].responseTransforms[endpoint]()Timeout Configuration
const priorai = new Priorai({
strategy: 'fallback',
targets: [...],
timeout: 15000, // 15 seconds for all requests
});Default: 30000ms (30s). Applied to all fetch paths including chat, embeddings, images, audio, speech, and translation.
Retry Configuration
const priorai = new Priorai({
strategy: 'fallback',
retry: { attempts: 3, onStatusCodes: [429, 500, 502, 503, 504] },
targets: [...]
});Default: 3 attempts, exponential backoff (100ms Γ 2^attempt), max 60s.
Error Handling
| Scenario | Behavior |
|---|---|
| 429 Rate Limited | Retry with backoff, then fallback |
| 500/502/503/504 | Retry with backoff, then fallback |
| 401 Unauthorized | Fail immediately |
Development
Pre-commit Hook
The project includes a pre-commit hook that runs biome lint --fix (safe fixes only) on staged .ts, .js, and .json files.
To enable it after cloning:
ln -sf ../../pre-commit.sh .git/hooks/pre-commitTesting
npm test # Run all tests (250 tests)
npm run test:watch # Watch modeDependencies
Production
@smithy/signature-v4: AWS SigV4 signing (Bedrock/SageMaker)@aws-crypto/sha256-js: SHA256 hashing (@smithy dependency)
Development
vitest: Testingtsup: Build (esbuild-based, ESM + CJS)typescript: Type checkingbiome: Linting + formatting
License
MIT - See LICENSE